/*@ Ox.canvas Generic canvas object Returns an object with the properties: canvas, context, data and imageData. # Usage -------------------------------------------------------------------- Ox.canvas(width, height) -> canvas Ox.canvas(image) -> canvas # Arguments ---------------------------------------------------------------- width Width in px height Height in px image Image object @*/ Ox.canvas = function() { // Ox.print("CANVAS", arguments) var c = {}, isImage = arguments.length == 1, image = isImage ? arguments[0] : { width: arguments[0], height: arguments[1] }; c.context = (c.canvas = Ox.element('').attr({ width: image.width, height: image.height })[0]).getContext('2d'); isImage && c.context.drawImage(image, 0, 0); c.data = (c.imageData = c.context.getImageData( 0, 0, image.width, image.height )).data; return c; }; /*@ Ox.documentReady Calls a callback function once the DOM is ready (callback) -> If true, the document was ready callback Callback function @*/ Ox.documentReady = (function() { var callbacks = []; document.onreadystatechange = function() { if (document.readyState == 'complete') { callbacks.forEach(function(callback) { callback(); }); delete callbacks; } }; return function(callback) { if (document.readyState == 'complete') { callback(); return true; } else { callbacks.push(callback); return false; } } }()); /*@ Ox.element Generic HTML element, mimics jQuery (str) -> Element object str Tagname ('') or selector ('tagname', '.classname', '#id') > Ox.element("
").addClass("red").hasClass("red") true > Ox.element("
").addClass("red").removeClass("red").hasClass("red") false > Ox.element("
").addClass("red").addClass("red")[0].className "red" > Ox.element("
").attr({id: "red"}).attr("id") "red" > Ox.element("
").css("color", "red").css("color") "red" > Ox.element("
").html("red").html() "red" @*/ Ox.element = function(str) { return { //@ 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], /*@ addClass Adds a class name (className) -> This element className Class name @*/ addClass: function(str) { this[0].className = this[0].className ? Ox.unique( (this[0].className + ' ' + str).split(' ') ).join(' ') : str; return this; }, /*@ append Appends another element to this element (element) -> This element element Another element @*/ append: function(element) { this[0].appendChild(element[0]); return this; }, /*@ appendTo appends this element object to another element object (element) -> This element element Another element @*/ appendTo: function(element) { element[0].appendChild(this[0]); return this; }, /*@ attr Gets or sets an attribute (key) -> Value (key, value) -> This element ({key, value}) -> This element key Attribute name value Attribute value @*/ attr: function() { var ret, that = this; if (arguments.length == 1 && Ox.isString(arguments[0])) { ret = this[0].getAttribute(arguments[0]); } else { Ox.forEach(Ox.makeObject(arguments), function(v, k) { that[0].setAttribute(k, v); }); ret = this; } return ret; }, /*@ click Binds a function to the click event (callback) -> This element callback Callback function event The DOM event @*/ // fixme: why not a generic bind? click: function(callback) { this[0].onclick = callback; return this; }, /*@ css Gets or sets a CSS attribute (key) -> Value (key, value) -> This element ({key, value}) -> This element key Attribute name value Attribute value @*/ css: function() { var ret, that = this; if (arguments.length == 1 && Ox.isString(arguments[0])) { ret = this[0].style[arguments[0]]; } else { Ox.forEach(Ox.makeObject(arguments), function(v, k) { that[0].style[k] = v; }); ret = this; } return ret; }, /*@ hasClass Returns true if the element has a given class (className) -> True if the element has the class className Class name @*/ hasClass: function(str) { return this[0].className.split(' ').indexOf(str) > -1; }, /*@ html Gets or sets the inner HTML () -> The inner HTML (html) -> This element html The inner HTML @*/ html: function(str) { var ret; if (Ox.isUndefined(str)) { ret = this[0].innerHTML; } else { this[0].innerHTML = str; ret = this; } return ret; }, /*@ mousedown Binds a function to the mousedown event (callback) -> This element callback Callback function event The DOM event @*/ // fixme: why not a generic bind? mousedown: function(callback) { this[0].onmousedown = callback; return this; }, /*@ removeAttr Removes an attribute (key) -> This element key The attribute @*/ removeAttr: function(key) { this[0].removeAttribute(key); return this; }, /*@ removeClass Removes a class name (className) -> This element className Class name @*/ removeClass: function(str) { this[0].className = Ox.filter( this[0].className.split(' '), function(className) { return className != str; } ).join(' '); return this; } } };