appInterface.js 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. console.log('%cGLOBAL_eventReducer Custom Event', 'color: #28a745', type, data)
  65. getSelectedApp(data.name).unmountApp('popupCreateContentContainer')
  66. break
  67. case 'unmount_app':
  68. console.log('%cGLOBAL_eventReducer Custom Event', 'color: #28a745', type, data)
  69. if (prevSelectedApp.name === '') return
  70. prevSelectedApp.unmountApp('appFeatureContainer')
  71. prevSelectedApp.unmountApp('popupCreateContentContainer')
  72. prevSelectedApp.isRendered = false
  73. break
  74. }
  75. }
  76. document.addEventListener('appCustomEvent', GLOBAL_eventReducer)
  77. })()