'use strict'; /** * Licensed Materials - Property of IBM * IBM Cognos Products: BI Cloud (C) Copyright IBM Corp. 2014, 2019 * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp. */ define(['../../../lib/@waca/dashboard-common/dist/core/Model', '../../../data/models/Conditions', '../../../data/models/VisPropertyCollection', '../../../filters/Filters', 'underscore'], function (Model, Conditions, VisPropertyCollection, FilterCollection, _) { /** * INTENT: LiveWidgetModel is the top level object representation of the liveWidgetSpec. */ var LiveWidgetModel = null; LiveWidgetModel = Model.extend({ nestedModels: { // data: VisDataModel, // slotmapping: VisMappingModel, conditions: Conditions }, nestedCollections: { properties: VisPropertyCollection, localFilters: FilterCollection, searchFilters: FilterCollection }, localizedProps: ['name', 'titleHtml'], runtimeAttrs: ['filters'], init: function init(spec, options) { options = options || {}; // Data item ids map with the keys corresponding to the generated data item ids and the values corresponding to the new hash Ids this.dataItemIdsMap = {}; options.widgetModel = this; if (!spec.properties) { spec.properties = []; } if (!spec.localFilters) { spec.localFilters = []; } if (!spec.data) { spec.data = {}; } LiveWidgetModel.inherited('init', this, [spec, options]); if (spec) { this.whitelistAttrs = ['type', 'id', 'visId', 'titleHtml', 'titleMode', 'name', 'data', 'visTypeLocked', 'slotmapping', 'augmentation', 'annotations', 'forecasting', 'possibleKeyDrivers', 'customData', 'thumbnailId', 'format']; for (var item in spec) { if (spec.hasOwnProperty(item)) { this.whitelistAttrs.push(item); } } } // The searchFilters are transient properties of the model // create the default in here after we set the whitelist.. otherwise they end up bbeing persisted if (!this.searchFilters) { this.set({ searchFilters: [] }); } this.colorProperties.push('elementColor'); }, /** * Given a property name, will return information about it's multilingualAttribute object * @override */ getMultilingualAttribute: function getMultilingualAttribute(propertyName) { var multilingualAttribute = LiveWidgetModel.inherited('getMultilingualAttribute', this, arguments); if (multilingualAttribute) { return multilingualAttribute; } else if (this.properties && this.properties.getMultilingualAttribute) { return this.properties.getMultilingualAttribute(propertyName); } return null; }, /** * Returns all the multilingual properties from this model * @override */ getMultilingualAttributes: function getMultilingualAttributes() { var multilingualAttributes = LiveWidgetModel.inherited('getMultilingualAttributes', this, arguments); if (this.properties && this.properties.getMultilingualAttributes) { multilingualAttributes = multilingualAttributes.concat(this.properties.getMultilingualAttributes()); } return multilingualAttributes; }, getDataItemIdsMap: function getDataItemIdsMap() { return this.dataItemIdsMap; }, set: function set(args, options) { args = args || {}; //ENSURE WE ARE RUNNING WITHOUT MAPPING - delete the mapping member on every model set. if (args.mapping) { delete args.mapping; } //In the case where a nested model (data, slotmapping) is being set to {}, we need to re-initialize it. //TODO: This behaviour should likely be in model.js but currently, the base set checks the empty object case // and does nothing(??). Keep the scope to the 2 nested models for liveWidgetModel until more base class testing is done. _.forEach(args, function (specItem, key) { if (this.nestedModels[key], _.isObject(specItem) && _.isEmpty(specItem)) { var nestedModelClass = this.nestedModels[key]; if (nestedModelClass) { //@todo Should not be doing this. Instead go through the correct //way of setting the model. For example: //this.set({ [key]: new nestedModelClass(/* empty model */null, {widgetModel: this})}, options) //This way the generic Model.set ensures that the model is listinen to events this[key] = new nestedModelClass( /* empty model */null, { widgetModel: this }); } } }.bind(this)); if (options) { options.widgetModel = this; } LiveWidgetModel.inherited('set', this, arguments); }, cloneWidget: function cloneWidget(idMap) { var clone = new LiveWidgetModel(this.toJSON()); clone.replaceIds(idMap); return clone; }, toJSON: function toJSON() { var jsonObj = LiveWidgetModel.inherited('toJSON', this, arguments); if (jsonObj.localFilters && jsonObj.localFilters.length === 0) { delete jsonObj['localFilters']; } if (jsonObj.properties && jsonObj.properties.length === 0) { delete jsonObj['properties']; } return jsonObj; }, /** * @override */ getUsedCustomColors: function getUsedCustomColors(customColors) { var _this = this; var usedColors = LiveWidgetModel.inherited('getUsedCustomColors', this, arguments); if (this.colorProperties && this.properties) { this.colorProperties.forEach(function (propName) { var visProperty = _this.properties.get(propName); if (visProperty) { var val = visProperty.getValue(); customColors.ids.forEach(function (color, index) { if (val && val.indexOf(color) !== -1) { usedColors.push(customColors.fills[index]); } }); } }); } return usedColors; } }); return LiveWidgetModel; }); //# sourceMappingURL=LiveWidgetModel.js.map