Last active
December 18, 2015 04:19
-
-
Save uniqname/5724542 to your computer and use it in GitHub Desktop.
Stash and unstash nodes in potentially very long list of childNodes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;(function (window, document, $, undefined) { | |
var module, moduleName, stash, unstash, noconflict, oldGlobal; | |
moduleName = 'infiniteStash'; | |
stash = function (containerNode, numNodesToStash, after) { | |
var side = (after === true || after === 'after') ? 'after' : 'before', | |
// set is uesd to stash all node up to the next element. | |
// This preserves text nodes (white space) and comment nodes | |
// while still providing expected functionality. | |
set = [], | |
node, nodes, theStash, index; | |
theStash = containerNode.stash; | |
if (! theStash ) { | |
theStash = containerNode.stash = { | |
before: [], | |
after: [] | |
}; | |
} | |
if (side === 'before') { | |
nodes = Array.prototype.slice.call(containerNode.children, 0, numNodesToStash); | |
for (; numNodesToStash > 0; numNodesToStash--) { | |
containerNode.removeChild(containerNode.children[0]) | |
} | |
} else { | |
nodes = Array.prototype.slice.call(containerNode.children, -numNodesToStash); | |
nodes.reverse(); | |
for (; numNodesToStash > 0; numNodesToStash--) { | |
containerNode.removeChild(containerNode.children[containerNode.childElementCount - 1]) | |
} | |
} | |
theStash[side] = theStash[side].concat(nodes); | |
}; | |
unstash = function (containerNode, numNodesToUnstash, after) { | |
var side = (after === true || after === 'after') ? 'after' : 'before', | |
set = [], | |
node, firstNode, theStash; | |
theStash = containerNode.stash; | |
if (theStash) { | |
for (; numNodesToUnstash > 0; numNodesToUnstash-- ) { | |
if (node = theStash[side].pop()) { | |
if (side === 'before' && (firstNode = containerNode.firstElementChild)) { | |
containerNode.insertBefore(node, firstNode); | |
} else { | |
containerNode.appendChild(node); | |
} | |
} else { | |
warn('Nothing left to unstash from' + containerNode + '.'); | |
} | |
} | |
} else { | |
warn('Could not unstash from' + containerNode + '. Nothing has been stashed.'); | |
} | |
}; | |
noconflict = function () { | |
window[moduleName] = oldGlobal; | |
return module; | |
}; | |
warn = function (msg) { | |
if (window.console && window.console.warn) { | |
window.console.warn(msg); | |
} | |
} | |
module = { | |
stash: stash, | |
unstash: unstash, | |
noconflict: noconflict | |
}; | |
oldGlobal = window[moduleName]; | |
window[moduleName] = module; | |
// Support for jQuery chaining | |
if ($ && !$.fn.stash && !$.fn.unstash) { | |
$.fn.stash = function () { | |
var i; | |
for (i = this.length - 1; i >= 0; i--) { | |
args = [this[i]].concat(Array.prototype.slice.call(arguments)) | |
module.stash.apply(this, args); | |
} | |
return this; | |
}; | |
$.fn.unstash = function () { | |
var i; | |
for (i = this.length - 1; i >= 0; i--) { | |
args = [this[i]].concat(Array.prototype.slice.call(arguments)) | |
module.unstash.apply(this, args); | |
} | |
return this; | |
}; | |
} | |
if (typeof define === 'function') { | |
define(moduleName, function () { | |
return module; | |
}); | |
} else { | |
return module; | |
} | |
})(window, document, typeof jQuery === 'function' ? jQuery : undefined); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment