123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681 |
- 'use strict';
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
- function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
- function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
- define(['underscore', '../lib/@waca/dashboard-common/dist/core/APIFactory', './ContentAPISpec', '../api/ContentAPI', '../features/FeatureLoader', '../dashboard/util/PropertyLayoutHelper', '../features/content/state/impl/StateImpl', '../features/content/internalModels/InternalModels', '../features/content/contentFilter/ContentFilter', '../features/content/serializer/Serializer', '../dashboard/util/DashboardSpecHelper'], function (_, APIFactory, ContentAPISpec, ContentAPI, FeatureLoader, PropertyLayoutHelper, State, InternalModels, ContentFilter, Serializer, DashboardSpecHelper) {
- var Content = function (_ContentAPISpec) {
- _inherits(Content, _ContentAPISpec);
- function Content(options) {
- _classCallCheck(this, Content);
- var _this = _possibleConstructorReturn(this, _ContentAPISpec.call(this));
- _this.boardModel = options.boardModel;
- _this.layoutModel = options.layoutModel;
- _this.profile = options.profile;
- _this.creationTransctionToken = options.transactionToken;
- _this.contentFeatureCollection = options.contentFeatureCollection || [];
-
-
-
-
- _this.widgetModel = options.widgetModel;
- _this.contentMap = {};
- _this.container = options.container;
- _this.featureLoaderClass = options.featureLoaderClass || FeatureLoader;
- _this.featureLoader = options.featureLoader;
- _this.canvas = options.canvas;
- _this.dashboardFeatures = options.dashboardFeatures;
- _this.transaction = _this.dashboardFeatures.getFeature('Transaction');
- _this.lifeCycleManager = _this.dashboardFeatures.getFeature('LifeCycle');
- _this.logger = _this.dashboardFeatures.getFeature('Logger');
- _this.contentFactory = _this.dashboardFeatures.getFeature('ContentFactory');
- if (!_this.featureLoader) {
- _this.createFeatureLoader();
- }
- _this._updateContentModelFeatures();
- var model = _this._getModel();
- model.on('add:item', _this.onAddItem, _this);
- model.on('remove:item', _this.onRemoveItem, _this);
- model.on('change:parent', _this.onChangeParent, _this);
- model.on('pre:remove:item', _this.preRemoveItem, _this);
- return _this;
- }
-
- Content.prototype._updateContentModelFeatures = function _updateContentModelFeatures() {
- var _this2 = this;
- var getLoadedFeatures = this.featureLoader.getLoadedFeatures.bind(this.featureLoader);
- var _getModel$getContentM = this._getModel().getContentModel(),
- contentModelFeatures = _getModel$getContentM.features;
- var IGNORE_CONTENT_FEATURES = ['toJSON', 'Serializer'];
- contentModelFeatures.toJSON = function () {
- var loadedFeatures = getLoadedFeatures();
- var featureNames = Object.keys(loadedFeatures).concat(Object.keys(contentModelFeatures)).filter(function (item) {
- return IGNORE_CONTENT_FEATURES.indexOf(item) === -1;
- });
- var features = {};
- featureNames.forEach(function (featureName) {
- var feature = null;
- if (loadedFeatures[featureName] && typeof loadedFeatures[featureName].toJSON === 'function') {
- feature = loadedFeatures[featureName].toJSON();
- } else if (contentModelFeatures[featureName]) {
- feature = contentModelFeatures[featureName];
- } else {
- _this2.logger.info('Data for feature "' + featureName + '" is not persisted');
- }
- if (feature && Object.keys(feature).length > 0) {
- features[featureName] = feature;
- }
- });
- if (features.hasOwnProperty('Models.internal')) {
- features['Models_internal'] = features['Models.internal'];
- delete features['Models.internal'];
- }
- return features;
- };
- };
- Content.prototype.onAddItem = function onAddItem(payload) {
- var newModelId = payload.value.parameter.model.id;
-
- this.getContentInstance(newModelId, false, payload.data ? payload.data.transactionToken : null);
- };
- Content.prototype.preRemoveItem = function preRemoveItem(payload) {
- var childId = payload.value.parameter;
- var contentImpl = this.getContentInstance(childId);
- if (contentImpl) {
- var content = contentImpl.getAPI();
- var contentViewDom = content.getFeature('ContentViewDOM');
- var contentNode = contentViewDom && contentViewDom.getNode();
- if (contentNode) {
- var dashboardAPI = this.dashboardFeatures.getFeature('API');
- var controller = dashboardAPI.getFeature('InteractionController.internal');
- controller.selectionHandler.deselectNode(contentNode);
- }
- }
- };
- Content.prototype.onRemoveItem = function onRemoveItem(payload) {
- var childId = payload.value.parameter;
- var contentImpl = this.getContentInstance(childId);
- if (contentImpl) {
- contentImpl.destroy();
- delete this.contentMap[childId];
- }
- };
- Content.prototype.onChangeParent = function onChangeParent(payload) {
- var newParentId = payload.value.parameter.parentId;
- if (this.getContainer().getId() !== newParentId) {
- var newContainerInst = this.canvas.getRootContentInstance().getContentInstance(newParentId);
- this.moveToContainer(newContainerInst);
- }
- };
-
- Content.prototype.createFeatureLoader = function createFeatureLoader() {
- var type = this.getType();
- var types = [type];
- if (type !== this._getModel().type) {
- types.push(this._getModel().type);
- }
- this.featureLoader = new this.featureLoaderClass({
- featureParams: {
-
- dashboardAPI: this.dashboardFeatures.getFeature('API'),
- content: this.getAPI(),
- featureSpecs: this._getModel().getContentModel().features,
- transactionToken: this.creationTransctionToken
- },
- externalFeatures: this.dashboardFeatures,
- lifeCycleManager: this.lifeCycleManager,
- types: types,
- parentAPI: this
- });
-
- this.featureLoader.registerFeature('state', new State({
- logger: this.logger
- }), {
- 'containerId': 'com.ibm.bi.dashboard.content-features',
- 'name': 'state',
- 'types': ['widget']
- });
- this.featureLoader.registerFeature('Models.internal', new InternalModels({
- widgetModel: this.widgetModel,
- layoutModel: this.layoutModel
- }), {
- 'containerId': 'com.ibm.bi.dashboard.content-features',
- 'name': 'InternalModels'
- });
-
- this.featureLoader.registerFeature('ContentFilter', new ContentFilter({
- content: this,
- logger: this.logger
- }), {
- 'containerId': 'com.ibm.bi.dashboard.content-features',
- 'name': 'ContentFilter'
- });
-
-
- this.featureLoader.registerFeature('Serializer', new Serializer({
- layoutModel: this.layoutModel,
- content: this,
- logger: this.logger
- }), {
- 'containerId': 'com.ibm.bi.dashboard.content-features.Serializer',
- 'name': 'Serializer'
- });
- this._createdFeatureLoader = true;
- };
- Content.prototype.initialize = function initialize() {
- var _this3 = this;
- if (!this._initializePromise) {
- var promises = [];
- promises.push(this.featureLoader.loadFeaturesFromArray(this.contentFeatureCollection));
- var items = this._getModelItems();
- if (items) {
-
- items.forEach(function (element) {
- var childContent = _this3.getContentInstance(element.id, true, _this3.creationTransctionToken);
- if (childContent) {
- promises.push(childContent.initialize());
- }
- });
- }
- this._initializePromise = Promise.all(promises);
- }
- return this._initializePromise.then(function () {
- return _this3.getAPI();
- });
- };
- Content.prototype.registerFeatures = function registerFeatures(features) {
- return this.featureLoader.loadFeaturesFromArray(features);
- };
-
- Content.prototype.registerFeature = function registerFeature(name, instance, spec, skipInitialization) {
- var _this4 = this;
- return this.initialize().then(function () {
- return _this4.featureLoader.registerFeature(name, instance, spec, skipInitialization);
- });
- };
- Content.prototype.getAPI = function getAPI() {
- if (!this.api) {
- this.api = APIFactory.createAPI(this, [ContentAPI]);
- }
- return this.api;
- };
- Content.prototype.destroyContentMap = function destroyContentMap() {
- var _this5 = this;
- Object.keys(this.contentMap).forEach(function (childID) {
- var childContent = _this5.contentMap[childID];
- childContent && childContent.destroy && childContent.destroy();
- });
- };
- Content.prototype.destroy = function destroy() {
- var model = this._getModel();
- model.off('add:item', this.onAddItem, this);
- model.off('remove:item', this.onRemoveItem, this);
- model.off('change:parent', this.onChangeParent, this);
- this.destroyContentMap();
- if (this._createdFeatureLoader) {
- this.featureLoader.destroy();
- }
- this.featureLoader = null;
- this.dashboardFeatures = null;
- this.boardModel = null;
- this.layoutModel = null;
- this.widgetModel = null;
- this.contentMap = null;
- this.canvas = null;
- this.api = null;
- };
- Content.prototype.getId = function getId() {
- return this._getModel().getId();
- };
- Content.prototype._getWidgetModel = function _getWidgetModel() {
- if (!this.widgetModel) {
- this.widgetModel = this.boardModel.getWidgetModel(this.getId());
- }
- return this.widgetModel;
- };
- Content.prototype.getType = function getType() {
- var type = this.layoutModel.type;
- var widgetModel = this._getWidgetModel();
- if (widgetModel) {
- type += '.' + widgetModel.type;
- }
- return type;
- };
- Content.prototype.addContent = function addContent(options, transactionToken) {
- var _this6 = this;
- if (_.isNumber(options.position)) {
- var beforeContent = this.getChildren()[options.position];
- options.insertBefore = beforeContent && beforeContent.getId();
- }
- if (options.content) {
- return this._getContentSpec(options.content).then(function (spec) {
- if (spec) {
- options.type = options.content.getType();
- spec.layout = spec.layout[0];
- options.spec = spec;
- }
- return _this6._addModel(options, transactionToken);
- });
- }
- return this._addModel(options, transactionToken);
- };
- Content.prototype._addModel = function _addModel(options, transactionToken) {
- var _this7 = this;
-
- options.properties = Object.assign(options.properties || {}, options.layout);
- delete options.layout;
- return this.contentFactory.addModel(options, {
- boardModel: this.boardModel,
- canvas: this.canvas,
- canvasContent: this
- }, transactionToken).then(function (modelId) {
- return _this7.getContentInstance(modelId).initialize();
- }).then(function (contentApi) {
- var properties = options.properties || {};
- Object.keys(properties).forEach(function (propertyName) {
- contentApi.setPropertyValue(propertyName, properties[propertyName], transactionToken);
- });
- return contentApi;
- });
- };
- Content.prototype._getContentSpec = function _getContentSpec(content) {
-
-
-
-
- var dashboard = content.getFeature('Models.internal').getLayoutModel().boardModel.dashboardApi;
- var layoutController = dashboard.getFeature('LegacyViewControllers').getLayoutController();
- return layoutController.getInteractionController().then(function (controller) {
- var specHelper = new DashboardSpecHelper(controller);
- var contentSpec = specHelper.getContentsToJSONSpec(content).spec;
- return specHelper.validateDashboardSpec(contentSpec);
- });
- };
- Content.prototype.removeChildContent = function removeChildContent(id, transactionToken) {
-
- var content = this.getContentInstance(id);
- if (content) {
- this.boardModel.removeLayouts(id, 'api', this._convertOptions(transactionToken));
- }
- };
- Content.prototype.removeContentReference = function removeContentReference(content) {
- delete this.contentMap[content.getId()];
- };
- Content.prototype.addContentReference = function addContentReference(content) {
- this.contentMap[content.getId()] = content;
- };
- Content.prototype.moveToContainer = function moveToContainer(newContainerInst) {
- this.container.removeContentReference(this);
- this.container = newContainerInst;
- newContainerInst.addContentReference(this);
- };
- Content.prototype.moveContent = function moveContent(containerId, contentIdList, transactionToken, insertBeforeMap) {
- var _this8 = this;
- var containerInst = containerId === this.getId() ? this : this.getContentInstance(containerId);
- if (containerInst) {
- var moveOptions = {
- updateArray: []
- };
- var contentInstList = contentIdList.map(function (id) {
- return _this8.canvas.getContentInstance(id);
- });
- contentInstList.forEach(function (content) {
- if (content) {
- var id = content.getId();
- var contentEntry = {
- id: id,
- parentId: containerInst.getId()
- };
- if (insertBeforeMap && insertBeforeMap[id]) {
- contentEntry.insertBefore = insertBeforeMap[id];
- }
- moveOptions.updateArray.push(contentEntry);
- content.moveToContainer(containerInst);
- }
- });
- if (moveOptions.updateArray.length > 0) {
- this.layoutModel.getTopLayoutModel().updateModel(moveOptions, null, this._convertOptions(transactionToken));
- }
- return contentInstList.map(function (instance) {
- return instance.getAPI();
- });
- } else {
- this.logger.error('cannot find container ' + containerId);
- }
- };
- Content.prototype.removeContent = function removeContent(id, transactionToken) {
- this.removeChildContent(id, transactionToken);
- };
- Content.prototype.getContentInstance = function getContentInstance(id, doNotInitialize, transactionToken) {
- var content = this.contentMap[id];
- var items = this._getModelItems();
- if (!content && items) {
- var matchedModel = items.find(function (model) {
- return model.id === id;
- });
- if (matchedModel) {
-
-
-
-
- content = this.contentFactory.createContent({
- contentFeatureCollection: this.contentFeatureCollection,
- dashboardFeatures: this.dashboardFeatures,
- boardModel: this.boardModel,
- layoutModel: matchedModel,
- featureLoaderClass: this.featureLoaderClass,
- canvas: this.canvas,
- container: this,
- profile: this.profile,
- transactionToken: transactionToken,
- type: matchedModel.type
- });
- APIFactory.setParentChildRelation(this.canvas, content);
- if (!doNotInitialize) {
- content.initialize();
- }
- this.contentMap[id] = content;
- } else {
-
- for (var i = 0; i < items.length && !content; i++) {
- var contentInstance = this.getContentInstance(items[i].id, false, transactionToken);
- content = contentInstance.getContentInstance(id, doNotInitialize, transactionToken);
- }
- }
- }
- return content;
- };
- Content.prototype.getContentPosition = function getContentPosition(id) {
- var position = -1;
- var items = this._getModelItems();
- if (items && items.length) {
- position = items.findIndex(function (model) {
- return model.id === id;
- });
- if (position === -1) {
-
- for (var i = 0; i < items.length && position === -1; i++) {
- var contentInstance = this.getContentInstance(items[i].id);
- position = contentInstance.getContentPosition(id);
- }
- }
- }
- return position;
- };
- Content.prototype.getContent = function getContent(id) {
- var content = this.getContentInstance(id);
- return content ? content.getAPI() : null;
- };
- Content.prototype.getContainer = function getContainer() {
- var containerInst = this.getContainerInstance();
- return containerInst && containerInst.getAPI();
- };
- Content.prototype.getContainerInstance = function getContainerInstance() {
- return this.container;
- };
- Content.prototype.getChildren = function getChildren() {
- var _this9 = this;
- var items = this._getModelItems() || [];
- return items.map(function (item) {
- return _this9.getContent(item.id);
- });
- };
- Content.prototype.findContent = function findContent() {
- var _this10 = this;
- var selector = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
- if (_.isEmpty(selector)) {
- selector.type = 'widget';
- }
- var contents = [];
- var items = this._getModelItems();
- if (items) {
- items.forEach(function (item) {
- var content = _this10.getContent(item.id);
- var contentFilter = content.getFeature('ContentFilter');
- if (contentFilter.match({ selector: selector, layout: item })) {
- contents.push(content);
- }
- contents.push.apply(contents, content.findContent(selector));
- });
- }
- return contents;
- };
- Content.prototype._convertOptions = function _convertOptions(transactionToken) {
- var oldOptions = {
- skipUndoRedo: true
- };
- if (transactionToken && transactionToken.transactionId) {
- oldOptions.undoRedoTransactionId = transactionToken.transactionId;
- oldOptions.transactionToken = transactionToken;
- }
- return oldOptions;
- };
- Content.prototype.getPropertyLayoutList = function getPropertyLayoutList() {
- var properties = this.getFeature('Properties');
- var layoutList = properties.getPropertyLayoutList();
- var propertyList = this.getPropertyList();
- return PropertyLayoutHelper.getExpandedLayoutList(layoutList, propertyList, this.profile);
- };
- Content.prototype.getPropertyNameList = function getPropertyNameList() {
- var properties = this.getFeature('Properties');
- return properties.getPropertyList().map(function (prop) {
- return prop.id;
- });
- };
-
-
-
- Content.prototype.setPropertyValue = function setPropertyValue(name, value, options) {
- var property = this.getPropertyList().find(function (property) {
- return property.id === name;
- });
- if (property) {
- return property.setPropertyValue(value, options);
- } else {
- return this._getModel().getContentModel().setPropertyValue(name, value, options);
- }
- };
- Content.prototype.getPropertyValue = function getPropertyValue(name) {
- var property = this.getPropertyList().find(function (property) {
- return property.id === name;
- });
- if (property) {
- return property.getPropertyValue();
- } else {
- return this._getModel().getContentModel().getPropertyValue(name);
- }
- };
- Content.prototype.__getPropertyValueFunc = function __getPropertyValueFunc(getterFunction, property) {
- var propValue = void 0;
- if (getterFunction) {
- propValue = getterFunction();
- } else {
- propValue = this._getModel().getContentModel().getPropertyValue(property.id);
- }
-
- if (propValue == null && property.defaultValue != null) {
- propValue = property.defaultValue;
- }
- return propValue;
- };
- Content.prototype.getPropertyList = function getPropertyList() {
- var _this11 = this;
- var propertiesFeature = this.getFeature('Properties');
- if (!propertiesFeature) {
- return [];
- }
- var properties = propertiesFeature.getPropertyList();
-
-
- var _loop = function _loop(i) {
- var property = properties[i];
- if (!property.isGetterSet) {
-
-
- property.isGetterSet = true;
- var originalPropertyGetter = property.getPropertyValue && property.getPropertyValue.bind(property);
- property.getPropertyValue = _this11.__getPropertyValueFunc.bind(_this11, originalPropertyGetter, property);
- }
- if (!property.setPropertyValue) {
-
-
- property.setPropertyValue = function (value, options) {
- _this11._getModel().getContentModel().setPropertyValue(property.id, value, options);
- };
- }
- };
- for (var i = 0; i < properties.length; i++) {
- _loop(i);
- }
- return properties;
- };
- Content.prototype.getFeature = function getFeature(id) {
- if (this.featureLoader) {
- return this.featureLoader.getFeature(id);
- }
- };
- Content.prototype.setFeatureEnabled = function setFeatureEnabled(id, isEnabled) {
- if (this.featureLoader) {
- this.featureLoader.setFeatureEnabled(id, isEnabled);
- }
- };
- Content.prototype._getModel = function _getModel() {
- return this.layoutModel;
- };
- Content.prototype._getModelItems = function _getModelItems() {
- return this.layoutModel.items;
- };
- return Content;
- }(ContentAPISpec);
- return Content;
- });
|