-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
278 lines (247 loc) · 9.22 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
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
(function (define) {
define(function () {
function getTransitionEvent() {
var type = null;
var el = document.createElement("div");
var transitions = {
'WebkitTransition' : 'webkitTransitionEnd',
'MozTransition' : 'transitionend',
'OTransition' : 'otransitionend',
'transition' : 'transitionend'
};
for (var t in transitions) {
if (el.style[t] !== undefined) {
type = transitions[t];
break;
}
}
return type;
}
function jQuery(selector, host) {
if (selector instanceof jQuery) {
return selector;
}
if (!(this instanceof jQuery)) {
return new jQuery(selector, host);
}
if (typeof selector === 'string') {
if (selector.startsWith('<')) {
var div = document.createElement('div');
div.innerHTML = selector;
this.nodes = Array.prototype.slice.call(div.childNodes);
} else {
if (!!host) {
this.nodes = host.nodes.map(function (node) {
return Array.prototype.slice.call(node.querySelectorAll(selector));
}).reduce(function (a, b) {
return a.concat(b);
});
} else {
this.nodes = Array.prototype.slice.call(document.querySelectorAll(selector));
}
}
} else if (selector instanceof HTMLElement) {
this.nodes = [selector];
}
this.eventHandlers = {};
}
Object.defineProperty(jQuery.prototype, 'length', {
get: function() {
return this.nodes.length;
}
});
// only for children().length or passing result to jQuery function
jQuery.prototype.children = function() {
var childs = [];
this.each(function (node) {
childs = childs.concat(Array.prototype.slice.call(node.childNodes));
});
return childs;
};
jQuery.prototype.each = function (callback) {
for (var i = 0, l = this.nodes.length; i < l; i++) {
if (callback.call(this, this.nodes[i]) === false) {
break;
}
}
return this;
};
jQuery.prototype.attr = function (name, value) {
return this.each(function (node) {
node.setAttribute(name, value);
});
};
jQuery.prototype.addClass = function (className) {
return this.each(function (node) {
var re = new RegExp('(?:^|\\s)' + className + '(?:\\s|$)', 'g');
if (!re.test(node.className)) {
node.className += (!node.className ? '' : ' ') + className;
}
});
};
jQuery.prototype.removeClass = function (classNames) {
var classes = classNames.trim().split(' ');
return this.each(function (node) {
classes.forEach(function (className) {
if (!className) return;
var re = new RegExp('(?:^|\\s)' + className + '(?:\\s|$)', 'g');
if (re.test(node.className)) {
node.className = node.className.replace(re, ' ').trim();
}
});
});
};
jQuery.prototype.appendTo = function (jquery) {
jquery.append(this);
return this;
};
jQuery.prototype.append = function (obj, first) {
if (this.nodes.length > 0) {
var firstNode = this.nodes[0];
if (obj instanceof jQuery) {
obj.each(function (node) {
firstNode.insertBefore(node, !!first ? firstNode.firstChild : null);
});
} else if (typeof obj === 'string') {
firstNode.innerHTML = obj;
}
}
return this;
};
jQuery.prototype.prepend = function (obj) {
return this.append(obj, true);
};
jQuery.prototype.toggle = function (value) {
return this.each(function (node) {
node.style.display = value;
});
};
jQuery.prototype.hide = function () {
return this.toggle('none');
};
jQuery.prototype.show = function () {
return this.toggle('');
};
jQuery.prototype.width = function (value) {
return this.each(function (node) {
node.style.width = value;
});
};
// only for ':visible' and non-fixed elements
jQuery.prototype.is = function (selector) {
var result = false;
if (selector === ':visible') {
this.each(function (node) {
if (node.offsetParent !== null) {
result = true;
}
});
}
return result;
};
jQuery.prototype.bind = function (event, fn) {
this.eventHandlers[event] = fn;
return this.each(function (el) {
if (typeof el.addEventListener === "function") {
el.addEventListener(event, fn, false);
} else if (el.attachEvent) {
el.attachEvent("on" + event, fn);
}
});
};
jQuery.prototype.unbind = function (event) {
var fn = this.eventHandlers[event];
if (!!fn) {
delete this.eventHandlers[event];
return this.each(function (el) {
if (typeof el.removeEventListener === "function") {
el.removeEventListener(event, fn, false);
} else if (el.detachEvent) {
el.detachEvent("on" + event, fn);
}
});
} else {
return this;
}
};
jQuery.prototype.unbindAll = function () {
Object.keys(this.eventHandlers).forEach(function (event) {
this.unbind(event);
}, this);
};
jQuery.prototype.remove = function () {
this.unbindAll();
return this.each(function (node) {
if (node.parentNode) {
node.parentNode.removeChild(node);
}
});
};
jQuery.prototype.hover = function (handlerIn, handlerOut) {
this.bind('mouseenter', handlerIn);
this.bind('mouseleave', handlerOut);
return this;
};
jQuery.prototype.click = function (handler) {
return this.bind('click', handler);
};
jQuery.prototype.stop = function () {
this.removeClass('alertify-log-hide');
return this.unbind(jQuery.transition);
};
jQuery.prototype.fadeIn = function (options) {
this.addClass('alertify-log');
setTimeout((function () {
if (jQuery.transition !== null) {
this.unbind(jQuery.transition);
this.bind(jQuery.transition, (function () {
this.unbind(jQuery.transition);
if (options.complete) {
options.complete();
}
}).bind(this));
this.addClass('alertify-log-show');
} else {
if (options.complete) {
options.complete();
}
}
}).bind(this), 50);
return this.show();
};
jQuery.prototype.fadeOut = function (options) {
setTimeout((function () {
if (jQuery.transition !== null) {
this.unbind(jQuery.transition);
this.bind(jQuery.transition, (function () {
this.unbind(jQuery.transition);
this.hide();
if (options.complete) {
options.complete();
}
}).bind(this));
this.addClass('alertify-log-hide');
} else {
this.hide();
if (options.complete) {
options.complete();
}
}
}).bind(this), 50);
return this;
};
jQuery.extend = function (target) {
var args = Array.prototype.slice.call(arguments, 0);
Object.assign.apply(null, args);
return target;
};
jQuery.transition = getTransitionEvent();
return jQuery;
});
}(function (factory) {
if (typeof module !== 'undefined' && module.exports) {
module.exports = factory();
} else {
window.jQuery = factory();
}
}));