129 lines
2.7 KiB
JavaScript
129 lines
2.7 KiB
JavaScript
|
var Class = {
|
||
|
create: function() {
|
||
|
return function() {
|
||
|
this.initialize.apply(this, arguments);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
var Abstract = new Object();
|
||
|
|
||
|
Object.extend = function(destination, source) {
|
||
|
for (var property in source) {
|
||
|
destination[property] = source[property];
|
||
|
}
|
||
|
return destination;
|
||
|
}
|
||
|
|
||
|
Object.extend(Object, {
|
||
|
inspect: function(object) {
|
||
|
try {
|
||
|
if (object === undefined) return 'undefined';
|
||
|
if (object === null) return 'null';
|
||
|
return object.inspect ? object.inspect() : object.toString();
|
||
|
} catch (e) {
|
||
|
if (e instanceof RangeError) return '...';
|
||
|
throw e;
|
||
|
}
|
||
|
},
|
||
|
|
||
|
keys: function(object) {
|
||
|
var keys = [];
|
||
|
for (var property in object)
|
||
|
keys.push(property);
|
||
|
return keys;
|
||
|
},
|
||
|
|
||
|
values: function(object) {
|
||
|
var values = [];
|
||
|
for (var property in object)
|
||
|
values.push(object[property]);
|
||
|
return values;
|
||
|
},
|
||
|
|
||
|
clone: function(object) {
|
||
|
return Object.extend({}, object);
|
||
|
}
|
||
|
});
|
||
|
|
||
|
Function.prototype.bind = function() {
|
||
|
var __method = this, args = $A(arguments), object = args.shift();
|
||
|
return function() {
|
||
|
return __method.apply(object, args.concat($A(arguments)));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
Function.prototype.bindAsEventListener = function(object) {
|
||
|
var __method = this, args = $A(arguments), object = args.shift();
|
||
|
return function(event) {
|
||
|
return __method.apply(object, [( event || window.event)].concat(args).concat($A(arguments)));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
Object.extend(Number.prototype, {
|
||
|
toColorPart: function() {
|
||
|
var digits = this.toString(16);
|
||
|
if (this < 16) return '0' + digits;
|
||
|
return digits;
|
||
|
},
|
||
|
|
||
|
succ: function() {
|
||
|
return this + 1;
|
||
|
},
|
||
|
|
||
|
times: function(iterator) {
|
||
|
$R(0, this, true).each(iterator);
|
||
|
return this;
|
||
|
}
|
||
|
});
|
||
|
|
||
|
var Try = {
|
||
|
these: function() {
|
||
|
var returnValue;
|
||
|
|
||
|
for (var i = 0, length = arguments.length; i < length; i++) {
|
||
|
var lambda = arguments[i];
|
||
|
try {
|
||
|
returnValue = lambda();
|
||
|
break;
|
||
|
} catch (e) {}
|
||
|
}
|
||
|
|
||
|
return returnValue;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/*--------------------------------------------------------------------------*/
|
||
|
|
||
|
var PeriodicalExecuter = Class.create();
|
||
|
PeriodicalExecuter.prototype = {
|
||
|
initialize: function(callback, frequency) {
|
||
|
this.callback = callback;
|
||
|
this.frequency = frequency;
|
||
|
this.currentlyExecuting = false;
|
||
|
|
||
|
this.registerCallback();
|
||
|
},
|
||
|
|
||
|
registerCallback: function() {
|
||
|
this.timer = setInterval(this.onTimerEvent.bind(this), this.frequency * 1000);
|
||
|
},
|
||
|
|
||
|
stop: function() {
|
||
|
if (!this.timer) return;
|
||
|
clearInterval(this.timer);
|
||
|
this.timer = null;
|
||
|
},
|
||
|
|
||
|
onTimerEvent: function() {
|
||
|
if (!this.currentlyExecuting) {
|
||
|
try {
|
||
|
this.currentlyExecuting = true;
|
||
|
this.callback(this);
|
||
|
} finally {
|
||
|
this.currentlyExecuting = false;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|