From e951578b37514c3086314530855dbdf57f0e6e63 Mon Sep 17 00:00:00 2001 From: rolux Date: Thu, 5 Apr 2012 17:30:00 +0200 Subject: [PATCH] make Ox.$ more useful --- source/Ox/js/DOM.js | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/source/Ox/js/DOM.js b/source/Ox/js/DOM.js index 315bedea..7acb6c18 100644 --- a/source/Ox/js/DOM.js +++ b/source/Ox/js/DOM.js @@ -71,16 +71,18 @@ Ox.element Generic HTML element, mimics jQuery > Ox.element("
").html("red").html() "red" @*/ -Ox.$ = Ox.element = function(str) { - // fixme: add generic bind (remove click and mousedown), +Ox.$ = Ox.element = function(val) { + // fixme: remove click and mousedown, // add native css selector - return { + // take all matches of getElementsBy... + var element = Ox.isObject(val) ? val // window + : val[0] == '<' ? document.createElement(Ox.sub(val, 1, -1)) + : val[0] == '#' ? document.getElementById(Ox.sub(val, 1)) + : val[0] == '.' ? document.getElementsByClassName(Ox.sub(val, 1))[0] + : document.getElementsByTagName(val)[0]; + return element ? { //@ 0 The DOM element itself - 0: str[0] == '<' ? document.createElement(str.substr(1, str.length - 2)) : - // fixme: why only take the first match? - str[0] == '.' ? document.getElementsByClassName(str.substr(1))[0] : - str[0] == '#' ? document.getElementById(str.substr(1)) : - document.getElementsByTagName(str)[0], + 0: element, /*@ addClass Adds a class name (className) -> This element @@ -137,6 +139,13 @@ Ox.$ = Ox.element = function(str) { } return ret; }, + bind: function(events) { + var that = this; + Ox.forEach(Ox.makeObject(arguments), function(callback, event) { + that[0]['on' + event] = callback; + }); + return this; + }, /*@ click Binds a function to the click event (callback) -> This element @@ -167,6 +176,10 @@ Ox.$ = Ox.element = function(str) { } return ret; }, + empty: function() { + this.html(''); + return this; + }, /*@ hasClass Returns true if the element has a given class (className) -> True if the element has the class @@ -222,6 +235,10 @@ Ox.$ = Ox.element = function(str) { } ).join(' '); return this; + }, + unbind: function(event) { + delete this[0]['on' + event]; + return this; } - } + } : null; }; \ No newline at end of file