appInterface.js 3.1KB

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