|
1 | | -var util = require('../util') |
2 | | -var hashRE = /#.*$/ |
| 1 | +import { resolvePath } from '../util' |
| 2 | +const hashRE = /#.*$/ |
3 | 3 |
|
4 | | -function HTML5History (options) { |
5 | | - var root = options.root |
6 | | - if (root) { |
7 | | - // make sure there's the starting slash |
8 | | - if (root.charAt(0) !== '/') { |
9 | | - root = '/' + root |
| 4 | +export default class HTML5History { |
| 5 | + |
| 6 | + constructor (options) { |
| 7 | + let root = options.root |
| 8 | + if (root) { |
| 9 | + // make sure there's the starting slash |
| 10 | + if (root.charAt(0) !== '/') { |
| 11 | + root = '/' + root |
| 12 | + } |
| 13 | + // remove trailing slash |
| 14 | + this.root = root.replace(/\/$/, '') |
| 15 | + this.rootRE = new RegExp('^\\' + this.root) |
| 16 | + } else { |
| 17 | + this.root = null |
10 | 18 | } |
11 | | - // remove trailing slash |
12 | | - this.root = root.replace(/\/$/, '') |
13 | | - this.rootRE = new RegExp('^\\' + this.root) |
14 | | - } else { |
15 | | - this.root = null |
| 19 | + this.onChange = options.onChange |
| 20 | + // check base tag |
| 21 | + let baseEl = document.querySelector('base') |
| 22 | + this.base = baseEl && baseEl.getAttribute('href') |
16 | 23 | } |
17 | | - this.onChange = options.onChange |
18 | | - // check base tag |
19 | | - var baseEl = document.querySelector('base') |
20 | | - this.base = baseEl && baseEl.getAttribute('href') |
21 | | -} |
22 | 24 |
|
23 | | -HTML5History.prototype.start = function () { |
24 | | - var self = this |
25 | | - this.listener = function (e) { |
26 | | - var url = decodeURI(location.pathname + location.search) |
27 | | - if (this.root) { |
28 | | - url = url.replace(this.rootRE, '') |
| 25 | + start () { |
| 26 | + let self = this |
| 27 | + this.listener = function (e) { |
| 28 | + let url = decodeURI(location.pathname + location.search) |
| 29 | + if (this.root) { |
| 30 | + url = url.replace(this.rootRE, '') |
| 31 | + } |
| 32 | + self.onChange(url, e && e.state, location.hash) |
29 | 33 | } |
30 | | - self.onChange(url, e && e.state, location.hash) |
| 34 | + window.addEventListener('popstate', this.listener) |
| 35 | + this.listener() |
31 | 36 | } |
32 | | - window.addEventListener('popstate', this.listener) |
33 | | - this.listener() |
34 | | -} |
35 | 37 |
|
36 | | -HTML5History.prototype.stop = function () { |
37 | | - window.removeEventListener('popstate', this.listener) |
38 | | -} |
| 38 | + stop () { |
| 39 | + window.removeEventListener('popstate', this.listener) |
| 40 | + } |
39 | 41 |
|
40 | | -HTML5History.prototype.go = function (path, replace) { |
41 | | - var root = this.root |
42 | | - var url = this.formatPath(path, root) |
43 | | - if (replace) { |
44 | | - history.replaceState({}, '', url) |
45 | | - } else { |
46 | | - // record scroll position by replacing current state |
47 | | - history.replaceState({ |
48 | | - pos: { |
49 | | - x: window.pageXOffset, |
50 | | - y: window.pageYOffset |
51 | | - } |
52 | | - }, '') |
53 | | - // then push new state |
54 | | - history.pushState({}, '', url) |
| 42 | + go (path, replace) { |
| 43 | + let root = this.root |
| 44 | + let url = this.formatPath(path, root) |
| 45 | + if (replace) { |
| 46 | + history.replaceState({}, '', url) |
| 47 | + } else { |
| 48 | + // record scroll position by replacing current state |
| 49 | + history.replaceState({ |
| 50 | + pos: { |
| 51 | + x: window.pageXOffset, |
| 52 | + y: window.pageYOffset |
| 53 | + } |
| 54 | + }, '') |
| 55 | + // then push new state |
| 56 | + history.pushState({}, '', url) |
| 57 | + } |
| 58 | + let hashMatch = path.match(hashRE) |
| 59 | + let hash = hashMatch && hashMatch[0] |
| 60 | + path = url |
| 61 | + // strip hash so it doesn't mess up params |
| 62 | + .replace(hashRE, '') |
| 63 | + // remove root before matching |
| 64 | + .replace(this.rootRE, '') |
| 65 | + this.onChange(path, null, hash) |
55 | 66 | } |
56 | | - var hashMatch = path.match(hashRE) |
57 | | - var hash = hashMatch && hashMatch[0] |
58 | | - path = url |
59 | | - // strip hash so it doesn't mess up params |
60 | | - .replace(hashRE, '') |
61 | | - // remove root before matching |
62 | | - .replace(this.rootRE, '') |
63 | | - this.onChange(path, null, hash) |
64 | | -} |
65 | 67 |
|
66 | | -HTML5History.prototype.formatPath = function (path) { |
67 | | - return path.charAt(0) === '/' |
68 | | - // absolute path |
69 | | - ? this.root |
70 | | - ? this.root + '/' + path.replace(/^\//, '') |
71 | | - : path |
72 | | - : util.resolvePath(this.base || location.pathname, path) |
| 68 | + formatPath (path) { |
| 69 | + return path.charAt(0) === '/' |
| 70 | + // absolute path |
| 71 | + ? this.root |
| 72 | + ? this.root + '/' + path.replace(/^\//, '') |
| 73 | + : path |
| 74 | + : resolvePath(this.base || location.pathname, path) |
| 75 | + } |
73 | 76 | } |
74 | | - |
75 | | -module.exports = HTML5History |
|
0 commit comments