'use strict'; /*@ Ox.$ Generic HTML element, mimics jQuery value tagname, selector, html element, `window`, or `document` Passing a tagname ('') creates an element, passing a selector ('tagname', '.classname' or '#id') selects an element. (value) -> Element object > Ox.$('
').addClass('red').hasClass('red') true > Ox.$('
').addClass('red').removeClass('red').hasClass('red') false > Ox.$('
').addClass('red').addClass('red')[0].className 'red' > Ox.$('
').attr({id: 'red'}).attr('id') 'red' > Ox.$('
').attr({id: 'red'}).removeAttr('id').attr('id') void 0 > Ox.$('
').css({color: 'red'}).css('color') 'red' > Ox.$('
').html('red').html() 'red' > Ox.$('
').html('red').empty().html() '' > Ox.$('').val('red').val() 'red' > !!Ox.$('
').on({click: function(e) { Ox.test(e.type, 'click'); }}).trigger('click') true @*/ Ox.$ = Ox.element = function(value) { var element = !Ox.isString(value) ? value // window, document or element : value[0] == '<' ? document.createElement(value.slice(1, -1)) : value[0] == '#' ? document.getElementById(value.slice(1)) : value[0] == '.' ? document.getElementsByClassName(value.slice(1))[0] : document.getElementsByTagName(value)[0]; return element ? { //@ 0 The DOM element itself 0: element, /*@ addClass Adds a class name (className) -> This element className Class name @*/ addClass: function(string) { this[0].className = Ox.unique((( this[0].className ? this[0].className + ' ' : '' ) + Ox.clean(string)).split(' ')).join(' '); return this; }, /*@ append Appends another element to this element (element) -> This element element One or more elements @*/ append: function() { var that = this; Ox.makeArray(arguments[0]).forEach(function(element) { that[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, key1: value1, ...}) -> 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]); if (ret === null) { ret = void 0; } } else { Ox.forEach(Ox.makeObject(arguments), function(value, key) { that[0].setAttribute(key, value); }); ret = this; } return ret; }, /*@ css Gets or sets a CSS attribute (key) -> Value (key, value) -> This element ({key0: value0, key1: value1, ...}) -> 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(value, key) { that[0].style[key] = value; }); ret = this; } return ret; }, /*@ empty Empties the inner HTML () -> This element @*/ empty: function() { return this.html(''); }, /*@ hasClass Returns true if this element has a given class (className) -> True if this element has the class className Class name @*/ hasClass: function(string) { return this[0].className.split(' ').indexOf(string) > -1; }, /*@ height Returns the height of this element () -> Height in px @*/ height: function() { return this[0].offsetHeight; }, /*@ hide Hides this element () -> This element @*/ hide: function() { return this.css({display: 'none'}); }, /*@ html Gets or sets the inner HTML () -> The inner HTML (html) -> This element html The inner HTML @*/ html: function(string) { var ret; if (arguments.length == 0) { ret = this[0].innerHTML; } else { this[0].innerHTML = string; ret = this; } return ret; }, /*@ on Binds a callback to an event (event, callback) -> This element ({event0: callback0, event1: callback1, ...}) -> This element event Event name callback Callback function e Event properties @*/ on: function() { var that = this; Ox.forEach(Ox.makeObject(arguments), function(callback, event) { that[0].addEventListener(event, callback); }); return this; }, /*@ one Binds a callback to an event and unbinds it on first invocation (event, callback) -> This element ({event0: callback0, event1: callback1, ...}) -> This element event Event name callback Callback function e Event properties @*/ one: function(events) { var that = this; Ox.forEach(Ox.makeObject(arguments), function(callback, event) { that.on(event, function fn() { that.off(event, fn); callback(); }); }); return this; }, /*@ off Unbinds a callback from an event (event) -> This element (unbinds all callbacks) (event, callback) -> This element ({event0: callback0, event1: callback1, ...}) -> This element event Event name callback Callback function @*/ off: function(event, callback) { var that = this; Ox.forEach(Ox.makeObject(arguments), function(callback, event) { if (callback) { that[0].removeEventListener(event, callback); } else { that[0]['on' + event] = null; } }); return this; }, /*@ remove Removes this element from the DOM () -> This element @*/ remove: function() { this[0].parentNode.removeChild(this[0]); return this; }, /*@ removeAttr Removes an attribute (key) -> This element ([key0, key1, ...]) -> This element key The attribute @*/ removeAttr: function() { var that = this; Ox.makeArray(arguments[0]).forEach(function(key) { that[0].removeAttribute(key); }); return this; }, /*@ removeClass Removes a class name (className) -> This element className Class name @*/ removeClass: function(string) { var array = Ox.clean(string).split(' '); this[0].className = this[0].className.split(' ').filter( function(className) { return array.indexOf(className) == -1; } ).join(' '); return this; }, /*@ show Show this element () -> This element @*/ show: function() { return this.css({display: 'block'}); }, /*@ trigger Triggers an event (event) -> This element @*/ trigger: function(event) { var e = document.createEvent('MouseEvents'); e.initEvent(event, true, true); this[0].dispatchEvent(e); return this; }, /*@ val Gets or sets the value property of this element () -> Value (value) -> This element value Value @*/ val: function(value) { var ret; if (arguments.length == 0) { ret = this[0].value; } else { this[0].value = value; ret = this; } return ret; }, /*@ width Returns the width of this element () -> Width in px @*/ width: function() { return this[0].offsetWidth; } } : null; }; /*@ Ox.canvas Generic canvas object Returns an object with the properties: `canvas`, `context`, `data` and `imageData`. (width, height) -> canvas (image) -> canvas 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.$('').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 = window.onload = function() { if (document.readyState == 'complete') { callbacks.forEach(function(callback) { callback(); }); document.onreadystatechange = window.onload = null; } }; return function(callback) { if (document.readyState == 'complete') { callback(); return true; } else { callbacks.push(callback); return false; } }; }());