/
fui.js
264 lines (205 loc) · 8.23 KB
/
fui.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
// fui 0.0.1
// ────────────────────────────────────────────────────────────────────────────────────────
// Functional User Interaction
// ────────────────────────────────────────────────────────────────────────────────────────
(function (glob) {
var chains = [],
eventSources = {};
var isIE = typeof window.attachEvent != 'undefined',
// define the bind function
bindEvent = isIE ?
// the ie version
function(target, evtName, callback) {
target.attachEvent('on' + evtName, callback);
} :
// everybody else
function(target, evtName, callback) {
target.addEventListener(evtName, callback, true);
},
// define the unbind function
unbindEvent = isIE ?
// the ie version
function(target, evtName, callback) {
target.detachEvent('on' + evtName, callback);
} :
// everbody else
function(target, evtName, callback) {
target.removeEventListener(evtName, callback, true);
};
function EventChain(opts) {
this._index = 0;
this._relative = false;
this._steps = [];
// initialise the state member
this.state = {};
this.lastTarget;
}
EventChain.prototype = {
_next: function(evt) {
var handler = this._steps[this._index++];
if (typeof handler == 'function') {
var result = handler.apply(this, this.args);
return typeof result == 'undefined' || result;
}
return false;
},
_on: function(eventName, handler) {
return this._step(function() {
if (this.event.name === eventName) {
return handler.apply(this, arguments);
}
});
},
_step: function(stepHandler) {
this._steps[this._steps.length] = stepHandler;
return this;
},
each: function(handler) {
return this._step(handler);
},
filter: function(selector) {
return this._step(function(target) {
return qwery.is(target, selector);
});
},
relative: function() {
return this._step(function(target, x, y) {
if (! this._relative) {
if (target.offsetParent) {
do {
x -= target.offsetLeft;
y -= target.offsetTop;
target = target.offsetParent;
} while (target);
} // if
this.args = this.args.slice(0, 1).concat([x, y]);
this._relative = true;
}
});
},
pipe: function(target) {
return this._step(function() {
target.process(this.event);
});
},
/**
## process(event)
The process function is called when an event is added to the chain.
*/
process: function(evt) {
var result;
// reset values to default
this._index = 0;
this._relative = false;
// initialise the args to the page x and page y
this.event = evt;
this.args = [evt.target, evt.pageX, evt.pageY];
do {
result = this._next(evt);
} while (result);
// update the last target
this.lastTarget = evt.target;
return result;
}
};
// autowire the down, move and up handlers
['down', 'up', 'move'].forEach(function(eventName) {
EventChain.prototype[eventName] = function(handler) {
return this._on(eventName, handler);
};
});
function fui(opts) {
var chain = chains[chains.length] = new EventChain(opts),
eventSource;
// initialise the options
opts = opts || {};
// determine whether we are using a touch interface
opts.source = opts.source || ('ontouchstart' in window ? 'touch' : 'mouse');
// if we don't have an event source for the source type, then create it
eventSource = eventSources[opts.souce];
if (! eventSource) {
var Source = fui.sources[opts.source];
// if we have a Source creator then do that now
if (Source) {
eventSource = eventSources[opts.source] = new Source(document);
}
}
// if we have an event source, then add the chain to the source
if (eventSource) {
eventSource.add(chain);
}
// return the chain
return chain;
}
// initialise the different source handlers
fui.sources = {};
// initialise the templates hash
fui.templates = {};
// create a template function, which is useful for creating chains not automatically bound to an event source
fui.define = function(name, opts) {
var chain;
// if we don't have a name specified, but have been passed options
// then remap arguments
if (typeof name == 'object' && (! name instanceof String)) {
opts = name;
name = '';
}
// initialise options
opts = opts || {};
opts.source = 'none';
// create a chain
chain = fui(opts);
// if we have a name specified, then add to the template
if (name) {
fui.templates[name] = chain;
}
return chain;
};
function EventSource() {
this.chains = [];
}
EventSource.prototype = {
_createEvent: function(name, evt) {
return {
// initialise the name
name: name,
// initialise the target
target: evt.target || evt.sourceElement,
// grab the pageX and pageY from the original event
pageX: evt.pageX,
pageY: evt.pageY,
// save a reference to the original event
original: evt
};
},
add: function(chain) {
this.chains.push(chain);
},
handle: function(eventName) {
var source = this;
return function(evt) {
var event = source._createEvent(eventName, evt);
// iterate through the chains and start the event on each of them
for (var ii = source.chains.length; ii--; ) {
source.chains[ii].process(event);
}
};
}
};
var MouseSource = fui.sources.mouse = function(target) {
// call the inherited constructor
EventSource.call(this, target);
// bind the event handlers
this._bindEvents(target);
};
MouseSource.prototype = new EventSource();
MouseSource.prototype.constructor = MouseSource;
MouseSource.prototype._bindEvents = function(target) {
var source = this;
// bind the mouse down event handler
bindEvent(target, 'mousedown', source.handle('down'));
bindEvent(target, 'mousemove', source.handle('move'));
bindEvent(target, 'mouseup', source.handle('up'));
};
(typeof module != "undefined" && module.exports) ? (module.exports = fui) : (typeof define != "undefined" ? (define("fui", [], function() { return fui; })) : (glob.fui = fui));
})(this);