      1 /**
      2  * @ignore
      3  */
      4 var Ozone = Ozone ? Ozone : {};
      5
      6 /**
      7  * @ignore
      8  * @namespace
      9  */
     10 Ozone.state = Ozone.state ? Ozone.state : {};
     11
     12 /**
     13  * @deprecated Since OWF 3.7.0  You should use <a href="#.getInstance">Ozone.state.WidgetState.getInstance</a>
     14
     15  * @constructor
     16  * @param {Object} [cfg] config object see below for properties
     17  * @param {String} [cfg.widgetEventingController]  The widgetEventingController
     18  * @param {Boolean} [cfg.autoInit] Whether or not to automatically start listening to the state channel.  Default is true.
     19  * @param {String} [cfg.widgetGuid]  The guid of the widget to monitor. Default is itself.
     20  * @param {String} [cfg.onStateEventReceived]  The callback function when an event is received.
     21  * @description The Ozone.state.WidgetState object manages the two-way communication between an OWF widget and its OWF Container.
     22  * @requires <a href="Ozone.eventing.Widget.html">Ozone.eventing.Widget</a> for eventing
     23  * @example
     24  *
     25  * var widgetState = new Ozone.state.WidgetState({
     26  *     onStateEventReceived: function(sender, msg) {
     27  *     		// do something
     28  *     }
     29  * });
     30  *
     31  */
     32 Ozone.state.WidgetState = function(cfg) {
     33
     34   if (Ozone.state.WidgetState.instance == null) {
     35
     36 	var STATE_EVENT_CHANNEL_NAME_prefix = "_WIDGET_STATE_CHANNEL_";
     37
     38     cfg = cfg || {};
     39
     40 	// initialize the state handling
     41     this.widgetEventingController = cfg.widgetEventingController || Ozone.eventing.Widget.instance;
     42 	this.widgetStateHandler = Ozone.state.WidgetStateHandler.getInstance(this.widgetEventingController);
     43 	this.widgetIdJSON = Ozone.util.parseJson(this.widgetEventingController.getWidgetId());
     44 	this.widgetGuid = cfg.widgetGuid ? cfg.widgetGuid : this.widgetIdJSON.id;
     45     this.stateChannel = STATE_EVENT_CHANNEL_NAME_prefix + this.widgetGuid;
     46 	this.onStateEventReceived = cfg.onStateEventReceived ? cfg.onStateEventReceived : this.onStateEventReceived;
     47
     48 	if (cfg.autoInit !== false) {
     49 		this.init();
     50 	}
     51
     52      Ozone.state.WidgetState.instance = this;
     53   }
     54
     55   return Ozone.state.WidgetState.instance;
     56 };
     57
     58 Ozone.state.WidgetState.prototype = {
     59 	/**
     60 	 * @field
     61 	 * @description version number
     62 	 */
     63 	version: Ozone.version.owfversion + Ozone.version.state,
     64
     65 	/**
     66      * @description Initializes the WidgetState object.  Using this function is only required if autoInit config is false
     67      * in the constructor.  This function is sometimes useful when it is necessary to defer event handling after
     68      * creating the Ozone.state.WidgetState object
     69      * @param {Object} [cfg] config object
     70      * @see <a href="#constructor">constructor</a>
     71      */
     72 	init : function () {
     73 		this.widgetEventingController.subscribe(this.stateChannel, this.onStateEventReceived);
     74 //		this.widgetEventingController.registerHandler(this.stateChannel, this.onStateEventReceived);
     75 	},
     76
     77 	/**
     78 	 * @description The default callback function when an event is received.
     79 	 */
     80 	onStateEventReceived: function () {
     81 		return true;
     82 	},
     83
     84 	getStateChannel: function () {
     85 		return this.stateChannel;
     86 	},
     87
     88 	/**
     89 	 * @description Gets current widget state.
     90      * @param {Object} [cfg] Config object. See below for properties.
     91      * @param {String} [cfg.guid]  Id of the widget whose state should be retrieved. Defaults to current widget guid.
     92      * @param {Function} [cfg.callback] Function to be called once after the state is retrieved.
     93      * This method will be passed the state object which has following properties. <br>
     94      * <br>
     95      *     {String} name: name of the widget <br>
     96      *     {Number} x: x-coordinate value of the top left corner of the widget<br>
     97      *     {Number} y: y-coordinate value of the top left corner of the widget<br>
     98      *     {Number} width: width of the widget <br>
     99      *     {Number} height: height of the widget <br>
    100      *     {Boolean} active: true if the widget is active, or else false. This property is applicable for desktop layout only. <br>
    101      *     {Boolean} maximized: true if the widget is maximized, or else false <br>
    102      *     {Boolean} minimized: true if the widget is minimized, or else false <br>
    103      *     {Boolean} collapsed: true if the widget is collapsed, or else false <br>
    104      *     {Boolean} singleton: true if the widget is singleton, or else false <br>
    105      *     {Boolean} pinned: true if the widget is pinned, or else false <br>
    106      *     {String} uniqueId: unique id of the widget <br>
    107      *     {String} widgetGuid: guid of the widget <br>
    108      *     {String} paneGuid: guid of the pane <br>
    109      *     {String} dashboardGuid: guid of the dashboard the widget is opened on <br>
    110      *     {String} region: region of the widget. This property is applicable for accordion layout only. Possible values for accordion layout are "accordion", "center" and "south". In other layouts, its value is "none". <br>
    111      *     {Number} columnPos: column the widget is opened in. This property is applicable for portal layout only. Possible values for portal layout are 0, 1 and 2. In other layouts, its value is 0. <br>
    112      *     {Number} zIndex: z-index of the widget. <br>
    113 	 *
    114 	 * @example
    115 	 *
    116 	 * widgetState.getWidgetState({
    117      *     callback: function(state) {
    118 	 *     		// Do something
    119 	 *     }
    120 	 * });
    121 	 *
    122 	 */
    123 	getWidgetState: function(cfg) {
    124 		cfg = cfg ? cfg : {};
    125
    126 		var config = {
    127 			fn: "getWidgetState",
    128 			params: {
    129 				guid: cfg.guid ? cfg.guid : this.widgetGuid
    130 			}
    131 		};
    132 		this.widgetStateHandler.handleWidgetRequest(config, cfg.callback);
    133 	},
    134
    135 	/**
    136 	 * @description Gets registered widget state events
    137      * @param {Object} [cfg] Config object. See below for properties.
    138      * @param {String} [cfg.guid]  Id of the widget whose events should be retrieved. Defaults to current widget guid.
    139      * @param {Function} [cfg.callback] Function to be called once after the stateWidget is executed.
    140 	 *
    141 	 * @example
    142 	 *
    143 	 * widgetState.getRegisteredStateEvents({
    144 	 *     callback: function(events) {
    145 	 *     		for (var i = 0; i < events.length; i++) {
    146 	 *     			// Do something
    147 	 *     		}
    148 	 *     }
    149 	 * });
    150 	 *
    151 	 */
    152 	getRegisteredStateEvents: function(cfg) {
    153 		cfg = cfg ? cfg : {};
    154
    155 		var config = {
    156 			fn: "getWidgetStateEvents",
    157 			params: {
    158 				guid: cfg.guid ? cfg.guid : this.widgetGuid
    159 			}
    160 		};
    161 		this.widgetStateHandler.handleWidgetRequest(config, cfg.callback);
    162 	},
    163
    164     /**
    165      * @description Activates a widget.
    166      * @param {Object} [cfg] Config object. See below for properties.
    167      * @param {String} [cfg.guid]  Id of the widget to activate. Defaults to current widget's guid.
    168      * @param {Function} [cfg.callback] Function to be called once after the widget has been activated.
    169      * This function is passed back the following parameters. <br>
    170      * <br>
    171      *          {Boolean} result: true if the widget has been activated, or else false.
    172      *
    173      * @example
    174      *
    175      * widgetState.activateWidget({
    176      *     guid: "GUID_OF_A_WIDGET",
    177      *     callback: function(result) {
    178      *     		// Do something
    179      *     }
    180      * });
    181      *
    182      */
    183     activateWidget: function(cfg) {
    184         cfg = cfg ? cfg : {};
    185
    186         var config = {
    187             fn: "activateWidget",
    188             params: {
    189                 guid: cfg.guid ? cfg.guid : this.widgetGuid
    190             }
    191         };
    192         this.widgetStateHandler.handleWidgetRequest(config, cfg.callback);
    193     },
    194
    195 	/**
    196 	 * @description Closes a widget.
    197      * @param {Object} [cfg] Config object. See below for properties.
    198      * @param {String} [cfg.guid]  Id of the widget to close. Defaults to current widget's guid.
    199      * @param {Function} [cfg.callback] Function to be called once after the widget is closed. Only use this if the widget is not closing itself.
    200      * This function is passed back the following parameters. <br>
    201      * <br>
    202      *          {Boolean} result: true if the widget has been closed, or else false.
    203 	 *
    204 	 * @example
    205 	 *
    206 	 * widgetState.closeWidget({
    207      *     guid: "GUID_OF_A_WIDGET",
    208      *     callback: function(result) {
    209 	 *     		// Do something
    210 	 *     }
    211 	 * });
    212 	 *
    213 	 */
    214 	closeWidget: function(cfg) {
    215 		cfg = cfg ? cfg : {};
    216
    217 		var config = {
    218 			fn: "closeWidget",
    219 			params: {
    220 				guid: cfg.guid ? cfg.guid : this.widgetGuid
    221 			}
    222 		};
    223 		this.widgetStateHandler.handleWidgetRequest(config, cfg.callback);
    224 	},
    225
    226 	/**
    227 	 * @description Adds custom state event handlers to listen to widget events.
    228      * @param {Object} [cfg] Config object. See below for properties.
    229      * @param {String} [cfg.guid]  Id of the widget whose events should be monitored. Defaults to current widget guid.
    230      * @param {Array} [cfg.events] Array of events. If no event is provided this method will add listeners to all registered events.
    231      * @param {Function} [cfg.callback] Function to be called once after the listener is added.
    232 	 *
    233 	 * @example
    234 	 *
    235 	 * widgetState.addStateEventListeners({
    236 	 * 	   events: ['beforeclose','maximize'],
    237 	 *     callback: function() {
    238 	 *     		// Do something
    239 	 *     }
    240 	 * });
    241 	 *
    242 	 */
    243 	addStateEventListeners: function(cfg) {
    244 		cfg = cfg ? cfg : {};
    245
    246 		var config = {
    247 			fn: "addStateEventListeners",
    248 			params: {
    249 				guid: cfg.guid ? cfg.guid : this.widgetGuid,
    250 				events: cfg.events
    251 			}
    252 		};
    253 		this.widgetStateHandler.handleWidgetRequest(config, cfg.callback);
    254 	},
    255
    256 	/**
    257 	 * @description Removes custom state event listeners from a widget.
    258      * @param {Object} [cfg] Config object. See below for properties.
    259      * @param {String} [cfg.guid]  Id of the widget whose events should be monitored. Defaults to current widget guid.
    260      * @param {Array} [cfg.events] Array of events. If no event is provided this method will remove only custom listeners from all registered events.
    261      * @param {Function} [cfg.callback] Function to be called once after the listener is removed.
    262 	 *
    263 	 * @example
    264 	 *
    265 	 * widgetState.removeStateEventListeners({
    266 	 * 	   events: ['beforeclose','maximize'],
    267 	 *     callback: function() {
    268 	 *     		// Do something
    269 	 *     }
    270 	 * });
    271 	 *
    272 	 */
    273 	removeStateEventListeners: function(cfg) {
    274 		cfg = cfg ? cfg : {};
    275
    276 		var config = {
    277 			fn: "removeStateEventListeners",
    278 			params: {
    279 				guid: cfg.guid ? cfg.guid : this.widgetGuid,
    280 				events: cfg.events
    281 			}
    282 		};
    283 		this.widgetStateHandler.handleWidgetRequest(config, cfg.callback);
    284 	},
    285
    286 	/**
    287 	 * @description Adds custom state event handlers to override a widget event.
    288      * @param {Object} [cfg] Config object. See below for properties.
    289      * @param {String} [cfg.guid]  Id of the widget whose events should be monitored. Defaults to current widget guid.
    290      * @param {Array} [cfg.events] Array of events. If no event is provided this method will add listeners to all registered events.
    291      * @param {Function} [cfg.callback] Function to be called once after the listener is added.
    292 	 *
    293 	 * @example
    294 	 *
    295 	 * widgetState.addStateEventOverrides({
    296 	 * 	   events: ['beforeclose','maximize'],
    297 	 *     callback: function() {
    298 	 *     		// Do something
    299 	 *     }
    300 	 * });
    301 	 *
    302 	 */
    303 	addStateEventOverrides: function(cfg) {
    304 		cfg = cfg ? cfg : {};
    305
    306 		var config = {
    307 			fn: "addStateEventOverrides",
    308 			params: {
    309 				guid: cfg.guid ? cfg.guid : this.widgetGuid,
    310 				events: cfg.events
    311 			}
    312 		};
    313 		this.widgetStateHandler.handleWidgetRequest(config, cfg.callback);
    314 	},
    315
    316 	/**
    317 	 * @description Removes custom state event listeners from a widget.
    318      * @param {Object} [cfg] Config object. See below for properties.
    319      * @param {String} [cfg.guid]  Id of the widget whose events should be monitored. Defaults to current widget guid.
    320      * @param {Array} [cfg.events] Array of events. If no event is provided this method will remove only custom listeners from all registered events.
    321      * @param {Function} [cfg.callback] Function to be called once after the listener is removed.
    322 	 *
    323 	 * @example
    324 	 *
    325 	 * widgetState.removeStateEventOverrides({
    326 	 * 	   events: ['beforeclose','maximize'],
    327 	 *     callback: function() {
    328 	 *     		// Do something
    329 	 *     }
    330 	 * });
    331 	 *
    332 	 */
    333 	removeStateEventOverrides: function(cfg) {
    334 		cfg = cfg ? cfg : {};
    335
    336 		var config = {
    337 			fn: "removeStateEventOverrides",
    338 			params: {
    339 				guid: cfg.guid ? cfg.guid : this.widgetGuid,
    340 				events: cfg.events
    341 			}
    342 		};
    343 		this.widgetStateHandler.handleWidgetRequest(config, cfg.callback);
    344 	}
    345
    346 };
    347
    348 /**
    349  * @param {Object} [cfg] config object see below for properties
    350  * @param {String} [cfg.widgetEventingController]  The widgetEventingController
    351  * @param {Boolean} [cfg.autoInit] Whether or not to automatically start listening to the state channel.  Default is true.
    352  * @param {String} [cfg.widgetGuid]  The guid of the widget to monitor. Default is itself.
    353  * @param {String} [cfg.onStateEventReceived]  The callback function when an event is received.
    354  * @description Retrieves Ozone.state.WidgetState Singleton instance. Manages the two-way communication between an OWF widget and its OWF Container.
    355  * @requires <a href="Ozone.eventing.Widget.html">Ozone.eventing.Widget</a> for eventing
    356  * @since OWF 3.7.0
    357  * @example
    358  * var widgetState = Ozone.state.WidgetState.getInstance({
    359  *     onStateEventReceived: function(sender, msg) {
    360  *          // do something
    361  *     }
    362  * });
    363  *
    364  */
    365 Ozone.state.WidgetState.getInstance = function(cfg) {
    366   if (Ozone.state.WidgetState.instance == null) {
    367     Ozone.state.WidgetState.instance = new Ozone.state.WidgetState(cfg);
    368   }
    369   return Ozone.state.WidgetState.instance;
    370 };
    371