'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 * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp. */ /** * @class ContentInfoImpl * @hideconstructor * @classdesc Widget Service Implementation class */ define(['../../../dashboard/dashboardServiceability/nls/StringResources'], function (StringResources) { var GENERAL_INFO = 'generalInfo'; var ContentInfoImpl = function () { function ContentInfoImpl(contentApi, logger) { _classCallCheck(this, ContentInfoImpl); this._logger = logger; this._contentAPI = contentApi; this._data = new Map(); this._isDisplayInfoOnDOM = false; } ContentInfoImpl.prototype.getId = function getId() { return this._contentAPI.getId(); }; /** * @public * @function getType * @description retrieves the type of the widget and if the widget is static it will transform the string to a user friendly * @return visType the type of the associated widget */ ContentInfoImpl.prototype.getType = function getType() { return this._contentAPI.getType(); }; /** * @public * @function getVisTypeLabel * @description retrieves the visualization type label * @return {string} the label for the visualization type */ ContentInfoImpl.prototype.getVisTypeLabel = function getVisTypeLabel() { var generalInfo = this.getData(GENERAL_INFO); var visLabel = ''; if (generalInfo.getLabel !== undefined) { visLabel = generalInfo.getLabel(); } else { // Remove the "widget." from the string "widget.shape" and capitalize the first letter visLabel = this._contentAPI.getType(); visLabel = visLabel.slice(7, visLabel.length); visLabel = visLabel[0].toUpperCase() + visLabel.slice(1); } return visLabel; }; ContentInfoImpl.prototype._getState = function _getState() { return this._contentAPI.getFeature('state'); }; ContentInfoImpl.prototype.setData = function setData(key, value) { if (typeof key !== 'string') { this._logger.error('invalid or undefined key', key); } else { this._data.set(key, value); } }; ContentInfoImpl.prototype.getData = function getData(key) { return this._data.get(key); }; ContentInfoImpl.prototype.getSpec = function getSpec() { var _this = this; var error = this._getState().getError(); var spec = { id: this.getId(), type: this.getType(), visType: this.getVisTypeLabel(), state: { error: error ? error.toJson() : undefined } }; this._data.forEach(function (value, key) { if (typeof value.toJSON === 'function') { try { spec[key] = value.toJSON(); } catch (error) { _this._logger.error('error when adding custom data in spec', key, error); } } else { if (key !== 'generalInfo') { spec[key] = value; } } }); return spec; }; ContentInfoImpl.prototype.toUIJSON = function toUIJSON() { var _this2 = this; var uiSpec = [{ sectionName: StringResources.get('generalInfo'), sectionValues: [{ fieldName: StringResources.get('widgetId'), fieldValue: this.getId() }, { fieldName: StringResources.get('widgetType'), fieldValue: this.getVisTypeLabel() }], properties: { open: true } }]; this._data.forEach(function (value, key) { if (typeof value.toUIJSON === 'function') { try { uiSpec = uiSpec.concat(value.toUIJSON()); } catch (error) { _this2._logger.error('error when adding custom UI spec', key, error); } } }); try { uiSpec = this._sanitizeUIJSON(uiSpec); } catch (error) { this._logger.error('error when adding custom UI spec', error); } return uiSpec; }; ContentInfoImpl.prototype._sanitizeUIJSON = function _sanitizeUIJSON(uiSpec) { uiSpec.forEach(function (sectionSpec) { if (sectionSpec.sectionName === undefined) { throw new Error('Error: no section name'); } if (sectionSpec.sectionValues === undefined) { sectionSpec.sectionValues = []; } if (sectionSpec.properties === undefined) { sectionSpec.properties = { open: true }; } else if (sectionSpec.properties !== undefined && sectionSpec.properties.open === undefined) { sectionSpec.properties.open = true; } sectionSpec.sectionValues.forEach(function (fieldSpec) { if (fieldSpec.type === undefined) { fieldSpec.type = 'Default'; } if (fieldSpec.type === 'SimpleTable') { if (!fieldSpec.fieldValue.columnLength) { throw new Error('Error: no columnLength. Need to specify the column length'); } if (!fieldSpec.fieldValue.rowLength) { throw new Error('Error: no rowLength. Need to specify the row length'); } if (!fieldSpec.fieldValue.cellValue || typeof fieldSpec.fieldValue.cellValue !== 'function') { throw new Error('Error: no cellValue or headerValue is not a function'); } } }); }); return uiSpec; }; ContentInfoImpl.prototype.isDisplayInfoOnDOM = function isDisplayInfoOnDOM() { return this._isDisplayInfoOnDOM; }; return ContentInfoImpl; }(); return ContentInfoImpl; }); //# sourceMappingURL=ContentInfoImpl.js.map