2017-01-29 21:06:21 +05:30
|
|
|
/**
|
|
|
|
* A helper wrapper service around window.history
|
|
|
|
*/
|
|
|
|
|
2020-01-18 02:07:52 +05:30
|
|
|
import { createBrowserHistory, History } from 'history';
|
2017-05-26 00:41:57 +05:30
|
|
|
|
|
|
|
export const browserHistory = createBrowserHistory();
|
|
|
|
|
|
|
|
browserHistory.listen(() => {
|
2020-05-24 04:38:24 +05:30
|
|
|
patchHistory(browserHistory);
|
2017-05-26 00:41:57 +05:30
|
|
|
});
|
|
|
|
|
2020-01-18 02:07:52 +05:30
|
|
|
function patchHistory(history: History): void {
|
2020-05-24 04:38:24 +05:30
|
|
|
Object.assign(history.location, {
|
|
|
|
query: new URLSearchParams(history.location.search),
|
|
|
|
});
|
2017-05-26 00:41:57 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
patchHistory(browserHistory);
|
|
|
|
|
2017-01-29 21:06:21 +05:30
|
|
|
export default {
|
2020-05-24 04:38:24 +05:30
|
|
|
initialLength: 0,
|
|
|
|
|
|
|
|
init() {
|
|
|
|
this.initialLength = window.history.length;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns {boolean} - whether history.back() can be safetly called
|
|
|
|
*/
|
|
|
|
canGoBack() {
|
|
|
|
return (
|
|
|
|
document.referrer.includes(`${location.protocol}//${location.host}`) ||
|
|
|
|
this.initialLength < window.history.length
|
|
|
|
);
|
|
|
|
},
|
2017-01-31 11:35:36 +05:30
|
|
|
};
|