This repository has been archived by the owner on Jan 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathdom.js
105 lines (96 loc) · 3.1 KB
/
dom.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
var dom = (function(win, doc, undefined) {
function dom(sel) {
if (sel === window) return window;
var ret = sel.nodeType ? [sel] : arr(doc.querySelectorAll(sel));
ret.on = function (type, handler) {
ret.forEach(function(el) {
on(el, type, handler)
});
return ret;
};
ret.delegate = function (type, sel, handler) {
ret.forEach(function(delegateEl) {
on(delegateEl, type, function(e,t) {
var matches = delegateEl.querySelectorAll(sel);
if (!matches.length) return;
for (var el = t; el.parentNode && el != delegateEl; el = el.parentNode) {
for (var i = 0; i < matches.length; i++) {
if (matches[i] == el) {
handler.call(el, e);
return;
}
}
}
});
});
return ret;
};
ret.css = function (o) {
if (typeof o == 'object') {
for (var i in o) {
if (!o.hasOwnProperty(i)) continue;
ret.forEach(function(el) {
el.style.setProperty(i, o[i]);
});
}
return ret;
}
return win.getComputedStyle(ret[0]).getPropertyValue(o);
};
ret.attr = function (o) {
if (typeof o == 'object') {
for (var i in o) {
if (!o.hasOwnProperty(i)) continue;
ret.forEach(function(el) {
el.setAttribute(i, o[i]);
});
}
return ret;
}
return ret[0].getAttribute(o);
};
ret.prop = function (o) {
ret.forEach(function(el) {
extend(el, o);
});
return ret;
};
ret.append = function (el) {
ret[0].appendChild(el.length ? el[0] : el);
return ret;
};
ret.empty = function () {
ret.forEach(empty);
return ret;
};
ret.text = function (s) {
ret[0].appendChild(doc.createTextNode(s));
return ret;
};
return ret;
};
var arr = dom.arr = function(a, i) {
return Array.prototype.slice.call(a, i || 0);
};
var extend = dom.extend = function extend(d, s) {
for (p in s) {
if (s.hasOwnProperty(p)) {
d[p] = s[p];
}
}
};
var on = dom.on = function on(obj, type, handler) {
obj.addEventListener(type, function(e) {
handler(e, e.target);
}, false);
};
var empty = dom.empty = function empty(el) {
while (el.childNodes.length) {
el.removeChild(el.firstChild);
}
};
dom.tag = function tag(name) {
return dom(doc.createElement(name));
};
return dom;
})(window, document);