1 /** |
|
2 * Observable.js |
|
3 * |
|
4 * Copyright, Moxiecode Systems AB |
|
5 * Released under LGPL License. |
|
6 * |
|
7 * License: http://www.tinymce.com/license |
|
8 * Contributing: http://www.tinymce.com/contributing |
|
9 */ |
|
10 |
|
11 /** |
|
12 * This mixin will add event binding logic to classes. |
|
13 * |
|
14 * @mixin tinymce.util.Observable |
|
15 */ |
|
16 define("tinymce/util/Observable", [ |
|
17 "tinymce/util/EventDispatcher" |
|
18 ], function(EventDispatcher) { |
|
19 function getEventDispatcher(obj) { |
|
20 if (!obj._eventDispatcher) { |
|
21 obj._eventDispatcher = new EventDispatcher({ |
|
22 scope: obj, |
|
23 toggleEvent: function(name, state) { |
|
24 if (EventDispatcher.isNative(name) && obj.toggleNativeEvent) { |
|
25 obj.toggleNativeEvent(name, state); |
|
26 } |
|
27 } |
|
28 }); |
|
29 } |
|
30 |
|
31 return obj._eventDispatcher; |
|
32 } |
|
33 |
|
34 return { |
|
35 /** |
|
36 * Fires the specified event by name. |
|
37 * |
|
38 * @method fire |
|
39 * @param {String} name Name of the event to fire. |
|
40 * @param {Object?} args Event arguments. |
|
41 * @param {Boolean?} bubble True/false if the event is to be bubbled. |
|
42 * @return {Object} Event args instance passed in. |
|
43 * @example |
|
44 * instance.fire('event', {...}); |
|
45 */ |
|
46 fire: function(name, args, bubble) { |
|
47 var self = this; |
|
48 |
|
49 // Prevent all events except the remove event after the instance has been removed |
|
50 if (self.removed && name !== "remove") { |
|
51 return args; |
|
52 } |
|
53 |
|
54 args = getEventDispatcher(self).fire(name, args, bubble); |
|
55 |
|
56 // Bubble event up to parents |
|
57 if (bubble !== false && self.parent) { |
|
58 var parent = self.parent(); |
|
59 while (parent && !args.isPropagationStopped()) { |
|
60 parent.fire(name, args, false); |
|
61 parent = parent.parent(); |
|
62 } |
|
63 } |
|
64 |
|
65 return args; |
|
66 }, |
|
67 |
|
68 /** |
|
69 * Binds an event listener to a specific event by name. |
|
70 * |
|
71 * @method on |
|
72 * @param {String} name Event name or space separated list of events to bind. |
|
73 * @param {callback} callback Callback to be executed when the event occurs. |
|
74 * @param {Boolean} first Optional flag if the event should be prepended. Use this with care. |
|
75 * @return {Object} Current class instance. |
|
76 * @example |
|
77 * instance.on('event', function(e) { |
|
78 * // Callback logic |
|
79 * }); |
|
80 */ |
|
81 on: function(name, callback, prepend) { |
|
82 return getEventDispatcher(this).on(name, callback, prepend); |
|
83 }, |
|
84 |
|
85 /** |
|
86 * Unbinds an event listener to a specific event by name. |
|
87 * |
|
88 * @method off |
|
89 * @param {String?} name Name of the event to unbind. |
|
90 * @param {callback?} callback Callback to unbind. |
|
91 * @return {Object} Current class instance. |
|
92 * @example |
|
93 * // Unbind specific callback |
|
94 * instance.off('event', handler); |
|
95 * |
|
96 * // Unbind all listeners by name |
|
97 * instance.off('event'); |
|
98 * |
|
99 * // Unbind all events |
|
100 * instance.off(); |
|
101 */ |
|
102 off: function(name, callback) { |
|
103 return getEventDispatcher(this).off(name, callback); |
|
104 }, |
|
105 |
|
106 /** |
|
107 * Bind the event callback and once it fires the callback is removed. |
|
108 * |
|
109 * @method once |
|
110 * @param {String} name Name of the event to bind. |
|
111 * @param {callback} callback Callback to bind only once. |
|
112 * @return {Object} Current class instance. |
|
113 */ |
|
114 once: function(name, callback) { |
|
115 return getEventDispatcher(this).once(name, callback); |
|
116 }, |
|
117 |
|
118 /** |
|
119 * Returns true/false if the object has a event of the specified name. |
|
120 * |
|
121 * @method hasEventListeners |
|
122 * @param {String} name Name of the event to check for. |
|
123 * @return {Boolean} true/false if the event exists or not. |
|
124 */ |
|
125 hasEventListeners: function(name) { |
|
126 return getEventDispatcher(this).has(name); |
|
127 } |
|
128 }; |
|
129 }); |
|