Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 

87 строки
1.7 KiB

  1. define([
  2. 'js/system/events',
  3. 'js/system/globals'
  4. ], function (
  5. events,
  6. globals
  7. ) {
  8. const hookEvent = function (e, cb) {
  9. if (!this.eventList[e])
  10. this.eventList[e] = [];
  11. this.eventList[e].push(cb);
  12. events.on(e, cb);
  13. };
  14. const unhookEvents = function () {
  15. Object.entries(this.eventList).forEach(([eventName, callbacks]) => {
  16. callbacks.forEach(c => events.off(eventName, c));
  17. });
  18. };
  19. let cpns = [];
  20. return {
  21. templates: {},
  22. init: function () {
  23. cpns = globals.clientConfig.clientComponents;
  24. cpns = cpns.map(c => ({
  25. ...c,
  26. promise: this.getComponent(c)
  27. }));
  28. return Promise.all(cpns.map(c => c.promise));
  29. },
  30. getComponent: function (cpn) {
  31. return new Promise(resolve => {
  32. require([cpn.path], this.onGetComponent.bind(this, resolve, cpn));
  33. });
  34. },
  35. onGetComponent: function (resolve, cpn, template) {
  36. if (cpn.type) {
  37. template.eventList = {};
  38. template.hookEvent = hookEvent;
  39. template.unhookEvents = unhookEvents;
  40. this.templates[cpn.type] = template;
  41. resolve();
  42. } else if (cpn.extends) {
  43. let target = cpn.extends;
  44. if (!this.templates[target]) {
  45. let waitFor = cpns.find(c => c.type === target);
  46. if (waitFor) {
  47. waitFor.promise.then(() => {
  48. this.templates[target] = $.extend(true, this.templates[target], template);
  49. resolve();
  50. });
  51. }
  52. } else {
  53. this.templates[target] = $.extend(true, this.templates[target], template);
  54. resolve();
  55. }
  56. } else {
  57. // This shouldn't get reached
  58. resolve();
  59. }
  60. },
  61. getTemplate: function (type) {
  62. if (type === 'lightpatch')
  63. type = 'lightPatch';
  64. let template = this.templates[type] || {
  65. type: type
  66. };
  67. return template;
  68. }
  69. };
  70. });