123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- 'use strict';
- var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
- 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. 2020
- * 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/APIFactory', './SerializerAPI', '../../../dashboard/layouts/SinglePageLayout', '../../../dashboard/layouts/TabLayout', '../../../dashboard/model/ModelUtils', 'text!../../../dashboard/layout/templates/Template1.json', '../../../app/nls/StringResources'], function (APIFactory, SerializerAPI, singlePageLayout, tabLayout, ModelUtils, template1Json, stringResources) {
- /**
- * @implements {SerializerAPI}
- */
- var Serializer = function () {
- /**
- * @param {Object} options
- * @param {Object} options.features An object of the defined dependencies from the feature contribution json
- */
- function Serializer(_ref) {
- var features = _ref.features;
- _classCallCheck(this, Serializer);
- this._boardModel = features.internal.getBoardModel();
- this._dashboard = features.API;
- this._api = APIFactory.createAPI(this, [SerializerAPI]);
- }
- Serializer.prototype.getAPI = function getAPI() {
- return this._api;
- };
- Serializer.prototype.destroy = function destroy() {
- delete this._boardModel;
- delete this._dashboard;
- delete this._api;
- };
- Serializer.prototype.toJSON = function toJSON() {
- var _ref2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
- contentIds = _ref2.contentIds,
- dashboardFeatures = _ref2.dashboardFeatures;
- var json = void 0;
- if (contentIds && Array.isArray(contentIds) && contentIds.length > 0) {
- json = this._buildDashboardSpecForContents(contentIds);
- } else {
- var canvas = this._dashboard.getFeature('Canvas');
- if (canvas) {
- json = this._boardModel.toJSON();
- } else {
- json = this._boardModel.getInitialSpec();
- }
- }
- ModelUtils.pullPropertiesFromContentModelSpec(json);
- if (dashboardFeatures && Array.isArray(dashboardFeatures) && dashboardFeatures.length > 0) {
- if (json.features) {
- Object.keys(json.features).forEach(function (feature) {
- if (dashboardFeatures.indexOf(feature) === -1) {
- delete json.features[feature];
- }
- });
- }
- }
- return json;
- };
- /**
- * build the minimum dashboard spec which only contains the specified list of contents
- * For now, we use the template1 (no drop zone) to generate the dashboard spec
- * TODO: dynamically retrieving the current template of the dashboard instead of hard code the template
- * @param {Array} contentIds - a list of content ids
- * @return {Object} spec
- */
- Serializer.prototype._buildDashboardSpecForContents = function _buildDashboardSpecForContents(contentIds) {
- // get unique ids
- contentIds = contentIds.filter(function (v, i) {
- return contentIds.indexOf(v) === i;
- });
- // first check if all the content has same parent
- var canvas = this._dashboard.getFeature('Canvas');
- var contents = contentIds.map(function (contentId) {
- var content = canvas.getContent(contentId);
- if (!content) {
- throw new Error('Cannot get the content by id ' + contentId);
- }
- return content;
- });
- var parent = contents[0].getContainer();
- if (parent && parent.getType() === 'genericPage') {
- for (var i = 1; i < contents.length; i++) {
- var parentId = contents[i].getContainer() ? contents[i].getContainer().getId() : null;
- if (parentId !== parent.getId()) {
- throw new Error('the list of contents must have same parent');
- }
- }
- } else {
- throw new Error('the list of contents must be the children of genericPage');
- }
- // build the spec
- var spec = {
- version: this._boardModel.version,
- content: this._boardModel.getContentModel().toJSON()
- };
- var template1 = JSON.parse(template1Json);
- var genericPage = template1.items[0];
- genericPage.items = genericPage.items.concat(contents.map(function (content) {
- var contentSpec = content.getFeature('Serializer').toJSON();
- return contentSpec;
- }));
- //get current dashboard layout type
- var dashboardLayoutType = this._boardModel.layout.type;
- if (dashboardLayoutType === 'tab') {
- var theTabLayout = JSON.parse(JSON.stringify(tabLayout.layout)); // multi tab layout
- theTabLayout.items = [_extends({}, template1, {
- title: stringResources.get('defaultTabTitle', {
- index: 1
- })
- })];
- spec.layout = theTabLayout;
- } else {
- spec.layout = _extends({}, JSON.parse(JSON.stringify(singlePageLayout.layout)), template1);
- }
- return spec;
- };
- return Serializer;
- }();
- return Serializer;
- });
- //# sourceMappingURL=Serializer.js.map
|