Add hash option (#667)

* Add hash option

* Avoid unecessary scroll into view-call

* Update readme with hash option

* Fix typo

* Remove nanolocation dep

* Default hash option to true
This commit is contained in:
Carl Törnqvist
2018-07-12 11:29:31 +02:00
committed by GitHub
parent 65d124b352
commit 6513f4288f
3 changed files with 19 additions and 11 deletions
+10 -6
View File
@@ -1,6 +1,5 @@
var scrollToAnchor = require('scroll-to-anchor')
var documentReady = require('document-ready')
var nanolocation = require('nanolocation')
var nanotiming = require('nanotiming')
var nanorouter = require('nanorouter')
var nanomorph = require('nanomorph')
@@ -39,8 +38,8 @@ function Choo (opts) {
// properties for internal use only
this._historyEnabled = opts.history === undefined ? true : opts.history
this._hrefEnabled = opts.href === undefined ? true : opts.href
this._hashEnabled = opts.hash === undefined ? true : opts.hash
this._hasWindow = typeof window !== 'undefined'
this._createLocation = nanolocation
this._cache = opts.cache
this._loaded = false
this._stores = []
@@ -128,8 +127,11 @@ Choo.prototype.start = function () {
if (self._hrefEnabled) {
nanohref(function (location) {
var href = location.href
var currHref = window.location.href
if (href === currHref) return
var hash = location.hash
if (href === window.location.href) {
if (!this._hashEnabled && hash) scrollToAnchor(hash)
return
}
self.emitter.emit(self._events.PUSHSTATE, href)
})
}
@@ -224,10 +226,12 @@ Choo.prototype.toString = function (location, state) {
Choo.prototype._matchRoute = function (locationOverride) {
var location, queryString
if (locationOverride) {
location = locationOverride.replace(/\?.+$/, '')
location = locationOverride.replace(/\?.+$/, '').replace(/\/$/, '')
if (!this._hashEnabled) location = location.replace(/#.+$/, '')
queryString = locationOverride
} else {
location = this._createLocation()
location = window.location.pathname.replace(/\/$/, '')
if (this._hashEnabled) location += window.location.hash.replace(/^#/, '/')
queryString = window.location.search
}
var matched = this.router.match(location)