forked from DracoBlue/jsb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsb.js
414 lines (372 loc) · 15 KB
/
jsb.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
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/*
* jsb
*
* This file is part of jsb (Javascript Behaviour Toolkit).
* Copyright (c) 2010-2015 DracoBlue, http://dracoblue.net/
*
* Licensed under the terms of MIT License. For the full copyright and license
* information, please see the LICENSE file in the root folder.
*/
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define('jsb', factory);
} else if (typeof module === 'object' && module.exports) {
module.exports = factory();
} else {
root.jsb = factory();
}
}(this, function () {
var jsb = {
prefix: 'jsb_',
prefix_regexp: /jsb_([^\s]+)/,
handlers: {},
listeners: [],
last_event_values: {},
sticky_event_values: {},
/**
* Set the prefix for the jsb toolkit.
*
* @param {String} prefix
*/
setPrefix: function(prefix) {
this.prefix = prefix + '_';
this.prefix_regexp = new RegExp(this.prefix + '([^\s]+)');
},
/**
* Register a new handler with the given constructor function
*
* @param {String} key
* @param {Function} handler_function
*/
registerHandler: function(key, handler_function) {
this.handlers[key] = handler_function;
},
/**
* Apply all behaviours on a given dom_element and it's children.
*
* @param {HTMLElement} dom_element
*/
applyBehaviour: function(parent_dom_element) {
var dom_elements = this.getJsbElementsInDomElement(parent_dom_element);
var dom_elements_length = dom_elements.length;
var dom_element = null;
var key = null;
var key_match = null;
for (var i = 0; i < dom_elements_length; i++) {
dom_element = dom_elements[i];
this.removeClassFromElement(dom_element, this.prefix);
do {
key_match = dom_element.className.match(this.prefix_regexp);
if (key_match) {
key = key_match[1];
this.callHandler(key, dom_element);
this.removeClassFromElement(dom_element, this.prefix + key);
}
} while(key_match);
}
this.fireEvent('Jsb::BEHAVIOURS_APPLIED');
},
/**
* Fires an event with the given name and values.
* @param {String} name
* @param {Object} [values={}]
*/
fireEvent: function(name, values, sticky) {
values = values || {};
sticky = sticky || false;
/*
* Remember the last values for calls to `jsb.whenFired`
*/
if (sticky) {
this.sticky_event_values[name] = this.sticky_event_values[name] || [];
this.sticky_event_values[name].push(values);
} else {
this.last_event_values[name] = values;
}
var listeners = this.listeners;
var listeners_length = listeners.length;
for (var i = 0; i < listeners_length; i++) {
this.rawFireEventToListener(listeners[i], name, values);
}
if (name === 'Jsb::REMOVED_INSTANCE') {
this.removeBoundListenersForInstance(values);
}
},
fireStickyEvent: function(name, values) {
this.fireEvent(name, values, true);
},
/**
* Adds an event listener for a given name or regular expression.
*
* @param {String|RegExp} name_or_regexp
* @param {Object|Function} [filter_or_cb=null]
* @param {Function} cb
*/
on: function(name_or_regexp, filter_or_cb, cb) {
var filter = filter_or_cb || null;
if (!cb) {
filter = null;
cb = filter_or_cb;
}
this.listeners.push([cb, name_or_regexp, filter]);
var that = this;
var off_handler = function() {
that.off(name_or_regexp, cb);
};
/*
* Call this method with your jsb instance, to allow automatic removal of the handler on
* disposal of the jsb instance.
*/
off_handler.dontLeak = function(element) {
for (var i = 0; i < that.listeners.length; i++) {
var listener = that.listeners[i];
if (listener[0] === cb && listener[1] === name_or_regexp && listener[2] === filter) {
listener[3] = element;
return ;
}
}
};
return off_handler;
},
/**
* Please call jsb.fireEvent('Jsb::REMOVED_INSTANCE', this) within your object
* to free all handlers which are bound to the element (by using the dontLeak-method).
*
* @private
*
* @param instance Jsb Instance
*/
removeBoundListenersForInstance: function(instance) {
var new_listeners = [];
var listeners = this.listeners;
var listeners_length = listeners.length;
for (var i = 0; i < listeners_length; i++) {
if (listeners[i][3] !== instance) {
new_listeners.push(listeners[i]);
}
}
this.listeners = new_listeners;
},
/**
* Removes an event listener for a given name or regular expression and handler function.
*
* The handler function needs to be the exact same Function object that was previously registered as an event handler.
*
* @param {String|RegExp} name_or_regexp
* @param {Function} cb
*/
off: function(name_or_regexp, cb) {
var listeners = this.listeners;
this.listeners = [];
var listeners_length = listeners.length;
for (var i = 0; i < listeners_length; i++) {
if (!(listeners[i][0] === cb && listeners[i][1].toString() === name_or_regexp.toString())) {
this.listeners.push(listeners[i]);
}
}
},
/**
* Register to an event as soon as it's fired for the first time
* even if that happend earlier!
*
* @param {String|RegExp} name_or_regexp
* @param {Object|Function} [filter_or_cb=null]
* @param {Function} cb
*/
whenFired: function(name_or_regexp, filter_or_cb, cb) {
var that = this;
var filter = filter_or_cb;
if (!cb) {
filter = null;
cb = filter_or_cb;
}
var off_handler = this.on(name_or_regexp, filter, cb);
var is_regexp = (name_or_regexp instanceof RegExp);
if (is_regexp) {
for (var key in this.last_event_values) {
if (this.last_event_values.hasOwnProperty(key) && key.match(name_or_regexp)) {
(function(key) {
var last_value = that.last_event_values[key];
setTimeout(function() {
that.rawFireEventToListener([cb, name_or_regexp, filter], key, last_value);
}, 0);
})(key);
}
}
for (var key in this.sticky_event_values) {
if (this.sticky_event_values.hasOwnProperty(key) && key.match(name_or_regexp)) {
(function(key) {
var last_values = that.sticky_event_values[key];
var last_values_length = last_values.length;
for (var i = 0; i < last_values_length; i++) {
(function(last_value) {
setTimeout(function() {
that.rawFireEventToListener([cb, name_or_regexp, filter], key, last_value);
}, 0);
})(last_values[i]);
}
})(key);
}
}
} else {
if (typeof this.last_event_values[name_or_regexp] !== 'undefined') {
var last_value = that.last_event_values[name_or_regexp];
setTimeout(function() {
that.rawFireEventToListener([cb, name_or_regexp, filter], name_or_regexp, last_value);
}, 0);
}
if (typeof this.sticky_event_values[name_or_regexp] !== 'undefined') {
var last_values = that.sticky_event_values[name_or_regexp];
var last_values_length = last_values.length;
for (var i = 0; i < last_values_length; i++) {
(function(last_value) {
setTimeout(function() {
that.rawFireEventToListener([cb, name_or_regexp, filter], name_or_regexp, last_value);
}, 0);
})(last_values[i]);
}
}
}
return off_handler;
},
/**
* @private
*/
rawFireEventToListener: function(listener, name, values) {
var is_regexp_match = (listener[1] instanceof RegExp && name.match(listener[1]));
if (is_regexp_match || listener[1] === name) {
var filter = listener[2];
var is_match = true;
if (filter) {
for (var filter_key in filter) {
if (filter.hasOwnProperty(filter_key)) {
is_match = is_match && (typeof values[filter_key] !== 'undefined' && filter[filter_key] === values[filter_key]);
}
}
}
if (is_match) {
listener[0](values, name);
}
}
},
/**
* Call a specific handler on a given dom element
* @private
* @param {String} key
* @param {HTMLElement} dom_element
*/
callHandler: function(key, dom_element) {
if (typeof this.handlers[key] === 'undefined') {
/* Hide require from webpack. It needs this fix. */
var need = window["require"];
if (typeof need === 'undefined') {
throw new Error('The handler ' + key + ' is not defined!');
} else {
need([key], function(require_result) {
if (typeof jsb.handlers[key] === 'undefined') {
if (typeof require_result === 'undefined') {
throw new Error('The handler ' + key + ' is not defined (even with requirejs)!');
} else {
jsb.registerHandler(key, require_result);
}
}
jsb.callHandler(key, dom_element);
});
return ;
}
}
var value_string = null;
var dashed_key_name = key.toString().replace(/\//g, '-');
if (dom_element.getAttribute('data-jsb-' + dashed_key_name)) {
/*
* Nice, we have a class specific data-jsb attribute -> let's use that one!
*/
value_string = dom_element.getAttribute('data-jsb-' + dashed_key_name);
} else if (dom_element.getAttribute('data-jsb')) {
/*
* Nice, we have a data-jsb attribute -> let's use that one!
*/
value_string = dom_element.getAttribute('data-jsb');
}
if (value_string !== null) {
new this.handlers[key](dom_element, this.parseValueString(value_string));
} else {
new this.handlers[key](dom_element);
}
},
/**
* Parse a json or a query string into an object hash
* @private
* @param {String} value_string
* @return {Object}
*/
parseValueString: function(value_string) {
if (value_string.substr(0, 1) == '{') {
return JSON.parse(value_string);
} else {
var value = {};
var parts = value_string.split('&');
var parts_length = parts.length;
for (var i = 0; i < parts_length; i++) {
var query_string_entry = parts[i].split('=');
var value_key = decodeURIComponent(query_string_entry[0]);
var value_content = decodeURIComponent(query_string_entry.slice(1).join('='));
value[value_key] = value_content;
}
return value;
}
},
/**
* @private
* @param {HTMLElement} dom_element
* @param {String} class_name
* @return {void}
*/
removeClassFromElement: function(dom_element, class_name) {
var element_class_name = dom_element.className;
element_class_name = element_class_name.replace(new RegExp('(^|[\\s]+)' + class_name + '($|[\\s]+)'), '$1$2');
dom_element.className = element_class_name;
},
/**
* Return all elements within the dom_element, which match the
* jsb prefix.
*
* @private
* @param {HTMLElement} dom_element
* @returns {HTMLElement[]}
*/
getJsbElementsInDomElement: function(dom_element) {
var dom_elements = [];
/*
* We need to use concat, because otherwise the returned array would
* change as soon as we remove the element's className.
*/
var raw_dom_elements = null;
if (dom_element.getElementsByClassName) {
raw_dom_elements = dom_element.getElementsByClassName(this.prefix);
} else {
raw_dom_elements = dom_element.querySelectorAll('.' + this.prefix);
}
var raw_dom_elements_length = raw_dom_elements.length;
for (var r = 0; r < raw_dom_elements_length; r++) {
dom_elements.push(raw_dom_elements[r]);
}
return dom_elements;
}
};
if (typeof window !== 'undefined') {
/*
* Fire domready in a native way!
*/
if (window.addEventListener) {
window.addEventListener('DOMContentLoaded', function() {
jsb.applyBehaviour(window.document);
}, true);
} else if(window.attachEvent) {
window.attachEvent('onLoad',function() {
jsb.applyBehaviour(window.document);
});
}
}
return jsb;
}));