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(() => {
|
2019-11-27 14:33:32 +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 {
|
2019-11-27 14:33:32 +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-01-18 02:07:52 +05:30
|
|
|
initialLength: 0,
|
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
init() {
|
|
|
|
this.initialLength = window.history.length;
|
|
|
|
},
|
2017-01-29 21:06:21 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
/**
|
2019-12-07 16:58:52 +05:30
|
|
|
* @returns {boolean} - whether history.back() can be safetly called
|
2019-11-27 14:33:32 +05:30
|
|
|
*/
|
|
|
|
canGoBack() {
|
|
|
|
return (
|
|
|
|
document.referrer.includes(`${location.protocol}//${location.host}`) ||
|
|
|
|
this.initialLength < window.history.length
|
|
|
|
);
|
|
|
|
},
|
2017-01-31 11:35:36 +05:30
|
|
|
};
|