123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- 'use strict';
- /*
- *+------------------------------------------------------------------------+
- *| Licensed Materials - Property of IBM
- *| IBM Cognos Products: Dashboard
- *| (C) Copyright IBM Corp. 2017, 2019
- *|
- *| US Government Users Restricted Rights - Use, duplication or disclosure
- *| restricted by GSA ADP Schedule Contract with IBM Corp.
- *+------------------------------------------------------------------------+
- */
- define(['../../lib/@waca/core-client/js/core-client/utils/ClassFactory', '../glass/SplitPaneView'], function (ClassFactory, SplitPaneView) {
- var _canvasExtensions = {
- _ClassFactory: ClassFactory,
- _SplitPaneView: SplitPaneView,
- /**
- * @param {object} options
- * @param {object} options.services
- * @param {object} options.eventRouter
- * @param {object} options.handlers - object of handers
- * @param {object} options.topNode - top position jquery node
- * @param {object} options.bottomNode - bottom position jquery node
- * @param {object[]} options.canvasExtensions
- * @param {integer} options.canvasExtensions[].order - the order, compared to other extensions to show in the DOM
- * @param {('top'|'bottom')} options.canvasExtensions[].position - The position in the DOM to place the extension.
- * @param {string} options.canvasExtensions[].class - The view class. MUST implement render() and return a promise.
- * @param {object} options.canvasExtensions[].class.$el - the node to be placed on the DOM
- * @param {function} [options.canvasExtensions[].class.initialize] - a method which returns a promise. Will be called BEFORE class.render
- * @param {function} options.canvasExtensions[].class.render - a method which returns a promise. Will be called BEFORE attaching $el to the DOM. Operations performed on $el ahead of time must be able to be constructed detached from the DOM
- * @param {function} options.canvasExtensions[].class.remove - will be called before the DOM node is removed
- */
- init: function init(options) {
- this.dashboardApi = options.dashboardApi;
- this.topNode = options.topNode;
- this.bottomNode = options.bottomNode;
- this.canvasExtensions = options.canvasExtensions || [];
- this.services = options.services;
- this.eventRouter = options.eventRouter;
- this.appSettings = options.appSettings;
- this.handlers = options.handlers;
- this.consumeHideHandles = [];
- this.views = new Map([]);
- },
- destroy: function destroy() {
- this.views.forEach(function (value) {
- if (typeof value.remove === 'function') {
- value.remove();
- } else {
- console.warn('view.remove should be implemented');
- }
- });
- this.views = new Map([]);
- if (this.splitPaneView) {
- this.splitPaneView.remove();
- }
- },
- render: function render() {
- var promise = Promise.resolve();
- if (this.canvasExtensions.length > 0) {
- this.canvasExtensions.sort(function (a, b) {
- return a.order - b.order;
- });
- var splitterItems = [];
- this.canvasExtensions.forEach(function (extension) {
- promise = promise.then(this._ClassFactory.loadModule.bind(this._ClassFactory, extension.class)).then(function (View) {
- var _this = this;
- var options = {
- //DO NOT add any more parameters here. The appSettings, services and eventrouter is all an extension needs. if you found your way here looking to add a dependency, you're doing it wrong.
- appSettings: this.appSettings,
- services: this.services,
- eventRouter: this.eventRouter,
- dashboardApi: this.dashboardApi
- };
- if (extension.extraOptions) {
- options.extraOptions = extension.extraOptions;
- }
- var view = new View(options);
- var initializationPromise;
- if (typeof view.initialize === 'function') {
- initializationPromise = view.initialize();
- } else {
- initializationPromise = Promise.resolve();
- }
- return initializationPromise.then(function () {
- var hasApi = !!view.getApi;
- if (hasApi) {
- return _this.dashboardApi.getCanvas().registerFeature(extension.name, view.getApi());
- }
- }).then(function () {
- var promise;
- if (extension.position === 'top') {
- _this.topNode.append(view.$el);
- } else if (extension.position === 'bottom') {
- _this.bottomNode.append(view.$el);
- } else if (extension.position === 'splitter') {
- promise = view.getSplitterOpts().then(function (opts) {
- if (opts.hideInConsume) {
- this.consumeHideHandles.push(opts.handleClass);
- if (!this.handlers.isAuthorMode()) {
- opts.hidden = true;
- }
- }
- splitterItems.push(opts);
- return opts;
- }.bind(_this));
- }
- _this.views.set(extension.name, view);
- return promise || Promise.resolve();
- }).then(function () {
- var renderPromise;
- if (extension.position !== 'splitter') {
- renderPromise = view.render();
- }
- return renderPromise || Promise.resolve();
- });
- }.bind(this));
- }.bind(this));
- promise = promise.then(function () {
- if (splitterItems.length > 0) {
- return this._createSplitPaneView(splitterItems);
- }
- }.bind(this));
- }
- return promise;
- },
- _createSplitPaneView: function _createSplitPaneView(splitterItems) {
- if (!this.splitPaneView) {
- this.splitPaneView = new this._SplitPaneView({
- splitterItems: splitterItems,
- handlers: {
- getParentSize: this.handlers.getParentSize
- },
- dashboardApi: this.dashboardApi,
- services: this.services
- });
- this._setupHideHandles();
- return this.splitPaneView.render().then(function () {
- this.bottomNode.prepend(this.splitPaneView.$el.children()); //TODO shouldn't need to take the children
- return this.splitPaneView.onContainerReady();
- }.bind(this));
- }
- return Promise.resolve();
- },
- _setupHideHandles: function _setupHideHandles() {
- this.eventRouter.on('mode:change', function (payload) {
- switch (this.handlers.getSplitterState()) {
- case 'close':
- this.splitPaneView.close();
- break;
- case 'open':
- this.splitPaneView.open();
- break;
- case 'nochange':
- default:
- break;
- }
- this.consumeHideHandles.forEach(function (handle) {
- if (payload.authoring) {
- this.splitPaneView.showHandle(handle);
- } else {
- this.splitPaneView.hideHandle(handle);
- }
- }.bind(this));
- }, this);
- },
- // Used to set a persistent state for the session
- setState: function setState(state) {
- if (state.bottomNode.isOpen) {
- this.splitPaneView.open();
- } else {
- this.splitPaneView.close();
- }
- },
- // Used to get the state from the saved options object
- getState: function getState() {
- return {
- bottomNode: {
- isOpen: this.bottomNode[0].clientHeight > 0 ? true : false
- }
- };
- },
- /**
- * @param {String} name extension name
- * @returns a registered extensions
- */
- getExtension: function getExtension(name) {
- return this.views.get(name);
- }
- };
- function CanvasExtensions(options) {
- this.init(options);
- }
- Object.keys(_canvasExtensions).forEach(function (key) {
- CanvasExtensions.prototype[key] = _canvasExtensions[key];
- });
- return CanvasExtensions;
- });
- //# sourceMappingURL=CanvasExtensions.js.map
|