ConvertToTemplate.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. 'use strict';
  2. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3. /**
  4. * Licensed Materials - Property of IBM
  5. * IBM Business Analytics (C) Copyright IBM Corp. 2019, 2020
  6. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  7. */
  8. /**
  9. * @implements ConvertToTemplateAPI
  10. */
  11. define(['./api/ConvertToTemplateAPI', '@waca/dashboard-common/js/core/APIFactory', 'text!../../../../../dashboard/schema/schemaDefs.json', 'text!../../../../../dashboard/schema/templateSchemaMappings.json', 'text!../../../../../dashboard/schema/nameSchema.json', 'text!../../../../../dashboard/schema/layoutSchema.json', 'text!../../../../../dashboard/schema/themeSchema.json', 'text!../../../../../dashboard/schema/versionSchema.json', 'text!../../../../../dashboard/schema/eventGroupsSchema.json', 'text!../../../../../dashboard/schema/propertiesSchema.json', 'text!../../../../../dashboard/schema/widgetsTemplateSchema.json'], function (ConvertToTemplateAPI, APIFactory, SchemaDefs, SchemaMappings, NameSchema, LayoutSchema, ThemeSchema, VersionSchema, EventGroupsSchema, PropertiesSchema, WidgetsTemplateSchema) {
  12. var ConvertToTemplate = function () {
  13. function ConvertToTemplate(_ref) {
  14. var features = _ref.features;
  15. _classCallCheck(this, ConvertToTemplate);
  16. this.dashboard = features.API;
  17. this._featureChecker = this.dashboard.getGlassCoreSvc('.FeatureChecker');
  18. }
  19. ConvertToTemplate.prototype.getAPI = function getAPI() {
  20. var isEnabled = !this._featureChecker.checkValue('dashboard', 'dashboardTemplates', 'disabled');
  21. if (!isEnabled && !this._api) {
  22. // A fake API that does nothing
  23. this._api = { convertToTemplate: function convertToTemplate() {
  24. return Promise.resolve();
  25. } };
  26. }
  27. if (!this._api) {
  28. this._api = APIFactory.createAPI(this, [ConvertToTemplateAPI]);
  29. }
  30. return this._api;
  31. };
  32. /**
  33. * Extracts a template JSON spec out of an existing dashboard spec
  34. * @param {object} dashboardSpec - dashboard spec to extract a template from
  35. * @returns returns a JSON object representing a template of the input dashboard spec
  36. */
  37. ConvertToTemplate.prototype.convertToTemplate = function convertToTemplate(dashboardSpec) {
  38. var _this = this;
  39. if (!dashboardSpec || !Object.keys(dashboardSpec).length) {
  40. return Promise.resolve(null);
  41. }
  42. return this._getAjv().then(function (ajv) {
  43. var templateSchmaMappings = _this._getSchemaMappings();
  44. var templateSpec = {};
  45. for (var _iterator = Object.keys(dashboardSpec), _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
  46. var _ref2;
  47. if (_isArray) {
  48. if (_i >= _iterator.length) break;
  49. _ref2 = _iterator[_i++];
  50. } else {
  51. _i = _iterator.next();
  52. if (_i.done) break;
  53. _ref2 = _i.value;
  54. }
  55. var propertyName = _ref2;
  56. var propertySchema = templateSchmaMappings[propertyName];
  57. if (!propertySchema) {
  58. continue;
  59. }
  60. var propertyValue = dashboardSpec[propertyName];
  61. var removeNonTemplateProperties = ajv.getSchema(propertySchema);
  62. if (!removeNonTemplateProperties) {
  63. continue;
  64. }
  65. removeNonTemplateProperties(propertyValue);
  66. if (propertyName === 'layout' && !Array.isArray(propertyValue)) {
  67. _this._lockVisualizationType(propertyValue.items);
  68. }
  69. templateSpec[propertyName] = propertyValue;
  70. }
  71. return templateSpec;
  72. });
  73. };
  74. ConvertToTemplate.prototype._lockVisualizationType = function _lockVisualizationType(items) {
  75. for (var i = 0; i < items.length; i++) {
  76. var item = items[i];
  77. if (item.items) {
  78. this._lockVisualizationType(item.items);
  79. }
  80. if (item.type === 'widget' && item.features && item.features.Models_internal && item.features.Models_internal.type === 'live') {
  81. item.features.Models_internal.visTypeLocked = true;
  82. }
  83. }
  84. };
  85. ConvertToTemplate.prototype._getSchemaMappings = function _getSchemaMappings() {
  86. if (!this.schemaMappings) {
  87. this.schemaMappings = JSON.parse(SchemaMappings);
  88. }
  89. return this.schemaMappings;
  90. };
  91. ConvertToTemplate.prototype._getAjv = function _getAjv() {
  92. var _this2 = this;
  93. if (this.ajv) {
  94. return Promise.resolve(this.ajv);
  95. }
  96. return new Promise(function (resolve, reject) {
  97. require(['ajv'], function (Ajv) {
  98. try {
  99. _this2.ajv = new Ajv({
  100. verbose: true,
  101. allErrors: true,
  102. removeAdditional: 'all',
  103. schemas: [JSON.parse(SchemaDefs), JSON.parse(NameSchema), JSON.parse(LayoutSchema), JSON.parse(ThemeSchema), JSON.parse(VersionSchema), JSON.parse(EventGroupsSchema), JSON.parse(PropertiesSchema), JSON.parse(WidgetsTemplateSchema)]
  104. });
  105. resolve(_this2.ajv);
  106. } catch (err) {
  107. reject(err);
  108. }
  109. }, reject);
  110. });
  111. };
  112. return ConvertToTemplate;
  113. }();
  114. return ConvertToTemplate;
  115. });
  116. //# sourceMappingURL=ConvertToTemplate.js.map