oxjs/source/Ox/js/DOM.js

756 lines
26 KiB
JavaScript
Raw Normal View History

2011-11-05 16:46:53 +00:00
'use strict';
/*@
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`
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')
true
2012-04-15 19:12:48 +00:00
> Ox.$('<div>').addClass('red').removeClass('red').hasClass('red')
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')
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'
> !!Ox.$('<div>').on({click: function(e) { Ox.test(e.type, 'click'); }}).trigger('click')
true
@*/
2012-05-25 07:52:57 +00:00
Ox.$ = Ox.element = function(value) {
2013-12-04 22:43:07 +00:00
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)),
2013-12-04 22:43:07 +00:00
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) {
if (includeParent) {
removeOxElement(parent);
} else {
2014-08-21 15:55:42 +00:00
Ox.$(parent).find('.OxElement').forEach(removeOxElement);
}
function removeOxElement(element) {
2014-08-21 15:55:42 +00:00
var $element = Ox.getOxElement(element);
$element && $element.removeElement();
}
}
return elements.length ? Ox.extend(
Ox.zipObject(Ox.range(elements.length), elements
), {
/*@
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;
},
/*@
addClass <f> Adds a class name to all elements
(className) -> <o> This DOM object
className <s> 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 <f> Appends one or more DOM objects to this DOM object
(object[, object[, ...]]) -> <o> This DOM object
element <o> 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);
});
});
2012-04-15 12:14:18 +00:00
});
return this;
},
/*@
appendTo <f> Appends this DOM object to another DOM object
(object) -> <o> This DOM object
object <o> Another DOM object
@*/
appendTo: function appendTo($other) {
$other.forEach(function(otherElement) {
elements.forEach(function(element) {
otherElement.appendChild(element);
});
});
return this;
},
/*@
attr <f> Gets or sets an attribute for all elements
2012-04-15 12:14:18 +00:00
(key) -> <s> Value
(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
@*/
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 <f> Returns the unique list of children of all elements
() -> <[h]> Children
@*/
children: function children() {
return Ox.unique(Ox.flatten(elements.map(function(element) {
2014-08-21 15:55:42 +00:00
return element.childNodes;
})));
},
/*@
css <f> Gets or sets a CSS attribute for all elements
2012-04-15 12:14:18 +00:00
(key) -> <s> Value
(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
@*/
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;
}
},
2013-12-06 20:59:26 +00:00
/*@
data <f> Gets or sets data
() -> <o> All data
(key) -> <s> Value
(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
@*/
data: function data() {
if (arguments.length == 0) {
return data;
} else if (arguments.length == 1 && Ox.isString(arguments[0])) {
return data[arguments[0]];
} else {
2013-12-06 20:55:45 +00:00
Ox.forEach(Ox.makeObject(arguments), function(value, key) {
data[key] = value;
});
return this;
}
},
2012-04-15 12:14:18 +00:00
/*@
elements <f> Returns all elements
() -> <[h]> All elements
2012-04-15 12:14:18 +00:00
@*/
elements: function elements() {
return elements;
},
/*@
empty <f> Empties the inner HTML of all elements
() -> <o> This DOM object
@*/
empty: function empty() {
return this.html('');
2012-04-05 15:30:00 +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);
},
/*@
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);
},
/*@
find <f> Find all descendant elements matching a CSS selector
([selector]) -> <[h]> Elements
selector <s|'*'> CSS selector
@*/
find: function find(string) {
return Ox.unique(elements.map(function(element) {
2014-08-21 15:55:42 +00:00
return element.querySelectorAll(string || '*');
}));
},
/*@
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;
},
/*@
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
className <s> Class name
@*/
hasClass: function hasClass(string) {
return elements.any(function(element) {
return Ox.contains(element.className.split(' '), string);
});
},
2012-04-15 12:14:18 +00:00
/*@
height <f> Returns the height of the first element
2012-04-15 12:14:18 +00:00
() -> <n> Height in px
@*/
height: function height() {
return elements[0].offsetHeight;
},
2012-04-15 12:14:18 +00:00
/*@
hide <f> Hides all elements
() -> <o> This DOM object
2012-04-15 12:14:18 +00:00
@*/
hide: function hide() {
return this.css({display: 'none'});
},
/*@
html <f> Gets or sets the innerHTML of all elements
2012-04-15 12:14:18 +00:00
() -> <s> The inner HTML
(html) -> <o> This DOM object
html <s> The inner HTML
@*/
html: function html(string) {
var html = '';
2012-05-25 07:52:57 +00:00
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 <f> Inserts this DOM object after another DOM object
(object) -> <o> This DOM object
object <o> 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 <f> Inserts this DOM object before another DOM object
(object) -> <o> This DOM object
object <o> Another DOM object
@*/
insertBefore: function insertBefore($other) {
elements.forEach(function(element) {
$other[0].parentNode.insertBefore(element, $other[0]);
});
return this;
},
/*@
is <f> Tests if any element matches a CSS selector
(selector) -> <b> True if the element matches the selector
selector <s> CSS selector
@*/
is: function is(string) {
return elements.some(function(element) {
return Ox.contains(
(element.parentNode || document).querySelectorAll(string),
element
);
});
},
/*@
length <n> Number of elements
@*/
length: elements.length,
/*@
map <f> Transforms all elements
(iterator) -> [] Transformed elements
iterator <f> Iterator function
2014-08-21 11:05:41 +00:00
@*/
map: function map() {
return Array.prototype.filter.map(elements, arguments);
2014-08-21 11:05:41 +00:00
},
/*@
next <f> 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 <f> 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 <f> Unbinds a callback from an event
(event) -> <o> This DOM object (unbinds all callbacks)
(event, callback) -> <o> This DOM object
({event0: callback0, event1: callback1, ...}) -> <o> This DOM object
event <s> Event name
callback <f> 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 <f> Binds a callback to an event
(event, callback) -> <o> This DOM object
({event0: callback0, event1: callback1, ...}) -> <o> This DOM object
event <s> Event name
callback <f> Callback function
e <o> 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;
},
/*@
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 DOM object
({event0: callback0, event1: callback1, ...}) -> <o> This DOM object
2012-04-15 12:14:18 +00:00
event <s> Event name
callback <f> Callback function
2012-04-15 12:14:18 +00:00
e <o> Event properties
@*/
one: function one(events) {
var args = Ox.slice(arguments), that = this;
2013-12-04 22:43:07 +00:00
Ox.forEach(normalizeEvents(arguments), function(callback, event) {
that.on(event, function fn() {
that.off(event, fn);
return callback.apply(that, args);
2012-04-15 12:14:18 +00:00
});
});
return this;
},
2012-04-15 12:14:18 +00:00
/*@
parent <f> 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 <f> 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 <f> Prepends one or more DOM objects to this DOM object
(object[, object[, ...]]) -> <o> DOM object
object <o> 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 <f> Prepends this DOM object to another DOM object
(object) -> <o> This DOM object
object <o> 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 <f> 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 <f> 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 <f> Applies `reduce` to all elements
2012-04-15 12:14:18 +00:00
@*/
reduce: function reduce() {
return Array.prototype.reduce.apply(elements, arguments);
},
/*@
remove <f> Removes all element from the DOM
() -> <o> This DOM object
@*/
remove: function remove() {
elements.forEach(function(element) {
if (element.parentNode) {
removeOxElements(element, true);
element.parentNode.removeChild(element);
}
});
2012-04-06 23:45:51 +00:00
return this;
},
/*@
removeAttr <f> Removes an attribute from all elements
(key) -> <o> This DOM object
([key0, key1, ...]) -> <o> This DOM object
key <s> The attribute
@*/
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
});
return this;
},
/*@
removeClass <f> Removes a class name from all elements
(className) -> <o> This DOM object
className <s> 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;
2012-04-05 15:30:00 +00:00
},
2012-04-15 12:14:18 +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) {
$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 <f> Replaces this DOM object with another DOM object
(object) -> <o> This DOM object
object <o> 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 <f> Shows all elements
() -> This DOM object
2012-04-15 12:14:18 +00:00
@*/
show: function show() {
return this.css({display: 'block'});
},
2012-04-15 12:14:18 +00:00
/*@
siblings <f> 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 <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);
},
/*@
text <f> Gets or sets the text contents of all elements
() -> <s> The text contents
(text) -> <o> This DOM object
text <s> 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 <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
@*/
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
},
/*@
trigger <f> Triggers an event
(event) -> <o> 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 <f> Gets the value of the first or sets the value of all elements
2012-04-15 12:14:18 +00:00
() -> <s> Value
(value) -> <o> This DOM object
2012-04-15 12:14:18 +00:00
value <s> Value
@*/
val: function val(value) {
2012-04-14 09:45:19 +00:00
var ret;
if (arguments.length == 0) {
return elements[0].value;
2012-04-14 09:45:19 +00:00
} else {
elements.forEach(function(element) {
element.value = value;
});
return this;
2012-04-14 09:45:19 +00:00
}
},
2012-04-15 12:14:18 +00:00
/*@
width <f> Returns the width of the first element
2012-04-15 12:14:18 +00:00
() -> <n> Width in px
@*/
width: function width() {
return elements[0].offsetWidth;
}
}) : null;
2013-12-04 22:43:07 +00:00
};
2012-05-27 21:17:40 +00:00
/*@
Ox.canvas <function> Generic canvas object
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;
}
};
}());