From 3bc67f245c1eea8bb69a7895de0b22f0f19aed52 Mon Sep 17 00:00:00 2001 From: Maxime Thirouin Date: Tue, 14 Oct 2014 11:42:36 +0200 Subject: [PATCH] Cleanup & travis (should break) --- .gitignore | 1 - .jscs.json => .jscsrc | 65 +++++++++-------------- .jshintrc | 7 ++- .travis.yml | 7 +++ bower.json | 2 - index.js | 67 +++++++++++++----------- lib/foreach-els.js | 2 + lib/polyfills/Function.prototype.bind.js | 14 ++--- lib/proto/attach-link.js | 10 ++-- lib/proto/parse-dom.js | 2 +- lib/request.js | 2 +- lib/switches.js | 17 +++--- tests/lib/events.js | 4 +- tests/lib/proto/parse-element.js | 1 - tests/lib/request.js | 2 +- tests/lib/switch-selectors.js | 2 +- 16 files changed, 104 insertions(+), 101 deletions(-) rename .jscs.json => .jscsrc (74%) create mode 100644 .travis.yml diff --git a/.gitignore b/.gitignore index 075b5c2..2c2e476 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ .DS_Store node_modules/ -dist/ tests/scripts/index.html diff --git a/.jscs.json b/.jscsrc similarity index 74% rename from .jscs.json rename to .jscsrc index e4169a9..29720b3 100644 --- a/.jscs.json +++ b/.jscsrc @@ -1,5 +1,10 @@ { - "excludeFiles": [], + "excludeFiles": [ + "node_modules/**" + ], + "fileExtensions": [ + ".js" + ], "requireCurlyBraces": [ "if", "else", @@ -20,16 +25,27 @@ "try", "catch" ], + "requireSpaceBeforeBlockStatements": true, + "requireParenthesesAroundIIFE": true, + "requireSpacesInConditionalExpression": { + "afterTest": true, + "beforeConsequent": true, + "afterConsequent": true, + "beforeAlternate": true + }, "requireSpacesInFunctionExpression": { "beforeOpeningCurlyBrace": true }, "disallowSpacesInFunctionExpression": { "beforeOpeningRoundBrace": true }, + "disallowMultipleVarDecl": true, + "requireBlocksOnNewline": 1, + "disallowPaddingNewlinesInBlocks": true, "disallowEmptyBlocks": true, - "disallowSpacesInsideParentheses": true, "disallowSpacesInsideObjectBrackets": true, "disallowSpacesInsideArrayBrackets": true, + "disallowSpacesInsideParentheses": true, "disallowQuotedKeysInObjects": "allButReserved", "disallowSpaceAfterObjectKeys": true, "requireCommaBeforeLineBreak": true, @@ -49,41 +65,6 @@ "<", "<=" ], - "disallowLeftStickedOperators": [ - "?", - "+", - "-", - "/", - "*", - "=", - "==", - "===", - "!=", - "!==", - ">", - ">=", - "<", - "<=" - ], - "requireRightStickedOperators": [ - "!" - ], - "disallowRightStickedOperators": [ - "?", - "+", - "/", - "*", - ":", - "=", - "==", - "===", - "!=", - "!==", - ">", - ">=", - "<", - "<=" - ], "disallowSpaceAfterPrefixUnaryOperators": [ "++", "--", @@ -124,20 +105,26 @@ "binary", "string" ], + "requireCamelCaseOrUpperCaseIdentifiers": "ignoreProperties", "disallowKeywords": [ "with" ], "disallowMultipleLineStrings": true, "validateQuoteMarks": "\"", + "validateIndentation": 2, "disallowMixedSpacesAndTabs": true, "disallowTrailingWhitespace": true, - "requireKeywordsOnNewLine": [], + "requireKeywordsOnNewLine": [ + "else" + ], "requireLineFeedAtFileEnd": true, "requireCapitalizedConstructors": true, "safeContextKeyword": "that", + "requireDotNotation": true, "validateJSDoc": { "checkParamNames": true, "checkRedundantParams": true, "requireParamTypes": true - } + }, + "requireSpaceAfterLineComment": true } diff --git a/.jshintrc b/.jshintrc index 182e34d..9f268f7 100644 --- a/.jshintrc +++ b/.jshintrc @@ -1,4 +1,9 @@ { + "newcap": false, + "undef": true, + "unused": true, "asi": true, - "laxcomma": true + "esnext": true, + "node": true, + "browser": true } diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..06ddbfa --- /dev/null +++ b/.travis.yml @@ -0,0 +1,7 @@ +language: "node_js" +before_script: + # testling use headless browser + # on travis-ci, firefox is the default one + # & it needs a display to works + - export DISPLAY=:99.0 + - sh -e /etc/init.d/xvfb start diff --git a/bower.json b/bower.json index 290a8c8..cdfed52 100644 --- a/bower.json +++ b/bower.json @@ -18,8 +18,6 @@ ], "license": "MIT", "moduleType": [ - "amd", - "globals", "node" ], "ignore": [ diff --git a/index.js b/index.js index a2ee1d5..94bcc09 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,11 @@ +/* global _gaq: true, ga: true */ + var newUid = require("./lib/uniqueid.js") +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 @@ -44,7 +50,7 @@ var Pjax = function(options) { this.parseDOM(document) - Pjax.on(window, "popstate", function(st) { + on(window, "popstate", function(st) { if (st.state) { var opt = Pjax.clone(this.options) opt.url = st.state.url @@ -63,7 +69,6 @@ var Pjax = function(options) { this.loadUrl(st.state.url, opt) } }.bind(this)) - } Pjax.prototype = { @@ -97,7 +102,7 @@ Pjax.prototype = { }, onSwitch: function() { - Pjax.trigger(window, "resize scroll") + trigger(window, "resize scroll") }, loadContent: function(html, options) { @@ -131,24 +136,24 @@ Pjax.prototype = { } // try { - this.switchSelectors(this.options.selectors, tmpEl, document, options) + this.switchSelectors(this.options.selectors, tmpEl, document, options) - // FF bug: Won’t autofocus fields that are inserted via JS. - // This behavior is incorrect. So if theres no current focus, autofocus - // the last field. - // - // http://www.w3.org/html/wg/drafts/html/master/forms.html - var autofocusEl = Array.prototype.slice.call(document.querySelectorAll("[autofocus]")).pop() - if (autofocusEl && document.activeElement !== autofocusEl) { - autofocusEl.focus(); - } + // FF bug: Won’t autofocus fields that are inserted via JS. + // This behavior is incorrect. So if theres no current focus, autofocus + // the last field. + // + // http://www.w3.org/html/wg/drafts/html/master/forms.html + var autofocusEl = Array.prototype.slice.call(document.querySelectorAll("[autofocus]")).pop() + if (autofocusEl && document.activeElement !== autofocusEl) { + autofocusEl.focus(); + } - // execute scripts when DOM have been completely updated - this.options.selectors.forEach(function(selector) { - Pjax.forEachEls(document.querySelectorAll(selector), function(el) { - Pjax.executeScripts(el) - }) + // execute scripts when DOM have been completely updated + this.options.selectors.forEach(function(selector) { + Pjax.forEachEls(document.querySelectorAll(selector), function(el) { + Pjax.executeScripts(el) }) + }) // } // catch(e) { // if (this.options.debug) { @@ -163,14 +168,13 @@ Pjax.prototype = { loadUrl: function(href, options) { this.log("load href", href, options) - Pjax.trigger(document, "pjax:send", options); + trigger(document, "pjax:send", options); // Do the request this.doRequest(href, function(html) { - // Fail if unable to load HTML via AJAX if (html === false) { - Pjax.trigger(document,"pjax:complete pjax:error", options) + trigger(document,"pjax:complete pjax:error", options) return } @@ -195,25 +199,24 @@ Pjax.prototype = { } if (options.history) { - if (this.firstrun) { this.lastUid = this.maxUid = newUid() this.firstrun = false window.history.replaceState({ - url: window.location.href, - title: document.title, - uid: this.maxUid - }, - document.title) + url: window.location.href, + title: document.title, + uid: this.maxUid + }, + document.title) } // Update browser history this.lastUid = this.maxUid = newUid() window.history.pushState({ - url: href, - title: options.title, - uid: this.maxUid - }, + url: href, + title: options.title, + uid: this.maxUid + }, options.title, href) } @@ -223,7 +226,7 @@ Pjax.prototype = { }, this) // Fire Events - Pjax.trigger(document,"pjax:complete pjax:success", options) + trigger(document,"pjax:complete pjax:success", options) options.analytics() diff --git a/lib/foreach-els.js b/lib/foreach-els.js index 7031776..5d1cba6 100644 --- a/lib/foreach-els.js +++ b/lib/foreach-els.js @@ -1,3 +1,5 @@ +/* global HTMLCollection: true */ + module.exports = function(els, fn, context) { if (els instanceof HTMLCollection || els instanceof NodeList || els instanceof Array) { return Array.prototype.forEach.call(els, fn, context) diff --git a/lib/polyfills/Function.prototype.bind.js b/lib/polyfills/Function.prototype.bind.js index f5fce7c..d54bcb5 100644 --- a/lib/polyfills/Function.prototype.bind.js +++ b/lib/polyfills/Function.prototype.bind.js @@ -1,19 +1,19 @@ if (!Function.prototype.bind) { - Function.prototype.bind = function (oThis) { + Function.prototype.bind = function(oThis) { if (typeof this !== "function") { // closest thing possible to the ECMAScript 5 internal IsCallable function throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable") } var aArgs = Array.prototype.slice.call(arguments, 1) - var fToBind = this - var fNOP = function () {} - var fBound = function () { - return fToBind.apply(this instanceof fNOP && oThis ? this : oThis, aArgs.concat(Array.prototype.slice.call(arguments))) + var that = this + var Fnoop = function() {} + var fBound = function() { + return that.apply(this instanceof Fnoop && oThis ? this : oThis, aArgs.concat(Array.prototype.slice.call(arguments))) } - fNOP.prototype = this.prototype - fBound.prototype = new fNOP() + Fnoop.prototype = this.prototype + fBound.prototype = new Fnoop() return fBound } diff --git a/lib/proto/attach-link.js b/lib/proto/attach-link.js index 8c60a22..d8ca18d 100644 --- a/lib/proto/attach-link.js +++ b/lib/proto/attach-link.js @@ -54,23 +54,21 @@ var linkAction = function(el, event) { } module.exports = function(el) { - var instance = this + var that = this on(el, "click", function(event) { - linkAction.call(instance, el, event) + linkAction.call(that, el, event) }) on(el, "keyup", function(event) { - // Don’t break browser special behavior on links (like page in new window) if (event.which > 1 || event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) { el.setAttribute(attrKey, "modifier") return } - if(event.keyCode == 13) { - linkAction.call(instance, el, event) + if (event.keyCode == 13) { + linkAction.call(that, el, event) } - }.bind(this)) } diff --git a/lib/proto/parse-dom.js b/lib/proto/parse-dom.js index a49a064..e9dfb64 100644 --- a/lib/proto/parse-dom.js +++ b/lib/proto/parse-dom.js @@ -3,5 +3,5 @@ var forEachEls = require("../foreach-els") var parseElement = require("../parse-element") module.exports = function(el) { - forEachEls(this.getElements(el), parseElement }, this) + forEachEls(this.getElements(el), parseElement, this) } diff --git a/lib/request.js b/lib/request.js index be729eb..fd6aca7 100644 --- a/lib/request.js +++ b/lib/request.js @@ -12,7 +12,7 @@ module.exports = function(location, callback) { } } - request.open("GET", location + (!/[?&]/.test(location) ? "?": "&") + (new Date().getTime()), true) + request.open("GET", location + (!/[?&]/.test(location) ? "?" : "&") + (new Date().getTime()), true) request.setRequestHeader("X-Requested-With", "XMLHttpRequest") request.send(null) return request diff --git a/lib/switches.js b/lib/switches.js index af14c43..56a91b6 100644 --- a/lib/switches.js +++ b/lib/switches.js @@ -1,10 +1,15 @@ +var on = require("./lib/events/on.js") +// var off = require("./lib/events/on.js") +// var trigger = require("./lib/events/trigger.js") + + module.exports = { - outerHTML: function(oldEl, newEl, options) { + outerHTML: function(oldEl, newEl) { oldEl.outerHTML = newEl.outerHTML this.onSwitch() }, - innerHTML: function(oldEl, newEl, options) { + innerHTML: function(oldEl, newEl) { oldEl.innerHTML = newEl.innerHTML oldEl.className = newEl.className this.onSwitch() @@ -17,7 +22,7 @@ module.exports = { var fragToAppend = document.createDocumentFragment() // height transition are shitty on safari // so commented for now (until I found something ?) - //var relevantHeight = 0 + // var relevantHeight = 0 var animationEventNames = "animationend webkitAnimationEnd MSAnimationEnd oanimationend" var animatedElsNumber = 0 var sexyAnimationEnd = function(e) { @@ -50,7 +55,7 @@ module.exports = { // this is to trigger some repaint (example: picturefill) this.onSwitch() - //Pjax.trigger(window, "scroll") + // Pjax.trigger(window, "scroll") } }.bind(this) @@ -79,7 +84,7 @@ module.exports = { el.className += " " + switchOptions.classNames.remove + " " + (options.backward ? switchOptions.classNames.backward : switchOptions.classNames.forward) } animatedElsNumber++ - Pjax.on(el, animationEventNames, sexyAnimationEnd, true) + on(el, animationEventNames, sexyAnimationEnd, true) } }) @@ -97,7 +102,7 @@ module.exports = { elsToAdd.push(el) fragToAppend.appendChild(el) animatedElsNumber++ - Pjax.on(el, animationEventNames, sexyAnimationEnd, true) + on(el, animationEventNames, sexyAnimationEnd, true) } }) diff --git a/tests/lib/events.js b/tests/lib/events.js index f13e112..0a9a3f8 100644 --- a/tests/lib/events.js +++ b/tests/lib/events.js @@ -7,8 +7,8 @@ var trigger = require("../../lib/events/trigger") var el = document.createElement("div") var el2 = document.createElement("span") var els = [el, el2] -var eventType2 = "resize" -var eventsType = "click resize" +// var eventType2 = "resize" +// var eventsType = "click resize" var classCb = function() { this.className += "on" } diff --git a/tests/lib/proto/parse-element.js b/tests/lib/proto/parse-element.js index 69de686..f9b760f 100644 --- a/tests/lib/proto/parse-element.js +++ b/tests/lib/proto/parse-element.js @@ -3,7 +3,6 @@ var tape = require("tape") var parseElement = require("../../../lib/proto/parse-element") var protoMock = {attachLink: function() { return true}} tape("test parse element prototype method", function(t) { - t.doesNotThrow(function() { var a = document.createElement("a") parseElement.call(protoMock, a) diff --git a/tests/lib/request.js b/tests/lib/request.js index 41f8252..42af8d5 100644 --- a/tests/lib/request.js +++ b/tests/lib/request.js @@ -3,7 +3,7 @@ var tape = require("tape") var request = require("../../lib/request.js") tape("test xhr request", function(t) { - var xhr = request("https://api.github.com/", function(result) { + request("https://api.github.com/", function(result) { try { result = JSON.parse(result) } diff --git a/tests/lib/switch-selectors.js b/tests/lib/switch-selectors.js index 19830da..84642aa 100644 --- a/tests/lib/switch-selectors.js +++ b/tests/lib/switch-selectors.js @@ -1,6 +1,6 @@ var tape = require("tape") -var switchesSelectors = require("../../lib/switches-selectors.js") +// var switchesSelectors = require("../../lib/switches-selectors.js") tape("test switchesSelectors", function(t) { t.fail()