-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
79 lines (69 loc) · 1.63 KB
/
index.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
'use strict';
var domify = require('domify')
, query = require('query')
, toString = Object.prototype.toString
;
function isArray(obj) {
return toString.call(obj) === '[object Array]';
}
function isArguments(obj) {
return toString.call(obj) === '[object Arguments]';
}
function processArguments(args, fn) {
if (isArray(args) || isArguments(args)) {
var i, l = args.length;
if (l > 1) {
i = 0;
while (i < l) {
processArguments(args[i], fn);
i += 1;
}
return;
} else {
args = args[0];
}
}
if (typeof(args) === 'string') {
try {
return processArguments(domify(args), fn);
} catch (e) {
args = document.createTextNode(args);
}
}
fn(args);
}
function Pend(el) {
if (typeof(el) === 'string') {
el = query(el);
}
this.el = el;
}
Pend.prototype.append = function () {
var self = this;
processArguments(arguments, function (el) {
self.el.appendChild(el);
});
return this;
};
Pend.prototype.prepend = function () {
var self = this;
processArguments(arguments, function (el) {
self.el.insertBefore(el, self.el.firstChild);
});
return this;
};
Pend.prototype.appendTo = function (el) {
if (typeof(el) === 'string') {
el = query(el);
}
el.appendChild(this.el);
};
Pend.prototype.prependTo = function (el) {
if (typeof(el) === 'string') {
el = query(el);
}
el.insertBefore(this.el, el.firstChild);
};
module.exports = function (el) {
return new Pend(el);
};