123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- '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([], function () {
- var _lifeCycleManager = {
- init: function init() {
- this._cycleEvents = {};
- },
- /**
- * Registers a LifeCycle callback
- *
- * @param {string} name - The name of the lifecycle hook provided by the dashboard
- * @param {function} handler - The callback handler to be invoked
- * @param {int} priority - The priority of the callback, higher numbers are executed first
- *
- */
- registerLifeCycleHandler: function registerLifeCycleHandler(name, handler, priority) {
- if (typeof handler !== 'function') {
- throw new Error('Action for ' + name + ' must be a function');
- }
- // Number.isInteger() is part of ES6 and therefore not supported on IE 11
- if (priority && !(typeof priority === 'number' && isFinite(priority) && Math.floor(priority) === priority)) {
- throw new Error('Priority for ' + name + ' must be an integer');
- }
- this._createEvent(name);
- var action = {
- handler: handler,
- priority: priority || 0
- };
- this._cycleEvents[name].actions.push(action);
- return {
- remove: function () {
- if (this._cycleEvents !== null && this._cycleEvents.hasOwnProperty(name)) {
- this._cycleEvents[name].actions = this._cycleEvents[name].actions.filter(function (element) {
- return element !== action;
- });
- }
- }.bind(this)
- };
- },
- /**
- * Invokes the LifeCycle callback handlers registered to the action
- * @param {string} name - Name of the life cycle action to invoke
- * @param {object} payload - Any data to pass to the action handlers
- *
- */
- invokeLifeCycleHandlers: function invokeLifeCycleHandlers(name, payload) {
- return Promise.resolve().then(function () {
- var promise = Promise.resolve();
- if (this._cycleEvents.hasOwnProperty(name)) {
- this._cycleEvents[name].actions.sort(function (a, b) {
- return b.priority - a.priority;
- });
- this._cycleEvents[name].actions.forEach(function (action) {
- promise = promise.then(action.handler.bind(action.handler, payload));
- });
- }
- return promise;
- }.bind(this));
- },
- destroy: function destroy() {
- this._cycleEvents = null;
- },
- _getCycleEvents: function _getCycleEvents() {
- return this._cycleEvents;
- },
- _createEvent: function _createEvent(eventName) {
- if (!this._cycleEvents.hasOwnProperty(eventName)) {
- this._cycleEvents[eventName] = {
- actions: []
- };
- }
- }
- };
- function LifeCycleManager(options) {
- this.init(options);
- }
- Object.keys(_lifeCycleManager).forEach(function (key) {
- LifeCycleManager.prototype[key] = _lifeCycleManager[key];
- });
- return LifeCycleManager;
- });
- //# sourceMappingURL=LifeCycleManager.js.map
|