__components_handler.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Core js fw functions
  3. * Do not edit this file
  4. */
  5. /*
  6. * Components Handler
  7. * collects all vue components/components templates and register it
  8. */
  9. var mgJsComponentHandler = {
  10. defaultComponentJs: {},
  11. customComponentJs: {},
  12. defaultBasedComponents: {},
  13. //adds default component js template
  14. addDefaultComponent: function (componentId, componentObj) {
  15. this.defaultComponentJs[componentId] = componentObj;
  16. },
  17. //returns default component js template by id
  18. getDefaultComponent: function (componentId) {
  19. if (this.defaultComponentJs.hasOwnProperty(componentId)) {
  20. return this.defaultComponentJs[componentId];
  21. } else {
  22. return false;
  23. }
  24. },
  25. //adds custom component js
  26. addCustomComponent: function (componentId, componentObj) {
  27. this.customComponentJs[componentId] = componentObj;
  28. },
  29. //adds a component based on default js template
  30. addComponentByDefaultTemplate: function (componentId, templateId) {
  31. this.defaultBasedComponents[componentId] = templateId;
  32. },
  33. //register all avalible components as a Vue components
  34. registerComponents: function () {
  35. this.registerDefaultComponents();
  36. this.registerCustomComponents();
  37. },
  38. //register custom components as a Vue components
  39. registerCustomComponents: function () {
  40. for (var key in this.customComponentJs) {
  41. if (!this.customComponentJs.hasOwnProperty(key)) {
  42. continue;
  43. }
  44. var componentObj = this.customComponentJs[key];
  45. Vue.component('mg-component-body-' + key, componentObj);
  46. }
  47. },
  48. //register default components as a Vue components
  49. registerDefaultComponents: function () {
  50. for (var key in this.defaultBasedComponents) {
  51. if (!this.defaultBasedComponents.hasOwnProperty(key)) {
  52. continue;
  53. }
  54. if (this.defaultComponentJs.hasOwnProperty(this.defaultBasedComponents[key])) {
  55. var componentObj = Object.assign({}, this.defaultComponentJs[this.defaultBasedComponents[key]]);
  56. componentObj.template = componentObj.template + '-' + key;
  57. Vue.component('mg-component-body-' + key, componentObj);
  58. } else {
  59. console.log('No default component ' + this.defaultBasedComponents[key] + ' found');
  60. }
  61. }
  62. },
  63. //register custom Vue component now
  64. registerNowCustomComponent: function(componentId, componentObj) {
  65. Vue.component('mg-component-body-' + componentId, componentObj);
  66. },
  67. //register Vue component based on default component template now
  68. registerNowByDefaultTemplate: function(componentId, templateName) {
  69. if (this.defaultComponentJs.hasOwnProperty(templateName)) {
  70. var componentObj = Object.assign({}, this.defaultComponentJs[templateName]);
  71. componentObj.template = componentObj.template + '-' + componentId;
  72. Vue.component('mg-component-body-' + componentId, componentObj);
  73. } else {
  74. console.log('No default component ' + templateName + ' found');
  75. }
  76. },
  77. returnByDefaultTemplate: function(componentId, templateName) {
  78. if (this.defaultComponentJs.hasOwnProperty(templateName)) {
  79. var componentObj = Object.assign({}, this.defaultComponentJs[templateName]);
  80. componentObj.template = componentObj.template + '-' + componentId;
  81. return { name: 'mg-component-body-' + componentId, obj: componentObj};
  82. } else {
  83. console.log('No default component ' + templateName + ' found');
  84. return null;
  85. }
  86. },
  87. extendRegisterByDefaultTemplate: function(componentId, templateName) {
  88. if (this.defaultComponentJs.hasOwnProperty(templateName) && typeof $('#' + componentId) !== 'undefined') {
  89. var componentObj = Object.assign({}, this.defaultComponentJs[templateName]);
  90. componentObj.template = componentObj.template + '-' + componentId;
  91. var renderComponent = Vue.extend(componentObj);
  92. new renderComponent().$mount('#' + componentId);
  93. } else if($('#' + componentId).length === 0) {
  94. console.log('No component item ' + componentId + ' found');
  95. return null;
  96. } else {
  97. console.log('No default component ' + templateName + ' found');
  98. return null;
  99. }
  100. }
  101. };