-
Notifications
You must be signed in to change notification settings - Fork 3
/
tethys.js
385 lines (319 loc) · 10.4 KB
/
tethys.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
// hapjs
// http://github.com/hapjs/tethys
;
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
define([], factory);
} else if (typeof exports === 'object') {
module.exports = factory();
} else {
root.tethys = factory();
root.$ = root.$ || root.tethys;
}
}(this, function() {
'use strict';
// "font-size" to "fontSize"
function camelCase(key) {
return key.replace(/(-([a-z]))/g, function(s, s1, s2) {
return s2.toUpperCase();
});
};
// 查询/设置:单个/多个值
function valueHandler(args, getter, setter) {
var attrs = {},
keys,
key = args[0],
value = args[1];
if (typeof key === 'object') {
// 设置多个值
attrs = key;
} else if (args.length > 1) {
// 设置单个值
attrs[key] = value;
} else {
// 查询单个值
return this[0] && key ? getter(this[0], key) : null;
};
keys = Object.keys(attrs);
// 设置值
return this.each(function(el) {
keys.forEach(function(key) {
setter(el, key, attrs[key]);
});
});
};
// 获取最终样式
function getComputedStyle(el, name){
return (window.getComputedStyle(el) || {})[name];
}
// 获取数值部分
function getNumber(val){
return +val.replace(/\D*/g, '');
}
function fireEvent(el, eventName){
if(typeof el.dispatchEvent !== 'function') return false;
var fire = function(eventClass, opt){
var event;
if(typeof eventClass === 'string'){
event = document.createEvent(eventClass);
event.initEvent(eventName, true, true);
event.synthetic = true;
}else{
event = new eventClass(eventName, tethys.extend({
view: window,
bubbles: true,
cancelable: true,
}, opt));
};
return el.dispatchEvent(event, true);
};
switch (eventName) {
case 'mousedown':
case 'mouseup':
case 'mousemove':
case 'click':
case 'dblclick':
case 'mouseover':
case 'mouseout':
case 'mouseenter':
case 'mouseleave':
case 'contextmenu':
return fire(MouseEvent);
case 'focus':
case 'blur':
return fire(FocusEvent);
case 'select':
case 'change':
return fire('HTMLEvents');
default:
throw 'trigger: Couldn\'t find an event class for event "' + eventName + '"';
};
}
// 查找节点,返回一个可操作的节点数组
function tethys(selector, context) {
var nodes = [];
// 把参数转换为包含Node的数组
if (selector.each && selector.on) {
// tethys 对象
return selector;
} else if (typeof selector === 'string') {
// html代码或选择器
if (selector.match(/^[^\b\B]*\</)) {
// html代码
nodes = tethys.parseHtml(selector);
} else {
// 选择器
nodes = (context || document).querySelectorAll(selector);
};
} else if (Array.isArray(selector) || selector.constructor === NodeList) {
// 包含节点的数组或NodeList
nodes = selector;
} else {
// 节点
nodes = [selector];
};
// 当Node被appendChild方法添加到其它元素中后,该Node会被从它所在的NodeList中移除
// 为了避免这种情况,我们要把NodeList转换成包含Node的数组
nodes = Array.prototype.map.call(nodes, function(n) {
return n;
});
// 给数组添加dom操作方法
tethys.extend(nodes, tethys.fn);
return nodes;
};
// 扩展
tethys.extend = function() {
var args = arguments,
deep = false,
dest,
prop = Array.prototype;
if (typeof args[0] === 'boolean') {
deep = prop.shift.call(args);
};
dest = prop.shift.call(args);
prop.forEach.call(args, function(src) {
if(!src) return;
Object.keys(src).forEach(function(key) {
if (deep && typeof src[key] === 'object' && typeof dest[key] === 'object') {
tethys.extend(true, dest[key], src[key]);
} else if (typeof src[key] !== 'undefined') {
dest[key] = src[key];
};
});
});
return dest;
};
// 合并数组
tethys.merge = function(ary1, ary2) {
(ary2 || []).forEach(function(n) {
ary1.push(n);
});
};
// 把html代码转换成NodeList
tethys.parseHtml = function(str) {
var div = document.createElement('DIV');
div.innerHTML = str;
return div.childNodes;
};
// 微型模板
tethys.tpl = function(s, o) {
var SUBREGEX = /\{\s*([^|}]+?)\s*(?:\|([^}]*))?\s*\}/g;
return s.replace ? s.replace(SUBREGEX, function(match, key) {
return typeof o[key] === 'undefined' ? match : o[key];
}) : s;
};
//
tethys.fn = {
// 遍历
each: function(fn) {
Array.prototype.forEach.call(this || [], fn);
return this;
},
// 绑定事件
on: function(events, fn) {
events = events.split(/\s*\,\s*/);
return this.each(function(el) {
fn = fn.bind(el);
events.forEach(function(event) {
el.addEventListener(event, fn);
});
});
},
// 触发事件
trigger: function (eventName) {
return this.each(function(el){
fireEvent(el, eventName);
});
},
// 查询/设置元素的实际样式
// css('color')
// css('color', 'red')
// css({ color: 'red' })
css: function(key, value) {
return valueHandler.call(this, arguments, function(el) {
// 查询样式
return getComputedStyle(el, camelCase(key));
}, function(el, key, val) {
// 设置样式
el.style[camelCase(key)] = val + '';
});
},
// 查询元素的内联样式
style: function(key) {
return this[0] ? this[0].style[camelCase(key)] : '';
},
// 查询/设置attribute
// attr('maxlength')
// attr('maxlength', '12')
// attr({ maxlength: '12' })
attr: function(key, value) {
return valueHandler.call(this, arguments, function(el, key) {
// 查询属性
return el.getAttribute(key);
}, function(el, key, val) {
// 设置属性
el.setAttribute(key, val);
});
},
// 检查是否有class
hasClass: function(cls) {
var has = false,
reg = new RegExp('\\b' + cls + '\\b');
this.each(function(el) {
has = has || !!el.className.match(reg);
});
return has;
},
// 添加class
addClass: function(cls, type) {
var reg = new RegExp('\\b' + cls + '\\b');
// 为所有节点添加或删除class
return this.each(function(el) {
var name = el.className;
if (typeof name !== 'string') return;
if (type === 'remove') {
// remove
if (name.match(reg)) {
el.className = name.replace(reg, '');
}
} else {
// add
if (!name.match(reg)) {
el.className += ' ' + cls;
}
}
});
},
// 删除class
removeClass: function(cls) {
return this.addClass(cls, 'remove');
},
// 设置html
html: function(html) {
return this.each(function(el) {
el.innerHTML = html;
});
},
// 显示
show: function() {
return this.each(function(el) {
if (el.style.display === 'none') {
el.style.display = el.getAttribute('od') || '';
};
});
},
// 隐藏
hide: function() {
return this.each(function(el) {
if (el.style.display !== 'none') {
el.setAttribute('od', getComputedStyle(el, 'display'));
el.style.display = 'none';
};
});
},
// 切换显示隐藏
toggle: function() {
return this.each(function(el) {
var e = $(el);
e.css("display") == "none" ? e.show() : e.hide();
});
},
// 追加节点
append: function(child) {
var children = tethys(child);
return this.each(function(el) {
children.each(function(child, i) {
el.appendChild(child);
});
});
},
// 查找
find: function(selector) {
var nodes = [];
this.each(function(el) {
tethys(selector, el).each(function(node) {
nodes.push(node);
});
});
return tethys(nodes);
},
// 延时
delay: function(time, fn){
setTimeout(fn.bind(this), time);
return this;
}
};
// 设置/查询:宽度/高度
['width', 'height'].forEach(function(name){
tethys.fn[name] = function(val){
// 如果有参数且参数时数值,设置样式,并且单位为px
if(typeof val === 'number'){
return this.css(name, val + 'px');
}else{
// 否则,返回实际样式的数值
return getNumber(getComputedStyle(this[0], name));
};
};
});
return tethys;
}));