This repository has been archived by the owner on Apr 15, 2020. It is now read-only.
/
util.js
76 lines (70 loc) · 2.06 KB
/
util.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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
var util = {};
util.urlParam = function(name)
{
var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
if (!results) { return undefined; }
return unescape(results[1] || undefined);
};
util.assert = function (cond) {
if (! cond) {
var args = ["Assertion error:"].concat(Array.prototype.slice.call(arguments, 1));
console.error.apply(console, args);
if (console.trace) {
console.trace();
}
throw new Error(args.join(" "));
}
};
util.extend = function (base, extensions) {
if (! extensions) {
extensions = base;
base = {};
}
for (var a in extensions) {
if (extensions.hasOwnProperty(a)) {
base[a] = extensions[a];
}
}
return base;
};
util.Class = function (superClass, prototype) {
var a;
if (prototype === undefined) {
prototype = superClass;
} else {
if (superClass.prototype) {
superClass = superClass.prototype;
}
var newPrototype = Object.create(superClass);
for (a in prototype) {
if (prototype.hasOwnProperty(a)) {
newPrototype[a] = prototype[a];
}
}
prototype = newPrototype;
}
var ClassObject = function () {
var obj = Object.create(prototype);
obj.constructor.apply(obj, arguments);
obj.constructor = ClassObject;
return obj;
};
ClassObject.prototype = prototype;
if (prototype.constructor.name) {
ClassObject.className = prototype.constructor.name;
ClassObject.toString = function () {
return '[Class ' + this.className + ']';
};
}
if (prototype.classMethods) {
for (a in prototype.classMethods) {
if (prototype.classMethods.hasOwnProperty(a)) {
ClassObject[a] = prototype.classMethods[a];
}
}
}
return ClassObject;
};