From 47059bdb04e843329ae90bc19cb2e573eef20327 Mon Sep 17 00:00:00 2001 From: darylteo Date: Tue, 5 Jan 2016 14:12:52 +1100 Subject: [PATCH 1/4] Add test for switch selectors --- tests/lib/switch-selectors.js | 36 +++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/tests/lib/switch-selectors.js b/tests/lib/switch-selectors.js index 84642aa..66c3421 100644 --- a/tests/lib/switch-selectors.js +++ b/tests/lib/switch-selectors.js @@ -1,9 +1,41 @@ var tape = require("tape") -// var switchesSelectors = require("../../lib/switches-selectors.js") +var switchesSelectors = require("../../lib/switches-selectors.js") +// @author darylteo tape("test switchesSelectors", function(t) { - t.fail() + // switchesSelectors relies on a higher level function callback + // should really be passed in instead so I'll leave it here as a TODO: + var pjax = { + onSwitch: function() { + console.log('Switched') + } + } + + var tmpEl = document.implementation.createHTMLDocument() + + // a div container is used because swapping the containers + // will generate a new element, so things get weird + // using "body" generates a lot of testling cruft that I don't + // want so let's avoid that + var container = document.createElement("div") + container.innerHTML = "

Original Text

No Change" + document.body.appendChild(container) + + var container2 = tmpEl.createElement("div") + container2.innerHTML = "

New Text

New Span" + tmpEl.body.appendChild(container2) + + switchesSelectors.bind(pjax)( + {}, // switches + {}, // switchesOptions + ['p'], //selectors, + tmpEl, // fromEl + document, // toEl, + {} // options + ) + + t.equals(container.innerHTML, '

New Text

No Change', 'Elements correctly switched') t.end() }) From 3d25bee131dc12b8a9c7373ed6678d2f2cc0fd42 Mon Sep 17 00:00:00 2001 From: darylteo Date: Tue, 5 Jan 2016 14:22:03 +1100 Subject: [PATCH 2/4] Bind required functions to pjax --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index e17eef1..f41a3c5 100644 --- a/index.js +++ b/index.js @@ -49,11 +49,11 @@ Pjax.prototype = { attachLink: require("./lib/proto/attach-link.js"), forEachSelectors: function(cb, context, DOMcontext) { - return require("./lib/foreach-selectors.js")(this.options.selectors, cb, context, DOMcontext) + return require("./lib/foreach-selectors.js").bind(this)(this.options.selectors, cb, context, DOMcontext) }, switchSelectors: function(selectors, fromEl, toEl, options) { - return require("./lib/switches-selectors.js")(this.options.switches, this.options.switchesOptions, selectors, fromEl, toEl, options) + return require("./lib/switches-selectors.js").bind(this)(this.options.switches, this.options.switchesOptions, selectors, fromEl, toEl, options) }, // too much problem with the code below From e882b8639a2f03f0f9ace88e255ada118b667eda Mon Sep 17 00:00:00 2001 From: darylteo Date: Tue, 5 Jan 2016 14:30:09 +1100 Subject: [PATCH 3/4] Use required forEachElse --- index.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index f41a3c5..ffef856 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,4 @@ +var forEachEls = require("./lib/foreach-els.js") var newUid = require("./lib/uniqueid.js") @@ -5,6 +6,7 @@ var on = require("./lib/events/on.js") // var off = require("./lib/events/on.js") var trigger = require("./lib/events/trigger.js") + var Pjax = function(options) { this.firstrun = true @@ -118,7 +120,7 @@ Pjax.prototype = { // execute scripts when DOM have been completely updated this.options.selectors.forEach(function(selector) { - Pjax.forEachEls(document.querySelectorAll(selector), function(el) { + forEachEls(document.querySelectorAll(selector), function(el) { Pjax.executeScripts(el) }) }) From 477d9678042631e56f3577a20148c389d12e0cfc Mon Sep 17 00:00:00 2001 From: darylteo Date: Tue, 5 Jan 2016 15:02:39 +1100 Subject: [PATCH 4/4] Add clone and executeScripts as well --- index.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index ffef856..460d045 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,6 @@ +var clone = require('./lib/clone.js') +var executeScripts = require('./lib/execute-scripts.js') + var forEachEls = require("./lib/foreach-els.js") var newUid = require("./lib/uniqueid.js") @@ -20,7 +23,7 @@ var Pjax = function(options) { on(window, "popstate", function(st) { if (st.state) { - var opt = Pjax.clone(this.options) + var opt = clone(this.options) opt.url = st.state.url opt.title = st.state.title opt.history = false @@ -121,7 +124,7 @@ Pjax.prototype = { // execute scripts when DOM have been completely updated this.options.selectors.forEach(function(selector) { forEachEls(document.querySelectorAll(selector), function(el) { - Pjax.executeScripts(el) + executeScripts(el) }) }) // }