1 /*
  2  * ***** BEGIN LICENSE BLOCK *****
  3  * Zimbra Collaboration Suite Web Client
  4  * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 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, 2008, 2009, 2010, 2013, 2014, 2016 Synacor, Inc. All Rights Reserved.
 21  * ***** END LICENSE BLOCK *****
 22  */
 23 
 24 /**
 25  * @overview
 26  * 
 27  * This file defines an event.
 28  *
 29  */
 30 
 31 /**
 32  * Creates an empty event of the given type.
 33  * @class
 34  * This class represents an event that encapsulates some sort of change to a model (data).
 35  * The event has a data type (for example, conversation), an event type (for example, delete), a source (the
 36  * data object generating the event), and a hash of arbitrary information (details).
 37  * 
 38  * @param {constant}		type	the source of the event
 39  */
 40 ZmEvent = function(type) {
 41 
 42 	this.type = type;	// source type (conv, msg, contact, folder, etc)
 43 	this.event = null;	// event type (create, modify, etc)
 44 	this.source = null;	// notifying model (often a list)
 45 	this.item = null;	// item that is subject of the notification
 46 	this._details = {};
 47 }
 48 
 49 // Listener types
 50 ZmEvent.L_MODIFY = 1;
 51 ZmEvent.L_PICKER = 2;
 52 
 53 // Source types (note: there are not separate types for list models)
 54 ZmEvent.S_TAG			= "TAG";
 55 ZmEvent.S_PICKER		= "PICKER";
 56 ZmEvent.S_SEARCH		= "SEARCH";
 57 ZmEvent.S_SETTING		= "SETTING";
 58 ZmEvent.S_SETTINGS		= "SETTINGS";
 59 ZmEvent.S_SHARE         = "SHARE";
 60 ZmEvent.S_MOUNTPOINT	= "MOUNTPOINT";
 61 ZmEvent.S_ZIMLET		= "ZIMLET";
 62 
 63 // Event types
 64 /**
 65  * Defines the "create" event type.
 66  */
 67 ZmEvent.E_CREATE		= "CREATE";
 68 /**
 69  * Defines the "delete" event type.
 70  */
 71 ZmEvent.E_DELETE		= "DELETE";
 72 /**
 73  * Defines the "modify" event type.
 74  */
 75 ZmEvent.E_MODIFY		= "MODIFY";
 76 /**
 77  * Defines the "load" event type.
 78  */
 79 ZmEvent.E_LOAD			= "LOAD";
 80 /**
 81  * Defines the "remove" event type.
 82  */
 83 ZmEvent.E_REMOVE		= "REMOVE";
 84 /**
 85  * Defines the "remove all" event type.
 86  */
 87 ZmEvent.E_REMOVE_ALL	= "REMOVE ALL";
 88 /**
 89  * Defines the "move" event type.
 90  */
 91 ZmEvent.E_MOVE			= "MOVE";
 92 /**
 93  * Defines the "flags" event type.
 94  */
 95 ZmEvent.E_FLAGS			= "FLAGS";
 96 /**
 97  * Defines the "tags" event type.
 98  */
 99 ZmEvent.E_TAGS			= "TAGS";
100 /**
101  * Defines the "zimlets" event type.
102  */
103 ZmEvent.E_ZIMLETS		= "ZIMLET";
104 /**
105  * Defines the "complete" event type.
106  */
107 ZmEvent.E_COMPLETE		= "COMPLETE";
108 
109 // Public methods
110 
111 /**
112  * Returns a string representation of the object.
113  * 
114  * @return		{String}		a string representation of the object
115  */
116 ZmEvent.prototype.toString = 
117 function() {
118 	return "ZmEvent";
119 };
120 
121 /**
122  * Sets the event type and source.
123  *
124  * @param {constant}	event	the event type (see <code>ZmEvent.E_</code> constants)
125  * @param {Object}	source		the object that generated the event (typically "this")
126  */
127 ZmEvent.prototype.set =
128 function(event, source) {
129 	this.event = event;
130 	this.source = source;
131 	this.handled = false;
132 };
133 
134 /**
135  * Adds info to the event details.
136  *
137  * @param {String}		field		the detail name
138  * @param {Object}		value		the detail value
139  */
140 ZmEvent.prototype.setDetail =
141 function(field, value) {
142 	this._details[field] = value;
143 };
144 
145 /**
146  * Gets info from the event details.
147  *
148  * @param {String}	field		the detail field name
149  * @return	{Object}	the details
150  */
151 ZmEvent.prototype.getDetail =
152 function(field) {
153 	return this._details[field];
154 };
155 
156 /**
157  * Gets items by checking for a detail with a name of "items" and returning it.
158  * 
159  * @return	{Array}		an array of items or empty array if "items" does not exist
160  */
161 ZmEvent.prototype.getItems =
162 function() {
163     var items = this._details["items"];
164     return items ? items : [];
165 };
166 
167 /**
168  * Sets the event details. Any existing details will be lost.
169  *
170  * @param {Hash}	details		a hash representing event details
171  */
172 ZmEvent.prototype.setDetails =
173 function(details) {
174 	this._details = details ? details : {};
175 };
176 
177 /**
178  * Gets the event details.
179  * 
180  * @return	{Hash}	the event details
181  */
182 ZmEvent.prototype.getDetails =
183 function() {
184 	return this._details;
185 };
186