-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.js
95 lines (81 loc) · 2.4 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
namespace = function() {
var a = arguments, obj = null, i, j, chunks;
for (i = 0; i < a.length; i = i + 1) {
chunks = a[i].split(".");
obj = window;
for (j = 0; j < chunks.length; j = j + 1) {
obj[chunks[j]] = obj[chunks[j]] || {};
obj = obj[chunks[j]];
}
}
return obj;
};
namespace("util");
util.log = function() {
var args = Array.prototype.slice.call(arguments, 0);
args.unshift(new Date().getTime());
if (util.isTouch) {
console.log.call(console, args.join(" "));
} else {
console.log.apply(console, args);
}
};
util.mixin = function(target, source) {
if (typeof source == "object") {
for (var key in source) {
source.hasOwnProperty(key) && (target[key] = source[key]);
}
}
return target;
};
util.isTouch = ("ontouchstart" in document.documentElement);
util.throttle = function(func, delay) {
var lastRun = null;
return function() {
var now = new Date().getTime();
if (lastRun === null || now - lastRun > delay) {
lastRun = now;
return func.apply(this, arguments);
}
return;
}
}
util.randomInt= function(min, max) {
return min + Math.floor(Math.random() * max);
}
namespace("util.dom");
util.dom.offset = function(domEl) {
var ox = 0, oy = 0;
if (domEl.offsetParent) {
do {
ox+= domEl.offsetLeft;
oy+= domEl.offsetTop;
}
while (domEl = domEl.offsetParent);
}
return {x: ox, y: oy};
}
util.dom.hasClass = function(domEl, className) {
return (" " + domEl.className + " ").indexOf(" " + className + " ") > -1;
}
util.dom.addClass = function(domEl, className) {
if (!util.dom.hasClass(domEl, className)) {
domEl.className = (domEl.className + " " + className).trim();
}
}
util.dom.removeClass = function(domEl, className) {
if (util.dom.hasClass(domEl, className)) {
domEl.className = (" " + domEl.className + " ").replace(" " + className + " ", " ").trim();
}
}
// ECMAScript 5 compatibility
if (!Function.prototype.bind) {
Function.prototype.bind = function(context) {
var self = this,
slice = Array.prototype.slice,
args = slice.call(arguments, 1);
return function() {
return self.apply(context, args.concat(slice.call(arguments)));
}
}
}