2020-12-17 13:18:18 -08:00
|
|
|
export default {
|
|
|
|
methods: {
|
2022-04-26 11:53:15 -07:00
|
|
|
getHash() {
|
|
|
|
if (!process.client) { return null; }
|
|
|
|
|
|
|
|
return decodeURIComponent(window.location.hash.substr(1).replace(/=$/, ''));
|
|
|
|
},
|
2020-12-17 13:18:18 -08:00
|
|
|
handleHash(namespace, callback, checkAnchor = true) {
|
2022-04-26 11:53:15 -07:00
|
|
|
if (!process.client) { return; }
|
2020-12-17 13:18:18 -08:00
|
|
|
|
2021-07-02 13:32:23 -07:00
|
|
|
const doHandle = () => {
|
2022-04-26 11:53:15 -07:00
|
|
|
const anchor = this.getHash();
|
2020-12-17 13:18:18 -08:00
|
|
|
const $anchor = document.getElementById(anchor);
|
|
|
|
if (checkAnchor && $anchor) {
|
|
|
|
$anchor.scrollIntoView();
|
|
|
|
} else if (!namespace) {
|
|
|
|
callback(anchor);
|
|
|
|
} else if (anchor.startsWith(namespace + '/')) {
|
|
|
|
callback(anchor.substring(namespace.length + 1));
|
|
|
|
}
|
2021-07-02 13:32:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
setTimeout(_ => {
|
|
|
|
doHandle();
|
|
|
|
window.addEventListener('hashchange', function() {
|
|
|
|
doHandle();
|
|
|
|
}, false);
|
2021-04-01 09:45:27 -07:00
|
|
|
}, 500);
|
2020-12-17 13:18:18 -08:00
|
|
|
},
|
|
|
|
setHash(namespace, value) {
|
2022-04-26 11:53:15 -07:00
|
|
|
if (!process.client) { return; }
|
|
|
|
const current = this.getHash();
|
|
|
|
const fullValue = namespace ? namespace + '/' + value : value
|
|
|
|
|
|
|
|
if (fullValue === current) { return; }
|
2020-12-17 13:18:18 -08:00
|
|
|
|
|
|
|
history.pushState(
|
|
|
|
'',
|
|
|
|
document.title,
|
|
|
|
window.location.pathname + window.location.search
|
2022-04-26 11:53:15 -07:00
|
|
|
+ (value ? '#' + fullValue : '')
|
2020-12-17 13:18:18 -08:00
|
|
|
);
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|