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