You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

78 lines
1.4 KiB

  1. define([
  2. 'js/system/events',
  3. 'js/system/globals'
  4. ], function (
  5. events,
  6. globals
  7. ) {
  8. //Store templates here after loading them
  9. const templates = [];
  10. const extenders = [];
  11. //Bound Methods
  12. const hookEvent = function (e, cb) {
  13. if (!this.eventList[e])
  14. this.eventList[e] = [];
  15. this.eventList[e].push(cb);
  16. events.on(e, cb);
  17. };
  18. const unhookEvents = function () {
  19. Object.entries(this.eventList).forEach(([eventName, callbacks]) => {
  20. callbacks.forEach(c => events.off(eventName, c));
  21. });
  22. };
  23. //Helpers
  24. const loadComponent = cpn => {
  25. return new Promise(res => {
  26. require([cpn.path], tpl => {
  27. if (cpn.type)
  28. templates.push(tpl);
  29. if (cpn.extends)
  30. extenders.push(tpl);
  31. res();
  32. });
  33. });
  34. };
  35. //Init Methods
  36. const loadComponents = paths => {
  37. return Promise.all(
  38. paths.map(p => loadComponent(p))
  39. );
  40. };
  41. const buildComponents = () => {
  42. templates.forEach(t => {
  43. const extensions = extenders.filter(e => e.extends === t.type);
  44. extensions.forEach(e => $.extend(true, t, e));
  45. t.eventList = {};
  46. t.hookEvent = hookEvent;
  47. t.unhookEvents = unhookEvents;
  48. });
  49. };
  50. //Export
  51. return {
  52. init: async function () {
  53. const paths = globals.clientConfig.clientComponents;
  54. await loadComponents(paths);
  55. buildComponents();
  56. },
  57. getTemplate: function (type) {
  58. if (type === 'lightpatch')
  59. type = 'lightPatch';
  60. return templates.find(t => t.type === type) || { type: type };
  61. }
  62. };
  63. });