appInterface.js 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. (function () {
  2. let prevSelectedApp = {name: ''}
  3. getSelectedApp = name => {
  4. switch (name) {
  5. case 'html-documents':
  6. return appHtmlDocument
  7. case 'thread':
  8. return appThread
  9. case 'file':
  10. return appFile
  11. default:
  12. return null
  13. }
  14. }
  15. //@TODO make a file action.tracimCustomEvent.js that will contains all customEvent that tracim_frontend call
  16. // => pb with that is that appInterface cant use import since it is not part of the build webpack
  17. // use module.export and require
  18. // doesn't work, cant resolve a file outside of the build dir
  19. GLOBAL_renderAppFull = app => {
  20. console.log('%cGLOBAL_renderAppFull', 'color: #5cebeb', app)
  21. const selectedApp = getSelectedApp(app.config.slug)
  22. if (selectedApp.isRendered) {
  23. GLOBAL_dispatchEvent({type: `${app.config.slug}_showApp`, data: app}) // handled by html-documents:src/container/HtmlDocument.jsx
  24. } else {
  25. selectedApp.renderAppFull(app)
  26. selectedApp.isRendered = true
  27. prevSelectedApp.isRendered = false
  28. prevSelectedApp = selectedApp
  29. }
  30. }
  31. GLOBAL_renderAppPopupCreation = app => {
  32. console.log('%cGLOBAL_renderAppPopupCreation', 'color: #5cebeb', app)
  33. const selectedApp = getSelectedApp(app.config.slug)
  34. if (!selectedApp) {
  35. console.log('Error in GLOBAL_renderAppPopupCreation, selectedApp is undefined', app)
  36. return
  37. }
  38. getSelectedApp(app.config.slug).renderAppPopupCreation(app)
  39. }
  40. GLOBAL_dispatchEvent = ({type, data}) => {
  41. console.log('%cGLOBAL_dispatchEvent', 'color: #fff', type, data)
  42. const event = new CustomEvent('appCustomEvent', {detail: {type, data}})
  43. document.dispatchEvent(event)
  44. }
  45. GLOBAL_eventReducer = ({detail: {type, data}}) => {
  46. switch (type) {
  47. case 'hide_popupCreateContent':
  48. console.log('%cGLOBAL_eventReducer Custom Event', 'color: #28a745', type, data)
  49. getSelectedApp(data.name).unmountApp('popupCreateContentContainer')
  50. break
  51. case 'unmount_app':
  52. console.log('%cGLOBAL_eventReducer Custom Event', 'color: #28a745', type, data)
  53. if (prevSelectedApp.name === '') return
  54. prevSelectedApp.unmountApp('appContainer')
  55. prevSelectedApp.unmountApp('popupCreateContentContainer')
  56. prevSelectedApp.isRendered = false
  57. break
  58. }
  59. }
  60. document.addEventListener('appCustomEvent', GLOBAL_eventReducer)
  61. })()