-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueries.js
238 lines (199 loc) · 7.04 KB
/
queries.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
// function walkTheDOM(node, func) {
// func(node);
// node = node.firstChild;
// while (node) {
// walkTheDOM(node, func);
// node = node.nextSibling;
// }
// }
function jDomItem(element) {
this.element = element;
}
jDomItem.prototype.addClass = function (className) {
this.element.classList.add(className);
return this;
};
jDomItem.prototype.removeClass = function (className) {
this.element.classList.remove(className);
return this;
};
jDomItem.prototype.hasClass = function (className) {
return this.element.classList.contains(className);
};
jDomItem.prototype.hasAttr = function (attr) {
return this.element.hasAttribute(attr);
};
jDomItem.prototype.addAttr = function (key, val) {
this.element.setAttribute(key, val);
return this;
};
jDomItem.prototype.removeAttr = function (key) {
this.element.removeAttribute(key);
return this;
};
jDomItem.prototype.html = function html (newInnerHTML) {
this.element.innerHTML = newInnerHTML;
};
jDomItem.prototype.clone = function (isDeep) {
var newEl = this.element.cloneNode(isDeep)
return new jDomItem(newEl);
};
jDomItem.prototype.empty = function empty() {
var currChild = this.element.firstChild;
while (currChild) {
this.element.removeChild(currChild);
currChild = this.element.firstChild;
}
return this;
};
// jDomItem.prototype.next = function () {
// return this.element.nextSibling;
// };
//////////////////////////////////////////////////////////////////////////////////
/////////////////////////// Collection ///////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
function jDOMCollection(jDomItems) {
this.nodes = jDomItems || [];
}
jDOMCollection.prototype.each = function (func) {
this.nodes.forEach(function(node) {
func.call(node, arguments);
});
return this;
};
jDOMCollection.prototype.hasClass = function (className) {;
return this.nodes[0].hasClass(className);
};
jDOMCollection.prototype.toggleClass = function (className) {;
this.each(function () {
if (!this.hasClass(className)) {
this.addClass(className);
} else {
this.removeClass(className);
}
});
return this;
};
// jDOMCollection.prototype.hasAttr = function hasAttr(attr) {
// return this.nodes[0].hasAttr(attr);
// };
jDOMCollection.prototype.addAttr = function addAttr(key, val) {;
this.each(function () {
this.addAttr(key, val);
});
return this;
};
jDOMCollection.prototype.removeAttr = function removeAttr(key) {;
this.each(function () {
this.removeAttr(key);
});
return this;
};
jDOMCollection.prototype.hasAttr = function hasAttr(attr) {;
return this.nodes[0].hasAttr(attr);
};
jDOMCollection.prototype.size = function size() {
return this.nodes.length;
};
jDOMCollection.prototype.remove = function remove() {
this.each(function(){
// TODO better to implement this in jDOMItem.remove?
this.element.parentNode.removeChild(this.element);
});
return this;
};
jDOMCollection.prototype.text = function text(txt) {
if (txt === undefined) {
return (this.nodes[0] && this.nodes[0].element.textContent) || '';
} else {
this.each(function () {
// TODO better to implement this in jDOMItem.text?
this.element.textContent = txt;
});
return this;
}
};
jDOMCollection.prototype.html = function html(html) {
if (html === undefined) {
return (this.nodes[0] && this.nodes[0].element.innerHTML) || '';
} else {
this.each(function () {
// TODO better to implement this in jDOMItem.text?
this.html(html);
});
return this;
}
};
jDOMCollection.prototype.clone = function clone(isDeep) {
//create new jDOMCollection
//each on this - create a new jDOM Item and push it into the jDOMCollection nodes
var newCollection = new jDOMCollection();
this.each(function () {
var newItem = this.clone(isDeep);
newCollection.nodes.push(newItem);
});
return newCollection;
};
jDOMCollection.prototype.empty = function empty () {
this.each(function () {
this.empty();
});
return this;
};
jDOMCollection.prototype.eq = function eq (idx) {
var idxToGet = this.nodes.length;
(idx < 0) ? idxToGet+=idx : idxToGet=idx;
var retColl;
if (idxToGet >= 0 && idxToGet < this.nodes.length) {
retColl = new jDOMCollection([this.nodes[idxToGet]]);
} else {
retColl = new jDOMCollection();
}
return retColl;
};
jDOMCollection.prototype.first = function first () {
return (this.nodes[0] && new jDOMCollection([this.nodes[0]])) || new jDOMCollection();
};
jDOMCollection.prototype.last = function last () {
var lastNodeIdx = this.nodes.length - 1;
return (this.nodes[lastNodeIdx] && new jDOMCollection([this.nodes[lastNodeIdx]])) || new jDOMCollection();
};
// jDOMCollection.prototype.append = function append (stringInnerHtml) {
// var newElem = document.createElement(stringInnerHtml);
//
// return this;
// };
// jDOMCollection.prototype.next = function next () {
// var newColl = new jDOMCollection();
// this.each(function (){
// console.log(this.outerHTML);
// var next = this.next();
// console.log(next.outerHTML);
// newColl.nodes.push(next);
// });
// return newColl;
// };
jDOMCollection.prototype.map = function () {};
jDOMCollection.prototype.filter = function () {};
//////////////////////////////////////////////////////////////////////
function $(selectorOrNode, root) {
root = root || document;
if (typeof selectorOrNode === 'string') {
// TODO
// if (selectorOrNode === '') {
// return new jDomItem([]);
// }
var nlist = root.querySelectorAll(selectorOrNode);
return new jDOMCollection($.nodeListToArrayOfjDomItems(nlist));
} else {
throw new Error('TODO: implement more modes');
}
}
$.is_jDomEntity = function(thing){
return thing instanceof jDomItem || thing instanceof jDOMCollection;
};
$.nodeListToArrayOfjDomItems = function(nodeList){
return Array.prototype.map.call(nodeList, function (element) {
return new jDomItem(element);
});
};