-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdime.js
84 lines (73 loc) · 1.91 KB
/
dime.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
/**
* DIME.JS
* Uber-micro library for selecting elements and handling custom events.
*
* (c) 2017 - Kevin Weber - http://kevinw.de
*/
/**
* CustomEvent polyfill to support IE9+
* Source: https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent/CustomEvent
*/
(function () {
if (typeof window.CustomEvent === 'function') return false;
function CustomEvent(event, params) {
params = params || {
bubbles: false,
cancelable: false,
detail: undefined
};
var evt = document.createEvent('CustomEvent');
evt.initCustomEvent(event, params.bubbles, params.cancelable, params.detail);
return evt;
}
CustomEvent.prototype = window.Event.prototype;
window.CustomEvent = CustomEvent;
}());
(function () {
window.$ = document.querySelectorAll.bind(document);
/**
* $.each(element, index)
*/
NodeList.prototype.each = function (fn) {
var nodes = this;
for (var i = 0, l = nodes.length; i < l; i++) {
fn.call(nodes[i], i, nodes[i]);
}
};
/**
* $.on('eventName', callback)
*/
Node.prototype.on = function (name, fn) {
var names = name.split(' ');
for (var i = 0, l = names.length; i < l; i++) {
this.addEventListener(names[i], function (event) {
fn.call(this, event, event.detail);
});
}
};
NodeList.prototype.on = function (name, fn) {
this.each(function (index, element) {
element.on(name, fn);
});
};
/**
* $.trigger('eventName', data)
*/
Node.prototype.trigger = function (eventName, data) {
var event = new CustomEvent(eventName, {
detail: data
});
this.dispatchEvent(event);
};
NodeList.prototype.trigger = function (eventName, data) {
this.each(function (index, element) {
element.trigger(eventName, data);
});
};
/**
* $.find('selector')
*/
Node.prototype.find = function (selector) {
return this.querySelectorAll(selector);
};
}());