Files
pjax/lib/switches-selectors.js

60 lines
1.3 KiB
JavaScript
Raw Normal View History

2019-02-11 23:17:28 -05:00
var forEachEls = require("./foreach-els");
2019-02-11 23:17:28 -05:00
var defaultSwitches = require("./switches");
2019-02-11 23:17:28 -05:00
module.exports = function(
switches,
switchesOptions,
selectors,
fromEl,
toEl,
options
) {
var switchesQueue = [];
selectors.forEach(function(selector) {
2019-02-11 23:17:28 -05:00
var newEls = fromEl.querySelectorAll(selector);
var oldEls = toEl.querySelectorAll(selector);
if (this.log) {
2019-02-11 23:17:28 -05:00
this.log("Pjax switch", selector, newEls, oldEls);
}
if (newEls.length !== oldEls.length) {
2019-02-11 23:17:28 -05:00
throw "DOM doesnt look the same on new loaded page: " +
selector +
" - new " +
newEls.length +
", old " +
oldEls.length;
}
2019-02-11 23:17:28 -05:00
forEachEls(
newEls,
function(newEl, i) {
var oldEl = oldEls[i];
if (this.log) {
this.log("newEl", newEl, "oldEl", oldEl);
}
var callback = switches[selector]
? switches[selector].bind(
this,
oldEl,
newEl,
options,
switchesOptions[selector]
)
: defaultSwitches.outerHTML.bind(this, oldEl, newEl, options);
switchesQueue.push(callback);
},
this
);
}, this);
this.state.numPendingSwitches = switchesQueue.length;
switchesQueue.forEach(function(queuedSwitch) {
2019-02-11 23:17:28 -05:00
queuedSwitch();
});
};