| 10 | updateRoute(); |
| 11 | |
| 12 | function updateRoute(noConfirm) { |
| 13 | const hash = window.location.hash.slice(1); |
| 14 | if (noConfirm || !route.confirmChange) { |
| 15 | const [pathname, search = ''] = hash.split('?'); |
| 16 | /** |
| 17 | * @typedef {Object} VMRoute |
| 18 | * @prop {string} hash - entire hash without # e.g. 'path/name?foo=1&bar=2' |
| 19 | * @prop {string} pathname - 'path/name' |
| 20 | * @prop {string[]} paths - ['path', 'name'] |
| 21 | * @prop {StringMap} query - {foo: '1', bar: '2'} |
| 22 | */ |
| 23 | Object.assign(route, { |
| 24 | hash, |
| 25 | pathname, |
| 26 | paths: pathname.split('/'), |
| 27 | query: loadQuery(search), |
| 28 | }); |
| 29 | } else if (route.hash !== hash) { |
| 30 | // restore the pinned route |
| 31 | setRoute(route.hash, false, true); |
| 32 | route.confirmChange(hash); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | // popstate should be the first to ensure hashchange listeners see the correct lastRoute |
| 37 | addEventListener('popstate', () => stack.pop()); |