-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmini.js
164 lines (138 loc) · 4.35 KB
/
mini.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
(function (global) {
const each = fn =>
function (...args) {
this.elements.forEach(elem =>
fn(elem, ...args));
return this;
};
const on = each((element, eventName, handler, ) =>
element.addEventListener(eventName, handler))
const off = each((element, eventName, handler, ) =>
element.removeEventListener(eventName, handler))
const addClass = each((element, className) => {
let classNames = className.split(' ');
classNames.forEach(name => {
element.classList.add(name)
});
})
const removeClass = each((element, className) => {
let classNames = className.split(' ');
classNames.forEach(name => {
element.classList.remove(name)
});
})
const toggleClass = each((element, className) => {
let classNames = className.split(' ');
classNames.forEach(name => element.classList.toggle(name))
})
const html = function (data) {
if (data != undefined) {
this.elements.forEach(elem => elem.innerHTML = data);
return this
}
else {
let values = [];
this.elements.forEach(elem => values.push(elem.innerHTML));
return values
}
}
const text = function (data) {
if (data != undefined) {
this.elements.forEach(elem => elem.textContent = data);
return this
}
else {
let values = [];
this.elements.forEach(elem => values.push(elem.textContent));
return values
}
}
const val = function (value) {
if (value != undefined) {;
this.elements.forEach(elem => elem.value = value);
return this
}
else {
let values = [];
this.elements.forEach(elem => values.push(elem.value));
return values
}
}
const attr = each((element, attr, value) => {
if (attr != undefined && value != undefined) {
return element.setAttribute(attr, value);
}
if (attr != undefined && value == undefined) {
return element.getAttribute(attr);
}
else {
throw "error takes atleast one argument recieved 0";
}
})
const parent = function(){
let elementsArray = []
elementsArray.push(this.elements[0].parentNode);
return Object.create(features,{
elements: {
value:elementsArray
}
});
}
const children = function(){
let childArray = Array.from(this.elements[0].children)
return Object.create(features,{
elements: {
value:childArray
}
});
}
const next = function(){
let elementsArray = []
elementsArray.push(this.elements[0].nextElementSibling);
return Object.create(features,{
elements: {
value:elementsArray
}
});
}
const is = function(selector){
return this.elements[0].matches(selector);
}
const append = each((elem,data) => {
elem.appendChild(data.cloneNode(true));
})
const prepend = each((elem,data) => {
elem.insertBefore(data.cloneNode(true),elem.firstChild);
})
const hide = each((elem) => {
elem.style.display = "none";
})
const show = each((elem) => {
elem.style.display = "";
})
const css = each((elem,styles) => {
console.log(styles)
for(let key in styles){
elem.style[key] = styles[key]
}
})
const styles = function(name){
return window.getComputedStyle(this.elements[0],null).getPropertyValue(name)
}
const features = {
on, off, addClass, removeClass, toggleClass, html, text, val, attr,
parent,children,next,is,append,prepend,hide,show,css,styles
};
global.$ = selector =>
Object.create(features, {
elements: {
value: document.querySelectorAll(selector)
}
});
global.$.create = function(selector,data){
let element = document.createElement(selector);
let textnode = document.createTextNode(data);
element.appendChild(textnode);
return element
}
})(typeof window !== 'undefined' ? window : this)