BoardModuleFactory.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. 'use strict';
  2. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3. /*
  4. *+------------------------------------------------------------------------+
  5. *| Licensed Materials - Property of IBM
  6. *| IBM Cognos Products: Dashboard
  7. *| (C) Copyright IBM Corp. 2019
  8. *|
  9. *| US Government Users Restricted Rights - Use, duplication or disclosure
  10. *| restricted by GSA ADP Schedule Contract with IBM Corp.
  11. *+------------------------------------------------------------------------+
  12. */
  13. define(['../../lib/@waca/core-client/js/core-client/utils/ClassFactory'], function (ClassFactory) {
  14. var BoardModuleFactory = function () {
  15. function BoardModuleFactory(props) {
  16. _classCallCheck(this, BoardModuleFactory);
  17. this.modules = props.modules || [];
  18. this._cache = props.cache || {};
  19. this._loader = props.loader || ClassFactory;
  20. this.logger = props.logger;
  21. }
  22. BoardModuleFactory.prototype.make = function make(type) {
  23. var _this = this;
  24. var module = this.getModuleByType(type);
  25. if (!module) {
  26. throw new Error('Module with type "' + type + '" not found');
  27. }
  28. var promise;
  29. if (this.isCached(module)) {
  30. promise = Promise.resolve(this.getFromCache(module));
  31. } else {
  32. promise = this.loadModule(module).then(function (ModuleClass) {
  33. _this.setToCache(module, ModuleClass);
  34. return ModuleClass;
  35. });
  36. }
  37. return promise.then(function (ModuleClass) {
  38. return new ModuleClass({
  39. logger: _this.logger
  40. });
  41. });
  42. };
  43. BoardModuleFactory.prototype.loadModule = function loadModule(module) {
  44. var _this2 = this;
  45. return this._loader.loadModule(module.path).catch(function (error) {
  46. _this2.logger.warn('[BoardModuleFactory]: attempted to load module "' + module.path + ' and failed"', error);
  47. throw error;
  48. });
  49. };
  50. BoardModuleFactory.prototype.getModuleByType = function getModuleByType(type) {
  51. var found;
  52. // TODO: update to find when everything is babelified
  53. for (var key in this.modules) {
  54. if (this.modules.hasOwnProperty(key) && this.modules[key].type === type) {
  55. found = this.modules[key];
  56. }
  57. }
  58. return found;
  59. };
  60. BoardModuleFactory.prototype.getDefaultModuleType = function getDefaultModuleType() {
  61. return BoardModuleFactory.defaultModuleType;
  62. };
  63. /* cache methods */
  64. BoardModuleFactory.prototype.isCached = function isCached(module) {
  65. return !!this._cache[module.type];
  66. };
  67. BoardModuleFactory.prototype.getFromCache = function getFromCache(module) {
  68. return this._cache[module.type];
  69. };
  70. BoardModuleFactory.prototype.setToCache = function setToCache(module, value) {
  71. this._cache[module.type] = value;
  72. return this;
  73. };
  74. return BoardModuleFactory;
  75. }();
  76. BoardModuleFactory.defaultModuleType = 'legacy';
  77. return BoardModuleFactory;
  78. });
  79. //# sourceMappingURL=BoardModuleFactory.js.map