-
Notifications
You must be signed in to change notification settings - Fork 19
/
utils.js
64 lines (45 loc) · 1.63 KB
/
utils.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
function isItemInList(list, item){
for (let i in list)
if (list[i] == item)
return true;
return false;
}
Element.prototype.setAttributes = function (attributes) {
for(let key in attributes) {
this.setAttribute(key, attributes[key]);
}
}
Element.prototype.appendChilds = function(...childs){
for(let child of childs){
this.appendChild(child);
}
return this;
}
Element.prototype.toggleClass = function(item){
let classIn = isItemInList(this.classList, item);
if (classIn == false)
this.setAttribute('class',`${this.classList.value} ${item}`);
else
this.setAttribute('class', this.classList.value.replace(item, ""));
return this;
}
Document.prototype.createElementWithAttributes = function(element, attributes){
element = document.createElement(element);
element.setAttributes(attributes);
return element;
}; const createElementWithAttributes = Document.prototype.createElementWithAttributes;
Document.prototype.createElementWithChilds = function(element, childs){
element = document.createElement(element);
element.appendChilds(childs);
return element;
}; const createElementWithChilds = Document.prototype.createElementWithChilds;
Document.prototype.createElementWithText = function(element, text){
element = document.createElement(element);
element.textContent = text;
return element;
}; const createElementWithText = Document.prototype.createElementWithText;
let $ = function(element, event, callback, option=true){
element.addEventListener(event, callback, option);
}
let _ = query => document.querySelector(query);
let __ = query => document.querySelectorAll(query);