-
Notifications
You must be signed in to change notification settings - Fork 1
/
tools.js
225 lines (196 loc) · 6.3 KB
/
tools.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
/*
FILE ARCHIVED ON 13:31:52 May 30, 2016 AND RETRIEVED FROM THE
INTERNET ARCHIVE ON 15:51:22 Aug 21, 2016.
JAVASCRIPT APPENDED BY WAYBACK MACHINE, COPYRIGHT INTERNET ARCHIVE.
ALL OTHER CONTENT MAY ALSO BE PROTECTED BY COPYRIGHT (17 U.S.C.
SECTION 108(a)(3)).
*/
// Misc Tools
// Copyright (c) 2010 Joseph Huckaby.
// Released under the LGPL v3.0: /web/20160530133152/http://www.opensource.org/licenses/lgpl-3.0.html
// getInnerWindowSize() was grabbed from: /web/20160530133152/http://www.howtocreate.co.uk/tutorials/javascript/browserwindow
function $(thingy) {
// universal DOM lookup function, extends object with hide/show/addClass/removeClass
// can pass in ID or actual DOM object reference
var obj = (typeof(thingy) == 'string') ? document.getElementById(thingy) : thingy;
if (obj && !obj.setOpacity) {
obj.hide = function() { this.style.display = 'none'; return this; };
obj.show = function() { this.style.display = ''; return this; };
obj.addClass = function(name) { this.removeClass(name); this.className += ' ' + name; return this; };
obj.removeClass = function(name) {
var classes = this.className.split(/\s+/);
var idx = find_idx_in_array( classes, name );
if (idx > -1) {
classes.splice( idx, 1 );
this.className = classes.join(' ');
}
return this;
};
obj.setClass = function(name, enabled) {
if (enabled) this.addClass(name);
else this.removeClass(name);
};
}
return obj;
}
function parseQueryString(queryString) {
// parse query string into object
var pair = null;
var queryObject = new Object();
queryString = queryString.replace(/^.*\?(.+)$/,'$1');
while ((pair = queryString.match(/(\w+)=([^\&]*)\&?/)) && pair[0].length) {
queryString = queryString.substring( pair[0].length );
pair[2] = unescape(pair[2]);
if (/^\-?\d+$/.test(pair[2])) pair[2] = parseInt(pair[2], 10);
else if (/^\-?\d+\.\d+$/.test(pair[2])) pair[2] = parseFloat(pair[2]);
queryObject[pair[1]] = pair[2];
}
return queryObject;
}
function GetTickCount() {
// milliseconds since page load
return Math.floor( (new Date()).getTime() - CanvasCycle.globalTimeStart );
}
function getInnerWindowSize(dom) {
// get size of inner window
// From: /web/20160530133152/http://www.howtocreate.co.uk/tutorials/javascript/browserwindow
if (!dom) dom = window;
var myWidth = 0, myHeight = 0;
if( typeof( dom.innerWidth ) == 'number' ) {
// Non-IE
myWidth = dom.innerWidth;
myHeight = dom.innerHeight;
}
else if( dom.document.documentElement && ( dom.document.documentElement.clientWidth || dom.document.documentElement.clientHeight ) ) {
// IE 6+ in 'standards compliant mode'
myWidth = dom.document.documentElement.clientWidth;
myHeight = dom.document.documentElement.clientHeight;
}
else if( dom.document.body && ( dom.document.body.clientWidth || dom.document.body.clientHeight ) ) {
// IE 4 compatible
myWidth = dom.document.body.clientWidth;
myHeight = dom.document.body.clientHeight;
}
return { width: myWidth, height: myHeight };
}
function find_idx_in_array(arr, elem) {
// return idx of elem in arr, or -1 if not found
for (var idx = 0, len = arr.length; idx < len; idx++) {
if (arr[idx] == elem) return idx;
}
return -1;
}
function isa_hash(arg) {
// determine if arg is a hash
return( !!arg && (typeof(arg) == 'object') && (typeof(arg.length) == 'undefined') );
}
function isa_array(arg) {
// determine if arg is an array or is array-like
if (typeof(arg) == 'array') return true;
return( !!arg && (typeof(arg) == 'object') && (typeof(arg.length) != 'undefined') );
}
function merge_objects(a, b) {
// merge keys from a and b into c and return c
// b has precedence over a
if (!a) a = {};
if (!b) b = {};
var c = {};
// also handle serialized objects for a and b
if (typeof(a) != 'object') eval( "a = " + a );
if (typeof(b) != 'object') eval( "b = " + b );
for (var key in a) c[key] = a[key];
for (var key in b) c[key] = b[key];
return c;
}
function serialize(thingy, glue) {
// serialize anything into json
// or perl object notation (just set glue to '=>')
if (!glue) glue = ':'; // default to json
var stream = '';
if (typeof(thingy) == 'boolean') {
stream += (thingy ? 'true' : 'false');
}
else if (typeof(thingy) == 'number') {
stream += thingy;
}
else if (typeof(thingy) == 'string') {
stream += '"' + thingy.replace(/([\"\\])/g, '\\$1').replace(/\r/g, "\\r").replace(/\n/g, "\\n") + '"';
}
else if (isa_hash(thingy)) {
var num = 0;
var buffer = [];
for (var key in thingy) {
buffer[num] = (key.match(/^[A-Za-z]\w*$/) ? key : ('"'+key+'"')) + glue + serialize(thingy[key], glue);
num++;
}
stream += '{' + buffer.join(',') + '}';
}
else if (isa_array(thingy)) {
var buffer = [];
for (var idx = 0, len = thingy.length; idx < len; idx++) {
buffer[idx] = serialize(thingy[idx], glue);
}
stream += '[' + buffer.join(',') + ']';
}
else {
// unknown type, just return 0
stream += '0';
}
return stream;
}
(function() {
// Browser detection
var u = navigator.userAgent;
var webkit = !!u.match(/webkit/i);
var chrome = !!u.match(/Chrome/);
var safari = !!u.match(/Safari/) && !chrome;
var ie = !!u.match(/MSIE/);
var ie6 = ie && !!u.match(/MSIE\s+6/);
var ie7 = ie && !!u.match(/MSIE\s+7/);
var ie8 = ie && !!u.match(/MSIE\s+8/);
var moz = !safari && !ie;
var op = !!window.opera;
var mac = !!u.match(/Mac/i);
var ff = !!u.match(/(Firefox|Minefield)/);
var iphone = !!u.match(/iPhone/);
var ipad = !!u.match(/iPad/);
var snow = !!u.match(/Mac\s+OS\s+X\s+10\D[6789]/);
var titanium = safari && !!u.match(/Titanium/);
var android = !!u.match(/android/i);
var ver = 0;
if (ff && u.match(/Firefox\D+(\d+(\.\d+)?)/)) {
ver = parseFloat( RegExp.$1 );
}
else if (safari && u.match(/Version\D(\d+(\.\d+)?)/)) {
ver = parseFloat( RegExp.$1 );
}
else if (chrome && u.match(/Chrome\D(\d+(\.\d+)?)/)) {
ver = parseFloat( RegExp.$1 );
}
else if (ie && u.match(/MSIE\D+(\d+(\.\d+)?)/)) {
ver = parseFloat( RegExp.$1 );
}
else if (op && u.match(/Opera\D+(\d+(\.\d+)?)/)) {
ver = parseFloat( RegExp.$1 );
}
window.ua = {
webkit: webkit,
safari: safari,
ie: ie,
ie8: ie8,
ie7: ie7,
ie6: ie6,
moz: moz,
op: op,
mac: mac,
ff: ff,
chrome: chrome,
iphone: iphone,
ipad: ipad,
snow: snow,
titanium: titanium,
android: android,
mobile: iphone || ipad || android,
ver: ver
};
})();