'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 data = {}, elements = Ox.isArray(value) ? value // array of elements : !Ox.isString(value) ? [value] // window, document or element : value[0] == '<' ? [document.createElement(value.slice(1, -1))] : Ox.slice(document.querySelectorAll(value)), mousewheelEvents = ['wheel', 'mousewheel'], originalMousewheelEvents = 'onwheel' in document ? ['wheel'] : ['mousewheel', 'DOMMouseScroll', 'MozMousePixelScroll']; function normalizeEvents(args) { var ret = {}; Ox.forEach(Ox.makeObject(args), function(callback, event) { if (Ox.contains(mousewheelEvents, event)) { originalMousewheelEvents.forEach(function(event) { ret[event] = callback; }); } else { ret[event] = callback; } }); return ret; } function removeOxElements(parent, includeParent) { return; if (includeParent) { removeOxElement(parent); } else { Ox.$(parent).find('.OxElement').forEach(removeOxElement); } function removeOxElement(element) { var $element = Ox.getOxElement(element); $element && $element.removeElement(); } } return elements.length ? Ox.extend( Ox.zipObject(Ox.range(elements.length), elements ), { /*@ add Adds another DOM object to this DOM object (other) -> This DOM object other Other DOM object @*/ add: function add($other) { elements = Ox.unique(elements.concat(other.elements())); this.length = elements.length; return this; }, /*@ addClass Adds a class name to all elements (className) -> This DOM object className Class name @*/ addClass: function addClass(string) { string = Ox.clean(string); elements.forEach(function(element) { element.className = Ox.unique((( element.className ? element.className + ' ' : '' ) + string).split(' ')).join(' '); }); return this; }, /*@ append Appends one or more DOM objects to this DOM object (object[, object[, ...]]) -> This DOM object element Another DOM object @*/ append: function append() { var $others = Ox.slice(arguments); elements.forEach(function(element) { $others.forEach(function($other) { $other.forEach(function(otherElement) { element.appendChild(otherElement); }); }); }); return this; }, /*@ appendTo Appends this DOM object to another DOM object (object) -> This DOM object object Another DOM object @*/ appendTo: function appendTo($other) { //FIXME: temp fix $other[0].appendChild(this[0]); return this; $other.forEach(function(otherElement) { elements.forEach(function(element) { otherElement.appendChild(element); }); }); return this; }, /*@ attr Gets or sets an attribute for all elements (key) -> Value (key, value) -> This DOM object ({key0: value0, key1: value1, ...}) -> This DOM object key Attribute name value Attribute value @*/ attr: function attr() { var args = arguments, ret, that = this; if (args.length == 1 && Ox.isString(args[0])) { ret = this[0].getAttribute ? this[0].getAttribute(args[0]) : void 0; // fixme: why exactly is this needed? return ret === null ? void 0 : ret; } else { args = Ox.makeObject(args); elements.forEach(function(element) { Ox.forEach(args, function(value, key) { if (element.setAttribute) { element.setAttribute(key, value); } }); }); return this; } }, /*@ children Returns the unique list of children of all elements () -> <[h]> Children @*/ children: function children() { return Ox.unique(Ox.flatten(elements.map(function(element) { return element.childNodes; }))); }, /*@ css Gets or sets a CSS attribute for all elements (key) -> Value (key, value) -> This DOM object ({key0: value0, key1: value1, ...}) -> This DOM object key Attribute name value Attribute value @*/ css: function css() { var args = arguments; if (args.length == 1 && Ox.isString(args[0])) { return elements[0].style[args[0]]; } else { args = Ox.makeObject(args); elements.forEach(function(element) { Ox.forEach(args, function(value, key) { element.style[key] = value; }); }); return this; } }, /*@ data Gets or sets data () -> All data (key) -> Value (key, value) -> This DOM object ({key0: value0, key1: value1, ...}) -> This DOM object key Property value <*> Value @*/ data: function data() { if (arguments.length == 0) { return data; } else if (arguments.length == 1 && Ox.isString(arguments[0])) { return data[arguments[0]]; } else { Ox.forEach(Ox.makeObject(arguments), function(value, key) { data[key] = value; }); return this; } }, /*@ elements Returns all elements () -> <[h]> All elements @*/ elements: function elements() { return elements; }, /*@ empty Empties the inner HTML of all elements () -> This DOM object @*/ empty: function empty() { return this.html(''); }, /*@ every Tests if every element satisfies a given condition (test) -> True if every element passes the test test Test function @*/ every: function every() { return Array.prototype.every.apply(elements, arguments); }, /*@ filter Filters all elements by a given condition (test) -> Array of matching elements test Test function @*/ filter: function filter() { return Array.prototype.filter.apply(elements, arguments); }, /*@ find Find all descendant elements matching a CSS selector ([selector]) -> <[h]> Elements selector CSS selector @*/ find: function find(string) { return Ox.unique(elements.map(function(element) { return element.querySelectorAll(string || '*'); })); }, /*@ forEach Loops over all elements (iterator) -> This DOM object iterator Iterator function @*/ forEach: function forEach() { Array.prototype.forEach.apply(elements, arguments); return this; }, /*@ hasClass Returns true if this element has a given class (className) -> True if this element has the class className Class name @*/ hasClass: function hasClass(string) { return elements.any(function(element) { return Ox.contains(element.className.split(' '), string); }); }, /*@ height Returns the height of the first element () -> Height in px @*/ height: function height() { return elements[0].offsetHeight; }, /*@ hide Hides all elements () -> This DOM object @*/ hide: function hide() { return this.css({display: 'none'}); }, /*@ html Gets or sets the innerHTML of all elements () -> The inner HTML (html) -> This DOM object html The inner HTML @*/ html: function html(string) { var html = ''; if (arguments.length == 0) { elements.forEach(function(element) { html += element.innerHTML; }) return html; } else { elements.forEach(function(element) { removeOxElements(element); element.innerHTML = string; }); return this; } }, /*@ insertAfter Inserts this DOM object after another DOM object (object) -> This DOM object object Another DOM object @*/ insertAfter: function insertAfter($other) { var nextSibling = $element[0].nextSibling; elements.forEach(function(element) { $other[0].parentNode.insertBefore(element, nextSibling); }) return this; }, /*@ insertBefore Inserts this DOM object before another DOM object (object) -> This DOM object object Another DOM object @*/ insertBefore: function insertBefore($other) { elements.forEach(function(element) { $other[0].parentNode.insertBefore(element, $other[0]); }); return this; }, /*@ is Tests if any element matches a CSS selector (selector) -> True if the element matches the selector selector CSS selector @*/ is: function is(string) { return elements.some(function(element) { return Ox.contains( (element.parentNode || document).querySelectorAll(string), element ); }); }, /*@ length Number of elements @*/ length: elements.length, /*@ map Transforms all elements (iterator) -> [] Transformed elements iterator Iterator function @*/ map: function map() { return Array.prototype.filter.map(elements, arguments); }, /*@ next Returns the unique list of siblings directly after all elements () -> <[h]> Siblings @*/ next: function next() { return Ox.unique(Ox.filter(elements.map(function(element) { return element.nextSibling; }))); }, /*@ nextAll Returns the unique list of siblings after all elements () -> <[h]> Siblings @*/ nextAll: function nextAll() { var siblings = []; elements.forEach(function(element) { var sibling = element; while (true) { sibling = sibling.nextSibling; if (!sibling) { break; } siblings.push(sibling); } }); return Ox.unique(siblings); }, /*@ off Unbinds a callback from an event (event) -> This DOM object (unbinds all callbacks) (event, callback) -> This DOM object ({event0: callback0, event1: callback1, ...}) -> This DOM object event Event name callback Callback function @*/ off: function off(event, callback) { var args = normalizeEvents(arguments); elements.forEach(function(element) { Ox.forEach(args, function(callback, event) { if (callback) { element.removeEventListener(event, callback, false); } else { element['on' + event] = null; } }); }); return this; }, /*@ on Binds a callback to an event (event, callback) -> This DOM object ({event0: callback0, event1: callback1, ...}) -> This DOM object event Event name callback Callback function e Event properties @*/ on: function on() { var args = normalizeEvents(arguments); elements.forEach(function(element) { Ox.forEach(args, function(callback, event) { element.addEventListener(event, callback, false); }); }); return this; }, /*@ one Binds a callback to an event and unbinds it on first invocation (event, callback) -> This DOM object ({event0: callback0, event1: callback1, ...}) -> This DOM object event Event name callback Callback function e Event properties @*/ one: function one(events) { var args = Ox.slice(arguments), that = this; Ox.forEach(normalizeEvents(arguments), function(callback, event) { that.on(event, function fn() { that.off(event, fn); return callback.apply(that, args); }); }); return this; }, /*@ parent Returns the unique list of parents of all elements () -> <[h]> Parent elements @*/ parent: function parent() { return Ox.unique(elements.map(function(element) { return element.parentNode; })); }, /*@ parents Returns the unique list of all ancestors of all elements () -> <[h]> Ancestor elements @*/ parents: function parents() { var parents = []; Ox.reverse(elements).forEach(function(element) { var parent = element; while (true) { parent = parent.parentNode; if (!parent) { break; } parents.unshift(parent); } }); return Ox.unique(parents); }, /*@ prepend Prepends one or more DOM objects to this DOM object (object[, object[, ...]]) -> DOM object object Another DOM objectt @*/ prepend: function prepend() { var $others = Ox.slice(arguments).reverse(); elements.forEach(function(element) { var parent = element.parentNode; $others.forEach(function($other) { $other.forEach(function(otherElement) { parent.insertBefore(otherElement, parent.firstChild); }); }); }); return this; }, /*@ prependTo Prepends this DOM object to another DOM object (object) -> This DOM object object Another DOM object @*/ prependTo: function prependTo($other) { $other.forEach(function(otherElement) { var firstChild = otherElement.firstChild elements.forEach(function(element) { otherElement.insertBefore(element, firstChild); }); }); return this; }, /*@ prev Returns the unique list of siblings directly before all elements () -> <[h]> Siblings @*/ prev: function prev() { return Ox.unique(Ox.filter(elements.map(function(element) { return element.previousSibling; }))); }, /*@ prevAll Returns the unique list of siblings before all elements () -> <[h]> Siblings @*/ prevAll: function prevAll() { var siblings = []; Ox.reverse(elements).forEach(function(element) { var sibling = element; while (true) { sibling = sibling.previousSibling; if (!sibling) { break; } siblings.unshift(sibling); } }); return Ox.unique(siblings); }, /*@ reduce Applies `reduce` to all elements @*/ reduce: function reduce() { return Array.prototype.reduce.apply(elements, arguments); }, /*@ remove Removes all element from the DOM () -> This DOM object @*/ remove: function remove() { elements.forEach(function(element) { if (element.parentNode) { removeOxElements(element, true); element.parentNode.removeChild(element); } }); return this; }, /*@ removeAttr Removes an attribute from all elements (key) -> This DOM object ([key0, key1, ...]) -> This DOM object key The attribute @*/ removeAttr: function removeAttr() { var keys = Ox.makeArray(arguments); elements.forEach(function(element) { keys.forEach(function(key) { element.removeAttribute(key); }); }); return this; }, /*@ removeClass Removes a class name from all elements (className) -> This DOM object className Class name @*/ removeClass: function removeClass(string) { var classNames = Ox.clean(string).split(' '); elements.forEach(function(element) { element.className = element.className.split(' ') .filter(function(className) { return !Ox.contains(classNames, className) }) .join(' '); }); return this; }, /*@ replace Replaces another DOM object with this DOM object (object) -> This DOM object object Another DOM object @*/ replace: function replace($other) { $other.forEach(function(otherElement) { var parent = otherElement.parentNode, sibling = otherElement.nextSibling; if (parent) { removeOxElements(otherElement, true); parent.removeChild(otherElement); elements.forEach(function(element) { parent.insertBefore(element, sibling) }); } }); return this; }, /*@ replaceWith Replaces this DOM object with another DOM object (object) -> This DOM object object Another DOM object @*/ replaceWith: function replaceWith($element) { elements.forEach(function(element) { var parent = element.parentNode, sibling = element.nextSibling; if (parent) { removeOxElements(element, true); parent.removeChild(element); $other.forEach(function(otherElement) { parent.insertBefore(otherElement, sibling); }); } }); return this; }, /*@ show Shows all elements () -> This DOM object @*/ show: function show() { return this.css({display: 'block'}); }, /*@ siblings Returns all siblings of all elements () -> <[h]> Siblings @*/ siblings: function siblings() { return Ox.unique(elements.map(function(element) { return Ox.filter( element.parentNode.childNodes, function(sibling) { return sibling !== element; } ); })); }, /*@ some Tests if some elements satisfy a given condition (test) -> True if some elements pass the test test Test function @*/ some: function some() { return Array.prototype.some.apply(elements, arguments); }, /*@ text Gets or sets the text contents of all elements () -> The text contents (text) -> This DOM object text The text contents @*/ text: function text(string) { var text = ''; if (arguments.length == 0) { elements.forEach(function(element) { text += Ox.isString(this.textContent) ? this.textContent : this.innerText; }); return text; } else { elements.forEach(function(element) { element.empty(); element.appendChild(document.createTextNode(string)); }); return this; } }, /*@ toggleClass Toggles a class name for all elements (className) -> This DOM object className Class name @*/ toggleClass: function toggleClass(string) { elements.forEach(function(element) { var $element = Ox.$(element); $element[ $element.hasClass(string) ? 'removeClass' : 'addClass' ](string); }) return this; }, /*@ trigger Triggers an event (event) -> This DOM object @*/ trigger: function trigger(event) { elements.forEach(function(element) { var e = document.createEvent('MouseEvents'); e.initEvent(event, true, true); element.dispatchEvent(e); }); return this; }, /*@ val Gets the value of the first or sets the value of all elements () -> Value (value) -> This DOM object value Value @*/ val: function val(value) { var ret; if (arguments.length == 0) { return elements[0].value; } else { elements.forEach(function(element) { element.value = value; }); return this; } }, /*@ width Returns the width of the first element () -> Width in px @*/ width: function width() { return elements[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; } }; }());