-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsugar.js
234 lines (229 loc) · 8.01 KB
/
sugar.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
(function (window, undefined) {
'use strict';
var Sugar = function (selector, context) {
return new Sugar.fn.init(selector, context);
}
function toArray(obj) {
return Array.prototype.slice.call(obj)
}
function isFunction(obj) {
return typeof (obj) === 'function'
}
function isString(obj) {
return typeof (obj) === 'string'
}
Sugar.fn = {
init: function (selector, context) {
// HANDLE: $(""), $(null), $(undefined), $(false) ||$(DOMElement)
if (!selector || selector.nodeType) {
return this
}
if (isString(selector)) {
var nodes = document.querySelectorAll(selector),
i = 0,
length
this.length = nodes.length
for (; length = nodes.length, i < length; i++) {
this[i] = nodes[i];
}
return this
// HANDLE: $(function)
} else if (isFunction(selector)) {
this.ready(selector)
}
},
ready: function (fn) {
if (document.attachEvent ? document.readyState === "complete" : document.readyState !== "loading") {
callback();
} else {
document.addEventListener('DOMContentLoaded', fn);
}
},
find: function (selector) {
var element, i = 0,
childs,
allChilds = []
if (isString(selector)) {
while ((element = this[i++])) {
childs = element.querySelectorAll(selector)
allChilds.push(childs)
}
return allChilds;
}
},
hide: function () {
var element, i = 0
while ((element = this[i++])) {
element.style.display = 'none'
}
return this
},
show: function () {
var element, i = 0
while ((element = this[i++])) {
element.style.display = 'block'
}
return this
},
hasClass: function (name) {
if (this[0].className.indexOf(name) !== -1) {
return true
} else {
return false
}
},
addClass: function (name) {
var element, i = 0
if (isString(name)) {
while ((element = this[i++])) {
//If the element type is ELEMENT_NODE
if (element.nodeType === 1) {
element.className += element.className ? ' ' + name : name
}
}
}
return this
},
removeClass: function (name) {
var element, i = 0,
className
if (isString(name)) {
while ((element = this[i++])) {
className = element.className
//If the element type is ELEMENT_NODE and have class
if (element.nodeType === 1) {
//If not have class or have only one class
if (!className || className.indexOf(' ') === -1) {
element.removeAttribute('class')
} else {
element.className = className.replace(' ' + name, '')
}
}
}
}
return this
},
toggleClass: function (name) {
if (this.hasClass(name)) {
this.removeClass(name)
} else {
this.addClass(name)
}
return this
},
css: function (property, value) {
var element, i = 0
while ((element = this[i++])) {
if (isString(property)) {
//HANDLE:$('div').css('width','200px')
if (value) {
element.style[property] = value
} else {
//HANDLE:$('div').css('width')
return getComputedStyle(element, null).getPropertyValue(property) //IE 9+
}
} else {
//HANDLE:$('div').css({'width':'200px','height':'200px'})
var key
for (key in property) {
element.style[key] = property[key]
}
}
}
return this
},
text: function (text) {
var element, i = 0
//HANDLE:$('div').text('Sugar')
while ((element = this[i++])) {
if (text) {
element.textContent = text
} else {
//HANDLE:$('div').text()
return element.textContent
}
}
return this
},
val: function (value) {
//HANDLE:$('input').val('Sugar')
if (isString(value)) {
this[0].value = value
} else {
//HANDLE:$('input').val()
return this[0].value
}
return this
},
html: function (html) {
//HANDLE:$('div').html('<h1>Sugar</h1>')
if (isString(html)) {
this[0].innerHTML = html
} else {
//HANDLE:$('div').html()
return this[0].textContent
}
return this
},
append: function (html) {
//HANDLE:$('div').append('<h1>Sugar</h1>')
if (isString(html)) {
var element, i = 0,
tmp = document.createElement('div')
while ((element = this[i++])) {
tmp.innerHTML = html
if (element.nodeType === 1) {
element.appendChild(tmp.firstChild)
}
}
}
return this
//https://davidwalsh.name/convert-html-stings-dom-nodes ->DOMParser|createDocumentFragment|ContextualFragment
//https://developer.mozilla.org/en-US/Add-ons/Code_snippets/HTML_to_DOM
},
on: function (types, selector, fn) {
//HANDLE:$('ul').on('click')
if (isString(types)) {
var element, i = 0
while ((element = this[i++])) {
if (element.nodeType === 1) {
//HANDLE:$('ul').on('click',function(){})
if (!fn && isFunction(selector)) {
fn = selector
element.addEventListener(types, fn)
}
//HANDLE:$('ul').on('click','li',function(){})
if (isString(selector) && isFunction(fn)) {
var childs = element.querySelectorAll(selector),
child,
j = 0;
while ((child = childs[j++])) {
child.addEventListener(types, fn)
}
}
}
}
}
return this
//http://www.cnblogs.com/ziyunfei/p/5545439.html
}
};
Sugar.fn.init.prototype = Sugar.fn
// Sugar.extend = Sugar.fn.extend = function () {
// }
// Sugar.fn.extend = {
// addClass: function (name) {
// var element, i = 0
// if (isString(name)) {
// while ((element = this[i++])) {
// //If the element type is ELEMENT_NODE
// if (element.nodeType === 1) {
// element.className += element.className ? ' ' + name : name
// }
// }
// }
// return this
// }
// }
window.Sugar = window.$ = Sugar;
}(window))