/
Element.Delegation.html
312 lines (258 loc) · 7.53 KB
/
Element.Delegation.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title>Element Delegation Tests</title>
<script src="../../../Source/Core/Core.js" type="text/javascript"></script>
<script src="../../../Source/Types/Array.js" type="text/javascript"></script>
<script src="../../../Source/Types/Function.js" type="text/javascript"></script>
<script src="../../../Source/Types/Number.js" type="text/javascript"></script>
<script src="../../../Source/Types/String.js" type="text/javascript"></script>
<script src="../../../Source/Types/Object.js" type="text/javascript"></script>
<script src="../../../Source/Types/DOMEvent.js" type="text/javascript"></script>
<script src="../../../Source/Browser/Browser.js" type="text/javascript"></script>
<script src="../../../Source/Slick/Slick.Parser.js" type="text/javascript"></script>
<script src="../../../Source/Slick/Slick.Finder.js" type="text/javascript"></script>
<script src="../../../Source/Element/Element.js" type="text/javascript"></script>
<script src="../../../Source/Element/Element.Event.js" type="text/javascript"></script>
<script src="../../../Source/Element/Element.Delegation.js" type="text/javascript"></script>
<style>
body {
font: 13px sans-serif;
}
h1 {
border-top: 1px dashed #AAA;
font: normal 22px sans-serif;
margin: 5px 0;
padding-top: 10px;
}
#content {
top: 0;
bottom: 0;
left: 370px;
position: absolute;
overflow: auto;
padding-bottom: 150px;
right: 0;
}
#aside {
background: #F5F5F5;
border-right: 1px dashed #AAA;
bottom: 0;
font-family: monospace;
left: 0;
margin-right: 10px;
overflow: auto;
padding: 10px;
position: absolute;
top: 0;
width: 340px;
}
.success {
background: greenyellow;
}
.fail {
background: orangered;
}
.box {
background: greenyellow;
border: 1px solid black;
padding: 10px;
width: 300px;
}
#mouseenter_table {
border-collapse: collapse;
}
#mouseenter_table tr {
background-color: cyan;
color: blue;
}
#mouseenter_table td {
border: 1px solid white;
padding: 10px;
}
</style>
</head>
<body>
<div id="aside">
<h2>Log</h2>
<div id="log"></div>
</div>
<script>
var logger = $('log');
var log = function(msg, success){
new Element('div', {
'class': success == true ? 'success' : (success == false ? 'fail' : ''),
text: msg
}).inject(logger, 'top');
};
</script>
<div id="content">
<h1>Simple Click events</h1>
<ul id="list">
<li>First</li>
<li><div style="font-weight: bold">Second</div></li>
<li>Third</li>
</ul>
<script>
var secondLI = document.getElement('#list li:nth-child(2)');
var listListener = function(event, matched){
log('Matched element is <li>', matched.get('tag') == 'li');
log('`this` == arguments[1]', matched == this);
if (matched == secondLI) log('2nd event target tag == p', event.target.get('tag') == 'div');
log('---');
};
var list = $('list');
var listAdd = list.addEvent.pass(['click:relay(li)', listListener], list);
var listRemove = list.removeEvent.pass(['click:relay(li)', listListener], list);
listAdd();
</script>
<p>
<button onclick="listAdd()">Add Events</button> -
<button onclick="listRemove()">Remove Events</button>
</p>
<h1>Mouseenter / Mouseleave</h1>
<div id="mouseenter_leave">
<div class="mouse box">
Enter or <strong>leave</strong> this element with your <em>mouse</em>.
The event should only fire once for each time you perform the action.
</div>
</div>
<h1>Mouseenter / Mouseleave with tr</h1>
<table id="mouseenter_table">
<tbody>
<tr class="box">
<td>COLUMN 1</td>
<td>COLUMN 2</td>
</tr>
</tbody>
</table>
<script>
$('mouseenter_leave').addEvents({
'mouseenter:relay(.mouse)': log.pass(['enter', true]),
'mouseleave:relay(.mouse)': function(){
log('leave', true);
log('---');
}
});
$('mouseenter_table').addEvents({
'mouseenter:relay(tr)': log.pass(['entered tr', true]),
'mouseleave:relay(tr)': function(){
log('left tr', true);
log('---');
}
})
</script>
<h1>Focus / Blur</h1>
<p>
Click or tab to one of the input fields and the focus and blur
events should fire.
</p>
<div id="focus_blur">
<input type="text" /><br />
<input type="text" />
</div>
<p>
<button onclick="focusAdd()">Add Events</button> -
<button onclick="focusRemove()">Remove Events</button>
</p>
<script>
var focusEvents = {
'focus:relay(input)': log.pass(['focus', true]),
'blur:relay(input)': function(){
log('blur', true);
log('---');
}
};
$('focus_blur').addEvents(focusEvents);
var focusAdd = function(){
$('focus_blur').addEvents(focusEvents);
}
var focusRemove = function(){
$('focus_blur').removeEvents(focusEvents);
}
</script>
<h1>Submit / Reset</h1>
<p>
Should work on the form and on a div wrapping the form.
Try tabbing to the buttons and press enter, or tabbing to
the element and press enter.
</p>
<div id="submit_wrapper">
<div>
<form>
<input type="text" value="some text" />
<input type="submit" value="Submit" />
<input type="reset" value="reset" />
</form>
<input type="submit" value="Does nothing" />
</div>
</div>
<script>
$('submit_wrapper').addEvents({
'submit:relay(div)': function(event, matcher){
event.preventDefault();
log('submit on div', matcher.get('tag') == 'div');
log('`event.target.tagName == form`', event.target.get('tag') == 'form');
},
'submit:relay(form)': function(event, matcher){
event.preventDefault();
log('submit on form', matcher.get('tag') == 'form');
},
'reset:relay(div)': function(){
log('reset', true);
}
});
</script>
<h1>Change</h1>
<p>
Change the text. When you blur the input field, the change event
should fire for both the element which wraps the input fields
and bubble to it, and the input fields itself.
</p>
<div id="change">
<input type="text" value="some text" class="someClass" /><br />
<select name="yo">
<option>First</option>
<option>Second</option>
<option>Third</option>
</select><br />
<input type="checkbox" value="1" /><br />
<input type="radio" name="rad[]" value="1" /><input type="radio" name="rad[]" value="2" />
</div>
<script>
$('change').addEvent('change:relay(input.someClass,select,input[type="checkbox"],input[type="radio"])', function(){
log('change', true);
});
$(document.body).addEvent('change:relay(#change)', function(){
log('change bubbles', true);
});
</script>
<h1>Select</h1>
<p>
Select some text and the select event should fire
</p>
<div id="select">
<form>
<input type="text" value="select some text in this input" id="select_input" />
</form>
</div>
<script>
var select_input = $('select_input');
$('select').addEvent('select:relay(form)', function(event){
log('select', true);
log('`event.target.tagName == input`', event.target.get('tag') == 'input');
});
</script>
<h1>Paste</h1>
<div id="paste">
<textarea></textarea>
</div>
<script>
$('paste').addEvent('paste:relay(textarea)', function(event){
log('Paste', true);
});
</script>
</div>
</body>
</html>