1 /**
  2  * The MIT License (MIT)
  3  *
  4  * Copyright (c) 2016 DeNA Co., Ltd.
  5  *
  6  * Permission is hereby granted, free of charge, to any person obtaining a copy
  7  * of this software and associated documentation files (the "Software"), to deal
  8  * in the Software without restriction, including without limitation the rights
  9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 10  * copies of the Software, and to permit persons to whom the Software is
 11  * furnished to do so, subject to the following conditions:
 12  *
 13  * The above copyright notice and this permission notice shall be included in
 14  * all copies or substantial portions of the Software.
 15  *
 16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 22  * SOFTWARE.
 23  */
 24 
 25 /// <reference path="base.js"/>
 26 /// <reference path="object.js"/>
 27 
 28 /**
 29  * A base class for all CreateJS events. A CreateJS event is an event dispatched
 30  * through CreateJS objects. It consists of events encapsulating DOM events
 31  * (e.g. mouse events, load events, etc.) and events fired by CreateJS objects
 32  * (e.g. animation events, tick events.) It is slow to bubble events on old
 33  * Android devices (4.3 or earlier) and this class currently disables event
 34  * bubbling.
 35  * @param {string} type
 36  * @param {boolean} bubbles
 37  * @param {boolean} cancelable
 38  * @extends {createjs.Object}
 39  * @constructor
 40  */
 41 createjs.Event = function(type, bubbles, cancelable) {
 42   /// <param type="string" name="type"/>
 43   /// <param type="boolean" name="bubbles"/>
 44   /// <param type="boolean" name="cancelable"/>
 45   /**
 46    * The type of this event.
 47    * @type {string}
 48    */
 49   this.type = type;
 50 };
 51 createjs.inherits('Event', createjs.Event, createjs.Object);
 52 
 53 /**
 54  * Key codes for common characters.
 55  * @enum {number}
 56  */
 57 createjs.Event.KeyCodes = {
 58   BACKSPACE: 8,
 59   TAB: 9,
 60   ENTER: 13,
 61   SHIFT: 16,
 62   CTRL: 17,
 63   ALT: 18,
 64   PAUSE: 19,
 65   CAPS_LOCK: 20,
 66   ESC: 27,
 67   SPACE: 32,
 68   PAGE_UP: 33,
 69   PAGE_DOWN: 34,
 70   END: 35,
 71   HOME: 36,
 72   LEFT: 37,
 73   UP: 38,
 74   RIGHT: 39,
 75   DOWN: 40,
 76   PRINT_SCREEN: 44,
 77   INSERT: 45,
 78   DELETE: 46,
 79   ZERO: 48,
 80   ONE: 49,
 81   TWO: 50,
 82   THREE: 51,
 83   FOUR: 52,
 84   FIVE: 53,
 85   SIX: 54,
 86   SEVEN: 55,
 87   EIGHT: 56,
 88   NINE: 57,
 89   QUESTION_MARK: 63,
 90   A: 65,
 91   B: 66,
 92   C: 67,
 93   D: 68,
 94   E: 69,
 95   F: 70,
 96   G: 71,
 97   H: 72,
 98   I: 73,
 99   J: 74,
100   K: 75,
101   L: 76,
102   M: 77,
103   N: 78,
104   O: 79,
105   P: 80,
106   Q: 81,
107   R: 82,
108   S: 83,
109   T: 84,
110   U: 85,
111   V: 86,
112   W: 87,
113   X: 88,
114   Y: 89,
115   Z: 90,
116   META: 91,
117   WIN_KEY_RIGHT: 92,
118   CONTEXT_MENU: 93,
119   NUM_ZERO: 96,
120   NUM_ONE: 97,
121   NUM_TWO: 98,
122   NUM_THREE: 99,
123   NUM_FOUR: 100,
124   NUM_FIVE: 101,
125   NUM_SIX: 102,
126   NUM_SEVEN: 103,
127   NUM_EIGHT: 104,
128   NUM_NINE: 105,
129   NUM_MULTIPLY: 106,
130   NUM_PLUS: 107,
131   NUM_MINUS: 109,
132   NUM_PERIOD: 110,
133   NUM_DIVIDE: 111,
134   F1: 112,
135   F2: 113,
136   F3: 114,
137   F4: 115,
138   F5: 116,
139   F6: 117,
140   F7: 118,
141   F8: 119,
142   F9: 120,
143   F10: 121,
144   F11: 122,
145   F12: 123,
146   NUMLOCK: 144,
147   SCROLL_LOCK: 145,
148   SEMICOLON: 186,
149   DASH: 189,
150   EQUALS: 187,
151   COMMA: 188,
152   PERIOD: 190,
153   SLASH: 191,
154   APOSTROPHE: 192,
155   TILDE: 192,
156   SINGLE_QUOTE: 222,
157   OPEN_SQUARE_BRACKET: 219,
158   BACKSLASH: 220,
159   CLOSE_SQUARE_BRACKET: 221,
160   WIN_KEY: 224
161 };
162 
163 /**
164  * An interface that adds event listeners or removes them. (This interface is
165  * for removing a cycling dependency.)
166  * @interface
167  */
168 createjs.Event.Target = function() {};
169 
170 /**
171  * Adds the specified event listener. Note that this method does not remove
172  * existing listeners even when the specified listener has been already added.
173  * So, adding a function three times results that the function is called three
174  * times as listed in the following code snippet.
175  *
176  *   function handleClick(event) {
177  *     // This function will be called three times on click.
178  *   }
179  *   displayObject.on('click', handleClick);
180  *   displayObject.on('click', handleClick);
181  *   displayObject.on('click', handleClick);
182  *
183  * @param {string} type
184  * @param {Function|Object} listener
185  * @param {Object=} opt_scope
186  * @param {boolean=} opt_once
187  * @param {*=} opt_data
188  * @param {boolean=} opt_useCapture
189  * @return {Function|Object}
190  */
191 createjs.Event.Target.prototype.on =
192     function(type, listener, opt_scope, opt_once, opt_data, opt_useCapture) {
193   /// <signature>
194   ///   <param type="string" name="type"/>
195   ///   <param type="Function" name="callback"/>
196   ///   <param type="Object" optional="true" name="opt_scope"/>
197   ///   <param type="boolean" optional="true" name="opt_once"/>
198   ///   <param optional="true" optional="true" name="opt_data"/>
199   ///   <param type="boolean" optional="true" name="opt_useCapture"/>
200   ///   <returns type="Function"/>
201   /// </signature>
202   /// <signature>
203   ///   <param type="string" name="type"/>
204   ///   <param type="Object" name="listener"/>
205   ///   <param type="Object" optional="true" name="opt_scope"/>
206   ///   <param type="boolean" optional="true" name="opt_once"/>
207   ///   <param optional="true" optional="true" name="opt_data"/>
208   ///   <param type="boolean" optional="true" name="opt_useCapture"/>
209   ///   <returns type="Object"/>
210   /// </signature>
211 };
212 
213 /**
214  * Removes the specified event listener. Note that this method needs the exact
215  * function object used in adding an event listener. If a closure function is
216  * added to an event, the closure function itself must be used and another new
217  * proxy or closure does not remove the handler as listed in the following code
218  * snippet.
219  *
220  *   function closure(fn, scope) {
221  *     return function() { fn.call(scope); };
222  *   }
223  *   function handleClick(event) {
224  *     // The following code does not remove this method.
225  *     displayObject.off('click', closure(handleClick, window));
226  *   }
227  *   displayObject.on('click', closure(handleClick, window));
228  *
229  * The 'Event.prototype.remove()' method can remove such closure functions as
230  * listed in the following code snippet.
231  *
232  *   function closure(fn, scope) {
233  *     return function() { fn.call(scope); };
234  *   }
235  *   function handleClick(event) {
236  *     // This event listener will be removed when it returns.
237  *     event.remove();
238  *   }
239  *   displayObject.on('click', closure(handleClick, window));
240  *
241  * @param {string} type
242  * @param {Function|Object} listener
243  * @param {boolean=} opt_useCapture
244  */
245 createjs.Event.Target.prototype.off =
246     function(type, listener, opt_useCapture) {
247   /// <signature>
248   ///   <param type="string" name="type"/>
249   ///   <param type="Function" name="callback"/>
250   ///   <param type="boolean" optional="true" name="opt_useCapture"/>
251   ///   <returns type="Function"/>
252   /// </signature>
253   /// <signature>
254   ///   <param type="string" name="type"/>
255   ///   <param type="Object" name="listener"/>
256   ///   <param type="boolean" optional="true" name="opt_useCapture"/>
257   ///   <returns type="Object"/>
258   /// </signature>
259 };
260 
261 /**
262  * Indicates whether the event will bubble through CreateJS objects. (This
263  * feature is disabled.)
264  * @type {boolean}
265  */
266 createjs.Event.prototype.bubbles = false;
267  
268 /**
269  * Indicates whether the Event.prototype.preventDefault() method can prevent the
270  * default behavior of an event. For CreateJS events encapsulating DOM events,
271  * their default behaviors are dispatching DOM events to the ancestors of the
272  * <canvas> element that receives them. For CreateJS events fired by CreateJS
273  * objects, they do not have default behaviors, i.e. their default behaviors are
274  * "do nothing".
275  * @const {boolean}
276  */
277 createjs.Event.prototype.cancelable = true;
278 
279 /**
280  * Indicates the current event phase:
281  * 1. Capture phase: starting from the top parent to the target;
282  * 2. at target phase: currently being dispatched from the target;
283  * 3. bubbling phase: from the target to the top parent.
284  * @const {number}
285  */
286 createjs.Event.prototype.eventPhase = 2;
287 
288 /**
289  * The epoch time at which this event was created.
290  * (This event is not exported to games.)
291  * @type {number}
292  */
293 createjs.Event.prototype.timeStamp = 0;
294 
295 /**
296  * Whether a handler has called the Event.prototype.preventDefault() method.
297  * (This event is not exported to games.)
298  * @type {boolean}
299  */
300 createjs.Event.prototype.defaultPrevented = false;
301 
302 /**
303  * Whether a handler has called the Event.prototype.stopPropagation() method or
304  * the Event.prototype.stopImmediatePropagation() method. (This event is not
305  * exported to games.)
306  * @type {boolean}
307  */
308 createjs.Event.prototype.propagationStopped = false;
309 
310 /**
311  * Whether a handler has called the Event.stopImmediatePropagation() method.
312  * (This event is not exported to games.)
313  * @type {boolean}
314  */
315 createjs.Event.prototype.immediatePropagationStopped = false;
316 
317 /**
318  * Whether a handler has called the Event.prototype.remove() method. (This event
319  * is not exported to games.)
320  * @type {boolean}
321  */
322 createjs.Event.prototype.removed = false;
323 
324 /**
325  * The current event target, i.e. an object that this event is being dispatched
326  * from.
327  * @type {createjs.Event.Target}
328  * @private
329  */
330 createjs.Event.prototype.currentTarget_ = null;
331 
332 /**
333  * Prevents the default action of this event.
334  * @const
335  */
336 createjs.Event.prototype['preventDefault'] = function() {
337   this.defaultPrevented = true;
338 };
339 
340 /**
341  * Stops the event propagation.
342  * @const
343  */
344 createjs.Event.prototype['stopPropagation'] = function() {
345   this.propagationStopped = true;
346 };
347 
348 /**
349  * Stops the immediate event propagation.
350  * @const
351  */
352 createjs.Event.prototype.stopImmediatePropagation = function() {
353   this.immediatePropagationStopped = this.propagationStopped = true;
354 };
355   
356 /**
357  * Resets the properties used by event targets. To reduce the number of new
358  * createjs.Event() calls, some event listeners (in this library) use object
359  * spools. This method is used by such event dispatchers to clean up an used
360  * event.
361  * @const
362  */
363 createjs.Event.prototype.resetProperties = function() {
364   this['target'] = null;
365   this['currentTarget'] = null;
366   this.currentTarget_ = null;
367 };
368 
369 /**
370  * Sets properties used by event targets.
371  * @param {createjs.Event.Target} target
372  * @param {number} phase
373  * @const
374  */
375 createjs.Event.prototype.setProperties = function(target, phase) {
376   /// <param type="createjs.Event.Target" name="target"/>
377   /// <param type="number" name="phase"/>
378   this['target'] = target;
379   this['currentTarget'] = target;
380   this.currentTarget_ = target;
381 };
382 
383 /**
384  * Removes the event listener which receives this event after returning from it.
385  * @const
386  */
387 createjs.Event.prototype['remove'] = function() {
388   this.removed = true;
389 };
390   
391 /**
392  * Adds the specified event listener to the target of this event.
393  * @param {string} type
394  * @param {Function|Object} listener
395  * @return {Function|Object}
396  * @const
397  */
398 createjs.Event.prototype['addEventListener'] = function(type, listener) {
399   /// <signature>
400   ///   <param type="string" name="type"/>
401   ///   <param type="Function" name="callback"/>
402   ///   <returns type="Function"/>
403   /// <signature>
404   /// <signature>
405   ///   <param type="string" name="type"/>
406   ///   <param type="Object" name="listener"/>
407   ///   <returns type="Object"/>
408   /// <signature>
409   return this.currentTarget_.on(type, listener);
410 };
411 
412 /**
413  * Removes the specified event listener from the target of this event.
414  * @param {string} type
415  * @param {Function|Object} listener
416  * @const
417  */
418 createjs.Event.prototype['removeEventListener'] = function(type, listener) {
419   /// <signature>
420   ///   <param type="string" name="type"/>
421   ///   <param type="Function" name="callback"/>
422   /// </signature>
423   /// <signature>
424   ///   <param type="string" name="type"/>
425   ///   <param type="Object" name="listener"/>
426   /// </signature>
427   this.currentTarget_.off(type, listener);
428 };
429