-
Notifications
You must be signed in to change notification settings - Fork 1
/
on.js
283 lines (260 loc) · 7.5 KB
/
on.js
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
define([
'dojo/on',
'dojo/aspect',
'./has',
'./lang',
'./topic'
], function(dojoOn, aspect, has, lang, topic){
// summary:
// The export of this module is a function, which also has other
// methods attached to it. This is a major combination of dojo.on and
// dojo.aspect. By default it also returns a pause-able handle. It also
// provides the following additional features:
//
// There is a psuedo-scroll event. By passing 'scroll' as an
// event name, a normalized event will be passed for scrolling a
// node.
//
// This module provides for a multiple connection, by passing an
// object with key-values that match up to event-functions.
//
// Allows for a stringified method to be used with a context.
//
// Allows for a string to be passed as an ID for a node.
//
// description:
// The methods provided and their maps to the Dojo equivalents are:
// stopEvent: dojo/event.stop
var
WKADJUST = -20, // chrome still seems to scroll faster than Safari
global = window,
normalizeScroll = function(evt){
var o = {
type:"scroll",
horizontal:0,
vertical:0,
x:0,
y:0
};
if(evt.wheelDelta){
// Safari
if(evt.wheelDeltaX){
o.horizontal = 1;
o.x = Math.ceil(evt.wheelDeltaX/WKADJUST);
o.wheelDeltaX = evt.wheelDeltaX;
}else{
o.vertical = 1;
o.y = evt.wheelDeltaY/WKADJUST;
}
}else{
if(evt.axis == evt.HORIZONTAL_AXIS){
o.horizontal = 1;
o.x = evt.detail;
}else{
o.vertical = 1;
o.y = evt.detail;
}
}
evt.scroll = o;
return evt;
},
groups = {},
addGroup = function(groupId, handle){
if(!groups[groupId]) groups[groupId] = [];
groups[groupId].push(handle);
},
on = function(/*DOMNode|Object|String*/target, /*String|Object*/event, /*Object|Function?*/ctx, /*Function|String*/scope, /*String*/group){
// summary:
// Combination dojo/on and dojo/aspect.
// target:DOMNode|Object|String
// If a string, the node is retrived via dojo.byId
// If an object, aspect will be used
// event:String|Object
// The event or function to listen to.
// If this argument is an object, the flow passes to on.multi
// ctx:Object|Function?
// Optionally pass the context (this). If no context is passed,
// this argument should be a Function.
// scope: Function|String?
// The callback function. If a string, resolves to the method
// name in the context.
// group: String?
// All connections using a unique group ID can be paused,
// removed, or resumed via on.group() methods:
// on.group.pause('MyGroupId');
//
if(typeof target == 'string' && target[0] === '/'){
return topic.sub(target, event, ctx, scope, group);
}
if(typeof event == 'object'){
return on.multi(target, event, ctx, scope);
}
// mandate that there is always a target and event
// may not be ctx though
var
fn,
handle,
_once = 0;
if(typeof ctx == 'function'){
fn = ctx;
group = scope;
}else if(typeof ctx == 'string'){
group = scope;
scope = ctx;
ctx = global;
}
fn = fn || lang.bind(ctx, scope);
if(typeof target == 'string'){
// race condition, no access to dx-alias/dom
target = document.getElementById(target);
}else if(!target.addEventListener && !target.attachEvent){
// ASPECT
// an object, not a node
var paused = 0;
handle = aspect.after(target, event, function(){
if(paused) return;
if(_once) handle.remove();
fn.apply(null, arguments);
}, true);
handle.once = function(){
_once = 1;
};
handle.pause = function(){
paused = 1;
}
handle.resume = function(){
paused = 0;
}
if(group) addGroup(group, handle);
return handle;
}
if(event == 'scroll'){
event = has('ff') ? "DOMMouseScroll" : "mousewheel";
return dojoOn.pausable(target, event, function(evt){
fn(normalizeScroll(evt));
});
}
if(event == 'press'){
return on.press(target, fn); // group?
}
// TODO:
// on-mouseleave, mouseenter
handle = dojoOn.pausable(target, event, function(){
if(_once) handle.remove();
fn.apply(null, arguments);
});
handle.once = function(){
_once = 1;
};
if(group) addGroup(group, handle);
return handle;
};
on.multi = function(/*DOMNode|String*/target, /*Object*/obj, /*Object?*/ctx, /*String*/group){
// summary:
// A way of making multiple connections with one call.
// note:
// If context is used, all methods will bind to it.
// example
// | on.multi(node, {
// | 'mousedown':'onMouseDown',
// | 'mouseup':this.onMouseUp
// | }, this);
//
var listeners = [];
ctx = ctx || null;
for(var nm in obj){
if(nm[0] === '/'){
listeners.push(on(nm, !!ctx ? ctx : obj[nm], !!ctx?obj[nm]:null, group));
}else{
listeners.push(on(target, nm, !!ctx?ctx:obj[nm], !!ctx?obj[nm]:null, group));
}
}
return {
remove: function(){
listeners.forEach(function(lis){ lis.remove(); });
},
pause: function(){
listeners.forEach(function(lis){ lis.pause(); });
},
resume: function(){
listeners.forEach(function(lis){ lis.resume(); });
},
once: function(){
console.error('once() cannot be applied to a multiple connection.');
}
};
};
on.press = function(node, ctx, method, arg, group){
// summary:
// A pseudo event. When used, the passed method continues to
// fire as long as the button remains pressed.
// on(button, 'press', this, fireMachinegun);
// TODO:
// Allow for a passed arg OR else use the native event. Also
// need to know if the group id was passed instead. Damn you
// magic arguments!!
//
var fn = lang.bind(ctx, method);
var passArg = arg;
var tmr, offHandle, downHandle;
var tch = 0; //bv.supports.touch();
var fire = function(evt){
tmr = setInterval(function(){
fn(evt);
}, 20);
}
var stop = function(evt){
offHandle.pause();
clearInterval(tmr);
}
downHandle = on(node, tch ? "touchstart" : "mousedown", function(evt){
on.stopEvent(evt);
offHandle.resume();
fire(evt);
});
offHandle = on(document, tch ? "touchend" : "mouseup", function(evt){
on.stopEvent(evt);
stop();
});
stop();
if(group) addGroup(group, downHandle);
return downHandle;
};
on.once = function(target, event, ctx, method){
// summary:
// Connect, then disconnect after it's been called once.
//
var fn = lang.bind(ctx, method);
var handle = on(target, event, function(){
handle.remove();
fn.apply(null, arguments);
});
};
on.group = {
remove: function(groupId){
groups[groupId].forEach(function(lis){ lis.remove(); });
},
pause: function(groupId){
groups[groupId].forEach(function(lis){ lis.pause(); });
},
resume: function(groupId){
groups[groupId].forEach(function(lis){ lis.resume(); });
}
};
on.pub = topic.pub;
on.sub = topic.sub;
on.selector = dojoOn.selector;
on.stopEvent = function(evt){
evt = evt || window.event;
if(!evt) return false;
if(evt.preventDefault){
evt.preventDefault();
evt.stopPropagation();
}else{
evt.cancelBubble = true;
evt.returnValue = false;
}
return false;
};
return on;
});