javascript:(function() { document.title = "YuichirouのJavaScriptメモ"; })(); このページをアンテナに追加 RSSフィード

2007年10月2日

Ten.js 10:52 Ten.js - javascript:(function() { document.title = "YuichirouのJavaScriptメモ"; })(); を含むブックマーク はてなブックマーク - Ten.js - javascript:(function() { document.title = "YuichirouのJavaScriptメモ"; })(); Ten.js - javascript:(function() { document.title = "YuichirouのJavaScriptメモ"; })(); のブックマークコメント


/* Ten */
if (typeof(Ten) == 'undefined') {
    Ten = {};
}
Ten.NAME = 'Ten';
Ten.VERSION = 0.16;

/* Ten.Class */
Ten.Class = function(klass, prototype) {
    if (klass && klass.initialize) {
	var c = klass.initialize;
    } else if(klass && klass.base) {
        var c = function() { return klass.base[0].apply(this, arguments) };
    } else {
	var c = function() {};
    }
    c.prototype = prototype || {};
    c.prototype.constructor = c;
    Ten.Class.inherit(c, klass);
    if (klass && klass.base) {
        for (var i = 0;  i < klass.base.length; i++) {
	    var parent = klass.base[i];
            if (i == 0) {
                c.SUPER = parent;
                c.prototype.SUPER = parent.prototype;
            }
            Ten.Class.inherit(c, parent);
            Ten.Class.inherit(c.prototype, parent.prototype);
        }
    }
    return c;
}
Ten.Class.inherit = function(child,parent) {
    for (var prop in parent) {
        if (typeof(child[prop]) != 'undefined' || prop == 'initialize') continue;
        child[prop] = parent[prop];
    }
}

/*
// Basic Ten Classes
*/
/* Ten.Function */
Ten.Function = new Ten.Class({
    bind: function(f,o) {
        return function() {
            f.apply(o, arguments);
        }
    }
});

/* Ten.JSONP */
Ten.JSONP = new Ten.Class({
    initialize: function(uri,obj,method) {
        if (Ten.JSONP.Callbacks.length) {
            setTimeout(function() {new Ten.JSONP(uri,obj,method)}, 500);
            return;
        }
        var del = uri.match(/\?/) ? '&' : '?';
        uri += del + 'callback=Ten.JSONP.callback';
        if (!uri.match(/timestamp=/)) {
            uri += '&' + encodeURI(new Date());
        }
        if (typeof(obj) == 'function' && typeof(method) == 'undefined') {
            obj = {callback: obj};
            method = 'callback';
        }
        if (obj && method) Ten.JSONP.addCallback(obj,method);
        this.script = document.createElement('script');
        this.script.src = uri;
        this.script.type = 'text/javascript';
        document.getElementsByTagName('head')[0].appendChild(this.script);
    },
    addCallback: function(obj,method) {
        Ten.JSONP.Callbacks.push({object: obj, method: method});
    },
    callback: function(args) {
        // alert('callback called');
        var cbs = Ten.JSONP.Callbacks;
        for (var i = 0; i < cbs.length; i++) {
            var cb = cbs[i];
            cb.object[cb.method].call(cb.object, args);
        }
        Ten.JSONP.Callbacks = [];
    },
    MaxBytes: 1800,
    Callbacks: []
});

/* Ten.XHR */
Ten.XHR = new Ten.Class({
    initialize: function(uri,opts,obj,method) {
        if (!uri) return;
        this.request = Ten.XHR.getXMLHttpRequest();
        if (obj && method) this.callback = {object: obj, method: method};
        var xhr = this;
        var prc = this.processReqChange;
        this.request.onreadystatechange = function() {
            prc.apply(xhr, arguments);
        }
        var method = opts.method || 'GET';
        this.request.open(method, uri, true);
        if (method == 'POST') {
            this.request.setRequestHeader('Content-Type',
                                          'application/x-www-form-urlencoded');
        }
        var data = opts.data ? Ten.XHR.makePostData(opts.data) : null;
        this.request.send(data);
    },
    getXMLHttpRequest: function() {
        var xhr;
        var tryThese = [
            function () { return new XMLHttpRequest(); },
            function () { return new ActiveXObject('Msxml2.XMLHTTP'); },
            function () { return new ActiveXObject('Microsoft.XMLHTTP'); },
            function () { return new ActiveXObject('Msxml2.XMLHTTP.4.0'); },
        ];
        for (var i = 0; i < tryThese.length; i++) {
            var func = tryThese[i];
            try {
                xhr = func;
                return func();
            } catch (e) {
                //alert(e);
            }
        }
        return xhr;
    },
    makePostData: function(data) {
        var pairs = [];
        var regexp = /%20/g;
        for (var k in data) {
            var v = data[k].toString();
            var pair = encodeURIComponent(k).replace(regexp,'+') + '=' +
                encodeURIComponent(v).replace(regexp,'+');
            pairs.push(pair);
        }
        return pairs.join('&');
    }
},{
    processReqChange: function() {
        var req = this.request;
        if (req.readyState == 4) {
            if (req.status == 200) {
                var cb = this.callback;
                if (cb) cb.object[cb.method].call(cb.object, req);
            } else {
                // alert("There was a problem retrieving the XML data:\n" +
                //       req.statusText);
            }
        }
    }
});

/* Ten.Observer */
Ten.Observer = new Ten.Class({
    initialize: function(element,event,obj,method) {
        var func = obj;
        if (typeof(method) == 'string') {
            func = obj[method];
        }
        this.element = element;
        this.event = event;
        this.listener = function(event) {
            return func.call(obj, new Ten.Event(event || window.event));
        }
        if (this.element.addEventListener) {
            if (this.event.match(/^on(.+)$/)) {
                this.event = RegExp.$1;
            }
            this.element.addEventListener(this.event, this.listener, false);
        } else if (this.element.attachEvent) {
            this.element.attachEvent(this.event, this.listener);
        }
    }
},{
    stop: function() {
        if (this.element.removeEventListener) {
            this.element.removeEventListener(this.event,this.listener,false);
        } else if (this.element.detachEvent) {
            this.element.detachEvent(this.event,this.listener);
        }
    }
});

/* Ten.Event */
Ten.Event = new Ten.Class({
    initialize: function(e) {
        this.event = e;
        if (e) {
            this.target = e.target || e.srcElement;
            this.shiftKey = e.shiftKey;
            this.ctrlKey = e.ctrlKey;
            this.altKey = e.altKey;
        }
    },
    keyMap: {
        8:"backspace", 9:"tab", 13:"enter", 19:"pause", 27:"escape", 32:"space",
        33:"pageup", 34:"pagedown", 35:"end", 36:"home", 37:"left", 38:"up",
        39:"right", 40:"down", 44:"printscreen", 45:"insert", 46:"delete",
        112:"f1", 113:"f2", 114:"f3", 115:"f4", 116:"f5", 117:"f6", 118:"f7",
        119:"f8", 120:"f9", 121:"f10", 122:"f11", 123:"f12",
        144:"numlock", 145:"scrolllock"
    }
},{
    mousePosition: function() {
        if (!this.event.clientX) return;
        return Ten.Geometry.getMousePosition(this.event);
    },
    isKey: function(name) {
        var ecode = this.event.keyCode;
        if (!ecode) return;
        var ename = Ten.Event.keyMap[ecode];
        if (!ename) return;
        return (ename == name);
    },
    targetIsFormElements: function() {
        if (!this.target) return;
        var T = (this.target.tagName || '').toUpperCase();
        return (T == 'INPUT' || T == 'SELECT' || T == 'OPTION' ||
                T == 'BUTTON' || T == 'TEXTAREA');
    },
    stop: function() {
        var e = this.event;
        if (e.stopPropagation) {
            e.stopPropagation();
            e.preventDefault();
        } else {
            e.cancelBubble = true;
            e.returnValue = false;
        }
    }
});

/* Ten.EventDispatcher */
Ten.EventDispatcher = new Ten.Class({
    initialize: function() {
        this._eventListeners = {};
    }, 
    implementEventDispatcher: function(obj) {
        Ten.Class.inherit(obj, Ten.EventDispatcher.prototype);
        obj._eventListeners = {};
    }
}, {
    hasEventListener: function(type) {
        return (this._eventListeners[type] instanceof Array && this._eventListeners[type].length > 0);
    },
    addEventListener: function(type, listener) {
        if (!this.hasEventListener(type)) {
            this._eventListeners[type] = [];
        }
        var listeners = this._eventListeners[type];
        for (var i = 0;  i < listeners.length; i++) {
            if (listeners[i] == listener) {
                return;
            }
        }
        listeners.push(listener);
    },
    removeEventListener: function(type, listener) {
        if (this.hasEventListener(type)) {
            var listeners = this._eventListeners[type];
            for (var i = 0;  i < listeners.length; i++) {
                if (listeners[i] == listener) {
                    listeners.splice(i, 1);
                    return;
                }
            }
        }
    },
    dispatchEvent: function(type, opt) {
        if (!this.hasEventListener(type)) return false;
        var listeners = this._eventListeners[type];
        for (var i = 0;  i < listeners.length; i++) {
            listeners[i].call(this, opt);
        }
    }
});

/* Ten.DOM */
Ten.DOM = new Ten.Class({
    getElementsByTagAndClassName: function(tagName, className, parent) {
        if (typeof(parent) == 'undefined') parent = document;
        if (!tagName) return Ten.DOM.getElementsByClassName(className, parent);
        var children = parent.getElementsByTagName(tagName);
        if (className) { 
            var elements = [];
            for (var i = 0; i < children.length; i++) {
                var child = children[i];
                if (Ten.DOM.hasClassName(child, className)) {
                    elements.push(child);
                }
            }
            return elements;
        } else {
            return children;
        }
    },
    getElementsByClassName: function(className, parent) {
        if (typeof(parent) == 'undefined') parent = document;
        var ret = [];
        if (!className) return ret;
        (function(parent) {
            var elems = parent.childNodes;
            for (var i = 0; i < elems.length; i++) {
                var e = elems[i];
                if (Ten.DOM.hasClassName(e, className)) {
                    ret.push(e);
                }
                arguments.callee(e);
            }
        })(parent);
        ret = Ten.Array.flatten(ret);
        return ret;
    },
    hasClassName: function(element, className) {
        if (!element || !className) return;
        var cname = element.className;
        if (!cname) return false;
        var cnames = cname.split(/\s+/);
        className = className.toLowerCase();
        for (var i = 0; i < cnames.length; i++) {
            if (cnames[i].toLowerCase() == className) {
                return true;
            }
        }
    },
    removeEmptyTextNodes: function(element) {
        var nodes = element.childNodes;
        for (var i = 0; i < nodes.length; i++) {
            var node = nodes[i];
            if (node.nodeType == 3 && !/\S/.test(node.nodeValue)) {
                node.parentNode.removeChild(node);
            }
        }
    },
    nextElement: function(elem) {
        do {
            elem = elem.nextSibling;
        } while (elem && elem.nodeType != 1);
        return elem;
    },
    prevElement: function(elem) {
        do {
            elem = elem.previousSibling;
        } while (elem && elem.nodeType != 1);
        return elem;
    },
    insertBefore: function(node, ref) {
        ref.parentNode.insertBefore(node, ref);
    },
    insertAfter: function(node, ref) {
        if (ref.nextSibling) {
            ref.parentNode.insertBefore(node, ref.nextSibling);
        } else {
            ref.parentNode.appendChild(node);
        }
    },
    replaceNode: function(newNode, oldNode) {
        Ten.DOM.insertBefore(newNode, oldNode);
        oldNode.parentNode.removeChild(oldNode);
    },
    scrapeText: function(node) {
        var rval = [];
        (function (node) {
            var cn = node.childNodes;
            if (cn) {
                for (var i = 0; i < cn.length; i++) {
                    arguments.callee.call(this, cn[i]);
                }
            }
            var nodeValue = node.nodeValue;
            if (typeof(nodeValue) == 'string') {
                rval.push(nodeValue);
            }
        })(node);
        return rval.join('');
    },
    getSelectedText: function() {
        if (window.getSelection)
            return window.getSelection().toString() || '';
        else if (document.getSelection)
            return document.getSelection();
        else if (document.selection)
            return document.selection.createRange().text;
        else
            return '';
    },
    show: function(elem) {
        elem.style.display = 'block';
    },
    hide: function(elem) {
        elem.style.display = 'none';
    },
    onLoadFunctions: [],
    loaded: false,
    timer: null,
    addEventListener: function(event,func) {
        if (event != 'load') return;
        Ten.DOM.onLoadFunctions.push(func);
        Ten.DOM.checkLoaded();
    },
    checkLoaded: function() {
        var c = Ten.DOM;
        if (c.loaded) return true;
        if (document && document.getElementsByTagName &&
            document.getElementById && document.body) {
            if (c.timer) {
                clearInterval(c.timer);
                c.timer = null;
            }
            for (var i = 0; i < c.onLoadFunctions.length; i++) {
                c.onLoadFunctions[i]();
            }
            c.onLoadFunctions = [];
            c.loaded = true;
        } else {
            c.timer = setInterval(c.checkLoaded, 13);
        }
    }
});

/* Ten.Array */
Ten.Array = new Ten.Class({
    flatten: function(arr) {
        var ret = [];
        (function(arr) {
            for (var i = 0; i < arr.length; i++) {
                var o = arr[i];
                if (Ten.Array.isArray(o)) {
                    arguments.callee(o);
                } else {
                    ret.push(o);
                }
            }
        })(arr);
        return ret;
    },
    isArray: function(o) {
        return (o instanceof Array ||
                (o && typeof(o.length) === 'number' && typeof(o) != 'string'));
    }
});

/* Ten.Selector */
Ten.Selector = new Ten.Class({
    initialize: function(selector) {
        this.selectorText = selector;
        var sels = selector.split(/\s+/);
        var child = null;
        var separator = null;
        for (var i = sels.length - 1; i >= 0; i--) {
            if (sels[i] == '>') {
                continue;
            } else if ((i > 0) && sels[i-1] == '>') {
                separator = sels[i-1];
            }
            var opt = separator ? {separator: separator} : null;
            separator = null;
            var node = new Ten.SelectorNode(sels[i],child,opt);
            child = node;
        }
        this.childNode = child;
    },
    getElementsBySelector: function(selector, parent) {
        sels = selector.split(/\s*,\s*/);
        var ret = [];
        for (var i = 0; i < sels.length; i++) {
            var sel = new Ten.Selector(sels[i]);
            ret = ret.concat(sel.getElements(parent));
        }
        ret = Ten.Array.flatten(ret);
        return ret;
    }
},{
    getElements: function(parent) {
        if (typeof(parent) == 'undefined') {
            parent = document;
        }
        return this.childNode.getElements(parent);
    }
});

/* Ten.SelectorNode */
Ten.SelectorNode = new Ten.Class({
    initialize: function(selector, child, opt) {
        if (selector) {
            selector = selector.replace(/\s/g,'');
        }
        this.option = opt;
        this.selectorText = selector;
        this.childNode = child;
        this.parseSelector();
    }
},{
    getElementsBySelector: null, // will be overridden by parser
    parseSelector: function() {
        var f = 'getElementsBySelector';
        var t = this.selectorText;
        var match;
        if (match = t.match(/^(.+)\:([\w-+()]+)$/)) {
            t = match[1];
            this.pseudoClass = match[2];
        }
        if (t.match(/^[\w-]+$/)) {
            this[f] = function(parent) {
                return parent.getElementsByTagName(t);
            };
        } else if (match = t.match(/^([\w-]+)?#([\w-]+)$/)) {
            var tname = match[1];
            var idname = match[2];
            this[f] = function(parent) {
                var e = document.getElementById(idname);
                if (!tname ||
                    e.tagName.toLowerCase() == tname.toLowerCase()) {
                        return [e];
                    } else {
                        return [];
                    }
            };
        } else if (match = t.match(/^([\w-]+)?\.([\w-]+)/)) {
            var tname = match[1];
            var cname = match[2];
            this[f] = function(parent) {
                return Ten.DOM.getElementsByTagAndClassName(tname,cname,parent);
            };
        }
        if (this.option && this.option.separator) this.parseSeparator();
        if (this.pseudoClass) this.parsePseudoClass();
    },
    parsePseudoClass: function() {
        if (!this.pseudoClass) return;
        var pseudo = this.pseudoClass;
        var f = 'getElementsBySelector';
        var func = this[f];
        var match;
        if (match = pseudo.match(/^(.+)-child(\((\d+)\))?$/)) {
            var type = match[1];
            var n = match[3];
            var index;
            if (type == 'first') {
                index = 0;
            } else if (type == 'last') {
                index = -1;
            } else if (type == 'nth' && n) {
                index = n - 1;
            }
            if (typeof index == 'number') {
                this[f] = function(parent) {
                    var elems = func(parent);
                    if (index < 0) index = elems.length + index;
                    if (elems[index]) {
                        return [elems[index]];
                    } else {
                        return [];
                    }
                }
            }
        } else if (match = pseudo.match(/^nth-child\((\d+)n\+(\d+)\)$/)) {
            var a = new Number(match[1]);
            var b = new Number(match[2]);
            this[f] = function(parent) {
                var elems = func(parent);
                var ret = [];
                for (var n = 0; n < 1000; n++) {
                    var i = a * n + b - 1;
                    if (i < 0) continue;
                    if (typeof elems[i] == 'undefined') break;
                    ret.push(elems[i]);
                }
                return ret;
            };
        }
    },
    parseSeparator: function() {
        if (!this.option) return;
        var sep = this.option.separator;
        if (!sep) return;
        var f = 'getElementsBySelector';
        var func = this[f];
        if (sep == '>') {
            this[f] = function(parent) {
                var elems = func(parent);
                var childs = parent.childNodes;
                var ret = [];
                var j0 = 0;
                for (var i = 0; i < elems.length; i++) {
                    for (var j = j0; j < childs.length; j++) {
                        if (elems[i] == childs[j]) {
                            ret.push(elems[i]);
                            j0 = j + 1;
                            break;
                        }
                    }
                }
                return ret;
            }
        }
    },
    getElements: function(parent) {
        if (typeof this.getElementsBySelector != 'function') return;
        var ret = [];
        var elems = this.getElementsBySelector(parent);
        if (elems && this.childNode) {
            for (var i = 0; i < elems.length; i++) {
                ret.push(this.childNode.getElements(elems[i]));
            }
            return ret;
        } else {
            return elems;
        }
    }
});

/* Ten.Color */
Ten.Color = new Ten.Class({
    initialize: function(r,g,b,a) {
        if (typeof(a) == 'undefined' || a === null) a = 1;
        this.r = r;
        this.g = g;
        this.b = b;
        this.a = a;
    },
    parseFromString: function(str) {
        var match;
        if (match = str.match(/^#([0-9a-f]{6}|[0-9a-f]{3})$/i)) {
            var hexstr = match[1];
            var w = hexstr.length / 3;
            var rgb = [];
            for (var i = 0; i < 3; i++) {
                var hex = hexstr.substr(w * i, w);
                if (hex.length == 1) hex += hex;
                rgb.push(parseInt(hex,16));
            }
            return new Ten.Color(rgb[0],rgb[1],rgb[2]);
        } else if (match = str.match(/^rgb\(([\d.,\s]+)\)/)) {
            var rdba = match[1].split(/[\s,]+/);
            return new Ten.Color(rdba[0],rdba[1],rdba[2],rdba[3]);
        }
        return null;
    },
    parseFromElementColor: function(elem,prop) {
        var ret;
        for (var color; elem; elem = elem.parentNode) {
            color = Ten.Style.getElementStyle(elem, prop);
            if (typeof(color) != 'undefined' && color != 'transparent') {
                ret = color;
                break;
            }
        }
        return ret ? Ten.Color.parseFromString(ret) : null;
    }
},{
    asRGBString: function() {
        if (this.a < 1) {
            return 'rgba(' + this.r + ',' + this.g + ',' + this.b +
                ',' + this.a + ')';
        } else {
            return 'rgb(' + this.r + ',' + this.g + ',' + this.b + ')';
        }
    },
    asHexString: function() {
        var str = '#';
        var cls = ['r','g','b'];
        for (var i = 0; i < 3; i ++) {
            var c = Math.round(this[cls[i]]);
            var s = c.toString(16);
            if (c < 16) s = '0' + s;
            str += s;
        }
        return str;
    },
    overlay: function(color) {
        if (color.a == 1) return color;
        r = Math.round(color.r * color.a + this.r * this.a * (1 - color.a));
        g = Math.round(color.g * color.a + this.g * this.a * (1 - color.a));
        b = Math.round(color.b * color.a + this.b * this.a * (1 - color.a));
        return new Ten.Color(r,g,b);
    }
});

/* Ten.Style */
Ten.Style = new Ten.Class({
    applyStyle: function(elem, style) {
        for (prop in style) {
            elem.style[prop] = style[prop];
        }
    },
    getGlobalRule: function(selector) {
        selector = selector.toLowerCase();
        if (Ten.Style._cache[selector]) {
            return Ten.Style._cache[selector];
        } else if (Ten.Style._cache[selector] === null) {
            return null;
        } else {
            for (var i = document.styleSheets.length - 1; i >= 0; i--) {
                var ss = document.styleSheets[i];
                try {
                    var cssRules = ss.cssRules || ss.rules;
                } catch(e) {
                    continue;
                }
                for (var j = cssRules.length - 1; j >= 0; j--) {
                    var rule = cssRules[j];
                    if (rule.selectorText &&
                        rule.selectorText.toLowerCase() == selector) {
                            Ten.Style._cache[selector] = rule;
                            return rule;
                        }
                }
            }
        }
        Ten.Style._cache[selector] = null;
        return null;
    },
    getGlobalStyle: function(selector, prop) {
        var rule = Ten.Style.getGlobalRule(selector);
        if (rule && rule.style[prop]) {
            return rule.style[prop];
        } else {
            return null;
        }
    },
    getElementStyle: function(elem, prop) {
        var style = elem.style ? elem.style[prop] : null;
        if (!style) {
            var dv = document.defaultView;
            if (dv && dv.getComputedStyle) {
                try {
                    var styles = dv.getComputedStyle(elem, null);
                } catch(e) {
                    return null;
                }
                prop = prop.replace(/([A-Z])/g, '-$1').toLowerCase();
                style = styles ? styles.getPropertyValue(prop) : null;
            } else if (elem.currentStyle) {
                style = elem.currentStyle[prop];
            }
        }
        return style;
    },
    scrapeURL: function(url) {
        if (url.match(/url\((.+)\)/)) {
            url = RegExp.$1;
            url = url.replace(/['"<>]/g, '');
            return url;
        }
        return null;
    },
    _cache: {}
});

/* Ten.Geometry */
Ten.Geometry = new Ten.Class({
    initialize: function() {
        if (Ten.Geometry._initialized) return;
        var func = Ten.Geometry._functions;
        var de = document.documentElement;
        if (window.innerWidth) {
            func.getWindowWidth = function() { return window.innerWidth; }
            func.getWindowHeight = function() { return window.innerHeight; }
            func.getXScroll = function() { return window.pageXOffset; }
            func.getYScroll = function() { return window.pageYOffset; }
        } else if (de && de.clientWidth) {
            func.getWindowWidth = function() { return de.clientWidth; }
            func.getWindowHeight = function() { return de.clientHeight; }
            func.getXScroll = function() { return de.scrollLeft; }
            func.getYScroll = function() { return de.scrollTop; }
        } else if (document.body.clientWidth) {
            func.getWindowWidth = function() { return document.body.clientWidth; }
            func.getWindowHeight = function() { return document.body.clientHeight; }
            func.getXScroll = function() { return document.body.scrollLeft; }
            func.getYScroll = function() { return document.body.scrollTop; }
        }
        Ten.Geometry._initialized = true;
    },
    _initialized: false,
    _functions: {},
    getScroll: function() {
        if (!Ten.Geometry._initialized) new Ten.Geometry;
        return {
            x: Ten.Geometry._functions.getXScroll(),
            y: Ten.Geometry._functions.getYScroll()
        };
    },
    getMousePosition: function(pos) {
        // pos should have clientX, clientY same as mouse event
        if ((navigator.userAgent.indexOf('Safari') > -1) &&
            (navigator.userAgent.indexOf('Version/') < 0)) {
            return {
                x: pos.clientX,
                y: pos.clientY
            };
        } else {
            var scroll = Ten.Geometry.getScroll();
            return {
                x: pos.clientX + scroll.x,
                y: pos.clientY + scroll.y
            };
        }
    },
    getElementPosition: function(e) {
        return {
            x: e.offsetLeft,
            y: e.offsetTop
        };
    },
    getWindowSize: function() {
        if (!Ten.Geometry._initialized) new Ten.Geometry;
        return {
            w: Ten.Geometry._functions.getWindowWidth(),
            h: Ten.Geometry._functions.getWindowHeight()
        };
    }
});

/* Ten.Position */
Ten.Position = new Ten.Class({
    initialize: function(x,y) {
        this.x = x;
        this.y = y;
    },
    add: function(a,b) {
        return new Ten.Position(a.x + b.x, a.y + b.y);
    },
    subtract: function(a,b) {
        return new Ten.Position(a.x - b.x, a.y - b.y);
    }
});

EmberlynnEmberlynn2011/07/23 18:16I really wish there were more aicrtles like this on the web.

piumdgibsbpiumdgibsb2011/07/24 23:38gG8LTr , [url=http://toxlkdypogau.com/]toxlkdypogau[/url], [link=http://cdfqodqyansm.com/]cdfqodqyansm[/link], http://yoklmpzjcfxe.com/

fzadifxfzadifx2011/07/26 19:135n2oon <a href="http://mtwptdcqccjk.com/">mtwptdcqccjk</a>

qrvluqdqrvluqd2011/07/26 23:29bm59Pf , [url=http://aewkxeueuebx.com/]aewkxeueuebx[/url], [link=http://ebcwefwnxaci.com/]ebcwefwnxaci[/link], http://lelmnzwofuep.com/

SlipsSlips2013/03/31 17:44That's a subtle way of tnhkinig about it.

jinicvjinicv2013/04/02 01:187T3p9q , [url=http://vasxrovzugjn.com/]vasxrovzugjn[/url], [link=http://wgwxzjfkghjl.com/]wgwxzjfkghjl[/link], http://jxbsftdquicf.com/

bhxbbvqozzhbhxbbvqozzh2013/04/02 07:47vnQV5g <a href="http://bcuqqpryjcje.com/">bcuqqpryjcje</a>

jvjgfvjvjgfv2013/04/04 19:19pMU6nL , [url=http://mkljjgqyltsl.com/]mkljjgqyltsl[/url], [link=http://dfmpnvtzwing.com/]dfmpnvtzwing[/link], http://hmqruhnkigjm.com/

MauveMauve2016/04/29 12:10Although along with its reputation came up this speculations concerning their adverse reactions. The idea is when something is always that beneficial, then there must be something from the factor that can bring the alternative response. This is often one of the thought processes the novsco--ontinced consumers intend.

EmmaEmma2016/05/03 12:47Celso Azevedo • 31 de Janeiro de 2011 às 20:45Olá <a href="http://edftxaqryx.com">Gae,nblriifelizmente</a> eu também não uso nenhum metodo de pagamento usado aí noBrasil, só mesmo PayPal e por isso não posso ajuda-lo

TeiyaTeiya2016/05/03 20:17Elienai Luciano / Preliminares parece desculpa pra obter atenção. Eu apenas pergunto a ela, está pronta?, e a noite é sempre das.qlasuGoetei deste comentário ou não: 0 http://qurbbiuy.com [url=http://labmxxqkf.com]labmxxqkf[/url] [link=http://vryfha.com]vryfha[/link]

JamPlaycleJamPlaycle2017/05/08 00:00Viagra Ointment On Sale Sleepwell Worldwide Zentel 400mg Amex Real Order Kamagra Ireland Que Es Cialis Y Viagra Amitriptyline Overnight [url=http://byuvaigranonile.com]viagra[/url] Achetez Priligy 30 Mg Entrega Rapida Keflex Sulfa The Most Inexpensive Cialis Deals Propecia

KennFeceVietKennFeceViet2017/06/23 20:57Viagra A Poco Prezzo [url=http://doxycycline.ccrpdc.com/cheap-vibramycin-fast.php]Cheap Vibramycin Fast[/url] Honeymoon Cystitis Keflex Cheapeast Macrobid Ups No Prescription [url=http://levitra-buying.buylevi.com]Levitra Buying[/url] Prix Du Viagra Forum Viagra Kosten Mit Rezept [url=http://cial40mg.com/fast-delivery-cialis.php]Fast Delivery Cialis[/url] Propecia Halcon Viagra Generico Italia Si Puo Avere [url=http://cheap-generic-viagra.via100mg.com]Cheap Generic Viagra[/url] Viagra Frei Verkauflich 2013

トラックバック - http://javascript.g.hatena.ne.jp/Yuichirou/20071002

2007年5月26日

質問 22:21 質問 - javascript:(function() { document.title = "YuichirouのJavaScriptメモ"; })(); を含むブックマーク はてなブックマーク - 質問 - javascript:(function() { document.title = "YuichirouのJavaScriptメモ"; })(); 質問 - javascript:(function() { document.title = "YuichirouのJavaScriptメモ"; })(); のブックマークコメント

"iterate over ***"って日本語だとなんて言うの?

「***の各要素について繰り返し同じ処理をする」ってことなのはわかるけど、これじゃ長すぎる……なるべく一言で。

BoztBozt2013/03/31 20:01Thanks for shiarng. Always good to find a real expert.

kmrlszylbkmrlszylb2013/04/01 18:46vpnDql <a href="http://clfhegslccoq.com/">clfhegslccoq</a>

トラックバック - http://javascript.g.hatena.ne.jp/Yuichirou/20070526

2006年9月29日

専門用語メモ 23:05 専門用語メモ - javascript:(function() { document.title = "YuichirouのJavaScriptメモ"; })(); を含むブックマーク はてなブックマーク - 専門用語メモ - javascript:(function() { document.title = "YuichirouのJavaScriptメモ"; })(); 専門用語メモ - javascript:(function() { document.title = "YuichirouのJavaScriptメモ"; })(); のブックマークコメント

よく使われたり訳に気をつけるべき英単語・熟語や、難しい用語の意味メモ

object
オブジェクトJavaScriptにおいてオブジェクトとは、プロパティ(property)の順不同な集合である。
property
プロパティJIS用語では「特性」と訳されているが、カタカナ語が普及したためこれは使わない。またDOMで定義されるインターフェースでは"attribute"の語が使われるが、HTML/XMLの「属性」と区別するため「プロパティ」と訳す。
method
メソッド。JavaScriptにおいてメソッドは関数を保持するプロパティのことであり、プロパティの一部である。
normalize
正規化。データ中の冗長な部分を最適化すること。
canonicalize
正準化。データ中の揺れが起こる表記を統一すること。
element
HTML/XMLの「要素」。
attribute
HTML/XMLの「属性」。
specify
指定する。
implement
実装する。ex.)"DOM implementation"=「DOM実装システム
represent
~を表す。たいてい「~に対応する」と言い換えられる。
-ate
~化する。ex.)"instantiate"=「インスタンス化する」
in use
利用する(何か)。
processing instruction
処理命令。
ID
識別子。

ReeganReegan2011/07/23 07:40You put the lime in the cocnout and drink the article up.

rfukjerfukje2011/07/23 17:57D5J0nE <a href="http://oseglndtbddg.com/">oseglndtbddg</a>

feyewmjjfeyewmjj2011/07/24 21:068AnUIQ , [url=http://papmevqqjsaj.com/]papmevqqjsaj[/url], [link=http://banemlhhxzes.com/]banemlhhxzes[/link], http://xyfxgdrowxzm.com/

ykkrarkbsykkrarkbs2011/07/25 20:55BRJCEE <a href="http://dsklmyshfake.com/">dsklmyshfake</a>

vssjfrhlfquvssjfrhlfqu2011/07/26 23:45qZHYPM , [url=http://qdswkpgofvkn.com/]qdswkpgofvkn[/url], [link=http://etlrbmqbolcm.com/]etlrbmqbolcm[/link], http://ggwjhspyngim.com/

CherryCherry2013/04/02 17:41Your's is a point of view where real intelligence shines trhough.

MandyMandy2013/07/05 19:36If my problem was a Death Star, this article is a photon <a href="http://ewxjhvybpc.com">todrpeo.</a>

MandyMandy2013/07/05 19:36If my problem was a Death Star, this article is a photon <a href="http://ewxjhvybpc.com">todrpeo.</a>

MandyMandy2013/07/05 19:36If my problem was a Death Star, this article is a photon <a href="http://ewxjhvybpc.com">todrpeo.</a>

EzzeddienEzzeddien2013/07/06 17:53I'm so glad that the inrentet allows free info like this! http://bpthhgoozfg.com [url=http://qwyorabjmd.com]qwyorabjmd[/url] [link=http://idgowxlayzc.com]idgowxlayzc[/link]

EzzeddienEzzeddien2013/07/06 17:53I'm so glad that the inrentet allows free info like this! http://bpthhgoozfg.com [url=http://qwyorabjmd.com]qwyorabjmd[/url] [link=http://idgowxlayzc.com]idgowxlayzc[/link]

EzzeddienEzzeddien2013/07/06 17:53I'm so glad that the inrentet allows free info like this! http://bpthhgoozfg.com [url=http://qwyorabjmd.com]qwyorabjmd[/url] [link=http://idgowxlayzc.com]idgowxlayzc[/link]

RaghavRaghav2013/07/08 12:18One or two to remrebme, that is. http://angppyf.com [url=http://rlmeqgkh.com]rlmeqgkh[/url] [link=http://uyxarnp.com]uyxarnp[/link]

RaghavRaghav2013/07/08 12:18One or two to remrebme, that is. http://angppyf.com [url=http://rlmeqgkh.com]rlmeqgkh[/url] [link=http://uyxarnp.com]uyxarnp[/link]

RaghavRaghav2013/07/08 12:18One or two to remrebme, that is. http://angppyf.com [url=http://rlmeqgkh.com]rlmeqgkh[/url] [link=http://uyxarnp.com]uyxarnp[/link]

JoeieJoeie2016/04/29 08:40Ho fatto un messaggio di ritrgaziamenno a : e mi ha risposto il magistrato con una mail di ringraziamento e di incitamento a sua volta. Ci sono ancora di questi italiani, ecco perche’ ha ragione Andrea!

WimpyWimpy2016/04/29 14:08can see to it that the <a href="http://ezizcxtqlo.com">coclnacepmy</a> ends today by taking your own action to get the word out. There are several ways that have been outlined in these threads that cost very little. Which one are you doing this week? Your individual action will inspire others. Rely on it.

BunnyBunny2016/05/02 19:44I just tried it and I'm happy to report that it works <a href="http://yjnwbxl.com">bellfituuay.</a> My ancient laptop has never been able to display YouTube videos smoothly... until today. (running Chrome on Fedora 11)What a rare treat that my computer actually got a little bit faster with age!I too hope for a widely supported open video format someday, but this is a giant leap in the right direction. Thanks Google!

FinchFinch2016/05/03 19:55Gina I can no longer share posts from my business pa#87ge21&;s news feed onto the timeline. I go through the process and get a message saying that the post was successfully shared, but it isn’t there. Anyone know what’s going on? And yes, I am making sure to designate the proper location from the top left drop down menu. Thanks! http://yzegwvsuikh.com [url=http://fxorjhyule.com]fxorjhyule[/url] [link=http://tsgcrsuf.com]tsgcrsuf[/link]

トラックバック - http://javascript.g.hatena.ne.jp/Yuichirou/20060929

2006年9月28日

すごくがんばった翻訳 22:06 すごくがんばった翻訳 - javascript:(function() { document.title = "YuichirouのJavaScriptメモ"; })(); を含むブックマーク はてなブックマーク - すごくがんばった翻訳 - javascript:(function() { document.title = "YuichirouのJavaScriptメモ"; })(); すごくがんばった翻訳 - javascript:(function() { document.title = "YuichirouのJavaScriptメモ"; })(); のブックマークコメント

Document Object Model (DOM) Level 3 Core SpecificationのcreateElementの項目を読みながらkeyword:createElementを書いていたら、"canonical form"という言葉にぶち当たりました。

"canonical"をググると一番にCanonicalの訳し方というページがあって、それによると

  • "canonical"の原義は「キリスト教教会法(教会の戒律)に基づく」
  • 正規(な)」と訳すと無難だが、これは"normal"の訳として広く知られており、実は全く違う言葉を同じ訳にするのはまずい。
    • ex) "normal form"=「正規形」、"normalize"=「正規化」

で、最終的にその人は「キャノニカル」としたそうです。


しかし私は諦めず、検索結果の下のほうを見てみました。

すると、"canonical"は物理学などの世界では「正準(な)」という一種の造語で訳されていることが判明。漢字意味合いもピッタリだったので早速コレを採用し、「正準(canonical)な形」としました。

物理学などでは割とポピュラーな訳し方のようなので、ぜひウェブの世界でも"canonical"=「正準(な)」という訳を使ってみるといいと思いました。

トラックバック - http://javascript.g.hatena.ne.jp/Yuichirou/20060928

2006年9月27日

「子孫をIDで検索」にすごい違和感 21:58 「子孫をIDで検索」にすごい違和感 - javascript:(function() { document.title = "YuichirouのJavaScriptメモ"; })(); を含むブックマーク はてなブックマーク - 「子孫をIDで検索」にすごい違和感 - javascript:(function() { document.title = "YuichirouのJavaScriptメモ"; })(); 「子孫をIDで検索」にすごい違和感 - javascript:(function() { document.title = "YuichirouのJavaScriptメモ"; })(); のブックマークコメント

いきなりこんなこと言うのは失礼だと思うけど、g:javascript:id:tetsu23:20060803:1154481469にすごい違和感を感じます。


関数gchildは『element以下のidが一致するノード』のうち最初にヒットしたもの、関数gchildsは全部取ってくる関数。それはコード上間違いないです。

でもそれは、HTMLにおいてidとは何か、ってのを勘違いしているんです。


HTMLにおいて、idとはその文書中の一部分を指し示すユニークな識別子なんです。

"unique"――カタカナ語では「独特の」って意味でよく使うけど、元々の意味は「唯一の」。

つまり、あるidの値は1つのHTML文書中にただ1つしか存在しないはずなんです。


ちなみに、そのHTML文書にただ1つしかないというidを使ってノードを取り出す、getElementByIdという関数があります。

var hatena = document.getElementById("hatena");

idは唯一無二であり、何か特定のノードの下にある、というような限定は必要ありませんので、getElementByIddocumentのみが持つメソッドです。


ちなみに、おそらくid:tetsu23さんはHTML初心者から中級者によくある、idclassの混同を起こしていると思います。

idは1つのHTML文書中にただ1つ、ですが、classはいくらでも使えます。

よって、gchildやgchildsは、『element以下のclassが一致するノード』を検索するようにすればいいと思います。

tetsu23tetsu232006/09/28 14:27あーなるほど。IDは一意なんですね。失礼しました。

CadenCaden2011/12/17 14:47No quseiton this is the place to get this info, thanks y'all.

akzicehtitcakzicehtitc2011/12/18 23:295acmXt , [url=http://ubtwzmlnvghj.com/]ubtwzmlnvghj[/url], [link=http://wxrxpqczikll.com/]wxrxpqczikll[/link], http://dkmsvukylfrs.com/

djwpfwqjdjwpfwqj2011/12/19 19:19FzmryQ <a href="http://azttshbqfddf.com/">azttshbqfddf</a>

トラックバック - http://javascript.g.hatena.ne.jp/Yuichirou/20060927

2006年9月25日

はじめまして! 22:14 はじめまして! - javascript:(function() { document.title = "YuichirouのJavaScriptメモ"; })(); を含むブックマーク はてなブックマーク - はじめまして! - javascript:(function() { document.title = "YuichirouのJavaScriptメモ"; })(); はじめまして! - javascript:(function() { document.title = "YuichirouのJavaScriptメモ"; })(); のブックマークコメント

Yuichirouと申します。


JavaScript大好きです。

HTMLCSSも好きです。

もちろんDOMはお手のものです。

Greasemonkey大好きです。

今までに「はてなRSSのRSSリーダーページに「手動更新」ボタンを付けるGMスクリプト」「はてなわんわんワールドに吹き出し表示切替機能を付けるGMスクリプト」などを作りました。


これからもよろしく!

DOMに関するグループキーワードに説明を入れる準備 23:02 DOMに関するグループキーワードに説明を入れる準備 - javascript:(function() { document.title = "YuichirouのJavaScriptメモ"; })(); を含むブックマーク はてなブックマーク - DOMに関するグループキーワードに説明を入れる準備 - javascript:(function() { document.title = "YuichirouのJavaScriptメモ"; })(); DOMに関するグループキーワードに説明を入れる準備 - javascript:(function() { document.title = "YuichirouのJavaScriptメモ"; })(); のブックマークコメント

interface Document : Node {
  readonly attribute DocumentType     doctype;
  readonly attribute DOMImplementation  implementation;
  readonly attribute Element          documentElement;
  Element            createElement(in DOMString tagName)
                                        raises(DOMException);
  DocumentFragment   createDocumentFragment();
  Text               createTextNode(in DOMString data);
  Comment            createComment(in DOMString data);
  CDATASection       createCDATASection(in DOMString data)
                                        raises(DOMException);
  ProcessingInstruction createProcessingInstruction(in DOMString target, 
                                                    in DOMString data)
                                        raises(DOMException);
  Attr               createAttribute(in DOMString name)
                                        raises(DOMException);
  EntityReference    createEntityReference(in DOMString name)
                                        raises(DOMException);
  NodeList           getElementsByTagName(in DOMString tagname);
  // Introduced in DOM Level 2:

  Node               importNode(in Node importedNode, 
                                in boolean deep)
                                        raises(DOMException);
  // Introduced in DOM Level 2:

  Element            createElementNS(in DOMString namespaceURI, 
                                     in DOMString qualifiedName)
                                        raises(DOMException);
  // Introduced in DOM Level 2:

  Attr               createAttributeNS(in DOMString namespaceURI, 
                                       in DOMString qualifiedName)
                                        raises(DOMException);
  // Introduced in DOM Level 2:

  NodeList           getElementsByTagNameNS(in DOMString namespaceURI, 
                                            in DOMString localName);
  // Introduced in DOM Level 2:
  Element            getElementById(in DOMString elementId);
};
Document Object Model Core - Interface Document
interface Node {

  // NodeType
  const unsigned short      ELEMENT_NODE                   = 1;
  const unsigned short      ATTRIBUTE_NODE                 = 2;
  const unsigned short      TEXT_NODE                      = 3;
  const unsigned short      CDATA_SECTION_NODE             = 4;
  const unsigned short      ENTITY_REFERENCE_NODE          = 5;
  const unsigned short      ENTITY_NODE                    = 6;
  const unsigned short      PROCESSING_INSTRUCTION_NODE    = 7;
  const unsigned short      COMMENT_NODE                   = 8;
  const unsigned short      DOCUMENT_NODE                  = 9;
  const unsigned short      DOCUMENT_TYPE_NODE             = 10;
  const unsigned short      DOCUMENT_FRAGMENT_NODE         = 11;
  const unsigned short      NOTATION_NODE                  = 12;

  readonly attribute DOMString        nodeName;
           attribute DOMString        nodeValue;
                                        // raises(DOMException) on setting

                                        // raises(DOMException) on retrieval

  readonly attribute unsigned short   nodeType;
  readonly attribute Node             parentNode;
  readonly attribute NodeList         childNodes;
  readonly attribute Node             firstChild;
  readonly attribute Node             lastChild;
  readonly attribute Node             previousSibling;
  readonly attribute Node             nextSibling;
  readonly attribute NamedNodeMap     attributes;
  // Modified in DOM Level 2:

  readonly attribute Document         ownerDocument;
  Node               insertBefore(in Node newChild, 
                                  in Node refChild)
                                        raises(DOMException);
  Node               replaceChild(in Node newChild, 
                                  in Node oldChild)
                                        raises(DOMException);
  Node               removeChild(in Node oldChild)
                                        raises(DOMException);
  Node               appendChild(in Node newChild)
                                        raises(DOMException);
  boolean            hasChildNodes();
  Node               cloneNode(in boolean deep);
  // Modified in DOM Level 2:

  void               normalize();
  // Introduced in DOM Level 2:
  boolean            isSupported(in DOMString feature, 
                                 in DOMString version);
  // Introduced in DOM Level 2:

  readonly attribute DOMString        namespaceURI;
  // Introduced in DOM Level 2:
           attribute DOMString        prefix;
                                        // raises(DOMException) on setting

  // Introduced in DOM Level 2:
  readonly attribute DOMString        localName;
  // Introduced in DOM Level 2:
  boolean            hasAttributes();
};
Document Object Model Core - Interface Node
トラックバック - http://javascript.g.hatena.ne.jp/Yuichirou/20060925