appInterface.js 3.0KB

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