From 2bc7b80f0d10ba62a4b3d8676b69d40e77415e37 Mon Sep 17 00:00:00 2001 From: rolux Date: Wed, 27 Aug 2014 12:15:34 +0200 Subject: [PATCH] Ox.$: support forEach over jQuery-wrapped elements --- source/Ox/js/DOM.js | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/source/Ox/js/DOM.js b/source/Ox/js/DOM.js index 184e6ee2..8a3ecd6c 100644 --- a/source/Ox/js/DOM.js +++ b/source/Ox/js/DOM.js @@ -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 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 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 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); }); }