123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- 'use strict';
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
- /**
- * Licensed Materials - Property of IBM
- * IBM Business Analytics (C) Copyright IBM Corp. 2019, 2020
- * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
- */
- /**
- * @implements ConvertToTemplateAPI
- */
- 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) {
- var ConvertToTemplate = function () {
- function ConvertToTemplate(_ref) {
- var features = _ref.features;
- _classCallCheck(this, ConvertToTemplate);
- this.dashboard = features.API;
- this._featureChecker = this.dashboard.getGlassCoreSvc('.FeatureChecker');
- }
- ConvertToTemplate.prototype.getAPI = function getAPI() {
- var isEnabled = !this._featureChecker.checkValue('dashboard', 'dashboardTemplates', 'disabled');
- if (!isEnabled && !this._api) {
- // A fake API that does nothing
- this._api = { convertToTemplate: function convertToTemplate() {
- return Promise.resolve();
- } };
- }
- if (!this._api) {
- this._api = APIFactory.createAPI(this, [ConvertToTemplateAPI]);
- }
- return this._api;
- };
- /**
- * Extracts a template JSON spec out of an existing dashboard spec
- * @param {object} dashboardSpec - dashboard spec to extract a template from
- * @returns returns a JSON object representing a template of the input dashboard spec
- */
- ConvertToTemplate.prototype.convertToTemplate = function convertToTemplate(dashboardSpec) {
- var _this = this;
- if (!dashboardSpec || !Object.keys(dashboardSpec).length) {
- return Promise.resolve(null);
- }
- return this._getAjv().then(function (ajv) {
- var templateSchmaMappings = _this._getSchemaMappings();
- var templateSpec = {};
- for (var _iterator = Object.keys(dashboardSpec), _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
- var _ref2;
- if (_isArray) {
- if (_i >= _iterator.length) break;
- _ref2 = _iterator[_i++];
- } else {
- _i = _iterator.next();
- if (_i.done) break;
- _ref2 = _i.value;
- }
- var propertyName = _ref2;
- var propertySchema = templateSchmaMappings[propertyName];
- if (!propertySchema) {
- continue;
- }
- var propertyValue = dashboardSpec[propertyName];
- var removeNonTemplateProperties = ajv.getSchema(propertySchema);
- if (!removeNonTemplateProperties) {
- continue;
- }
- removeNonTemplateProperties(propertyValue);
- if (propertyName === 'layout' && !Array.isArray(propertyValue)) {
- _this._lockVisualizationType(propertyValue.items);
- }
- templateSpec[propertyName] = propertyValue;
- }
- return templateSpec;
- });
- };
- ConvertToTemplate.prototype._lockVisualizationType = function _lockVisualizationType(items) {
- for (var i = 0; i < items.length; i++) {
- var item = items[i];
- if (item.items) {
- this._lockVisualizationType(item.items);
- }
- if (item.type === 'widget' && item.features && item.features.Models_internal && item.features.Models_internal.type === 'live') {
- item.features.Models_internal.visTypeLocked = true;
- }
- }
- };
- ConvertToTemplate.prototype._getSchemaMappings = function _getSchemaMappings() {
- if (!this.schemaMappings) {
- this.schemaMappings = JSON.parse(SchemaMappings);
- }
- return this.schemaMappings;
- };
- ConvertToTemplate.prototype._getAjv = function _getAjv() {
- var _this2 = this;
- if (this.ajv) {
- return Promise.resolve(this.ajv);
- }
- return new Promise(function (resolve, reject) {
- require(['ajv'], function (Ajv) {
- try {
- _this2.ajv = new Ajv({
- verbose: true,
- allErrors: true,
- removeAdditional: 'all',
- 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)]
- });
- resolve(_this2.ajv);
- } catch (err) {
- reject(err);
- }
- }, reject);
- });
- };
- return ConvertToTemplate;
- }();
- return ConvertToTemplate;
- });
- //# sourceMappingURL=ConvertToTemplate.js.map
|