-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathX.js
195 lines (158 loc) · 4.87 KB
/
X.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
/*
*
* xxxxxxx xxxxxxx
* x:::::x x:::::x
* x:::::x x:::::x
* x:::::xx:::::x
* x::::::::::x
* x::::::::x
* x::::::::x
* x::::::::::x
* x:::::xx:::::x
* x:::::x x:::::x
* x:::::x x:::::x
* THE xxxxxxx xxxxxxx TOOLKIT
*
* http://www.goXTK.com
*
* Copyright (c) 2012 The X Toolkit Developers <[email protected]>
*
* The X Toolkit (XTK) is licensed under the MIT License:
* http://www.opensource.org/licenses/mit-license.php
*
* "Free software" is a matter of liberty, not price.
* "Free" as in "free speech", not as in "free beer".
* - Richard M. Stallman
*
*
*/
// entry point
// namespace
goog.provide('X');
goog.provide('X.counter');
/**
* The XTK namespace.
*
* @const
*/
var X = X || {};
/**
* Can be used to check if the XTK library was compiled.
*
* <pre>
* if (X.DEV === undefined) {
* // xtk was compiled
* }
* </pre>
*
* @type {boolean}
*/
X.DEV = true;
/**
* The counter class, keeping track of instance ids.
*
* @constructor
*/
X.counter = function() {
this._counter = 0;
/**
* Get a unique id.
*
* @return {number} A unique id
*/
this.uniqueId = function() {
// return a unique id
return this._counter++;
};
};
window["X.counter"] = new X.counter();
/**
* Injection mechanism for mixins (from
* http://ejohn.org/blog/javascript-getters-and-setters/) which means copying
* properties, getters/setters and functions from a source object to a target.
* Works best on instances.
*
* @param {Object} a The target object.
* @param {Object} b The source object.
* @return {Object} The altered object.
*/
function inject(a, b) {
for ( var i in b) { // iterate over all properties
// get getter and setter functions
var g = b.__lookupGetter__(i), s = b.__lookupSetter__(i);
if (g || s) { // if there is a getter or setter
if (g) {
a.__defineGetter__(i, g); // copy getter to new object
}
if (s) {
a.__defineSetter__(i, s); // copy setter to new object
}
} else {
a[i] = b[i]; // just copy the value; nothing special
}
}
return a; // return the altered object
}
//
// BROWSER COMPATIBILITY FIXES GO HERE
//
//
// 1. Safari does not support the .bind(this) functionality which is crucial for
// XTK's event mechanism. This hack fixes this.
//
if (!Function.prototype.bind) {
Function.prototype.bind = function(oThis) {
if (typeof this !== "function") {
// closest thing possible to the ECMAScript 5 internal IsCallable function
throw new TypeError(
"Function.prototype.bind - what is trying to be bound is not callable");
}
var fSlice = Array.prototype.slice, aArgs = fSlice.call(arguments, 1), fToBind = this;
/**
* @constructor
*/
var fNOP = function() {
};
var fBound = function() {
return fToBind.apply(this instanceof fNOP ? this : oThis || window, aArgs
.concat(fSlice.call(arguments)));
};
fNOP.prototype = this.prototype;
fBound.prototype = new fNOP();
return fBound;
};
}
// http://paulirish.com/2011/requestanimationframe-for-smart-animating/
// http://my.opera.com/emoller/blog/2011/12/20/requestanimationframe-for-smart-er-animating
// requestAnimationFrame polyfill by Erik Mšller
// fixes from Paul Irish and Tino Zijdel
//
// from: https://gist.github.com/1579671
(function() {
var lastTime = 0;
var vendors = ['ms', 'moz', 'webkit', 'o'];
for ( var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
window.requestAnimationFrame = window[vendors[x] + 'RequestAnimationFrame'];
window.cancelAnimationFrame = window[vendors[x] + 'CancelAnimationFrame'] ||
window[vendors[x] + 'CancelRequestAnimationFrame'];
}
if (!window.requestAnimationFrame) {
window.requestAnimationFrame = function(callback, element) {
var currTime = Date.now(); // changed this to avoid new object each time
var timeToCall = Math.max(0, 16 - (currTime - lastTime));
var id = window.setTimeout(function() {
callback(currTime + timeToCall);
}, timeToCall);
lastTime = currTime + timeToCall;
return id;
};
}
if (!window.cancelAnimationFrame) {
window.cancelAnimationFrame = function(id) {
clearTimeout(id);
};
}
}());
goog.exportSymbol('Function.prototype.bind', Function.prototype.bind);
goog.exportSymbol('window.requestAnimationFrame', window.requestAnimationFrame);
goog.exportSymbol('window.cancelAnimationFrame', window.cancelAnimationFrame);