Ox.$: support forEach over jQuery-wrapped elements

This commit is contained in:
rolux 2014-08-27 12:15:34 +02:00
parent f524e612c7
commit 2bc7b80f0d

View file

@ -38,6 +38,13 @@ Ox.$ = Ox.element = function $(value) {
originalMousewheelEvents = 'onwheel' in document ? ['wheel'] originalMousewheelEvents = 'onwheel' in document ? ['wheel']
: ['mousewheel', 'DOMMouseScroll', 'MozMousePixelScroll']; : ['mousewheel', 'DOMMouseScroll', 'MozMousePixelScroll'];
function getElements($other) {
return $other.forEach ? $other
: Ox.range($other.length).map(function(index) {
return $other[index];
});
}
function normalizeEvents(args) { function normalizeEvents(args) {
var ret = {}; var ret = {};
Ox.forEach(Ox.makeObject(args), function(callback, event) { Ox.forEach(Ox.makeObject(args), function(callback, event) {
@ -101,7 +108,7 @@ Ox.$ = Ox.element = function $(value) {
var $others = Ox.slice(arguments); var $others = Ox.slice(arguments);
elements.forEach(function(element) { elements.forEach(function(element) {
$others.forEach(function($other) { $others.forEach(function($other) {
$other.forEach(function(otherElement) { getElements($other).forEach(function(otherElement) {
element.appendChild(otherElement); element.appendChild(otherElement);
}); });
}); });
@ -114,10 +121,7 @@ Ox.$ = Ox.element = function $(value) {
object <o> Another DOM object object <o> Another DOM object
@*/ @*/
appendTo: function appendTo($other) { appendTo: function appendTo($other) {
//FIXME: temp fix getElements($other).forEach(function(otherElement) {
$other[0].appendChild(this[0]);
return this;
$other.forEach(function(otherElement) {
elements.forEach(function(element) { elements.forEach(function(element) {
otherElement.appendChild(element); otherElement.appendChild(element);
}); });
@ -466,7 +470,7 @@ Ox.$ = Ox.element = function $(value) {
elements.forEach(function(element) { elements.forEach(function(element) {
var parent = element.parentNode; var parent = element.parentNode;
$others.forEach(function($other) { $others.forEach(function($other) {
$other.forEach(function(otherElement) { getElements($other).forEach(function(otherElement) {
parent.insertBefore(otherElement, parent.firstChild); parent.insertBefore(otherElement, parent.firstChild);
}); });
}); });
@ -479,7 +483,7 @@ Ox.$ = Ox.element = function $(value) {
object <o> Another DOM object object <o> Another DOM object
@*/ @*/
prependTo: function prependTo($other) { prependTo: function prependTo($other) {
$other.forEach(function(otherElement) { getElements($other).forEach(function(otherElement) {
var firstChild = otherElement.firstChild var firstChild = otherElement.firstChild
elements.forEach(function(element) { elements.forEach(function(element) {
otherElement.insertBefore(element, firstChild); otherElement.insertBefore(element, firstChild);
@ -570,7 +574,7 @@ Ox.$ = Ox.element = function $(value) {
object <o> Another DOM object object <o> Another DOM object
@*/ @*/
replace: function replace($other) { replace: function replace($other) {
$other.forEach(function(otherElement) { getElements($other).forEach(function(otherElement) {
var parent = otherElement.parentNode, var parent = otherElement.parentNode,
sibling = otherElement.nextSibling; sibling = otherElement.nextSibling;
if (parent) { if (parent) {
@ -595,7 +599,7 @@ Ox.$ = Ox.element = function $(value) {
if (parent) { if (parent) {
removeOxElements(element, true); removeOxElements(element, true);
parent.removeChild(element); parent.removeChild(element);
$other.forEach(function(otherElement) { getElements($other).forEach(function(otherElement) {
parent.insertBefore(otherElement, sibling); parent.insertBefore(otherElement, sibling);
}); });
} }