-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathplatform.js
124 lines (109 loc) · 3.07 KB
/
platform.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
// Production steps of ECMA-262, Edition 5, 15.4.4.18
// Reference: http://es5.github.com/#x15.4.4.18
if (!Array.prototype.forEach) {
Array.prototype.forEach = function(callback, thisArg) {
var T, k;
if (this === null) {
throw new TypeError("this is null or not defined");
}
var O = Object(this);
var len = O.length >>> 0; // Hack to convert O.length to a UInt32
if ({}.toString.call(callback) != "[object Function]") {
throw new TypeError( callback + " is not a function");
}
if ( thisArg ) {
T = thisArg;
}
k = 0;
while( k < len ) {
var kValue;
if ( k in O ) {
kValue = O[ k ];
callback.call( T, kValue, k, O );
}
k++;
}
};
}
Array.prototype.remove = function(elem) {
var i = this.indexOf(elem);
if (i >= 0) {
return this.splice(i,1);
}
return null;
};
Array.prototype.insertBefore = function(elem,before) {
var i = this.indexOf(before);
if (i < 0) { throw "insertBefore: before not found"; }
this.splice(i,0,elem);
};
Array.prototype.insertAfter = function(elem,after) {
var i = this.indexOf(after);
if (i < 0) { throw "insertAfter: after not found"; }
this.splice(i+1,0,elem);
};
Array.prototype.sample = function() {
return this[Math.floor(this.length*Math.random())];
};
Array.prototype.max = function(){
return Math.max.apply(Math, array);
};
Array.prototype.min = function(){
return Math.min.apply(Math, array);
};
Array.prototype.maxF = function(compare) {
return this.reduce(function(prev,current) {
if (prev === undefined || compare(prev,current) < 0) { return current; }
return prev;
},undefined);
};
Array.prototype.minF = function(compare) {
return this.reduce(function(prev,current) {
if (prev === undefined || compare(prev,current) > 0) { return current; }
return prev;
},undefined);
};
Boolean.prototype.toNumber = function() {
return (this ? 1 : 0);
};
// Random number between -1 and 1
Math.rnd = function() {
return (Math.random()-0.5)*2;
};
Math.clamp = function(v,min,max) {
return Math.max(min,Math.min(max,v));
};
Math.sign = function(v) {
return v === 0 ? 0 :
(v < 0 ? -1 : +1);
};
define(['eventemitter'],function(eventemitter) {
var platform = {};
eventemitter._inherit(platform);
// Handle onload
(function() {
var loaded = false;
function callback() {
if (!loaded) {
loaded=true;
platform.emit('load');
}
}
/* Mozilla, Chrome, Opera */
if (document.addEventListener) {
document.addEventListener('DOMContentLoaded', callback, false);
}
/* Safari, iCab, Konqueror */
if (/KHTML|WebKit|iCab/i.test(navigator.userAgent)) {
var DOMLoadTimer = setInterval(function () {
if (/loaded|complete/i.test(document.readyState)) {
callback();
clearInterval(DOMLoadTimer);
}
}, 10);
}
/* Other web browsers */
window.onload = callback;
})();
return platform;
});