]>
Commit | Line | Data |
---|---|---|
0982546d BB |
1 | Module('CustomEventSupport')({ |
2 | ||
3 | eventListeners : null, | |
4 | ||
5 | bind : function(type, eventHandler) { | |
6 | var found, i, listeners; | |
7 | ||
8 | if(!this.eventListeners) { | |
9 | this.eventListeners = {}; | |
10 | } | |
11 | ||
12 | if(!this.eventListeners[type]) { | |
13 | this.eventListeners[type] = []; | |
14 | } | |
15 | ||
16 | found = false; | |
17 | ||
18 | listeners = this.eventListeners[type]; | |
19 | for (i = 0; i < listeners.length; i++) { | |
20 | if (listeners[i] === eventHandler) { | |
21 | found = true; | |
22 | break; | |
23 | } | |
24 | } | |
25 | ||
26 | if(!found) { | |
27 | this.eventListeners[type].push(eventHandler); | |
28 | } | |
29 | ||
30 | return this; | |
31 | }, | |
32 | ||
33 | unbind : function(type, eventHandler) { | |
34 | var i, found, listeners; | |
35 | ||
36 | found = false; | |
37 | ||
38 | if(!this.eventListeners) { | |
39 | this.eventListeners = {}; | |
40 | } | |
41 | ||
42 | if(typeof eventHandler == 'undefined') { | |
43 | this.eventListeners[type] = []; | |
44 | } | |
45 | ||
46 | listeners = this.eventListeners[type]; | |
47 | for (i = 0; i < listeners.length; i++) { | |
48 | if(listeners[i] === eventHandler) { | |
49 | found = true; | |
50 | break; | |
51 | } | |
52 | } | |
53 | ||
54 | if(found) { | |
55 | this.eventListeners[type].splice(i, 1); | |
56 | } | |
57 | ||
58 | return this; | |
59 | }, | |
60 | ||
61 | dispatch : function(type, data) { | |
62 | var event, listeners, instance, i; | |
63 | ||
64 | if (this.eventListeners === null) { | |
65 | this.eventListeners = {}; | |
66 | } | |
67 | ||
68 | if (typeof data === 'undefined') { | |
69 | data = {}; | |
70 | } | |
71 | ||
72 | if (data.hasOwnProperty('target') === false) { | |
73 | data.target = this; | |
74 | } | |
75 | ||
76 | event = new CustomEvent(type, data); | |
77 | listeners = this.eventListeners[type] || []; | |
78 | instance = this; | |
79 | ||
80 | for (i = 0; i < listeners.length; i = i + 1) { | |
81 | listeners[i].call(instance, event); | |
82 | if (event.areImmediateHandlersPrevented === true) { | |
83 | break; | |
84 | } | |
85 | } | |
86 | ||
87 | return event; | |
88 | }, | |
89 | ||
90 | prototype : { | |
91 | ||
92 | eventListeners : null, | |
93 | ||
94 | bind : function(type, eventHandler) { | |
95 | var found, i, listeners; | |
96 | ||
97 | if(!this.eventListeners) { | |
98 | this.eventListeners = {}; | |
99 | } | |
100 | ||
101 | if(!this.eventListeners[type]) { | |
102 | this.eventListeners[type] = []; | |
103 | } | |
104 | ||
105 | found = false; | |
106 | ||
107 | listeners = this.eventListeners[type]; | |
108 | for (i = 0; i < listeners.length; i++) { | |
109 | if(listeners[i] === eventHandler) { | |
110 | found = true; | |
111 | break; | |
112 | } | |
113 | } | |
114 | ||
115 | if(!found) { | |
116 | this.eventListeners[type].push(eventHandler); | |
117 | } | |
118 | ||
119 | return this; | |
120 | }, | |
121 | ||
122 | unbind : function(type, eventHandler) { | |
123 | var i, found, listeners; | |
124 | ||
125 | found = false; | |
126 | i = 0; | |
127 | ||
128 | if(!this.eventListeners) { | |
129 | this.eventListeners = {}; | |
130 | } | |
131 | ||
132 | if(typeof eventHandler == 'undefined') { | |
133 | this.eventListeners[type] = []; | |
134 | } | |
135 | ||
136 | listeners = this.eventListeners[type]; | |
137 | for (i = 0; i < listeners.length; i++) { | |
138 | if(listeners[i] == eventHandler) { | |
139 | found = true; | |
140 | break; | |
141 | } | |
142 | } | |
143 | ||
144 | if(found) { | |
145 | this.eventListeners[type].splice(i, 1); | |
146 | } | |
147 | ||
148 | return this; | |
149 | }, | |
150 | ||
151 | dispatch : function(type, data) { | |
152 | var event, listeners, instance, i; | |
153 | ||
154 | if (this.eventListeners === null) { | |
155 | this.eventListeners = {}; | |
156 | } | |
157 | ||
158 | if (typeof data === 'undefined') { | |
159 | data = {}; | |
160 | } | |
161 | ||
162 | if (data.hasOwnProperty('target') === false) { | |
163 | data.target = this; | |
164 | } | |
165 | ||
166 | event = new CustomEvent(type, data); | |
167 | listeners = this.eventListeners[type] || []; | |
168 | instance = this; | |
169 | ||
170 | for (i = 0; i < listeners.length; i = i + 1) { | |
171 | listeners[i].call(instance, event); | |
172 | if (event.areImmediateHandlersPrevented === true) { | |
173 | break; | |
174 | } | |
175 | } | |
176 | ||
177 | return event; | |
178 | } | |
179 | } | |
180 | }); |