'use strict'; /*@ Ox.canvas Generic canvas object # Description -------------------------------------------------------------- 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() { 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(); }); } }; 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.$ = Ox.element = function(val) { // fixme: remove click and mousedown, // add native css selector // 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: element, /*@ addClass Adds a class name (className) -> This element className Class name @*/ addClass: function(str) { /* fixme: this[0].className = Ox.unique( this[0].className.split(' ').push(className) ).join(' '); */ 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; }, 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 callback Callback function event The DOM event @*/ 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; }, empty: function() { this.html(''); return this; }, /*@ 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 @*/ 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; }, unbind: function(event) { delete this[0]['on' + event]; return this; } } : null; };