1 /*
  2  * ***** BEGIN LICENSE BLOCK *****
  3  * Zimbra Collaboration Suite Web Client
  4  * Copyright (C) 2004, 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) 2004, 2005, 2006, 2007, 2009, 2010, 2011, 2013, 2014, 2016 Synacor, Inc. All Rights Reserved.
 21  * ***** END LICENSE BLOCK *****
 22  */
 23 
 24 /**
 25  * @overview
 26  * 
 27  * This class represents a data model which can process change events.
 28  *
 29  */
 30 
 31 /**
 32  * Creates the data model.
 33  * @class
 34  * This class represents a data model which can process change events.
 35  * 
 36  * @author Conrad Damon
 37  *
 38  * @param {constant}		type	the event source type {@see ZmEvent}
 39  */
 40 ZmModel = function(type) {
 41  	if (arguments.length == 0) return;
 42 
 43 	this._evt = new ZmEvent(type);
 44 	this._evtMgr = new AjxEventMgr();
 45 }
 46 
 47 ZmModel.prototype.isZmModel = true;
 48 ZmModel.prototype.toString = function() { return "ZmModel"; }
 49 
 50 /**
 51 * Adds a change listener.
 52 *
 53 * @param {AjxListener}	listener	the change listener to add
 54 */
 55 ZmModel.prototype.addChangeListener = 
 56 function(listener) {
 57 	return this._evtMgr.addListener(ZmEvent.L_MODIFY, listener);
 58 }
 59 
 60 /**
 61 * Removes the given change listener.
 62 *
 63 * @param {AjxListener}	listener		the change listener to remove
 64 */
 65 ZmModel.prototype.removeChangeListener = 
 66 function(listener) {
 67 	return this._evtMgr.removeListener(ZmEvent.L_MODIFY, listener);    	
 68 }
 69 
 70 /**
 71 * Removes all change listeners.
 72 * 
 73 */
 74 ZmModel.prototype.removeAllChangeListeners = 
 75 function() {
 76 	return this._evtMgr.removeAll(ZmEvent.L_MODIFY);    	
 77 }
 78 
 79 /**
 80 * Notifies listeners of the given change event.
 81 *
 82 * @param {constant}		event		the event type {@see ZmEvent}
 83 * @param {Hash}			details		additional information
 84 * 
 85 * @private
 86 */
 87 ZmModel.prototype._notify =
 88 function(event, details) {
 89 	if (this._evtMgr.isListenerRegistered(ZmEvent.L_MODIFY)) {
 90 		this._evt.set(event, this);
 91 		this._evt.setDetails(details);
 92 		this._evtMgr.notifyListeners(ZmEvent.L_MODIFY, this._evt);
 93 	}
 94 };
 95 
 96 /**
 97  * @private
 98  */
 99 ZmModel.notifyEach =
100 function(list, event, details) {
101 	if (!(list && list.length)) { return; }
102 	for (var i = 0; i < list.length; i++) {
103 		list[i]._notify(event, details);
104 	}
105 };
106