2014-10-14 08:19:44 +02:00
|
|
|
|
var forEachEls = require("./foreach-els")
|
|
|
|
|
|
|
|
|
|
|
|
var defaultSwitches = require("./switches")
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = function(switches, switchesOptions, selectors, fromEl, toEl, options) {
|
2018-01-31 22:17:06 +00:00
|
|
|
|
var switchesQueue = []
|
2018-01-22 10:55:29 -05:00
|
|
|
|
|
2014-10-14 08:19:44 +02:00
|
|
|
|
selectors.forEach(function(selector) {
|
|
|
|
|
|
var newEls = fromEl.querySelectorAll(selector)
|
|
|
|
|
|
var oldEls = toEl.querySelectorAll(selector)
|
|
|
|
|
|
if (this.log) {
|
|
|
|
|
|
this.log("Pjax switch", selector, newEls, oldEls)
|
|
|
|
|
|
}
|
|
|
|
|
|
if (newEls.length !== oldEls.length) {
|
|
|
|
|
|
throw "DOM doesn’t look the same on new loaded page: ’" + selector + "’ - new " + newEls.length + ", old " + oldEls.length
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
forEachEls(newEls, function(newEl, i) {
|
|
|
|
|
|
var oldEl = oldEls[i]
|
|
|
|
|
|
if (this.log) {
|
|
|
|
|
|
this.log("newEl", newEl, "oldEl", oldEl)
|
|
|
|
|
|
}
|
2018-01-22 10:55:29 -05:00
|
|
|
|
|
|
|
|
|
|
var callback = (switches[selector]) ?
|
|
|
|
|
|
switches[selector].bind(this, oldEl, newEl, options, switchesOptions[selector]) :
|
|
|
|
|
|
defaultSwitches.outerHTML.bind(this, oldEl, newEl, options)
|
|
|
|
|
|
|
|
|
|
|
|
switchesQueue.push(callback)
|
2014-10-14 08:19:44 +02:00
|
|
|
|
}, this)
|
|
|
|
|
|
}, this)
|
2018-01-22 10:55:29 -05:00
|
|
|
|
|
|
|
|
|
|
this.state.numPendingSwitches = switchesQueue.length
|
|
|
|
|
|
|
|
|
|
|
|
switchesQueue.forEach(function(queuedSwitch) {
|
|
|
|
|
|
queuedSwitch()
|
|
|
|
|
|
})
|
2014-10-14 08:19:44 +02:00
|
|
|
|
}
|