Switch linting to ESLint and Prettier (#191)

* Switch linting to ESLint and Prettier
* Clean up config
* Prettier fixes
This commit was merged in pull request #191.
This commit is contained in:
BehindTheMath
2019-02-13 22:26:57 -05:00
committed by GitHub
parent 2c6506af65
commit 3c1a4b2e18
23 changed files with 4674 additions and 2222 deletions

300
index.js
View File

@@ -1,164 +1,189 @@
var executeScripts = require("./lib/execute-scripts.js")
var forEachEls = require("./lib/foreach-els.js")
var parseOptions = require("./lib/parse-options.js")
var switches = require("./lib/switches")
var newUid = require("./lib/uniqueid.js")
var executeScripts = require("./lib/execute-scripts");
var forEachEls = require("./lib/foreach-els");
var parseOptions = require("./lib/parse-options");
var switches = require("./lib/switches");
var newUid = require("./lib/uniqueid");
var on = require("./lib/events/on.js")
var trigger = require("./lib/events/trigger.js")
var on = require("./lib/events/on");
var trigger = require("./lib/events/trigger");
var clone = require("./lib/util/clone.js")
var contains = require("./lib/util/contains.js")
var extend = require("./lib/util/extend.js")
var noop = require("./lib/util/noop")
var clone = require("./lib/util/clone");
var contains = require("./lib/util/contains");
var extend = require("./lib/util/extend");
var noop = require("./lib/util/noop");
var Pjax = function(options) {
this.state = {
numPendingSwitches: 0,
href: null,
options: null
}
this.state = {
numPendingSwitches: 0,
href: null,
options: null
};
this.options = parseOptions(options);
this.log("Pjax options", this.options);
this.options = parseOptions(options)
this.log("Pjax options", this.options)
if (this.options.scrollRestoration && "scrollRestoration" in history) {
history.scrollRestoration = "manual"
}
this.maxUid = this.lastUid = newUid()
this.parseDOM(document)
on(window, "popstate", function(st) {
if (st.state) {
var opt = clone(this.options)
opt.url = st.state.url
opt.title = st.state.title
// Since state already exists, prevent it from being pushed again
opt.history = false
opt.scrollPos = st.state.scrollPos
if (st.state.uid < this.lastUid) {
opt.backward = true
}
else {
opt.forward = true
}
this.lastUid = st.state.uid
// @todo implement history cache here, based on uid
this.loadUrl(st.state.url, opt)
}
}.bind(this))
if (this.options.scrollRestoration && "scrollRestoration" in history) {
history.scrollRestoration = "manual";
}
Pjax.switches = switches
this.maxUid = this.lastUid = newUid();
this.parseDOM(document);
on(
window,
"popstate",
function(st) {
if (st.state) {
var opt = clone(this.options);
opt.url = st.state.url;
opt.title = st.state.title;
// Since state already exists, prevent it from being pushed again
opt.history = false;
opt.scrollPos = st.state.scrollPos;
if (st.state.uid < this.lastUid) {
opt.backward = true;
} else {
opt.forward = true;
}
this.lastUid = st.state.uid;
// @todo implement history cache here, based on uid
this.loadUrl(st.state.url, opt);
}
}.bind(this)
);
};
Pjax.switches = switches;
Pjax.prototype = {
log: require("./lib/proto/log.js"),
log: require("./lib/proto/log"),
getElements: function(el) {
return el.querySelectorAll(this.options.elements)
return el.querySelectorAll(this.options.elements);
},
parseDOM: function(el) {
var parseElement = require("./lib/proto/parse-element")
forEachEls(this.getElements(el), parseElement, this)
var parseElement = require("./lib/proto/parse-element");
forEachEls(this.getElements(el), parseElement, this);
},
refresh: function(el) {
this.parseDOM(el || document)
this.parseDOM(el || document);
},
reload: function() {
window.location.reload()
window.location.reload();
},
attachLink: require("./lib/proto/attach-link.js"),
attachLink: require("./lib/proto/attach-link"),
attachForm: require("./lib/proto/attach-form.js"),
attachForm: require("./lib/proto/attach-form"),
forEachSelectors: function(cb, context, DOMcontext) {
return require("./lib/foreach-selectors.js").bind(this)(this.options.selectors, cb, context, DOMcontext)
return require("./lib/foreach-selectors").bind(this)(
this.options.selectors,
cb,
context,
DOMcontext
);
},
switchSelectors: function(selectors, fromEl, toEl, options) {
return require("./lib/switches-selectors.js").bind(this)(this.options.switches, this.options.switchesOptions, selectors, fromEl, toEl, options)
return require("./lib/switches-selectors").bind(this)(
this.options.switches,
this.options.switchesOptions,
selectors,
fromEl,
toEl,
options
);
},
latestChance: function(href) {
window.location = href
window.location = href;
},
onSwitch: function() {
trigger(window, "resize scroll")
trigger(window, "resize scroll");
this.state.numPendingSwitches--
this.state.numPendingSwitches--;
// debounce calls, so we only run this once after all switches are finished.
if (this.state.numPendingSwitches === 0) {
this.afterAllSwitches()
this.afterAllSwitches();
}
},
loadContent: function(html, options) {
var tmpEl = document.implementation.createHTMLDocument("pjax")
var tmpEl = document.implementation.createHTMLDocument("pjax");
// parse HTML attributes to copy them
// since we are forced to use documentElement.innerHTML (outerHTML can't be used for <html>)
var htmlRegex = /<html[^>]+>/gi
var htmlAttribsRegex = /\s?[a-z:]+(?:\=(?:\'|\")[^\'\">]+(?:\'|\"))*/gi
var matches = html.match(htmlRegex)
var htmlRegex = /<html[^>]+>/gi;
var htmlAttribsRegex = /\s?[a-z:]+(?:=['"][^'">]+['"])*/gi;
var matches = html.match(htmlRegex);
if (matches && matches.length) {
matches = matches[0].match(htmlAttribsRegex)
matches = matches[0].match(htmlAttribsRegex);
if (matches.length) {
matches.shift()
matches.shift();
matches.forEach(function(htmlAttrib) {
var attr = htmlAttrib.trim().split("=")
var attr = htmlAttrib.trim().split("=");
if (attr.length === 1) {
tmpEl.documentElement.setAttribute(attr[0], true)
tmpEl.documentElement.setAttribute(attr[0], true);
} else {
tmpEl.documentElement.setAttribute(attr[0], attr[1].slice(1, -1));
}
else {
tmpEl.documentElement.setAttribute(attr[0], attr[1].slice(1, -1))
}
})
});
}
}
tmpEl.documentElement.innerHTML = html
this.log("load content", tmpEl.documentElement.attributes, tmpEl.documentElement.innerHTML.length)
tmpEl.documentElement.innerHTML = html;
this.log(
"load content",
tmpEl.documentElement.attributes,
tmpEl.documentElement.innerHTML.length
);
// Clear out any focused controls before inserting new page contents.
if (document.activeElement && contains(document, this.options.selectors, document.activeElement)) {
if (
document.activeElement &&
contains(document, this.options.selectors, document.activeElement)
) {
try {
document.activeElement.blur()
} catch (e) { }
document.activeElement.blur();
} catch (e) {} // eslint-disable-line no-empty
}
this.switchSelectors(this.options.selectors, tmpEl, document, options)
this.switchSelectors(this.options.selectors, tmpEl, document, options);
},
abortRequest: require("./lib/abort-request.js"),
abortRequest: require("./lib/abort-request"),
doRequest: require("./lib/send-request.js"),
doRequest: require("./lib/send-request"),
handleResponse: require("./lib/proto/handle-response.js"),
handleResponse: require("./lib/proto/handle-response"),
loadUrl: function(href, options) {
options = typeof options === "object" ?
extend({}, this.options, options) :
clone(this.options)
options =
typeof options === "object"
? extend({}, this.options, options)
: clone(this.options);
this.log("load href", href, options)
this.log("load href", href, options);
// Abort any previous request
this.abortRequest(this.request)
this.abortRequest(this.request);
trigger(document, "pjax:send", options)
trigger(document, "pjax:send", options);
// Do the request
this.request = this.doRequest(href, options, this.handleResponse.bind(this))
this.request = this.doRequest(
href,
options,
this.handleResponse.bind(this)
);
},
afterAllSwitches: function() {
@@ -167,114 +192,121 @@ Pjax.prototype = {
// the last field.
//
// http://www.w3.org/html/wg/drafts/html/master/forms.html
var autofocusEl = Array.prototype.slice.call(document.querySelectorAll("[autofocus]")).pop()
var autofocusEl = Array.prototype.slice
.call(document.querySelectorAll("[autofocus]"))
.pop();
if (autofocusEl && document.activeElement !== autofocusEl) {
autofocusEl.focus()
autofocusEl.focus();
}
// execute scripts when DOM have been completely updated
this.options.selectors.forEach(function(selector) {
forEachEls(document.querySelectorAll(selector), function(el) {
executeScripts(el)
})
})
executeScripts(el);
});
});
var state = this.state
var state = this.state;
if (state.options.history) {
if (!window.history.state) {
this.lastUid = this.maxUid = newUid()
window.history.replaceState({
this.lastUid = this.maxUid = newUid();
window.history.replaceState(
{
url: window.location.href,
title: document.title,
uid: this.maxUid,
scrollPos: [0, 0]
},
document.title)
document.title
);
}
// Update browser history
this.lastUid = this.maxUid = newUid()
this.lastUid = this.maxUid = newUid();
window.history.pushState({
window.history.pushState(
{
url: state.href,
title: state.options.title,
uid: this.maxUid,
scrollPos: [0, 0]
},
state.options.title,
state.href)
state.href
);
}
this.forEachSelectors(function(el) {
this.parseDOM(el)
}, this)
this.parseDOM(el);
}, this);
// Fire Events
trigger(document,"pjax:complete pjax:success", state.options)
trigger(document, "pjax:complete pjax:success", state.options);
if (typeof state.options.analytics === "function") {
state.options.analytics()
state.options.analytics();
}
if (state.options.history) {
// First parse url and check for hash to override scroll
var a = document.createElement("a")
a.href = this.state.href
var a = document.createElement("a");
a.href = this.state.href;
if (a.hash) {
var name = a.hash.slice(1)
name = decodeURIComponent(name)
var name = a.hash.slice(1);
name = decodeURIComponent(name);
var curtop = 0
var target = document.getElementById(name) || document.getElementsByName(name)[0]
var curtop = 0;
var target =
document.getElementById(name) || document.getElementsByName(name)[0];
if (target) {
// http://stackoverflow.com/questions/8111094/cross-browser-javascript-function-to-find-actual-position-of-an-element-in-page
if (target.offsetParent) {
do {
curtop += target.offsetTop
curtop += target.offsetTop;
target = target.offsetParent
} while (target)
target = target.offsetParent;
} while (target);
}
}
window.scrollTo(0, curtop)
}
else if (state.options.scrollTo !== false) {
window.scrollTo(0, curtop);
} else if (state.options.scrollTo !== false) {
// Scroll page to top on new page load
if (state.options.scrollTo.length > 1) {
window.scrollTo(state.options.scrollTo[0], state.options.scrollTo[1])
}
else {
window.scrollTo(0, state.options.scrollTo)
window.scrollTo(state.options.scrollTo[0], state.options.scrollTo[1]);
} else {
window.scrollTo(0, state.options.scrollTo);
}
}
}
else if (state.options.scrollRestoration && state.options.scrollPos) {
window.scrollTo(state.options.scrollPos[0], state.options.scrollPos[1])
} else if (state.options.scrollRestoration && state.options.scrollPos) {
window.scrollTo(state.options.scrollPos[0], state.options.scrollPos[1]);
}
this.state = {
numPendingSwitches: 0,
href: null,
options: null
}
};
}
}
};
Pjax.isSupported = require("./lib/is-supported.js")
Pjax.isSupported = require("./lib/is-supported");
// 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")()) {` but that might be a little to simple
if (Pjax.isSupported()) {
module.exports = Pjax
module.exports = Pjax;
}
// if there isnt required browser functions, returning stupid api
else {
var stupidPjax = noop
var stupidPjax = noop;
for (var key in Pjax.prototype) {
if (Pjax.prototype.hasOwnProperty(key) && typeof Pjax.prototype[key] === "function") {
stupidPjax[key] = noop
if (
Pjax.prototype.hasOwnProperty(key) &&
typeof Pjax.prototype[key] === "function"
) {
stupidPjax[key] = noop;
}
}
module.exports = stupidPjax
module.exports = stupidPjax;
}