1 | define(["require", "exports", "./object"], function (require, exports, object_1) {
|
2 | "use strict";
|
3 | Object.defineProperty(exports, "__esModule", { value: true });
|
4 | |
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | var EventGroup = (function () {
|
16 |
|
17 | function EventGroup(parent) {
|
18 | this._id = EventGroup._uniqueId++;
|
19 | this._parent = parent;
|
20 | this._eventRecords = [];
|
21 | }
|
22 | |
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | EventGroup.raise = function (target, eventName, eventArgs, bubbleEvent) {
|
29 | var retVal;
|
30 | if (EventGroup._isElement(target)) {
|
31 | if (typeof document !== 'undefined' && document.createEvent) {
|
32 | var ev = document.createEvent('HTMLEvents');
|
33 | ev.initEvent(eventName, bubbleEvent || false, true);
|
34 | object_1.assign(ev, eventArgs);
|
35 | retVal = target.dispatchEvent(ev);
|
36 | }
|
37 | else if (typeof document !== 'undefined' && document.createEventObject) {
|
38 |
|
39 | var evObj = document.createEventObject(eventArgs);
|
40 |
|
41 | target.fireEvent('on' + eventName, evObj);
|
42 | }
|
43 | }
|
44 | else {
|
45 | while (target && retVal !== false) {
|
46 | var events = target.__events__;
|
47 | var eventRecords = events ? events[eventName] : null;
|
48 | if (eventRecords) {
|
49 | for (var id in eventRecords) {
|
50 | if (eventRecords.hasOwnProperty(id)) {
|
51 | var eventRecordList = eventRecords[id];
|
52 | for (var listIndex = 0; retVal !== false && listIndex < eventRecordList.length; listIndex++) {
|
53 | var record = eventRecordList[listIndex];
|
54 | if (record.objectCallback) {
|
55 | retVal = record.objectCallback.call(record.parent, eventArgs);
|
56 | }
|
57 | }
|
58 | }
|
59 | }
|
60 | }
|
61 |
|
62 | target = bubbleEvent ? target.parent : null;
|
63 | }
|
64 | }
|
65 | return retVal;
|
66 | };
|
67 | EventGroup.isObserved = function (target, eventName) {
|
68 | var events = target && target.__events__;
|
69 | return !!events && !!events[eventName];
|
70 | };
|
71 |
|
72 | EventGroup.isDeclared = function (target, eventName) {
|
73 | var declaredEvents = target && target.__declaredEvents;
|
74 | return !!declaredEvents && !!declaredEvents[eventName];
|
75 | };
|
76 | EventGroup.stopPropagation = function (event) {
|
77 | if (event.stopPropagation) {
|
78 | event.stopPropagation();
|
79 | }
|
80 | else {
|
81 |
|
82 | event.cancelBubble = true;
|
83 | }
|
84 | };
|
85 | EventGroup._isElement = function (target) {
|
86 | return (!!target && (!!target.addEventListener || (typeof HTMLElement !== 'undefined' && target instanceof HTMLElement)));
|
87 | };
|
88 | EventGroup.prototype.dispose = function () {
|
89 | if (!this._isDisposed) {
|
90 | this._isDisposed = true;
|
91 | this.off();
|
92 | this._parent = null;
|
93 | }
|
94 | };
|
95 |
|
96 | EventGroup.prototype.onAll = function (target, events, useCapture) {
|
97 | for (var eventName in events) {
|
98 | if (events.hasOwnProperty(eventName)) {
|
99 | this.on(target, eventName, events[eventName], useCapture);
|
100 | }
|
101 | }
|
102 | };
|
103 | |
104 |
|
105 |
|
106 |
|
107 | EventGroup.prototype.on = function (target, eventName, callback, options) {
|
108 | var _this = this;
|
109 | if (eventName.indexOf(',') > -1) {
|
110 | var events = eventName.split(/[ ,]+/);
|
111 | for (var i = 0; i < events.length; i++) {
|
112 | this.on(target, events[i], callback, options);
|
113 | }
|
114 | }
|
115 | else {
|
116 | var parent_1 = this._parent;
|
117 | var eventRecord = {
|
118 | target: target,
|
119 | eventName: eventName,
|
120 | parent: parent_1,
|
121 | callback: callback,
|
122 | options: options,
|
123 | };
|
124 |
|
125 | var events = (target.__events__ = target.__events__ || {});
|
126 | events[eventName] =
|
127 | events[eventName] ||
|
128 | {
|
129 | count: 0,
|
130 | };
|
131 | events[eventName][this._id] = events[eventName][this._id] || [];
|
132 | events[eventName][this._id].push(eventRecord);
|
133 | events[eventName].count++;
|
134 | if (EventGroup._isElement(target)) {
|
135 | var processElementEvent = function () {
|
136 | var args = [];
|
137 | for (var _i = 0; _i < arguments.length; _i++) {
|
138 | args[_i] = arguments[_i];
|
139 | }
|
140 | if (_this._isDisposed) {
|
141 | return;
|
142 | }
|
143 | var result;
|
144 | try {
|
145 | result = callback.apply(parent_1, args);
|
146 | if (result === false && args[0]) {
|
147 | var e = args[0];
|
148 | if (e.preventDefault) {
|
149 | e.preventDefault();
|
150 | }
|
151 | if (e.stopPropagation) {
|
152 | e.stopPropagation();
|
153 | }
|
154 | e.cancelBubble = true;
|
155 | }
|
156 | }
|
157 | catch (e) {
|
158 |
|
159 | }
|
160 | return result;
|
161 | };
|
162 | eventRecord.elementCallback = processElementEvent;
|
163 | if (target.addEventListener) {
|
164 | target.addEventListener(eventName, processElementEvent, options);
|
165 | }
|
166 | else if (target.attachEvent) {
|
167 |
|
168 | target.attachEvent('on' + eventName, processElementEvent);
|
169 | }
|
170 | }
|
171 | else {
|
172 | var processObjectEvent = function () {
|
173 | var args = [];
|
174 | for (var _i = 0; _i < arguments.length; _i++) {
|
175 | args[_i] = arguments[_i];
|
176 | }
|
177 | if (_this._isDisposed) {
|
178 | return;
|
179 | }
|
180 | return callback.apply(parent_1, args);
|
181 | };
|
182 | eventRecord.objectCallback = processObjectEvent;
|
183 | }
|
184 |
|
185 | this._eventRecords.push(eventRecord);
|
186 | }
|
187 | };
|
188 | EventGroup.prototype.off = function (target, eventName, callback, options) {
|
189 | for (var i = 0; i < this._eventRecords.length; i++) {
|
190 | var eventRecord = this._eventRecords[i];
|
191 | if ((!target || target === eventRecord.target) &&
|
192 | (!eventName || eventName === eventRecord.eventName) &&
|
193 | (!callback || callback === eventRecord.callback) &&
|
194 | (typeof options !== 'boolean' || options === eventRecord.options)) {
|
195 | var events = eventRecord.target.__events__;
|
196 | var targetArrayLookup = events[eventRecord.eventName];
|
197 | var targetArray = targetArrayLookup ? targetArrayLookup[this._id] : null;
|
198 |
|
199 | if (targetArray) {
|
200 | if (targetArray.length === 1 || !callback) {
|
201 | targetArrayLookup.count -= targetArray.length;
|
202 | delete events[eventRecord.eventName][this._id];
|
203 | }
|
204 | else {
|
205 | targetArrayLookup.count--;
|
206 | targetArray.splice(targetArray.indexOf(eventRecord), 1);
|
207 | }
|
208 | if (!targetArrayLookup.count) {
|
209 | delete events[eventRecord.eventName];
|
210 | }
|
211 | }
|
212 | if (eventRecord.elementCallback) {
|
213 | if (eventRecord.target.removeEventListener) {
|
214 | eventRecord.target.removeEventListener(eventRecord.eventName, eventRecord.elementCallback, eventRecord.options);
|
215 | }
|
216 | else if (eventRecord.target.detachEvent) {
|
217 |
|
218 | eventRecord.target.detachEvent('on' + eventRecord.eventName, eventRecord.elementCallback);
|
219 | }
|
220 | }
|
221 | this._eventRecords.splice(i--, 1);
|
222 | }
|
223 | }
|
224 | };
|
225 |
|
226 | EventGroup.prototype.raise = function (eventName, eventArgs, bubbleEvent) {
|
227 | return EventGroup.raise(this._parent, eventName, eventArgs, bubbleEvent);
|
228 | };
|
229 |
|
230 | EventGroup.prototype.declare = function (event) {
|
231 | var declaredEvents = (this._parent.__declaredEvents = this._parent.__declaredEvents || {});
|
232 | if (typeof event === 'string') {
|
233 | declaredEvents[event] = true;
|
234 | }
|
235 | else {
|
236 | for (var i = 0; i < event.length; i++) {
|
237 | declaredEvents[event[i]] = true;
|
238 | }
|
239 | }
|
240 | };
|
241 | EventGroup._uniqueId = 0;
|
242 | return EventGroup;
|
243 | }());
|
244 | exports.EventGroup = EventGroup;
|
245 | });
|
246 |
|
\ | No newline at end of file |