forked from kupriyanenko/jbone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.js
167 lines (140 loc) · 3.84 KB
/
core.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
var
// cache previous versions
_$ = win.$,
_jBone = win.jBone,
// Quick match a standalone tag
rquickSingleTag = /^<(\w+)\s*\/?>$/,
// A simple way to check for HTML strings
// Prioritize #id over <tag> to avoid XSS via location.hash
rquickExpr = /^(?:[^#<]*(<[\w\W]+>)[^>]*$|#([\w\-]*)$)/,
// Alias for function
slice = [].slice,
splice = [].splice,
keys = Object.keys,
// Alias for global variables
doc = document,
isString = function(el) {
return typeof el === "string";
},
isObject = function(el) {
return el instanceof Object;
},
isFunction = function(el) {
return ({}).toString.call(el) === "[object Function]";
},
isArray = function(el) {
return Array.isArray(el);
},
jBone = function(element, data) {
return new fn.init(element, data);
},
fn;
// set previous values and return the instance upon calling the no-conflict mode
jBone.noConflict = function() {
win.$ = _$;
win.jBone = _jBone;
return jBone;
};
fn = jBone.fn = jBone.prototype = {
init: function(element, data) {
var elements, tag, wraper, fragment;
if (!element) {
return this;
}
if (isString(element)) {
// Create single DOM element
if (tag = rquickSingleTag.exec(element)) {
this[0] = doc.createElement(tag[1]);
this.length = 1;
if (isObject(data)) {
this.attr(data);
}
return this;
}
// Create DOM collection
if ((tag = rquickExpr.exec(element)) && tag[1]) {
fragment = doc.createDocumentFragment();
wraper = doc.createElement("div");
wraper.innerHTML = element;
while (wraper.lastChild) {
fragment.appendChild(wraper.firstChild);
}
elements = slice.call(fragment.childNodes);
return jBone.merge(this, elements);
}
// Find DOM elements with querySelectorAll
if (jBone.isElement(data)) {
return jBone(data).find(element);
}
try {
elements = doc.querySelectorAll(element);
return jBone.merge(this, elements);
} catch (e) {
return this;
}
}
// Wrap DOMElement
if (element.nodeType) {
this[0] = element;
this.length = 1;
return this;
}
// Run function
if (isFunction(element)) {
return element();
}
// Return jBone element as is
if (element instanceof jBone) {
return element;
}
// Return element wrapped by jBone
return jBone.makeArray(element, this);
},
pop: [].pop,
push: [].push,
reverse: [].reverse,
shift: [].shift,
sort: [].sort,
splice: [].splice,
slice: [].slice,
indexOf: [].indexOf,
forEach: [].forEach,
unshift: [].unshift,
concat: [].concat,
join: [].join,
every: [].every,
some: [].some,
filter: [].filter,
map: [].map,
reduce: [].reduce,
reduceRight: [].reduceRight,
length: 0
};
fn.constructor = jBone;
fn.init.prototype = fn;
jBone.setId = function(el) {
var jid = el.jid;
if (el === win) {
jid = "window";
} else if (el.jid === undefined) {
el.jid = jid = ++jBone._cache.jid;
}
if (!jBone._cache.events[jid]) {
jBone._cache.events[jid] = {};
}
};
jBone.getData = function(el) {
el = el instanceof jBone ? el[0] : el;
var jid = el === win ? "window" : el.jid;
return {
jid: jid,
events: jBone._cache.events[jid]
};
};
jBone.isElement = function(el) {
return el && el instanceof jBone || el instanceof HTMLElement || isString(el);
};
jBone._cache = {
events: {},
jid: 0
};