2011-11-05 16:46:53 +00:00
|
|
|
'use strict';
|
|
|
|
|
2011-10-07 01:04:47 +00:00
|
|
|
/*@
|
2012-04-15 12:14:18 +00:00
|
|
|
Ox.$ <f> Generic HTML element, mimics jQuery
|
2012-05-30 22:47:57 +00:00
|
|
|
value <s|h|w|?> tagname, selector, html element, `window`, or `document`
|
2012-05-30 20:38:48 +00:00
|
|
|
Passing a tagname ('<tagname>') creates an element, passing a selector
|
|
|
|
('tagname', '.classname' or '#id') selects an element.
|
|
|
|
(value) -> <o> Element object
|
2012-04-15 19:12:48 +00:00
|
|
|
> Ox.$('<div>').addClass('red').hasClass('red')
|
2011-10-07 01:04:47 +00:00
|
|
|
true
|
2012-04-15 19:12:48 +00:00
|
|
|
> Ox.$('<div>').addClass('red').removeClass('red').hasClass('red')
|
2011-10-07 01:04:47 +00:00
|
|
|
false
|
2012-04-15 19:12:48 +00:00
|
|
|
> Ox.$('<div>').addClass('red').addClass('red')[0].className
|
|
|
|
'red'
|
2014-09-22 12:16:00 +00:00
|
|
|
> Ox.$('<a>').append(Ox.$('<b>')).children('b').length
|
|
|
|
1
|
|
|
|
> Ox.$('<b>').appendTo(Ox.$('<a>')).parents('a').length
|
|
|
|
1
|
2012-04-15 19:12:48 +00:00
|
|
|
> Ox.$('<div>').attr({id: 'red'}).attr('id')
|
|
|
|
'red'
|
|
|
|
> Ox.$('<div>').attr({id: 'red'}).removeAttr('id').attr('id')
|
2012-05-19 09:16:04 +00:00
|
|
|
void 0
|
2012-04-15 19:12:48 +00:00
|
|
|
> Ox.$('<div>').css({color: 'red'}).css('color')
|
|
|
|
'red'
|
|
|
|
> Ox.$('<div>').html('red').html()
|
|
|
|
'red'
|
|
|
|
> Ox.$('<div>').html('red').empty().html()
|
|
|
|
''
|
2012-05-30 20:38:48 +00:00
|
|
|
> !!Ox.$('<div>').on({click: function(e) { Ox.test(e.type, 'click'); }}).trigger('click')
|
|
|
|
true
|
2014-09-22 12:16:00 +00:00
|
|
|
> Ox.$('<input>').val('red').val()
|
|
|
|
'red'
|
2011-10-07 01:04:47 +00:00
|
|
|
@*/
|
2014-08-22 16:29:15 +00:00
|
|
|
Ox.$ = Ox.element = function $(value) {
|
2013-12-04 22:43:07 +00:00
|
|
|
|
2014-09-22 13:55:29 +00:00
|
|
|
var elements = Ox.isArray(value) ? value // array of elements
|
2014-09-22 12:31:19 +00:00
|
|
|
: Ox.isNodeList(value) ? Ox.slice(value) // nodelist
|
2014-08-21 15:23:33 +00:00
|
|
|
: !Ox.isString(value) ? [value] // window, document or element
|
|
|
|
: value[0] == '<' ? [document.createElement(value.slice(1, -1))]
|
|
|
|
: Ox.slice(document.querySelectorAll(value)),
|
2013-12-04 22:43:07 +00:00
|
|
|
mousewheelEvents = ['wheel', 'mousewheel'],
|
|
|
|
originalMousewheelEvents = 'onwheel' in document ? ['wheel']
|
2014-09-22 12:11:55 +00:00
|
|
|
: ['mousewheel', 'DOMMouseScroll', 'MozMousePixelScroll'],
|
|
|
|
previousDisplay;
|
2013-12-04 22:43:07 +00:00
|
|
|
|
2014-08-27 10:15:34 +00:00
|
|
|
function getElements($other) {
|
|
|
|
return $other.forEach ? $other
|
|
|
|
: Ox.range($other.length).map(function(index) {
|
|
|
|
return $other[index];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-12-04 22:43:07 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-08-21 15:23:33 +00:00
|
|
|
return elements.length ? Ox.extend(
|
|
|
|
Ox.zipObject(Ox.range(elements.length), elements
|
|
|
|
), {
|
2014-09-22 12:34:44 +00:00
|
|
|
|
2014-08-21 15:23:33 +00:00
|
|
|
/*@
|
|
|
|
add <f> Adds another DOM object to this DOM object
|
|
|
|
(other) -> This DOM object
|
|
|
|
other <o> Other DOM object
|
|
|
|
@*/
|
|
|
|
add: function add($other) {
|
|
|
|
elements = Ox.unique(elements.concat(other.elements()));
|
|
|
|
this.length = elements.length;
|
|
|
|
return this;
|
|
|
|
},
|
2014-09-22 12:34:44 +00:00
|
|
|
|
2011-10-07 01:04:47 +00:00
|
|
|
/*@
|
2014-08-21 15:23:33 +00:00
|
|
|
addClass <f> Adds a class name to all elements
|
|
|
|
(className) -> <o> This DOM object
|
2011-10-07 01:04:47 +00:00
|
|
|
className <s> Class name
|
|
|
|
@*/
|
2014-08-21 15:23:33 +00:00
|
|
|
addClass: function addClass(string) {
|
|
|
|
string = Ox.clean(string);
|
|
|
|
elements.forEach(function(element) {
|
|
|
|
element.className = Ox.unique(((
|
|
|
|
element.className ? element.className + ' ' : ''
|
|
|
|
) + string).split(' ')).join(' ');
|
|
|
|
});
|
2011-10-07 01:04:47 +00:00
|
|
|
return this;
|
|
|
|
},
|
2014-09-22 12:34:44 +00:00
|
|
|
|
2011-10-07 01:04:47 +00:00
|
|
|
/*@
|
2014-08-21 15:23:33 +00:00
|
|
|
append <f> Appends one or more DOM objects to this DOM object
|
|
|
|
(object[, object[, ...]]) -> <o> This DOM object
|
|
|
|
element <o> Another DOM object
|
2011-10-07 01:04:47 +00:00
|
|
|
@*/
|
2014-08-21 15:23:33 +00:00
|
|
|
append: function append() {
|
|
|
|
var $others = Ox.slice(arguments);
|
|
|
|
elements.forEach(function(element) {
|
|
|
|
$others.forEach(function($other) {
|
2014-08-27 10:15:34 +00:00
|
|
|
getElements($other).forEach(function(otherElement) {
|
2014-08-21 15:23:33 +00:00
|
|
|
element.appendChild(otherElement);
|
|
|
|
});
|
|
|
|
});
|
2012-04-15 12:14:18 +00:00
|
|
|
});
|
2011-10-07 01:04:47 +00:00
|
|
|
return this;
|
|
|
|
},
|
2014-09-22 12:34:44 +00:00
|
|
|
|
2011-10-07 01:04:47 +00:00
|
|
|
/*@
|
2014-08-21 15:23:33 +00:00
|
|
|
appendTo <f> Appends this DOM object to another DOM object
|
|
|
|
(object) -> <o> This DOM object
|
|
|
|
object <o> Another DOM object
|
2011-10-07 01:04:47 +00:00
|
|
|
@*/
|
2014-08-21 15:23:33 +00:00
|
|
|
appendTo: function appendTo($other) {
|
2014-08-27 10:15:34 +00:00
|
|
|
getElements($other).forEach(function(otherElement) {
|
2014-08-21 15:23:33 +00:00
|
|
|
elements.forEach(function(element) {
|
|
|
|
otherElement.appendChild(element);
|
|
|
|
});
|
|
|
|
});
|
2011-10-07 01:04:47 +00:00
|
|
|
return this;
|
|
|
|
},
|
2014-09-22 12:34:44 +00:00
|
|
|
|
2011-10-07 01:04:47 +00:00
|
|
|
/*@
|
2014-08-21 15:23:33 +00:00
|
|
|
attr <f> Gets or sets an attribute for all elements
|
2012-04-15 12:14:18 +00:00
|
|
|
(key) -> <s> Value
|
2014-08-21 15:23:33 +00:00
|
|
|
(key, value) -> <o> This DOM object
|
|
|
|
({key0: value0, key1: value1, ...}) -> <o> This DOM object
|
2013-12-06 20:59:26 +00:00
|
|
|
key <s> Attribute name
|
|
|
|
value <s> Attribute value
|
2011-10-07 01:04:47 +00:00
|
|
|
@*/
|
2014-08-21 15:23:33 +00:00
|
|
|
attr: function attr() {
|
|
|
|
var args = arguments, ret, that = this;
|
|
|
|
if (args.length == 1 && Ox.isString(args[0])) {
|
Ox.$: add children, find, insertAfter, insertBefore, next, nextAll, parent, parents, prepend, prependTo, prev, and prevAll, fix attr and one
2013-11-29 20:19:09 +00:00
|
|
|
ret = this[0].getAttribute
|
2014-08-21 15:23:33 +00:00
|
|
|
? this[0].getAttribute(args[0])
|
|
|
|
: void 0;
|
Ox.$: add children, find, insertAfter, insertBefore, next, nextAll, parent, parents, prepend, prependTo, prev, and prevAll, fix attr and one
2013-11-29 20:19:09 +00:00
|
|
|
// fixme: why exactly is this needed?
|
2014-08-21 15:23:33 +00:00
|
|
|
return ret === null ? void 0 : ret;
|
2011-10-07 01:04:47 +00:00
|
|
|
} else {
|
2014-08-21 15:23:33 +00:00
|
|
|
args = Ox.makeObject(args);
|
|
|
|
elements.forEach(function(element) {
|
|
|
|
Ox.forEach(args, function(value, key) {
|
2014-09-22 12:11:55 +00:00
|
|
|
if (
|
|
|
|
element.setAttribute
|
|
|
|
&& !Ox.contains([false, null, void 0], value)
|
|
|
|
) {
|
2014-08-21 15:23:33 +00:00
|
|
|
element.setAttribute(key, value);
|
|
|
|
}
|
|
|
|
});
|
2011-10-07 01:04:47 +00:00
|
|
|
});
|
2014-08-21 15:23:33 +00:00
|
|
|
return this;
|
2011-10-07 01:04:47 +00:00
|
|
|
}
|
|
|
|
},
|
2014-09-22 12:34:44 +00:00
|
|
|
|
2011-10-07 01:04:47 +00:00
|
|
|
/*@
|
2014-08-21 15:23:33 +00:00
|
|
|
children <f> Returns the unique list of children of all elements
|
2014-09-22 12:11:55 +00:00
|
|
|
([selector]) -> <[h]> Children
|
|
|
|
selector <s|'*'> CSS selector
|
Ox.$: add children, find, insertAfter, insertBefore, next, nextAll, parent, parents, prepend, prependTo, prev, and prevAll, fix attr and one
2013-11-29 20:19:09 +00:00
|
|
|
@*/
|
2014-09-22 12:14:13 +00:00
|
|
|
children: function children(selector) {
|
2014-09-22 12:11:55 +00:00
|
|
|
var children = Ox.unique(Ox.flatten(elements.map(function(element) {
|
|
|
|
return Ox.slice(element.childNodes);
|
2014-08-21 15:23:33 +00:00
|
|
|
})));
|
2014-09-22 12:11:55 +00:00
|
|
|
return Ox.$(selector ? children.filter(function(child) {
|
|
|
|
return Ox.$(child).is(selector);
|
|
|
|
}) : children);
|
Ox.$: add children, find, insertAfter, insertBefore, next, nextAll, parent, parents, prepend, prependTo, prev, and prevAll, fix attr and one
2013-11-29 20:19:09 +00:00
|
|
|
},
|
2014-09-22 12:34:44 +00:00
|
|
|
|
Ox.$: add children, find, insertAfter, insertBefore, next, nextAll, parent, parents, prepend, prependTo, prev, and prevAll, fix attr and one
2013-11-29 20:19:09 +00:00
|
|
|
/*@
|
2014-08-21 15:23:33 +00:00
|
|
|
css <f> Gets or sets a CSS attribute for all elements
|
2012-04-15 12:14:18 +00:00
|
|
|
(key) -> <s> Value
|
2014-08-21 15:23:33 +00:00
|
|
|
(key, value) -> <o> This DOM object
|
|
|
|
({key0: value0, key1: value1, ...}) -> <o> This DOM object
|
2013-12-06 20:59:26 +00:00
|
|
|
key <s> Attribute name
|
|
|
|
value <s> Attribute value
|
2011-10-07 01:04:47 +00:00
|
|
|
@*/
|
2014-08-21 15:23:33 +00:00
|
|
|
css: function css() {
|
|
|
|
var args = arguments;
|
|
|
|
if (args.length == 1 && Ox.isString(args[0])) {
|
|
|
|
return elements[0].style[args[0]];
|
2011-10-07 01:04:47 +00:00
|
|
|
} else {
|
2014-08-21 15:23:33 +00:00
|
|
|
elements.forEach(function(element) {
|
2014-09-22 12:11:55 +00:00
|
|
|
Ox.forEach(Ox.makeObject(args), function(value, key) {
|
2014-08-21 15:23:33 +00:00
|
|
|
element.style[key] = value;
|
|
|
|
});
|
2011-10-07 01:04:47 +00:00
|
|
|
});
|
2014-08-21 15:23:33 +00:00
|
|
|
return this;
|
2011-10-07 01:04:47 +00:00
|
|
|
}
|
|
|
|
},
|
2014-09-22 12:34:44 +00:00
|
|
|
|
2013-12-06 20:59:26 +00:00
|
|
|
/*@
|
|
|
|
data <f> Gets or sets data
|
|
|
|
() -> <o> All data
|
|
|
|
(key) -> <s> Value
|
2014-08-21 15:23:33 +00:00
|
|
|
(key, value) -> <o> This DOM object
|
|
|
|
({key0: value0, key1: value1, ...}) -> <o> This DOM object
|
2013-12-06 20:59:26 +00:00
|
|
|
key <s> Property
|
|
|
|
value <*> Value
|
|
|
|
@*/
|
2014-08-21 15:23:33 +00:00
|
|
|
data: function data() {
|
2014-09-22 13:55:29 +00:00
|
|
|
var args;
|
|
|
|
if (arguments.length == 1 && Ox.isString(arguments[0])) {
|
|
|
|
return element.getAttribute('data-' + arguments[0]);
|
2013-12-06 20:54:26 +00:00
|
|
|
} else {
|
2014-09-22 13:55:29 +00:00
|
|
|
args = Ox.makeObject(arguments);
|
|
|
|
elements.forEach(function(element) {
|
|
|
|
Ox.forEach(args, function(value, key) {
|
|
|
|
element.setAttribute('data-' + key, value);
|
|
|
|
});
|
2013-12-06 20:54:26 +00:00
|
|
|
});
|
2014-08-21 15:23:33 +00:00
|
|
|
return this;
|
2013-12-06 20:54:26 +00:00
|
|
|
}
|
|
|
|
},
|
2014-09-22 12:34:44 +00:00
|
|
|
|
2012-04-15 12:14:18 +00:00
|
|
|
/*@
|
2014-09-22 12:11:55 +00:00
|
|
|
elements <a> All elements
|
2012-04-15 12:14:18 +00:00
|
|
|
@*/
|
2014-09-22 12:11:55 +00:00
|
|
|
elements: elements,
|
2014-09-22 12:34:44 +00:00
|
|
|
|
2014-09-22 12:11:55 +00:00
|
|
|
/*@
|
|
|
|
eq <f> Reduces the list of elements to the one at the given index
|
|
|
|
() -> <o> This DOM object
|
|
|
|
@*/
|
|
|
|
eq: function eq() {
|
|
|
|
var that = this;
|
|
|
|
Ox.loop(1, this.length, function(index) {
|
|
|
|
delete that[index];
|
|
|
|
});
|
|
|
|
this.elements = [this.elements[index]];
|
|
|
|
this.length = 1;
|
|
|
|
return this;
|
2014-08-21 15:23:33 +00:00
|
|
|
},
|
2014-09-22 12:34:44 +00:00
|
|
|
|
2014-08-21 15:23:33 +00:00
|
|
|
/*@
|
|
|
|
empty <f> Empties the inner HTML of all elements
|
|
|
|
() -> <o> This DOM object
|
|
|
|
@*/
|
|
|
|
empty: function empty() {
|
2012-04-12 19:21:03 +00:00
|
|
|
return this.html('');
|
2012-04-05 15:30:00 +00:00
|
|
|
},
|
2014-09-22 12:34:44 +00:00
|
|
|
|
2011-10-07 01:04:47 +00:00
|
|
|
/*@
|
2014-08-21 15:23:33 +00:00
|
|
|
every <f> Tests if every element satisfies a given condition
|
|
|
|
(test) -> True if every element passes the test
|
|
|
|
test <f> Test function
|
|
|
|
@*/
|
|
|
|
every: function every() {
|
|
|
|
return Array.prototype.every.apply(elements, arguments);
|
|
|
|
},
|
2014-09-22 12:34:44 +00:00
|
|
|
|
2014-08-21 15:23:33 +00:00
|
|
|
/*@
|
|
|
|
filter <f> Filters all elements by a given condition
|
|
|
|
(test) -> Array of matching elements
|
|
|
|
test <f> Test function
|
|
|
|
@*/
|
|
|
|
filter: function filter() {
|
|
|
|
return Array.prototype.filter.apply(elements, arguments);
|
|
|
|
},
|
2014-09-22 12:34:44 +00:00
|
|
|
|
2014-08-21 15:23:33 +00:00
|
|
|
/*@
|
|
|
|
find <f> Find all descendant elements matching a CSS selector
|
2013-12-06 09:26:31 +00:00
|
|
|
([selector]) -> <[h]> Elements
|
Ox.$: add children, find, insertAfter, insertBefore, next, nextAll, parent, parents, prepend, prependTo, prev, and prevAll, fix attr and one
2013-11-29 20:19:09 +00:00
|
|
|
selector <s|'*'> CSS selector
|
|
|
|
@*/
|
2014-09-22 12:11:55 +00:00
|
|
|
find: function find(selector) {
|
|
|
|
return Ox.$(Ox.unique(Ox.flatten(elements.map(function(element) {
|
|
|
|
return Ox.slice(element.querySelectorAll(selector || '*'));
|
|
|
|
}))));
|
2014-08-21 15:23:33 +00:00
|
|
|
},
|
2014-09-22 12:34:44 +00:00
|
|
|
|
2014-08-21 15:23:33 +00:00
|
|
|
/*@
|
|
|
|
forEach <f> Loops over all elements
|
|
|
|
(iterator) -> This DOM object
|
|
|
|
iterator <f> Iterator function
|
|
|
|
@*/
|
|
|
|
forEach: function forEach() {
|
|
|
|
Array.prototype.forEach.apply(elements, arguments);
|
|
|
|
return this;
|
Ox.$: add children, find, insertAfter, insertBefore, next, nextAll, parent, parents, prepend, prependTo, prev, and prevAll, fix attr and one
2013-11-29 20:19:09 +00:00
|
|
|
},
|
2014-09-22 12:34:44 +00:00
|
|
|
|
Ox.$: add children, find, insertAfter, insertBefore, next, nextAll, parent, parents, prepend, prependTo, prev, and prevAll, fix attr and one
2013-11-29 20:19:09 +00:00
|
|
|
/*@
|
2012-04-15 12:14:18 +00:00
|
|
|
hasClass <f> Returns true if this element has a given class
|
|
|
|
(className) -> <b> True if this element has the class
|
2011-10-07 01:04:47 +00:00
|
|
|
className <s> Class name
|
|
|
|
@*/
|
2014-08-21 15:23:33 +00:00
|
|
|
hasClass: function hasClass(string) {
|
2014-08-27 10:04:03 +00:00
|
|
|
return elements.some(function(element) {
|
2014-08-21 15:23:33 +00:00
|
|
|
return Ox.contains(element.className.split(' '), string);
|
|
|
|
});
|
2011-10-07 01:04:47 +00:00
|
|
|
},
|
2014-09-22 12:34:44 +00:00
|
|
|
|
2012-04-15 12:14:18 +00:00
|
|
|
/*@
|
2014-08-21 15:23:33 +00:00
|
|
|
height <f> Returns the height of the first element
|
2012-04-15 12:14:18 +00:00
|
|
|
() -> <n> Height in px
|
|
|
|
@*/
|
2014-08-21 15:23:33 +00:00
|
|
|
height: function height() {
|
|
|
|
return elements[0].offsetHeight;
|
2012-04-12 19:21:03 +00:00
|
|
|
},
|
2014-09-22 12:34:44 +00:00
|
|
|
|
2012-04-15 12:14:18 +00:00
|
|
|
/*@
|
2014-08-21 15:23:33 +00:00
|
|
|
hide <f> Hides all elements
|
|
|
|
() -> <o> This DOM object
|
2012-04-15 12:14:18 +00:00
|
|
|
@*/
|
2014-08-21 15:23:33 +00:00
|
|
|
hide: function hide() {
|
2014-09-22 12:11:55 +00:00
|
|
|
previousDisplay = this.css('display');
|
2012-04-12 19:21:03 +00:00
|
|
|
return this.css({display: 'none'});
|
|
|
|
},
|
2014-09-22 12:34:44 +00:00
|
|
|
|
2011-10-07 01:04:47 +00:00
|
|
|
/*@
|
2014-08-21 15:23:33 +00:00
|
|
|
html <f> Gets or sets the innerHTML of all elements
|
2012-04-15 12:14:18 +00:00
|
|
|
() -> <s> The inner HTML
|
2014-08-21 15:23:33 +00:00
|
|
|
(html) -> <o> This DOM object
|
2011-10-07 01:04:47 +00:00
|
|
|
html <s> The inner HTML
|
|
|
|
@*/
|
2014-08-21 15:23:33 +00:00
|
|
|
html: function html(string) {
|
|
|
|
var html = '';
|
2012-05-25 07:52:57 +00:00
|
|
|
if (arguments.length == 0) {
|
2014-08-21 15:23:33 +00:00
|
|
|
elements.forEach(function(element) {
|
|
|
|
html += element.innerHTML;
|
|
|
|
})
|
|
|
|
return html;
|
2011-10-07 01:04:47 +00:00
|
|
|
} else {
|
2014-08-21 15:23:33 +00:00
|
|
|
elements.forEach(function(element) {
|
|
|
|
element.innerHTML = string;
|
|
|
|
});
|
|
|
|
return this;
|
2011-10-07 01:04:47 +00:00
|
|
|
}
|
|
|
|
},
|
2014-09-22 12:34:44 +00:00
|
|
|
|
2011-10-07 01:04:47 +00:00
|
|
|
/*@
|
2014-08-21 15:23:33 +00:00
|
|
|
insertAfter <f> Inserts this DOM object after another DOM object
|
|
|
|
(object) -> <o> This DOM object
|
|
|
|
object <o> Another DOM object
|
Ox.$: add children, find, insertAfter, insertBefore, next, nextAll, parent, parents, prepend, prependTo, prev, and prevAll, fix attr and one
2013-11-29 20:19:09 +00:00
|
|
|
@*/
|
2014-08-21 15:23:33 +00:00
|
|
|
insertAfter: function insertAfter($other) {
|
2014-09-22 12:11:55 +00:00
|
|
|
var nextSibling = $other[0].nextSibling;
|
2014-08-21 15:23:33 +00:00
|
|
|
elements.forEach(function(element) {
|
|
|
|
$other[0].parentNode.insertBefore(element, nextSibling);
|
|
|
|
})
|
Ox.$: add children, find, insertAfter, insertBefore, next, nextAll, parent, parents, prepend, prependTo, prev, and prevAll, fix attr and one
2013-11-29 20:19:09 +00:00
|
|
|
return this;
|
|
|
|
},
|
2014-09-22 12:34:44 +00:00
|
|
|
|
Ox.$: add children, find, insertAfter, insertBefore, next, nextAll, parent, parents, prepend, prependTo, prev, and prevAll, fix attr and one
2013-11-29 20:19:09 +00:00
|
|
|
/*@
|
2014-08-21 15:23:33 +00:00
|
|
|
insertBefore <f> Inserts this DOM object before another DOM object
|
|
|
|
(object) -> <o> This DOM object
|
|
|
|
object <o> Another DOM object
|
Ox.$: add children, find, insertAfter, insertBefore, next, nextAll, parent, parents, prepend, prependTo, prev, and prevAll, fix attr and one
2013-11-29 20:19:09 +00:00
|
|
|
@*/
|
2014-08-21 15:23:33 +00:00
|
|
|
insertBefore: function insertBefore($other) {
|
|
|
|
elements.forEach(function(element) {
|
|
|
|
$other[0].parentNode.insertBefore(element, $other[0]);
|
|
|
|
});
|
Ox.$: add children, find, insertAfter, insertBefore, next, nextAll, parent, parents, prepend, prependTo, prev, and prevAll, fix attr and one
2013-11-29 20:19:09 +00:00
|
|
|
return this;
|
|
|
|
},
|
2014-09-22 12:34:44 +00:00
|
|
|
|
Ox.$: add children, find, insertAfter, insertBefore, next, nextAll, parent, parents, prepend, prependTo, prev, and prevAll, fix attr and one
2013-11-29 20:19:09 +00:00
|
|
|
/*@
|
2014-08-21 15:23:33 +00:00
|
|
|
is <f> Tests if any element matches a CSS selector
|
|
|
|
(selector) -> <b> True if the element matches the selector
|
|
|
|
selector <s> CSS selector
|
|
|
|
@*/
|
2014-09-22 12:11:55 +00:00
|
|
|
is: function is(selector) {
|
2014-08-21 15:23:33 +00:00
|
|
|
return elements.some(function(element) {
|
2014-09-22 12:11:55 +00:00
|
|
|
var parent = element.parentNode;
|
|
|
|
if (!parent) {
|
|
|
|
parent = document.createElement('div');
|
|
|
|
parent.appendChild(element);
|
|
|
|
}
|
|
|
|
return Ox.contains(parent.querySelectorAll(selector), element);
|
2014-08-21 15:23:33 +00:00
|
|
|
});
|
|
|
|
},
|
2014-09-22 12:34:44 +00:00
|
|
|
|
2014-08-21 15:23:33 +00:00
|
|
|
/*@
|
|
|
|
length <n> Number of elements
|
|
|
|
@*/
|
|
|
|
length: elements.length,
|
2014-09-22 12:34:44 +00:00
|
|
|
|
2014-08-21 15:23:33 +00:00
|
|
|
/*@
|
|
|
|
map <f> Transforms all elements
|
|
|
|
(iterator) -> [] Transformed elements
|
|
|
|
iterator <f> Iterator function
|
2014-08-21 11:05:41 +00:00
|
|
|
@*/
|
2014-08-21 15:23:33 +00:00
|
|
|
map: function map() {
|
|
|
|
return Array.prototype.filter.map(elements, arguments);
|
2014-08-21 11:05:41 +00:00
|
|
|
},
|
2014-09-22 12:34:44 +00:00
|
|
|
|
2014-08-21 11:05:41 +00:00
|
|
|
/*@
|
2014-08-21 15:23:33 +00:00
|
|
|
next <f> Returns the unique list of siblings directly after all elements
|
|
|
|
() -> <[h]> Siblings
|
Ox.$: add children, find, insertAfter, insertBefore, next, nextAll, parent, parents, prepend, prependTo, prev, and prevAll, fix attr and one
2013-11-29 20:19:09 +00:00
|
|
|
@*/
|
2014-08-21 15:23:33 +00:00
|
|
|
next: function next() {
|
2014-09-22 12:11:55 +00:00
|
|
|
return Ox.$(Ox.unique(Ox.filter(elements.map(function(element) {
|
2014-08-21 15:23:33 +00:00
|
|
|
return element.nextSibling;
|
2014-09-22 12:11:55 +00:00
|
|
|
}))));
|
Ox.$: add children, find, insertAfter, insertBefore, next, nextAll, parent, parents, prepend, prependTo, prev, and prevAll, fix attr and one
2013-11-29 20:19:09 +00:00
|
|
|
},
|
2014-09-22 12:34:44 +00:00
|
|
|
|
Ox.$: add children, find, insertAfter, insertBefore, next, nextAll, parent, parents, prepend, prependTo, prev, and prevAll, fix attr and one
2013-11-29 20:19:09 +00:00
|
|
|
/*@
|
2014-08-21 15:23:33 +00:00
|
|
|
nextAll <f> Returns the unique list of siblings after all elements
|
|
|
|
() -> <[h]> Siblings
|
Ox.$: add children, find, insertAfter, insertBefore, next, nextAll, parent, parents, prepend, prependTo, prev, and prevAll, fix attr and one
2013-11-29 20:19:09 +00:00
|
|
|
@*/
|
2014-08-21 15:23:33 +00:00
|
|
|
nextAll: function nextAll() {
|
|
|
|
var siblings = [];
|
|
|
|
elements.forEach(function(element) {
|
|
|
|
var sibling = element;
|
|
|
|
while (true) {
|
|
|
|
sibling = sibling.nextSibling;
|
|
|
|
if (!sibling) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
siblings.push(sibling);
|
Ox.$: add children, find, insertAfter, insertBefore, next, nextAll, parent, parents, prepend, prependTo, prev, and prevAll, fix attr and one
2013-11-29 20:19:09 +00:00
|
|
|
}
|
2014-08-21 15:23:33 +00:00
|
|
|
});
|
2014-09-22 12:11:55 +00:00
|
|
|
return Ox.$(Ox.unique(siblings));
|
Ox.$: add children, find, insertAfter, insertBefore, next, nextAll, parent, parents, prepend, prependTo, prev, and prevAll, fix attr and one
2013-11-29 20:19:09 +00:00
|
|
|
},
|
2014-09-22 12:34:44 +00:00
|
|
|
|
Ox.$: add children, find, insertAfter, insertBefore, next, nextAll, parent, parents, prepend, prependTo, prev, and prevAll, fix attr and one
2013-11-29 20:19:09 +00:00
|
|
|
/*@
|
|
|
|
off <f> Unbinds a callback from an event
|
2014-08-21 15:23:33 +00:00
|
|
|
(event) -> <o> This DOM object (unbinds all callbacks)
|
|
|
|
(event, callback) -> <o> This DOM object
|
|
|
|
({event0: callback0, event1: callback1, ...}) -> <o> This DOM object
|
Ox.$: add children, find, insertAfter, insertBefore, next, nextAll, parent, parents, prepend, prependTo, prev, and prevAll, fix attr and one
2013-11-29 20:19:09 +00:00
|
|
|
event <s> Event name
|
|
|
|
callback <f> Callback function
|
|
|
|
@*/
|
2014-08-21 15:23:33 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
});
|
Ox.$: add children, find, insertAfter, insertBefore, next, nextAll, parent, parents, prepend, prependTo, prev, and prevAll, fix attr and one
2013-11-29 20:19:09 +00:00
|
|
|
});
|
|
|
|
return this;
|
|
|
|
},
|
2014-09-22 12:34:44 +00:00
|
|
|
|
Ox.$: add children, find, insertAfter, insertBefore, next, nextAll, parent, parents, prepend, prependTo, prev, and prevAll, fix attr and one
2013-11-29 20:19:09 +00:00
|
|
|
/*@
|
2012-05-28 13:53:10 +00:00
|
|
|
on <f> Binds a callback to an event
|
2014-08-21 15:23:33 +00:00
|
|
|
(event, callback) -> <o> This DOM object
|
|
|
|
({event0: callback0, event1: callback1, ...}) -> <o> This DOM object
|
2012-05-28 13:53:10 +00:00
|
|
|
event <s> Event name
|
|
|
|
callback <f> Callback function
|
|
|
|
e <o> Event properties
|
|
|
|
@*/
|
2014-08-21 15:23:33 +00:00
|
|
|
on: function on() {
|
|
|
|
var args = normalizeEvents(arguments);
|
|
|
|
elements.forEach(function(element) {
|
|
|
|
Ox.forEach(args, function(callback, event) {
|
|
|
|
element.addEventListener(event, callback, false);
|
|
|
|
});
|
2012-05-28 13:53:10 +00:00
|
|
|
});
|
|
|
|
return this;
|
|
|
|
},
|
2014-09-22 12:34:44 +00:00
|
|
|
|
2012-05-28 13:53:10 +00:00
|
|
|
/*@
|
2012-04-15 12:14:18 +00:00
|
|
|
one <f> Binds a callback to an event and unbinds it on first invocation
|
2014-08-21 15:23:33 +00:00
|
|
|
(event, callback) -> <o> This DOM object
|
|
|
|
({event0: callback0, event1: callback1, ...}) -> <o> This DOM object
|
2012-04-15 12:14:18 +00:00
|
|
|
event <s> Event name
|
2011-10-07 01:04:47 +00:00
|
|
|
callback <f> Callback function
|
2012-04-15 12:14:18 +00:00
|
|
|
e <o> Event properties
|
2011-10-07 01:04:47 +00:00
|
|
|
@*/
|
2014-08-21 15:23:33 +00:00
|
|
|
one: function one(events) {
|
2013-12-06 09:26:31 +00:00
|
|
|
var args = Ox.slice(arguments), that = this;
|
2013-12-04 22:43:07 +00:00
|
|
|
Ox.forEach(normalizeEvents(arguments), function(callback, event) {
|
2012-05-28 13:53:10 +00:00
|
|
|
that.on(event, function fn() {
|
|
|
|
that.off(event, fn);
|
Ox.$: add children, find, insertAfter, insertBefore, next, nextAll, parent, parents, prepend, prependTo, prev, and prevAll, fix attr and one
2013-11-29 20:19:09 +00:00
|
|
|
return callback.apply(that, args);
|
2012-04-15 12:14:18 +00:00
|
|
|
});
|
|
|
|
});
|
2011-10-07 01:04:47 +00:00
|
|
|
return this;
|
|
|
|
},
|
2014-09-22 12:34:44 +00:00
|
|
|
|
2012-04-15 12:14:18 +00:00
|
|
|
/*@
|
2014-08-21 15:23:33 +00:00
|
|
|
parent <f> Returns the unique list of parents of all elements
|
|
|
|
() -> <[h]> Parent elements
|
2012-05-28 13:53:10 +00:00
|
|
|
@*/
|
2014-08-21 15:23:33 +00:00
|
|
|
parent: function parent() {
|
2014-09-22 12:11:55 +00:00
|
|
|
return Ox.$(Ox.unique(Ox.compact(elements.map(function(element) {
|
2014-08-21 15:23:33 +00:00
|
|
|
return element.parentNode;
|
2014-09-22 12:11:55 +00:00
|
|
|
}))));
|
Ox.$: add children, find, insertAfter, insertBefore, next, nextAll, parent, parents, prepend, prependTo, prev, and prevAll, fix attr and one
2013-11-29 20:19:09 +00:00
|
|
|
},
|
2014-09-22 12:34:44 +00:00
|
|
|
|
Ox.$: add children, find, insertAfter, insertBefore, next, nextAll, parent, parents, prepend, prependTo, prev, and prevAll, fix attr and one
2013-11-29 20:19:09 +00:00
|
|
|
/*@
|
2014-08-21 15:23:33 +00:00
|
|
|
parents <f> Returns the unique list of all ancestors of all elements
|
2014-09-22 12:11:55 +00:00
|
|
|
([selector]) -> <[h]> Ancestor elements
|
|
|
|
selector <s|'*'> CSS selector
|
Ox.$: add children, find, insertAfter, insertBefore, next, nextAll, parent, parents, prepend, prependTo, prev, and prevAll, fix attr and one
2013-11-29 20:19:09 +00:00
|
|
|
@*/
|
2014-09-22 12:14:13 +00:00
|
|
|
parents: function parents(selector) {
|
2014-08-21 15:23:33 +00:00
|
|
|
var parents = [];
|
|
|
|
Ox.reverse(elements).forEach(function(element) {
|
|
|
|
var parent = element;
|
|
|
|
while (true) {
|
|
|
|
parent = parent.parentNode;
|
2014-09-22 13:55:29 +00:00
|
|
|
if (!parent || parent == document) {
|
2014-08-21 15:23:33 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
parents.unshift(parent);
|
2012-05-28 13:53:10 +00:00
|
|
|
}
|
2014-08-21 15:23:33 +00:00
|
|
|
});
|
2014-09-22 12:11:55 +00:00
|
|
|
parents = Ox.unique(parents);
|
|
|
|
return Ox.$(selector ? parents.filter(function(parent) {
|
|
|
|
return Ox.$(parent).is(selector);
|
|
|
|
}) : parents);
|
Ox.$: add children, find, insertAfter, insertBefore, next, nextAll, parent, parents, prepend, prependTo, prev, and prevAll, fix attr and one
2013-11-29 20:19:09 +00:00
|
|
|
},
|
2014-09-22 12:34:44 +00:00
|
|
|
|
Ox.$: add children, find, insertAfter, insertBefore, next, nextAll, parent, parents, prepend, prependTo, prev, and prevAll, fix attr and one
2013-11-29 20:19:09 +00:00
|
|
|
/*@
|
2014-08-21 15:23:33 +00:00
|
|
|
prepend <f> Prepends one or more DOM objects to this DOM object
|
|
|
|
(object[, object[, ...]]) -> <o> DOM object
|
|
|
|
object <o> Another DOM objectt
|
Ox.$: add children, find, insertAfter, insertBefore, next, nextAll, parent, parents, prepend, prependTo, prev, and prevAll, fix attr and one
2013-11-29 20:19:09 +00:00
|
|
|
@*/
|
2014-08-21 15:23:33 +00:00
|
|
|
prepend: function prepend() {
|
|
|
|
var $others = Ox.slice(arguments).reverse();
|
|
|
|
elements.forEach(function(element) {
|
|
|
|
var parent = element.parentNode;
|
|
|
|
$others.forEach(function($other) {
|
2014-08-27 10:15:34 +00:00
|
|
|
getElements($other).forEach(function(otherElement) {
|
2014-08-21 15:23:33 +00:00
|
|
|
parent.insertBefore(otherElement, parent.firstChild);
|
|
|
|
});
|
|
|
|
});
|
2012-05-28 13:53:10 +00:00
|
|
|
});
|
|
|
|
return this;
|
|
|
|
},
|
2014-09-22 12:34:44 +00:00
|
|
|
|
2012-05-28 13:53:10 +00:00
|
|
|
/*@
|
2014-08-21 15:23:33 +00:00
|
|
|
prependTo <f> Prepends this DOM object to another DOM object
|
|
|
|
(object) -> <o> This DOM object
|
|
|
|
object <o> Another DOM object
|
Ox.$: add children, find, insertAfter, insertBefore, next, nextAll, parent, parents, prepend, prependTo, prev, and prevAll, fix attr and one
2013-11-29 20:19:09 +00:00
|
|
|
@*/
|
2014-08-21 15:23:33 +00:00
|
|
|
prependTo: function prependTo($other) {
|
2014-08-27 10:15:34 +00:00
|
|
|
getElements($other).forEach(function(otherElement) {
|
2014-08-21 15:23:33 +00:00
|
|
|
var firstChild = otherElement.firstChild
|
|
|
|
elements.forEach(function(element) {
|
|
|
|
otherElement.insertBefore(element, firstChild);
|
|
|
|
});
|
|
|
|
});
|
Ox.$: add children, find, insertAfter, insertBefore, next, nextAll, parent, parents, prepend, prependTo, prev, and prevAll, fix attr and one
2013-11-29 20:19:09 +00:00
|
|
|
return this;
|
|
|
|
},
|
2014-09-22 12:34:44 +00:00
|
|
|
|
Ox.$: add children, find, insertAfter, insertBefore, next, nextAll, parent, parents, prepend, prependTo, prev, and prevAll, fix attr and one
2013-11-29 20:19:09 +00:00
|
|
|
/*@
|
2014-08-21 15:23:33 +00:00
|
|
|
prev <f> Returns the unique list of siblings directly before all elements
|
|
|
|
() -> <[h]> Siblings
|
Ox.$: add children, find, insertAfter, insertBefore, next, nextAll, parent, parents, prepend, prependTo, prev, and prevAll, fix attr and one
2013-11-29 20:19:09 +00:00
|
|
|
@*/
|
2014-08-21 15:23:33 +00:00
|
|
|
prev: function prev() {
|
2014-09-22 12:11:55 +00:00
|
|
|
return Ox.$(Ox.unique(Ox.filter(elements.map(function(element) {
|
2014-08-21 15:23:33 +00:00
|
|
|
return element.previousSibling;
|
2014-09-22 12:11:55 +00:00
|
|
|
}))));
|
Ox.$: add children, find, insertAfter, insertBefore, next, nextAll, parent, parents, prepend, prependTo, prev, and prevAll, fix attr and one
2013-11-29 20:19:09 +00:00
|
|
|
},
|
2014-09-22 12:34:44 +00:00
|
|
|
|
Ox.$: add children, find, insertAfter, insertBefore, next, nextAll, parent, parents, prepend, prependTo, prev, and prevAll, fix attr and one
2013-11-29 20:19:09 +00:00
|
|
|
/*@
|
2014-08-21 15:23:33 +00:00
|
|
|
prevAll <f> Returns the unique list of siblings before all elements
|
|
|
|
() -> <[h]> Siblings
|
Ox.$: add children, find, insertAfter, insertBefore, next, nextAll, parent, parents, prepend, prependTo, prev, and prevAll, fix attr and one
2013-11-29 20:19:09 +00:00
|
|
|
@*/
|
2014-08-21 15:23:33 +00:00
|
|
|
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);
|
Ox.$: add children, find, insertAfter, insertBefore, next, nextAll, parent, parents, prepend, prependTo, prev, and prevAll, fix attr and one
2013-11-29 20:19:09 +00:00
|
|
|
}
|
2014-08-21 15:23:33 +00:00
|
|
|
});
|
2014-09-22 12:11:55 +00:00
|
|
|
return Ox.$(Ox.unique(siblings));
|
Ox.$: add children, find, insertAfter, insertBefore, next, nextAll, parent, parents, prepend, prependTo, prev, and prevAll, fix attr and one
2013-11-29 20:19:09 +00:00
|
|
|
},
|
2014-09-22 12:34:44 +00:00
|
|
|
|
Ox.$: add children, find, insertAfter, insertBefore, next, nextAll, parent, parents, prepend, prependTo, prev, and prevAll, fix attr and one
2013-11-29 20:19:09 +00:00
|
|
|
/*@
|
2014-08-21 15:23:33 +00:00
|
|
|
reduce <f> Applies `reduce` to all elements
|
2012-04-15 12:14:18 +00:00
|
|
|
@*/
|
2014-08-21 15:23:33 +00:00
|
|
|
reduce: function reduce() {
|
|
|
|
return Array.prototype.reduce.apply(elements, arguments);
|
|
|
|
},
|
2014-09-22 12:34:44 +00:00
|
|
|
|
2014-08-21 15:23:33 +00:00
|
|
|
/*@
|
|
|
|
remove <f> Removes all element from the DOM
|
|
|
|
() -> <o> This DOM object
|
|
|
|
@*/
|
|
|
|
remove: function remove() {
|
|
|
|
elements.forEach(function(element) {
|
|
|
|
if (element.parentNode) {
|
|
|
|
element.parentNode.removeChild(element);
|
|
|
|
}
|
|
|
|
});
|
2012-04-06 23:45:51 +00:00
|
|
|
return this;
|
|
|
|
},
|
2014-09-22 12:34:44 +00:00
|
|
|
|
2011-10-07 01:04:47 +00:00
|
|
|
/*@
|
2014-08-21 15:23:33 +00:00
|
|
|
removeAttr <f> Removes an attribute from all elements
|
|
|
|
(key) -> <o> This DOM object
|
|
|
|
([key0, key1, ...]) -> <o> This DOM object
|
2011-10-07 01:04:47 +00:00
|
|
|
key <s> The attribute
|
|
|
|
@*/
|
2014-08-21 15:23:33 +00:00
|
|
|
removeAttr: function removeAttr() {
|
|
|
|
var keys = Ox.makeArray(arguments);
|
|
|
|
elements.forEach(function(element) {
|
|
|
|
keys.forEach(function(key) {
|
|
|
|
element.removeAttribute(key);
|
|
|
|
});
|
2012-04-15 12:14:18 +00:00
|
|
|
});
|
2011-10-07 01:04:47 +00:00
|
|
|
return this;
|
|
|
|
},
|
2014-09-22 12:34:44 +00:00
|
|
|
|
2011-10-07 01:04:47 +00:00
|
|
|
/*@
|
2014-08-21 15:23:33 +00:00
|
|
|
removeClass <f> Removes a class name from all elements
|
|
|
|
(className) -> <o> This DOM object
|
2011-10-07 01:04:47 +00:00
|
|
|
className <s> Class name
|
|
|
|
@*/
|
2014-08-21 15:23:33 +00:00
|
|
|
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(' ');
|
|
|
|
});
|
2011-10-07 01:04:47 +00:00
|
|
|
return this;
|
2012-04-05 15:30:00 +00:00
|
|
|
},
|
2014-09-22 12:34:44 +00:00
|
|
|
|
2012-04-15 12:14:18 +00:00
|
|
|
/*@
|
2014-08-21 15:23:33 +00:00
|
|
|
replace <f> Replaces another DOM object with this DOM object
|
|
|
|
(object) -> <o> This DOM object
|
|
|
|
object <o> Another DOM object
|
|
|
|
@*/
|
|
|
|
replace: function replace($other) {
|
2014-08-27 10:15:34 +00:00
|
|
|
getElements($other).forEach(function(otherElement) {
|
2014-08-21 15:23:33 +00:00
|
|
|
var parent = otherElement.parentNode,
|
|
|
|
sibling = otherElement.nextSibling;
|
|
|
|
if (parent) {
|
|
|
|
parent.removeChild(otherElement);
|
|
|
|
elements.forEach(function(element) {
|
|
|
|
parent.insertBefore(element, sibling)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2013-12-06 09:26:31 +00:00
|
|
|
return this;
|
|
|
|
},
|
2014-09-22 12:34:44 +00:00
|
|
|
|
2013-12-06 09:26:31 +00:00
|
|
|
/*@
|
2014-08-21 15:23:33 +00:00
|
|
|
replaceWith <f> Replaces this DOM object with another DOM object
|
|
|
|
(object) -> <o> This DOM object
|
|
|
|
object <o> Another DOM object
|
|
|
|
@*/
|
2014-08-27 10:06:54 +00:00
|
|
|
replaceWith: function replaceWith($other) {
|
2014-08-21 15:23:33 +00:00
|
|
|
elements.forEach(function(element) {
|
|
|
|
var parent = element.parentNode,
|
|
|
|
sibling = element.nextSibling;
|
|
|
|
if (parent) {
|
|
|
|
parent.removeChild(element);
|
2014-08-27 10:15:34 +00:00
|
|
|
getElements($other).forEach(function(otherElement) {
|
2014-08-21 15:23:33 +00:00
|
|
|
parent.insertBefore(otherElement, sibling);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2013-12-06 09:26:31 +00:00
|
|
|
return this;
|
|
|
|
},
|
2014-09-22 12:34:44 +00:00
|
|
|
|
2013-12-06 09:26:31 +00:00
|
|
|
/*@
|
2014-08-21 15:23:33 +00:00
|
|
|
show <f> Shows all elements
|
|
|
|
() -> This DOM object
|
2012-04-15 12:14:18 +00:00
|
|
|
@*/
|
2014-08-21 15:23:33 +00:00
|
|
|
show: function show() {
|
2014-09-22 12:11:55 +00:00
|
|
|
return this.css({display: previousDisplay || 'block'});
|
2012-04-12 19:21:03 +00:00
|
|
|
},
|
2014-09-22 12:34:44 +00:00
|
|
|
|
2012-04-15 12:14:18 +00:00
|
|
|
/*@
|
2014-08-21 15:23:33 +00:00
|
|
|
siblings <f> Returns all siblings of all elements
|
2014-09-22 12:11:55 +00:00
|
|
|
([selector]) -> <[h]> Siblings
|
|
|
|
selector <s|'*'> CSS selector
|
2013-12-06 09:26:31 +00:00
|
|
|
@*/
|
2014-09-22 12:14:13 +00:00
|
|
|
siblings: function siblings(selector) {
|
2014-09-22 12:11:55 +00:00
|
|
|
var siblings = Ox.unique(elements.map(function(element) {
|
2014-08-21 15:23:33 +00:00
|
|
|
return Ox.filter(
|
|
|
|
element.parentNode.childNodes,
|
|
|
|
function(sibling) {
|
|
|
|
return sibling !== element;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}));
|
2014-09-22 12:11:55 +00:00
|
|
|
return Ox.$(selector ? siblings.filter(function(sibling) {
|
|
|
|
return Ox.$(sibling).is(selector);
|
|
|
|
}) : siblings);
|
2013-12-06 09:26:31 +00:00
|
|
|
},
|
2014-09-22 12:34:44 +00:00
|
|
|
|
2013-12-06 09:26:31 +00:00
|
|
|
/*@
|
2014-08-21 15:23:33 +00:00
|
|
|
some <f> Tests if some elements satisfy a given condition
|
|
|
|
(test) -> True if some elements pass the test
|
|
|
|
test <f> Test function
|
|
|
|
@*/
|
|
|
|
some: function some() {
|
|
|
|
return Array.prototype.some.apply(elements, arguments);
|
|
|
|
},
|
2014-09-22 12:34:44 +00:00
|
|
|
|
2014-08-21 15:23:33 +00:00
|
|
|
/*@
|
|
|
|
text <f> Gets or sets the text contents of all elements
|
2013-12-06 09:26:31 +00:00
|
|
|
() -> <s> The text contents
|
2014-08-21 15:23:33 +00:00
|
|
|
(text) -> <o> This DOM object
|
2013-12-06 09:26:31 +00:00
|
|
|
text <s> The text contents
|
|
|
|
@*/
|
2014-08-21 15:23:33 +00:00
|
|
|
text: function text(string) {
|
|
|
|
var text = '';
|
2013-12-06 09:26:31 +00:00
|
|
|
if (arguments.length == 0) {
|
2014-08-21 15:23:33 +00:00
|
|
|
elements.forEach(function(element) {
|
2014-09-22 12:11:55 +00:00
|
|
|
text += Ox.isString(element.textContent)
|
|
|
|
? element.textContent : element.innerText;
|
2014-08-21 15:23:33 +00:00
|
|
|
});
|
|
|
|
return text;
|
2013-12-06 09:26:31 +00:00
|
|
|
} else {
|
2014-08-21 15:23:33 +00:00
|
|
|
elements.forEach(function(element) {
|
|
|
|
element.empty();
|
|
|
|
element.appendChild(document.createTextNode(string));
|
|
|
|
});
|
|
|
|
return this;
|
2013-12-06 09:26:31 +00:00
|
|
|
}
|
|
|
|
},
|
2014-09-22 12:34:44 +00:00
|
|
|
|
2014-09-22 13:55:29 +00:00
|
|
|
/*@
|
|
|
|
toggle <f> Toggle visibility of all elements
|
|
|
|
() -> This DOM object
|
|
|
|
@*/
|
|
|
|
toggle: function toggle() {
|
|
|
|
return this[
|
|
|
|
Ox.$(element).css('display') == 'none' ? 'show' : 'hide'
|
|
|
|
]();
|
|
|
|
},
|
|
|
|
|
2013-12-06 09:26:31 +00:00
|
|
|
/*@
|
2014-08-21 15:23:33 +00:00
|
|
|
toggleClass <f> Toggles a class name for all elements
|
|
|
|
(className) -> <o> This DOM object
|
2013-01-08 10:51:12 +00:00
|
|
|
className <s> Class name
|
|
|
|
@*/
|
2014-08-21 15:23:33 +00:00
|
|
|
toggleClass: function toggleClass(string) {
|
|
|
|
elements.forEach(function(element) {
|
|
|
|
var $element = Ox.$(element);
|
|
|
|
$element[
|
|
|
|
$element.hasClass(string) ? 'removeClass' : 'addClass'
|
|
|
|
](string);
|
|
|
|
})
|
|
|
|
return this;
|
2013-01-08 10:51:12 +00:00
|
|
|
},
|
2014-09-22 12:34:44 +00:00
|
|
|
|
2013-01-08 10:51:12 +00:00
|
|
|
/*@
|
2012-05-30 20:38:48 +00:00
|
|
|
trigger <f> Triggers an event
|
2014-08-21 15:23:33 +00:00
|
|
|
(event) -> <o> This DOM object
|
2012-05-30 20:38:48 +00:00
|
|
|
@*/
|
2014-08-21 15:23:33 +00:00
|
|
|
trigger: function trigger(event) {
|
|
|
|
elements.forEach(function(element) {
|
|
|
|
var e = document.createEvent('MouseEvents');
|
|
|
|
e.initEvent(event, true, true);
|
|
|
|
element.dispatchEvent(e);
|
|
|
|
});
|
2012-05-30 20:38:48 +00:00
|
|
|
return this;
|
|
|
|
},
|
2014-09-22 12:34:44 +00:00
|
|
|
|
2012-05-30 20:38:48 +00:00
|
|
|
/*@
|
2014-08-21 15:23:33 +00:00
|
|
|
val <f> Gets the value of the first or sets the value of all elements
|
2012-04-15 12:14:18 +00:00
|
|
|
() -> <s> Value
|
2014-08-21 15:23:33 +00:00
|
|
|
(value) -> <o> This DOM object
|
2012-04-15 12:14:18 +00:00
|
|
|
value <s> Value
|
|
|
|
@*/
|
2014-08-21 15:23:33 +00:00
|
|
|
val: function val(value) {
|
2012-04-14 09:45:19 +00:00
|
|
|
var ret;
|
|
|
|
if (arguments.length == 0) {
|
2014-08-21 15:23:33 +00:00
|
|
|
return elements[0].value;
|
2012-04-14 09:45:19 +00:00
|
|
|
} else {
|
2014-08-21 15:23:33 +00:00
|
|
|
elements.forEach(function(element) {
|
|
|
|
element.value = value;
|
|
|
|
});
|
|
|
|
return this;
|
2012-04-14 09:45:19 +00:00
|
|
|
}
|
|
|
|
},
|
2014-09-22 12:34:44 +00:00
|
|
|
|
2012-04-15 12:14:18 +00:00
|
|
|
/*@
|
2014-08-21 15:23:33 +00:00
|
|
|
width <f> Returns the width of the first element
|
2012-04-15 12:14:18 +00:00
|
|
|
() -> <n> Width in px
|
|
|
|
@*/
|
2014-08-21 15:23:33 +00:00
|
|
|
width: function width() {
|
|
|
|
return elements[0].offsetWidth;
|
2011-10-07 01:04:47 +00:00
|
|
|
}
|
2014-09-22 12:34:44 +00:00
|
|
|
|
2014-08-21 15:23:33 +00:00
|
|
|
}) : null;
|
2013-12-04 22:43:07 +00:00
|
|
|
|
2012-05-22 23:17:17 +00:00
|
|
|
};
|
2012-05-27 21:17:40 +00:00
|
|
|
|
|
|
|
/*@
|
|
|
|
Ox.canvas <function> Generic canvas object
|
2012-06-02 11:06:44 +00:00
|
|
|
Returns an object with the properties: `canvas`, `context`, `data` and
|
|
|
|
`imageData`.
|
2012-07-05 17:47:57 +00:00
|
|
|
(width, height) -> <o> canvas
|
|
|
|
(image) -> <o> canvas
|
|
|
|
width <n> Width in px
|
2012-05-27 21:17:40 +00:00
|
|
|
height <n> Height in px
|
2012-07-05 17:47:57 +00:00
|
|
|
image <e> Image object
|
2012-05-27 21:17:40 +00:00
|
|
|
@*/
|
|
|
|
Ox.canvas = function() {
|
|
|
|
var c = {}, isImage = arguments.length == 1,
|
|
|
|
image = isImage ? arguments[0] : {
|
|
|
|
width: arguments[0], height: arguments[1]
|
|
|
|
};
|
|
|
|
c.context = (c.canvas = Ox.$('<canvas>').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 <function> Calls a callback function once the DOM is ready
|
|
|
|
(callback) -> <b> If true, the document was ready
|
|
|
|
callback <f> Callback function
|
|
|
|
@*/
|
|
|
|
Ox.documentReady = (function() {
|
|
|
|
var callbacks = [];
|
2012-07-05 17:05:52 +00:00
|
|
|
document.onreadystatechange = window.onload = function() {
|
2012-05-27 21:17:40 +00:00
|
|
|
if (document.readyState == 'complete') {
|
|
|
|
callbacks.forEach(function(callback) {
|
|
|
|
callback();
|
|
|
|
});
|
2012-07-05 17:05:52 +00:00
|
|
|
document.onreadystatechange = window.onload = null;
|
2012-05-27 21:17:40 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
return function(callback) {
|
|
|
|
if (document.readyState == 'complete') {
|
|
|
|
callback();
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
callbacks.push(callback);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}());
|