-
Notifications
You must be signed in to change notification settings - Fork 0
/
widget.js
40 lines (31 loc) · 1.01 KB
/
widget.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
// widgets
(function (root, factory) {
if (typeof module === 'object' && module.exports) {
module.exports = factory( require('./live-dom') );
} else if (typeof define === 'function' && define.amd && typeof require === 'function') {
require(['$live'], function ($live) {
return factory($live);
});
} else {
factory(root.$live);
}
})(this, function ($live) {
var widgetRunning = null,
widgetHandlers = {};
$live.widget = function (widgetName, handler) {
widgetHandlers[widgetName] = handler;
if( widgetRunning === null ) {
widgetRunning = false;
$live('[data-widget]', function () {
if( widgetHandlers[this.getAttribute('data-widget')] ) {
widgetHandlers[this.getAttribute('data-widget')].apply(this, arguments);
}
}, function () {
widgetRunning = true;
});
} else if( widgetRunning ) {
[].forEach.call(document.querySelectorAll('[data-widget="' + widgetName + '"]'), handler);
}
};
return $live.widget;
});