EventManager.js

JavaScript/News 2007.05.18 19:45
/*
* EventManager.js
* by Keith Gaughan
*
* This allows event handlers to be registered unobtrusively, and cleans
* them up on unload to prevent memory leaks.
*
* Copyright (c) Keith Gaughan, 2005.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v1.0
* (CPL) which accompanies this distribution, and is available at
* http://www.opensource.org/licenses/cpl.php
*
* This software is covered by a modified version of the Common Public License
* (CPL), where Keith Gaughan is the Agreement Steward, and the licensing
* agreement is covered by the laws of the Republic of Ireland.
*/

// For implementations that don't include the push() methods for arrays.

if (!Array.prototype.push) {
    Array.prototype.push = function(elem) {
        this[this.length] = elem;
    }
}


var EventManager = {
    _registry: null,

    Initialise: function() {
        if (this._registry == null) {
            this._registry = [];

            // Register the cleanup handler on page unload.
            EventManager.Add(window, "unload", this.CleanUp);
        }
    },


    /**
     * Registers an event and handler with the manager.
     *
     * @param  obj         Object handler will be attached to.
     * @param  type        Name of event handler responds to.
     * @param  fn          Handler function.
     * @param  useCapture  Use event capture. False by default.
     *                     If you don't understand this, ignore it.
     *
     * @return True if handler registered, else false.
     */

    Add: function(obj, type, fn, useCapture) {
        this.Initialise();

        // If a string was passed in, it's an id.
        if (typeof obj == "string") {
            obj = document.getElementById(obj);
        }

        if (obj == null || fn == null) {
            return false;
        }


        // Mozilla/W3C listeners?
        if (obj.addEventListener) {
            obj.addEventListener(type, fn, useCapture);
            this._registry.push({obj: obj, type: type, fn: fn, useCapture: useCapture});
            return true;
        }


        // IE-style listeners?
        if (obj.attachEvent && obj.attachEvent("on" + type, fn)) {
            this._registry.push({obj: obj, type: type, fn: fn, useCapture: false});
            return true;
        }


        return false;
    },


    /**
     * Cleans up all the registered event handlers.
     */

    CleanUp: function() {
        for (var i = 0; i < EventManager._registry.length; i++) {
            with (EventManager._registry[i]) {
                // Mozilla/W3C listeners?
                if (obj.removeEventListener) {
                    obj.removeEventListener(type, fn, useCapture);
                }

                // IE-style listeners?
                else if (obj.detachEvent) {
                    obj.detachEvent("on" + type, fn);
                }
            }
        }


        // Kill off the registry itself to get rid of the last remaining
        // references.

        EventManager._registry = null;
    }
};

신고
Posted by Rhio.kim
// prototypes.js

Number.max = function (a,b) {
return a<b?b:a;
}

Number.min = function (a,b) {
return a>b?b:a;
}

Math.mod = function(val,mod) {
if (val < 0) {
while(val<0) val += mod;
return val;
} else {
return val%mod;
}
}

window.getInnerWidth = function() {
if (window.innerWidth) {
return window.innerWidth;
} else if (document.body.clientWidth) {
return document.body.clientWidth;
} else if (document.documentElement.clientWidth) {
return document.documentElement.clientWidth;
}
}

window.getInnerHeight = function() {
if (window.innerHeight) {
return window.innerHeight;
} else if (document.body.clientHeight) {
return document.body.clientHeight;
} else if (document.documentElement.clientHeight) {
return document.documentElement.clientHeight;
}
}

String.prototype.endsWith = function(str) {
return (this.length-str.length)==this.lastIndexOf(str);
}

String.prototype.reverse = function() {
var s = "";
var i = this.length;
while (i>0) {
s += this.substring(i-1,i);
i--;
}
return s;
}

// this trim was suggested by Tobias Hinnerup
String.prototype.trim = function() {
return(this.replace(/^\s+/,'').replace(/\s+$/,''));
}

String.prototype.toInt = function() {
var a = new Array();
for (var i = 0; i < this.length; i++) {
a[i] = this.charCodeAt(i);
}
return a;
}

Array.prototype.intArrayToString = function() {
var a = new String();
for (var i = 0; i < this.length; i++) {
if(typeof this[i] != "number") {
throw new Error("Array must be all numbers");
} else if (this[i] < 0) {
throw new Error("Numbers must be 0 and up");
}
a += String.fromCharCode(this[i]);
}
return a;
}

Array.prototype.compareArrays = function(arr) {
if (this.length != arr.length) return false;
for (var i = 0; i < arr.length; i++) {
if (this[i].compareArrays) { //likely nested array
if (!this[i].compareArrays(arr[i])) return false;
else continue;
}
if (this[i] != arr[i]) return false;
}
return true;
}

Array.prototype.map = function(fnc) {
var a = new Array(this.length);
for (var i = 0; i < this.length; i++) {
a[i] = fnc(this[i]);
}
return a;
}

Array.prototype.foldr = function(fnc,start) {
var a = start;
for (var i = this.length-1; i > -1; i--) {
a = fnc(this[i],a);
}
return a;
}

Array.prototype.foldl = function(fnc,start) {
var a = start;
for (var i = 0; i < this.length; i++) {
a = fnc(this[i],a);
}
return a;
}

Array.prototype.exists = function (x) {
for (var i = 0; i < this.length; i++) {
if (this[i] == x) return true;
}
return false;
}

Array.prototype.filter = function(fnc) {
var a = new Array();
for (var i = 0; i < this.length; i++) {
if (fnc(this[i])) {
a.push(this[i]);
}
}
return a;
}

Array.prototype.random = function() {
return this[Math.floor((Math.random()*this.length))];
}
신고
Posted by Rhio.kim