diff --git a/.jscsrc b/.jscsrc index 29720b3..f548996 100644 --- a/.jscsrc +++ b/.jscsrc @@ -1,6 +1,7 @@ { "excludeFiles": [ - "node_modules/**" + "node_modules/**", + "pjax.js" ], "fileExtensions": [ ".js" @@ -121,7 +122,7 @@ "requireCapitalizedConstructors": true, "safeContextKeyword": "that", "requireDotNotation": true, - "validateJSDoc": { + "jsDoc": { "checkParamNames": true, "checkRedundantParams": true, "requireParamTypes": true diff --git a/index.js b/index.js index 24e3df5..7794b1a 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,5 @@ -var clone = require('./lib/clone.js') -var executeScripts = require('./lib/execute-scripts.js') +var clone = require("./lib/clone.js") +var executeScripts = require("./lib/execute-scripts.js") var forEachEls = require("./lib/foreach-els.js") @@ -71,12 +71,12 @@ Pjax.prototype = { // too much problem with the code below // + it’s too dangerous -// switchFallback: function(fromEl, toEl) { -// this.switchSelectors(["head", "body"], fromEl, toEl) -// // execute script when DOM is like it should be -// Pjax.executeScripts(document.querySelector("head")) -// Pjax.executeScripts(document.querySelector("body")) -// } + // switchFallback: function(fromEl, toEl) { + // this.switchSelectors(["head", "body"], fromEl, toEl) + // // execute script when DOM is like it should be + // Pjax.executeScripts(document.querySelector("head")) + // Pjax.executeScripts(document.querySelector("body")) + // } latestChance: function(href) { window.location = href @@ -233,7 +233,7 @@ Pjax.prototype = { Pjax.isSupported = require("./lib/is-supported.js"); -//arguably could do `if( require("./lib/is-supported.js")()) {` but that might be a little to simple +// arguably could do `if( require("./lib/is-supported.js")()) {` but that might be a little to simple if (Pjax.isSupported()) { module.exports = Pjax } diff --git a/lib/proto/attach-form.js b/lib/proto/attach-form.js index ef8a63a..1fdb537 100644 --- a/lib/proto/attach-form.js +++ b/lib/proto/attach-form.js @@ -5,16 +5,15 @@ var clone = require("../clone") var attrClick = "data-pjax-click-state" -var formAction = function(el, event){ - +var formAction = function(el, event) { this.options.requestOptions = { - requestUrl : el.getAttribute('action') || window.location.href, - requestMethod : el.getAttribute('method') || 'GET', + requestUrl: el.getAttribute("action") || window.location.href, + requestMethod: el.getAttribute("method") || "GET", } - //create a testable virtual link of the form action - var virtLinkElement = document.createElement('a'); - virtLinkElement.setAttribute('href', this.options.requestOptions.requestUrl); + // create a testable virtual link of the form action + var virtLinkElement = document.createElement("a"); + virtLinkElement.setAttribute("href", this.options.requestOptions.requestUrl); // Ignore external links. if (virtLinkElement.protocol !== window.location.protocol || virtLinkElement.host !== window.location.host) { @@ -35,7 +34,7 @@ var formAction = function(el, event){ } // if declared as a full reload, just normally submit the form - if ( this.options.currentUrlFullReload) { + if (this.options.currentUrlFullReload) { el.setAttribute(attrClick, "reload"); return; } @@ -43,17 +42,19 @@ var formAction = function(el, event){ event.preventDefault() var paramObject = []; - for(var elementKey in el.elements) { + for (var elementKey in el.elements) { var element = el.elements[elementKey]; - if (!!element.name && element.attributes !== undefined && element.tagName.toLowerCase() !== 'button'){ - if ((element.attributes.type !== 'checkbox' && element.attributes.type !== 'radio') || element.checked) { - paramObject.push({ name: encodeURIComponent(element.name), value: encodeURIComponent(element.value)}); + // jscs:disable disallowImplicitTypeConversion + if (!!element.name && element.attributes !== undefined && element.tagName.toLowerCase() !== "button") { + // jscs:enable disallowImplicitTypeConversion + if ((element.attributes.type !== "checkbox" && element.attributes.type !== "radio") || element.checked) { + paramObject.push({name: encodeURIComponent(element.name), value: encodeURIComponent(element.value)}); } } } - //Creating a getString - var paramsString = (paramObject.map(function(value){return value.name+"="+value.value;})).join('&'); + // Creating a getString + var paramsString = (paramObject.map(function(value) {return value.name + "=" + value.value;})).join("&"); this.options.requestOptions.requestPayload = paramObject; this.options.requestOptions.requestPayloadString = paramsString; @@ -63,7 +64,6 @@ var formAction = function(el, event){ const options = clone(this.options); options.triggerElement = el; this.loadUrl(virtLinkElement.href, options); - }; var isDefaultPrevented = function(event) { diff --git a/lib/proto/parse-element.js b/lib/proto/parse-element.js index af88ca3..e79b4e0 100644 --- a/lib/proto/parse-element.js +++ b/lib/proto/parse-element.js @@ -1,20 +1,20 @@ module.exports = function(el) { switch (el.tagName.toLowerCase()) { - case "a": - // only attach link if el does not already have link attached - if (!el.hasAttribute('data-pjax-click-state')) { - this.attachLink(el) - } - break + case "a": + // only attach link if el does not already have link attached + if (!el.hasAttribute("data-pjax-click-state")) { + this.attachLink(el) + } + break case "form": // only attach link if el does not already have link attached - if (!el.hasAttribute('data-pjax-click-state')) { + if (!el.hasAttribute("data-pjax-click-state")) { this.attachForm(el) } - break + break - default: - throw "Pjax can only be applied on or
submit" + default: + throw "Pjax can only be applied on or submit" } } diff --git a/lib/proto/parse-options.js b/lib/proto/parse-options.js index d773cad..fce3ec8 100644 --- a/lib/proto/parse-options.js +++ b/lib/proto/parse-options.js @@ -2,7 +2,7 @@ var defaultSwitches = require("../switches") -module.exports = function(options){ +module.exports = function(options) { this.options = options this.options.elements = this.options.elements || "a[href], form[action]" this.options.selectors = this.options.selectors || ["title", ".js-Pjax"] @@ -20,8 +20,8 @@ module.exports = function(options){ ga("send", "pageview", {page: location.pathname, title: document.title}) } } - this.options.scrollTo = (typeof this.options.scrollTo === 'undefined') ? 0 : this.options.scrollTo; - this.options.cacheBust = (typeof this.options.cacheBust === 'undefined') ? true : this.options.cacheBust + this.options.scrollTo = (typeof this.options.scrollTo === "undefined") ? 0 : this.options.scrollTo; + this.options.cacheBust = (typeof this.options.cacheBust === "undefined") ? true : this.options.cacheBust this.options.debug = this.options.debug || false this.options.timeout = this.options.timeout || 0 diff --git a/package.json b/package.json index 48adb94..550629a 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ ], "devDependencies": { "browserify": "^3.46.0", - "jscs": "^1.6.2", + "jscs": "^3.0.7", "jsdom": "^11.5.1", "jsdom-global": "^3.0.2", "jshint": "^2.5.6", @@ -35,7 +35,7 @@ "tape": "^3.0.0" }, "scripts": { - "lint": "jscs **/*.js && jshint . --exclude-path .gitignore", + "lint": "jscs . && jshint . --exclude-path .gitignore", "standalone": "browserify index.js --standalone Pjax > pjax.js", "build-debug": "browserify index.js --debug --standalone Pjax > pjax.js", "tests": "tape -r ./tests/index.js ./tests/**/*.js", diff --git a/tests/lib/proto/attach-form.js b/tests/lib/proto/attach-form.js index d23a4da..701b89f 100644 --- a/tests/lib/proto/attach-form.js +++ b/tests/lib/proto/attach-form.js @@ -46,12 +46,12 @@ tape("test attach form prototype method", function(t) { // see reload defined above form.action = window.location.protocol + "//" + window.location.host + "/internal" - form.method = 'POST' + form.method = "POST" trigger(form, "submit") // see post defined above form.action = window.location.protocol + "//" + window.location.host + "/internal" - form.method = 'GET' + form.method = "GET" trigger(form, "submit") // see post defined above diff --git a/tests/lib/proto/parse-options.js b/tests/lib/proto/parse-options.js index e9c2044..2b79435 100644 --- a/tests/lib/proto/parse-options.js +++ b/tests/lib/proto/parse-options.js @@ -4,70 +4,75 @@ var parseOptions = require("../../../lib/proto/parse-options.js") tape("test parse initalization options function", function(t) { // via http://stackoverflow.com/questions/1173549/how-to-determine-if-an-object-is-an-object-literal-in-javascript function isObjLiteral(_obj) { - var _test = _obj; - return ( typeof _obj !== 'object' || _obj === null ? - false : - ( - (function () { - while (!false) { - if ( Object.getPrototypeOf( _test = Object.getPrototypeOf(_test) ) === null) { - break; - } - } - return Object.getPrototypeOf(_obj) === _test; - })() - ) + var _test = _obj; + return (typeof _obj !== "object" || _obj === null ? + false : + ( + (function() { + while (!false) { + if (Object.getPrototypeOf(_test = Object.getPrototypeOf(_test)) === null) { + break; + } + } + return Object.getPrototypeOf(_obj) === _test; + })() + ) ); } + function enumerableKeys(_obj) { var c = 0; - for(var n in _obj){ n = n; c++; } + for (var n in _obj) { + n = n; + c++; + } return c; } - t.test("- default options", function(t){ - var body_1 = {}; - var options_1 = {}; - parseOptions.apply(body_1,[options_1]); - t.deepEqual(body_1.options.elements,"a[href], form[action]"); - t.deepEqual(body_1.options.selectors.length,2,"selectors length"); - t.deepEqual(body_1.options.selectors[0],"title"); - t.deepEqual(body_1.options.selectors[1],".js-Pjax"); - - t.deepEqual(isObjLiteral(body_1.options.switches),true); - t.deepEqual(enumerableKeys(body_1.options.switches),2);//head and body + t.test("- default options", function(t) { + var body1 = {}; + var options1 = {}; + parseOptions.apply(body1, [options1]); - t.deepEqual(isObjLiteral(body_1.options.switchesOptions),true); - t.deepEqual(enumerableKeys(body_1.options.switchesOptions),0); + t.deepEqual(body1.options.elements, "a[href], form[action]"); + t.deepEqual(body1.options.selectors.length, 2, "selectors length"); + t.deepEqual(body1.options.selectors[0], "title"); + t.deepEqual(body1.options.selectors[1], ".js-Pjax"); - t.deepEqual(body_1.options.history,true); + t.deepEqual(isObjLiteral(body1.options.switches), true); + t.deepEqual(enumerableKeys(body1.options.switches), 2);// head and body - //TODO analytics is a little weird right now - t.deepEqual(typeof body_1.options.analytics,"function"); + t.deepEqual(isObjLiteral(body1.options.switchesOptions), true); + t.deepEqual(enumerableKeys(body1.options.switchesOptions), 0); - t.deepEqual(body_1.options.scrollTo,0); - t.deepEqual(body_1.options.cacheBust,true); - t.deepEqual(body_1.options.debug,false); + t.deepEqual(body1.options.history, true); + + // TODO analytics is a little weird right now + t.deepEqual(typeof body1.options.analytics, "function"); + + t.deepEqual(body1.options.scrollTo, 0); + t.deepEqual(body1.options.cacheBust, true); + t.deepEqual(body1.options.debug, false); t.end(); }); - //verify analytics always ends up as a function even when passed not a function - t.test("- analytics is a function", function(t){ - var body_2 = {}; - var options_2 = {analytics:"some string"}; - parseOptions.apply(body_2,[options_2]); + // verify analytics always ends up as a function even when passed not a function + t.test("- analytics is a function", function(t) { + var body2 = {}; + var options2 = {analytics: "some string"}; + parseOptions.apply(body2, [options2]); - t.deepEqual(typeof body_2.options.analytics,"function"); + t.deepEqual(typeof body2.options.analytics, "function"); t.end(); }); - //verify that the value false for scrollTo is not squashed - t.test("- scrollTo remains false", function(t){ - var body_3 = {}; - var options_3 = {scrollTo:false}; - parseOptions.apply(body_3,[options_3]); + // verify that the value false for scrollTo is not squashed + t.test("- scrollTo remains false", function(t) { + var body3 = {}; + var options3 = {scrollTo: false}; + parseOptions.apply(body3, [options3]); - t.deepEqual( body_3.options.scrollTo,false); + t.deepEqual(body3.options.scrollTo, false); t.end(); }); t.end() -}) \ No newline at end of file +}) diff --git a/tests/lib/request.js b/tests/lib/request.js index f99763e..0f1e365 100644 --- a/tests/lib/request.js +++ b/tests/lib/request.js @@ -6,9 +6,9 @@ var request = require("../../lib/request.js") // just for the purposes of this test // This polyfill is not complete; it won't show the updated location if a // redirection occurred, but it's fine for our purposes. -if (!('responseURL' in XMLHttpRequest.prototype)) { +if (!("responseURL" in XMLHttpRequest.prototype)) { var nativeOpen = XMLHttpRequest.prototype.open - XMLHttpRequest.prototype.open = function (method, url) { + XMLHttpRequest.prototype.open = function(method, url) { this.responseURL = url return nativeOpen.apply(this, arguments) } diff --git a/tests/lib/switch-selectors.js b/tests/lib/switch-selectors.js index 66c3421..8653227 100644 --- a/tests/lib/switch-selectors.js +++ b/tests/lib/switch-selectors.js @@ -8,7 +8,7 @@ tape("test switchesSelectors", function(t) { // should really be passed in instead so I'll leave it here as a TODO: var pjax = { onSwitch: function() { - console.log('Switched') + console.log("Switched") } } @@ -29,13 +29,13 @@ tape("test switchesSelectors", function(t) { switchesSelectors.bind(pjax)( {}, // switches {}, // switchesOptions - ['p'], //selectors, + ["p"], // selectors, tmpEl, // fromEl document, // toEl, {} // options ) - t.equals(container.innerHTML, '

New Text

No Change', 'Elements correctly switched') + t.equals(container.innerHTML, "

New Text

No Change", "Elements correctly switched") t.end() })