1 /*
  2  * ***** BEGIN LICENSE BLOCK *****
  3  * Zimbra Collaboration Suite Web Client
  4  * Copyright (C) 2005, 2006, 2007, 2009, 2010, 2011, 2013, 2014, 2016 Synacor, Inc.
  5  *
  6  * The contents of this file are subject to the Common Public Attribution License Version 1.0 (the "License");
  7  * you may not use this file except in compliance with the License.
  8  * You may obtain a copy of the License at: https://www.zimbra.com/license
  9  * The License is based on the Mozilla Public License Version 1.1 but Sections 14 and 15
 10  * have been added to cover use of software over a computer network and provide for limited attribution
 11  * for the Original Developer. In addition, Exhibit A has been modified to be consistent with Exhibit B.
 12  *
 13  * Software distributed under the License is distributed on an "AS IS" basis,
 14  * WITHOUT WARRANTY OF ANY KIND, either express or implied.
 15  * See the License for the specific language governing rights and limitations under the License.
 16  * The Original Code is Zimbra Open Source Web Client.
 17  * The Initial Developer of the Original Code is Zimbra, Inc.  All rights to the Original Code were
 18  * transferred by Zimbra, Inc. to Synacor, Inc. on September 14, 2015.
 19  *
 20  * All portions of the code are Copyright (C) 2005, 2006, 2007, 2009, 2010, 2011, 2013, 2014, 2016 Synacor, Inc. All Rights Reserved.
 21  * ***** END LICENSE BLOCK *****
 22  */
 23 
 24 /**
 25  * @class
 26  * This class represents the event manager.
 27  * 
 28  * @private
 29  */
 30 AjxEventMgr = function() {
 31 	this._listeners = new Object();
 32 }
 33 
 34 /**
 35  * Returns a string representation of the object.
 36  * 
 37  * @return	{string}		a string representation of the object
 38  */
 39 AjxEventMgr.prototype.toString = 
 40 function() {
 41 	return "AjxEventMgr";
 42 }
 43 
 44 AjxEventMgr.prototype.addListener =
 45 function(eventType, listener, index) {
 46 	var lv = this._listeners[eventType];
 47 	if (lv == null) {
 48 		lv = this._listeners[eventType] = new AjxVector();
 49 	}         	 
 50 	if (!lv.contains(listener)) {
 51 		if (this._notifyingListeners) {
 52 			lv = this._listeners[eventType] = lv.clone();
 53 		}
 54 		lv.add(listener, index);
 55 		return true;
 56 	}
 57 	return false;
 58 }
 59 
 60 AjxEventMgr.prototype.notifyListeners =
 61 function(eventType, event) {
 62 	this._notifyingListeners = true;
 63 	var lv = this._listeners[eventType];
 64 	if (lv != null) {
 65 		var a = lv.getArray();
 66 		var s = lv.size();
 67 		var retVal = null;
 68 		var c = null;
 69 		for (var i = 0; i < s; i++) {
 70 			c = a[i];
 71 			// listener must be an AjxListener or a function
 72 			if (!(c && ((c instanceof AjxListener) || (typeof c == "function")))) {
 73 				continue;
 74 			}
 75 			retVal = c.handleEvent ? c.handleEvent(event) : c(event);
 76 			if (retVal === false) {
 77 				break;
 78 			}
 79 		}
 80 	}	
 81 	this._notifyingListeners = false;
 82 	return retVal;
 83 }
 84 
 85 AjxEventMgr.prototype.isListenerRegistered =
 86 function(eventType) {
 87 	var lv = this._listeners[eventType];
 88 	return (lv != null && lv.size() > 0);
 89 }
 90 
 91 AjxEventMgr.prototype.removeListener = 
 92 function(eventType, listener) {
 93 	var lv = this._listeners[eventType];
 94 	if (lv != null) {
 95 		if (this._notifyingListeners) {
 96 			lv = this._listeners[eventType] = lv.clone();
 97 		}
 98 		lv.remove(listener);
 99 		return true;
100 	}
101 	return false;
102 }
103 
104 AjxEventMgr.prototype.removeAll = 
105 function(eventType) {
106 	var lv = this._listeners[eventType];
107 	if (lv != null) {
108 		if (this._notifyingListeners) {
109 			lv = this._listeners[eventType] = lv.clone();
110 		}
111 		lv.removeAll();
112 		return true;
113 	}
114 	return false;
115 }
116 
117 AjxEventMgr.prototype.clearAllEvents =
118 function() {
119 	var listeners = this._listeners;
120     for (var eventType in listeners) {
121         this.removeAll(eventType);
122     }
123 };