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'
|
|
|
|
> 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()
|
|
|
|
''
|
|
|
|
> Ox.$('<input>').val('red').val()
|
|
|
|
'red'
|
2012-05-30 20:38:48 +00:00
|
|
|
> !!Ox.$('<div>').on({click: function(e) { Ox.test(e.type, 'click'); }}).trigger('click')
|
|
|
|
true
|
2011-10-07 01:04:47 +00:00
|
|
|
@*/
|
2012-05-25 07:52:57 +00:00
|
|
|
Ox.$ = Ox.element = function(value) {
|
2013-12-04 22:43:07 +00:00
|
|
|
|
2013-12-06 20:54:26 +00:00
|
|
|
var data = {},
|
|
|
|
element = !Ox.isString(value) ? value // window, document or element
|
2013-12-04 22:43:07 +00:00
|
|
|
: 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],
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-04-05 15:30:00 +00:00
|
|
|
return element ? {
|
2012-05-30 22:47:57 +00:00
|
|
|
//@ 0 <h> The DOM element itself
|
2012-04-05 15:30:00 +00:00
|
|
|
0: element,
|
2011-10-07 01:04:47 +00:00
|
|
|
/*@
|
|
|
|
addClass <f> Adds a class name
|
|
|
|
(className) -> <o> This element
|
|
|
|
className <s> Class name
|
|
|
|
@*/
|
2012-05-25 07:52:57 +00:00
|
|
|
addClass: function(string) {
|
2012-04-15 12:14:18 +00:00
|
|
|
this[0].className = Ox.unique(((
|
|
|
|
this[0].className ? this[0].className + ' ' : ''
|
2012-05-25 07:52:57 +00:00
|
|
|
) + Ox.clean(string)).split(' ')).join(' ');
|
2011-10-07 01:04:47 +00:00
|
|
|
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
|
|
|
append <f> Appends one or more elements to this element
|
2013-11-29 21:19:01 +00:00
|
|
|
(element[, element[, ...]]) -> <o> This element
|
|
|
|
element <o> Another element
|
2011-10-07 01:04:47 +00:00
|
|
|
@*/
|
2012-04-15 12:14:18 +00:00
|
|
|
append: function() {
|
|
|
|
var that = this;
|
2013-11-29 21:19:01 +00:00
|
|
|
Ox.slice(arguments).forEach(function($element) {
|
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
|
|
|
that[0].appendChild($element[0]);
|
2012-04-15 12:14:18 +00:00
|
|
|
});
|
2011-10-07 01:04:47 +00:00
|
|
|
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
|
|
|
appendTo <f> Appends this element object to another element object
|
2011-10-07 01:04:47 +00:00
|
|
|
(element) -> <o> This element
|
|
|
|
element <o> Another element
|
|
|
|
@*/
|
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
|
|
|
appendTo: function($element) {
|
|
|
|
$element[0].appendChild(this[0]);
|
2011-10-07 01:04:47 +00:00
|
|
|
return this;
|
|
|
|
},
|
|
|
|
/*@
|
|
|
|
attr <f> Gets or sets an attribute
|
2012-04-15 12:14:18 +00:00
|
|
|
(key) -> <s> Value
|
|
|
|
(key, value) -> <o> This element
|
2013-12-06 20:59:26 +00:00
|
|
|
({key0: value0, key1: value1, ...}) -> <o> This element
|
|
|
|
key <s> Attribute name
|
|
|
|
value <s> Attribute value
|
2011-10-07 01:04:47 +00:00
|
|
|
@*/
|
|
|
|
attr: function() {
|
|
|
|
var ret, that = this;
|
|
|
|
if (arguments.length == 1 && Ox.isString(arguments[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
|
|
|
|
? this[0].getAttribute(arguments[0])
|
|
|
|
: null;
|
|
|
|
// fixme: why exactly is this needed?
|
2012-05-19 09:16:04 +00:00
|
|
|
if (ret === null) {
|
|
|
|
ret = void 0;
|
|
|
|
}
|
2011-10-07 01:04:47 +00:00
|
|
|
} else {
|
2012-05-25 07:52:57 +00:00
|
|
|
Ox.forEach(Ox.makeObject(arguments), function(value, key) {
|
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
|
|
|
that[0].setAttribute && that[0].setAttribute(key, value);
|
2011-10-07 01:04:47 +00:00
|
|
|
});
|
|
|
|
ret = this;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
},
|
|
|
|
/*@
|
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
|
|
|
children <f> Returns the children of this element
|
2013-12-06 09:26:31 +00:00
|
|
|
() -> <[h]> 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
|
|
|
@*/
|
|
|
|
children: function() {
|
|
|
|
return Ox.slice(this[0].childNodes);
|
|
|
|
},
|
|
|
|
/*@
|
2011-10-07 01:04:47 +00:00
|
|
|
css <f> Gets or sets a CSS attribute
|
2012-04-15 12:14:18 +00:00
|
|
|
(key) -> <s> Value
|
|
|
|
(key, value) -> <o> This element
|
|
|
|
({key0: value0, key1: value1, ...}) -> <o> This element
|
2013-12-06 20:59:26 +00:00
|
|
|
key <s> Attribute name
|
|
|
|
value <s> Attribute value
|
2011-10-07 01:04:47 +00:00
|
|
|
@*/
|
|
|
|
css: function() {
|
|
|
|
var ret, that = this;
|
|
|
|
if (arguments.length == 1 && Ox.isString(arguments[0])) {
|
|
|
|
ret = this[0].style[arguments[0]];
|
|
|
|
} else {
|
2012-05-25 07:52:57 +00:00
|
|
|
Ox.forEach(Ox.makeObject(arguments), function(value, key) {
|
|
|
|
that[0].style[key] = value;
|
2011-10-07 01:04:47 +00:00
|
|
|
});
|
|
|
|
ret = this;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
},
|
2013-12-06 20:59:26 +00:00
|
|
|
/*@
|
|
|
|
data <f> Gets or sets data
|
|
|
|
() -> <o> All data
|
|
|
|
(key) -> <s> Value
|
|
|
|
(key, value) -> <o> This element
|
|
|
|
({key0: value0, key1: value1, ...}) -> <o> This element
|
|
|
|
key <s> Property
|
|
|
|
value <*> Value
|
|
|
|
@*/
|
2013-12-06 20:54:26 +00:00
|
|
|
data: function() {
|
|
|
|
var ret;
|
|
|
|
if (arguments.length == 0) {
|
|
|
|
ret = data;
|
|
|
|
} else if (arguments.length == 1 && Ox.isString(arguments[0])) {
|
2014-08-19 08:18:56 +00:00
|
|
|
ret = data[arguments[0]];
|
2013-12-06 20:54:26 +00:00
|
|
|
} else {
|
2013-12-06 20:55:45 +00:00
|
|
|
Ox.forEach(Ox.makeObject(arguments), function(value, key) {
|
2013-12-06 20:54:26 +00:00
|
|
|
data[key] = value;
|
|
|
|
});
|
|
|
|
ret = this;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
},
|
2012-04-15 12:14:18 +00:00
|
|
|
/*@
|
|
|
|
empty <f> Empties the inner HTML
|
|
|
|
() -> <o> This element
|
|
|
|
@*/
|
2012-04-05 15:30:00 +00:00
|
|
|
empty: function() {
|
2012-04-12 19:21:03 +00:00
|
|
|
return this.html('');
|
2012-04-05 15:30:00 +00:00
|
|
|
},
|
2011-10-07 01:04:47 +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
|
|
|
find <f> Find descendant elements
|
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
|
|
|
|
@*/
|
|
|
|
find: function(string) {
|
|
|
|
return Ox.slice(this[0].querySelectorAll(string || '*'));
|
|
|
|
},
|
|
|
|
/*@
|
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
|
|
|
|
@*/
|
2012-05-25 07:52:57 +00:00
|
|
|
hasClass: function(string) {
|
|
|
|
return this[0].className.split(' ').indexOf(string) > -1;
|
2011-10-07 01:04:47 +00:00
|
|
|
},
|
2012-04-15 12:14:18 +00:00
|
|
|
/*@
|
|
|
|
height <f> Returns the height of this element
|
|
|
|
() -> <n> Height in px
|
|
|
|
@*/
|
2012-04-12 19:21:03 +00:00
|
|
|
height: function() {
|
|
|
|
return this[0].offsetHeight;
|
|
|
|
},
|
2012-04-15 12:14:18 +00:00
|
|
|
/*@
|
|
|
|
hide <f> Hides this element
|
|
|
|
() -> <o> This element
|
|
|
|
@*/
|
2012-04-12 19:21:03 +00:00
|
|
|
hide: function() {
|
|
|
|
return this.css({display: 'none'});
|
|
|
|
},
|
2011-10-07 01:04:47 +00:00
|
|
|
/*@
|
|
|
|
html <f> Gets or sets the inner HTML
|
2012-04-15 12:14:18 +00:00
|
|
|
() -> <s> The inner HTML
|
2011-10-07 01:04:47 +00:00
|
|
|
(html) -> <o> This element
|
|
|
|
html <s> The inner HTML
|
|
|
|
@*/
|
2012-05-25 07:52:57 +00:00
|
|
|
html: function(string) {
|
2011-10-07 01:04:47 +00:00
|
|
|
var ret;
|
2012-05-25 07:52:57 +00:00
|
|
|
if (arguments.length == 0) {
|
2011-10-07 01:04:47 +00:00
|
|
|
ret = this[0].innerHTML;
|
|
|
|
} else {
|
2012-05-25 07:53:55 +00:00
|
|
|
this[0].innerHTML = string;
|
2011-10-07 01:04:47 +00:00
|
|
|
ret = this;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
},
|
|
|
|
/*@
|
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
|
|
|
insertAfter <f> Inserts this element after another element
|
|
|
|
(element) -> <o> This element
|
|
|
|
element <o> Another element
|
|
|
|
@*/
|
|
|
|
insertAfter: function($element) {
|
|
|
|
$element[0].parentNode.insertBefore(this[0], $element[0].nextSibling);
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
/*@
|
|
|
|
insertBefore <f> Inserts this element before another element
|
|
|
|
(element) -> <o> This element
|
|
|
|
element <o> Another element
|
|
|
|
@*/
|
|
|
|
insertBefore: function($element) {
|
|
|
|
$element[0].parentNode.insertBefore(this[0], $element[0]);
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
/*@
|
|
|
|
next <f> Returns the sibling after this element
|
2013-12-06 09:26:31 +00:00
|
|
|
() -> <h> Next element
|
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
|
|
|
@*/
|
|
|
|
next: function() {
|
|
|
|
return this[0].nextSibling;
|
|
|
|
},
|
|
|
|
/*@
|
|
|
|
nextAll <f> Returns all siblings after this element
|
2013-12-06 09:26:31 +00:00
|
|
|
() -> <[h]> Next 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
|
|
|
@*/
|
|
|
|
nextAll: function() {
|
2013-12-06 09:26:31 +00:00
|
|
|
var sibling = this[0], 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
|
|
|
while (true) {
|
2014-08-19 08:18:56 +00:00
|
|
|
sibling = sibling.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
|
|
|
if (!sibling) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
siblings.push(sibling);
|
|
|
|
}
|
|
|
|
return siblings;
|
|
|
|
},
|
|
|
|
/*@
|
|
|
|
off <f> Unbinds a callback from an event
|
|
|
|
(event) -> <o> This element (unbinds all callbacks)
|
|
|
|
(event, callback) -> <o> This element
|
|
|
|
({event0: callback0, event1: callback1, ...}) -> <o> This element
|
|
|
|
event <s> Event name
|
|
|
|
callback <f> Callback function
|
|
|
|
@*/
|
|
|
|
off: function(event, callback) {
|
|
|
|
var that = this;
|
2013-12-04 22:43:07 +00:00
|
|
|
Ox.forEach(normalizeEvents(arguments), function(callback, event) {
|
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
|
|
|
if (callback) {
|
|
|
|
that[0].removeEventListener(event, callback, false);
|
|
|
|
} else {
|
|
|
|
that[0]['on' + event] = null;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
/*@
|
2012-05-28 13:53:10 +00:00
|
|
|
on <f> Binds a callback to an event
|
|
|
|
(event, callback) -> <o> This element
|
|
|
|
({event0: callback0, event1: callback1, ...}) -> <o> This element
|
|
|
|
event <s> Event name
|
|
|
|
callback <f> Callback function
|
|
|
|
e <o> Event properties
|
|
|
|
@*/
|
|
|
|
on: function() {
|
|
|
|
var that = this;
|
2013-12-04 22:43:07 +00:00
|
|
|
Ox.forEach(normalizeEvents(arguments), function(callback, event) {
|
2012-07-05 17:50:01 +00:00
|
|
|
that[0].addEventListener(event, callback, false);
|
2012-05-28 13:53:10 +00:00
|
|
|
});
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
/*@
|
2012-04-15 12:14:18 +00:00
|
|
|
one <f> Binds a callback to an event and unbinds it on first invocation
|
|
|
|
(event, callback) -> <o> This element
|
|
|
|
({event0: callback0, event1: callback1, ...}) -> <o> This element
|
|
|
|
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
|
|
|
@*/
|
2012-04-15 12:14:18 +00:00
|
|
|
one: function(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;
|
|
|
|
},
|
2012-04-15 12:14:18 +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
|
|
|
parent <f> Returns the parent of this element
|
2013-12-06 09:26:31 +00:00
|
|
|
() -> <h> Parent element
|
2012-05-28 13:53:10 +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
|
|
|
parent: function() {
|
|
|
|
return this[0].parentNode;
|
|
|
|
},
|
|
|
|
/*@
|
|
|
|
parents <f> Returns all ancestors of this element
|
2013-12-06 09:26:31 +00:00
|
|
|
() -> <[h]> Ancestor 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
|
|
|
@*/
|
|
|
|
parents: function() {
|
2013-12-06 09:26:31 +00:00
|
|
|
var parent = this[0], 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
|
|
|
while (true) {
|
2014-08-19 08:18:56 +00:00
|
|
|
parent = parent.parentNode;
|
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
|
|
|
if (!parent) {
|
|
|
|
break;
|
2012-05-28 13:53:10 +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
|
|
|
parents.unshift(parent);
|
|
|
|
}
|
|
|
|
return parents;
|
|
|
|
},
|
|
|
|
/*@
|
|
|
|
prepend <f> Prepends one or more elements to this element
|
2013-11-29 21:19:01 +00:00
|
|
|
(element[, element[, ...]]) -> <o> This element
|
|
|
|
element <o> Another element
|
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
|
|
|
@*/
|
|
|
|
prepend: function() {
|
|
|
|
var parent = this[0].parentNode;
|
2013-11-29 21:19:01 +00:00
|
|
|
Ox.slice(arguments).reverse().forEach(function($element) {
|
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
|
|
|
parent.insertBefore($element[0], parent.firstChild);
|
2012-05-28 13:53:10 +00:00
|
|
|
});
|
|
|
|
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
|
|
|
prependTo <f> Prepends this element object to another element object
|
|
|
|
(element) -> <o> This element
|
|
|
|
element <o> Another element
|
|
|
|
@*/
|
|
|
|
prependTo: function($element) {
|
|
|
|
var element = $element[0];
|
|
|
|
element.insertBefore(this[0], element.firstChild);
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
/*@
|
|
|
|
prev <f> Returns the sibling before this element
|
2013-12-06 09:26:31 +00:00
|
|
|
() -> <h> Next element
|
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
|
|
|
@*/
|
|
|
|
prev: function() {
|
|
|
|
return this[0].previousSibling;
|
|
|
|
},
|
|
|
|
/*@
|
|
|
|
prevAll <f> Returns all siblings before this element
|
2013-12-06 09:26:31 +00:00
|
|
|
() -> <[h]> Next 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
|
|
|
@*/
|
|
|
|
prevAll: function() {
|
2013-12-06 09:26:31 +00:00
|
|
|
var sibling = this[0], 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
|
|
|
while (true) {
|
2014-08-19 08:18:56 +00:00
|
|
|
sibling = sibling.previousSibling;
|
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
|
|
|
if (!sibling) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
siblings.unshift(sibling);
|
|
|
|
}
|
|
|
|
return siblings;
|
|
|
|
},
|
|
|
|
/*@
|
2012-04-15 12:14:18 +00:00
|
|
|
remove <f> Removes this element from the DOM
|
|
|
|
() -> <o> This element
|
|
|
|
@*/
|
2012-04-06 23:45:51 +00:00
|
|
|
remove: function() {
|
|
|
|
this[0].parentNode.removeChild(this[0]);
|
|
|
|
return this;
|
|
|
|
},
|
2011-10-07 01:04:47 +00:00
|
|
|
/*@
|
|
|
|
removeAttr <f> Removes an attribute
|
|
|
|
(key) -> <o> This element
|
2012-04-15 12:14:18 +00:00
|
|
|
([key0, key1, ...]) -> <o> This element
|
2011-10-07 01:04:47 +00:00
|
|
|
key <s> The attribute
|
|
|
|
@*/
|
2012-04-15 12:14:18 +00:00
|
|
|
removeAttr: function() {
|
2012-05-19 09:16:04 +00:00
|
|
|
var that = this;
|
2012-05-19 08:40:59 +00:00
|
|
|
Ox.makeArray(arguments[0]).forEach(function(key) {
|
2012-05-19 09:16:04 +00:00
|
|
|
that[0].removeAttribute(key);
|
2012-04-15 12:14:18 +00:00
|
|
|
});
|
2011-10-07 01:04:47 +00:00
|
|
|
return this;
|
|
|
|
},
|
|
|
|
/*@
|
|
|
|
removeClass <f> Removes a class name
|
|
|
|
(className) -> <o> This element
|
|
|
|
className <s> Class name
|
|
|
|
@*/
|
2012-05-25 07:52:57 +00:00
|
|
|
removeClass: function(string) {
|
|
|
|
var array = Ox.clean(string).split(' ');
|
2012-04-15 12:14:18 +00:00
|
|
|
this[0].className = this[0].className.split(' ').filter(
|
|
|
|
function(className) {
|
2012-05-25 07:52:57 +00:00
|
|
|
return array.indexOf(className) == -1;
|
2011-10-07 01:04:47 +00:00
|
|
|
}
|
|
|
|
).join(' ');
|
|
|
|
return this;
|
2012-04-05 15:30:00 +00:00
|
|
|
},
|
2012-04-15 12:14:18 +00:00
|
|
|
/*@
|
2013-12-06 09:26:31 +00:00
|
|
|
replace <f> Replaces another element with this element
|
|
|
|
(element) -> <o> This element
|
|
|
|
element <o> Another element
|
|
|
|
@*/
|
|
|
|
replace: function($element) {
|
|
|
|
var next = $element[0].nextSibling, parent = $element[0].parentNode;
|
|
|
|
$element.remove();
|
|
|
|
parent.insertBefore(this[0], next);
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
/*@
|
|
|
|
replaceWith <f> Replaces this element with another element
|
|
|
|
(element) -> <o> This element
|
|
|
|
element <o> Another element
|
|
|
|
@*/
|
|
|
|
replaceWith: function($element) {
|
|
|
|
var next = this[0].nextSibling, parent = this[0].parentNode;
|
|
|
|
this.remove();
|
|
|
|
parent.insertBefore($element[0], next);
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
/*@
|
|
|
|
show <f> Shows this element
|
2012-04-15 12:14:18 +00:00
|
|
|
() -> This element
|
|
|
|
@*/
|
2012-04-12 19:21:03 +00:00
|
|
|
show: function() {
|
|
|
|
return this.css({display: 'block'});
|
|
|
|
},
|
2012-04-15 12:14:18 +00:00
|
|
|
/*@
|
2013-12-06 09:26:31 +00:00
|
|
|
siblings <f> Returns all siblings of this element
|
|
|
|
() -> <[oh]> Sibling elements
|
|
|
|
@*/
|
|
|
|
siblings: function() {
|
|
|
|
var that = this;
|
|
|
|
return Ox.filter(this[0].parentNode.childNodes, function(element) {
|
|
|
|
return element !== that[0];
|
|
|
|
});
|
|
|
|
},
|
|
|
|
/*@
|
|
|
|
text <f> Gets or sets the text contents
|
|
|
|
() -> <s> The text contents
|
|
|
|
(text) -> <o> This element
|
|
|
|
text <s> The text contents
|
|
|
|
@*/
|
2014-08-05 00:27:34 +00:00
|
|
|
text: function(string) {
|
2013-12-06 09:26:31 +00:00
|
|
|
var ret;
|
|
|
|
if (arguments.length == 0) {
|
|
|
|
ret = Ox.isString(this.textContent)
|
|
|
|
? this.textContent : this.innerText;
|
|
|
|
} else {
|
2014-08-18 17:10:25 +00:00
|
|
|
this.empty()[0].appendChild(document.createTextNode(string));
|
|
|
|
ret = this;
|
2013-12-06 09:26:31 +00:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
},
|
|
|
|
/*@
|
2013-01-08 10:51:12 +00:00
|
|
|
toggleClass <f> Toggles a class name
|
|
|
|
(className) -> <o> This element
|
|
|
|
className <s> Class name
|
|
|
|
@*/
|
|
|
|
toggleClass: function(string) {
|
|
|
|
return this[
|
|
|
|
this.hasClass(string) ? 'removeClass' : 'addClass'
|
|
|
|
](string);
|
|
|
|
},
|
|
|
|
/*@
|
2012-05-30 20:38:48 +00:00
|
|
|
trigger <f> Triggers an event
|
2012-05-30 22:47:57 +00:00
|
|
|
(event) -> <o> This element
|
2012-05-30 20:38:48 +00:00
|
|
|
@*/
|
|
|
|
trigger: function(event) {
|
|
|
|
var e = document.createEvent('MouseEvents');
|
|
|
|
e.initEvent(event, true, true);
|
|
|
|
this[0].dispatchEvent(e);
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
/*@
|
2012-04-15 12:14:18 +00:00
|
|
|
val <f> Gets or sets the value property of this element
|
|
|
|
() -> <s> Value
|
|
|
|
(value) -> <o> This element
|
|
|
|
value <s> Value
|
|
|
|
@*/
|
2012-05-25 07:52:57 +00:00
|
|
|
val: function(value) {
|
2012-04-14 09:45:19 +00:00
|
|
|
var ret;
|
|
|
|
if (arguments.length == 0) {
|
2012-05-30 22:47:57 +00:00
|
|
|
ret = this[0].value;
|
2012-04-14 09:45:19 +00:00
|
|
|
} else {
|
2012-05-25 07:52:57 +00:00
|
|
|
this[0].value = value;
|
2012-04-14 09:45:19 +00:00
|
|
|
ret = this;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
},
|
2012-04-15 12:14:18 +00:00
|
|
|
/*@
|
|
|
|
width <f> Returns the width of this element
|
|
|
|
() -> <n> Width in px
|
|
|
|
@*/
|
2012-04-12 19:21:03 +00:00
|
|
|
width: function() {
|
|
|
|
return this[0].offsetWidth;
|
2011-10-07 01:04:47 +00:00
|
|
|
}
|
2012-04-05 15:30:00 +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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}());
|