-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathThinDOM.js
78 lines (73 loc) · 1.9 KB
/
ThinDOM.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
/**
* A little thin DOM wrapper with chaining
*/
function ThinDOM(tag, attributes) {
this.el = document.createElement(tag);
if(typeof attributes != 'undefined') {
this.attr(attributes);
}
}
/**
* Iterate over all of the element's attributes.
* @param cb(key, value)
*/
ThinDOM.prototype.append = (function(other) {
if(other instanceof ThinDOM) {
this.el.appendChild(other.get());
} else if (other instanceof jQuery) {
if(other.length > 1) {
var self = this;
other.each(function(i, otherEl) {
self.el.appendChild(otherEl);
});
}
else {
this.el.appendChild(other[0]);
}
} else if (other instanceof Element) {
this.el.appendChild(other);
}
return this;
});
/**
* Set the element's style attributes
*/
ThinDOM.prototype.css = (function(properties, value) {
if(properties.constructor === String) {
this.el.style.properties = value;
} else if (properties instanceof Object) {
for(var key in properties) {
if(properties.hasOwnProperty(key)) {
this.el.style.key = properties[key];
}
}
}
return this;
});
/**
* Set the inner HTML of the element.
*/
ThinDOM.prototype.html = (function(html) {
if(typeof html == 'undefined') {
return this.el.innerHTML;
}
else {
this.el.innerHTML = html;
return this;
}
});
ThinDOM.prototype.attr = (function(properties, value) {
if(properties.constructor === String) {
this.el.setAttribute(properties, value);
} else if (properties instanceof Object) {
for(var key in properties) {
if(properties.hasOwnProperty(key)) {
this.el.setAttribute(key, properties[key]);
}
}
}
return this;
});
ThinDOM.prototype.get = (function() {
return this.el;
});