-
Notifications
You must be signed in to change notification settings - Fork 57
/
demo.html
291 lines (229 loc) · 8.32 KB
/
demo.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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Event.js documentation</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
html, body {
font: 13px Arial;
margin: 0;
padding: 10px
}
h1, h2 {
margin: 0
}
h1 {
font-size: 18px
}
h2 {
font-size: 16px;
margin-top: 25px
}
pre {
border: solid 1px #C1E7FF;
background-color: #E8F6FF;
padding: 5px;
font-size: 12px;
margin: 0 0 10px 0
}
.main-wrapper {
width: 600px
}
.section {
border: solid 1px #FFBCBC;
background-color: #FFE8E8;
padding: 3px
}
.info {
width: 350px;
height: 200px;
overflow: auto;
border: solid 1px #ccc;
padding: 5px;
margin-top: 10px
}
#upper {
border: solid 1px green
}
#lower {
border: solid 1px red
}
</style>
</head>
<body>
<div class="main-wrapper">
<h1>Event.js</h1>
<p>This Javascript package unifies event handling across browsers.<br>
Allows registering and unregistering of event handlers.<br>
Injects event object and involved DOM element to listener.
</p>
<h2>Registering an event</h2>
<p class="section">Events can be registered using <strong>attach</strong> method:</p>
<pre>
var evt = new Event(),
click = document.getElementById('click'),
message = function (e, src) {
window.alert(e.type + ' event on "' + src.id + '"');
};
evt.attach('click', click, message, false);
</pre>
<div><button id="click">Click</button></div>
<p>The first parameter is event name, one of the "mousedown", "click", "mouseover" etc.</p>
<p>The second is DOM element attached to event.</p>
<p>The third is listener which will be executed when event will occur.<br>
Listener receives two parameters: <strong>e</strong> (event object) and <strong>src</strong> (DOM element).
</p>
<p>The last parameter is boolean telling to use event capturing (true - default) or bubbling (false)</p>
<p><strong>attach</strong> method returns listener, which can be used to unregister previously registered event</p>
<p class="section">Multiple events:</p>
<pre>
var evt = new Event(),
multipleEvents = document.getElementById('multiple-events'),
messageHTML = function (e, src) {
var infoElement = document.getElementById(src.id + '-info');
infoElement.innerHTML += e.type + ' event on "' + src.id + '"\r\n';
infoElement.scrollTop = infoElement.scrollHeight;
};
evt.attach('mouseover', multipleEvents, messageHTML);
evt.attach('mouseout', multipleEvents, messageHTML);
evt.attach('mousedown', multipleEvents, messageHTML);
evt.attach('mouseup', multipleEvents, messageHTML);
</pre>
<div><button id="multiple-events">Multiple events</button></div>
<div id="multiple-events-info" class="info"></div>
<p class="section">Multiple listeners:</p>
<pre>
var evt = new Event(),
multipleListeners = document.getElementById('multiple-listeners'),
messageMulti = function (e, src, lName) {
var infoElement = document.getElementById(src.id + '-info');
infoElement.innerHTML += '<div>' + e.type + ' event on "'
+ src.id + ' with "' + lName + '"</div>';
infoElement.scrollTop = infoElement.scrollHeight;
};
evt.attach('click', multipleListeners, function (e, src) {
messageMulti(e, src, 'listener 1');
});
evt.attach('click', multipleListeners, function (e, src) {
messageMulti(e, src, 'listener 2');
});
evt.attach('click', multipleListeners, function (e, src) {
messageMulti(e, src, 'listener 3');
});</pre>
<div><button id="multiple-listeners">Multiple listeners</button></div>
<div id="multiple-listeners-info" class="info"></div>
<h2>Unregistering an event</h2>
<p class="section">Events can be unregistered using <strong>detach</strong> method:</p>
<pre>
var evt = new Event(),
noevent = document.getElementById('no-event'),
message = function (e, src) {
window.alert(e.type + ' event on "' + src.id + '"');
};
var noeventHandler = evt.attach('mousedown', noevent, message);
evt.detach('mousedown', noevent, noeventHandler);
</pre>
<div><button id="no-event">No event</button></div>
<p><strong>detach</strong> method accepts four parameters,
first and second are event name and DOM element respectively,
the third is listener returned by <strong>attach</strong> method,
and the last one is capturing/bubbling boolean (which should be the same as the used in <strong>attach</strong> method)</p>
<p class="section">Fire an event once:</p>
<pre>
var evt = new Event(),
once = document.getElementById('once'),
messageHTML = function (e, src) {
var infoElement = document.getElementById(src.id + '-info');
infoElement.innerHTML += e.type + ' event on "' + src.id + '"\r\n';
infoElement.scrollTop = infoElement.scrollHeight;
};
var onceHandler = evt.attach('mousedown', once, function (e, src) {
messageHTML(e, src);
evt.detach('mousedown', once, onceHandler);
});
</pre>
<div><button id="once">Run once</button></div>
<div id="once-info" class="info" style="height: 50px"></div>
<h2>Preventing an event</h2>
<p class="section">Default events can be prevented using <strong>prevent</strong> method:</p>
<pre>
var evt = new Event(),
input = document.getElementById('prevent-click');
evt.attach('keypress', input, function (e) {
evt.prevent(e);
});
</pre>
<div><input type="text" id="prevent-click"></div>
<h2>Turning off event capturing and bubbling</h2>
<p class="section">Event capturing and bubbling can be turned off using <strong>stop</strong> method:</p>
<pre>
var evt = new Event(),
upper = document.getElementById('upper'),
lower = document.getElementById('lower');
evt.attach('mousedown', upper, function (e, src) {
evt.stop(e);
alert(src.id);
});
</pre>
<div id="upper">Upper
<div id="lower">Lower</div>
</div>
<p>Tested in IE 6.0, FF 17, Chrome 22, Safari 5.1.1</p>
</div>
<script type="text/javascript" src="Event.js"></script>
<script type="text/javascript">
"use strict";
var click = document.getElementById('click'),
multipleEvents = document.getElementById('multiple-events'),
multipleListeners = document.getElementById('multiple-listeners'),
noevent = document.getElementById('no-event'),
input = document.getElementById('prevent-click'),
once = document.getElementById('once'),
upper = document.getElementById('upper'),
lower = document.getElementById('lower'),
evt = new Event(),
message = function (e, src) {
window.alert(e.type + ' event on "' + src.id + '"');
},
messageHTML = function (e, src) {
var infoElement = document.getElementById(src.id + '-info');
infoElement.innerHTML += '<div>' + e.type + ' event on "' + src.id + '"</div>';
infoElement.scrollTop = infoElement.scrollHeight;
},
messageMulti = function (e, src, lName) {
var infoElement = document.getElementById(src.id + '-info');
infoElement.innerHTML += '<div>' + e.type + ' event on "'
+ src.id + ' with "' + lName + '"</div>';
infoElement.scrollTop = infoElement.scrollHeight;
};
evt.attach('click', document.getElementById('click'), message, false);
evt.attach('mouseover', multipleEvents, messageHTML);
evt.attach('mouseout', multipleEvents, messageHTML);
evt.attach('mousedown', multipleEvents, messageHTML);
evt.attach('mouseup', multipleEvents, messageHTML);
var noeventHandler = evt.attach('mousedown', noevent, message);
evt.detach('mousedown', noevent, noeventHandler);
var onceHandler = evt.attach('mousedown', once, function (e, src) {
messageHTML(e, src);
evt.detach('mousedown', once, onceHandler);
});
evt.attach('click', multipleListeners, function (e, src) {
messageMulti(e, src, 'listener 1');
});
evt.attach('click', multipleListeners, function (e, src) {
messageMulti(e, src, 'listener 2');
});
evt.attach('click', multipleListeners, function (e, src) {
messageMulti(e, src, 'listener 3');
});
evt.attach('keypress', input, function (e) {
evt.prevent(e);
});
evt.attach('mousedown', upper, function (e, src) {
evt.stop(e);
alert(src.id);
});
</script>
</body>
</html>