forked from galaxyproject/galaxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrouter-push.js
More file actions
52 lines (51 loc) · 2.21 KB
/
router-push.js
File metadata and controls
52 lines (51 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { getGalaxyInstance } from "app";
import { addSearchParams } from "utils/url";
/**
* Is called before the regular router.push() and allows us to provide logs,
* handle the window manager, avoid duplication warnings, and force a component
* refresh if needed.
*
* @param {String} Location as parsed to original router.push()
* @param {Object} Custom options, to provide a title, force reload, and/or prevent window manager
*/
export function patchRouterPush(VueRouter) {
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location, options = {}) {
// add key to location to force component refresh
const { title, force, preventWindowManager } = options;
if (force) {
// since location can either be string or object, we need to pass the string url to addSearchParams
if (typeof location === "string") {
location = addSearchParams(location, { __vkey__: Date.now() });
} else if (typeof location === "object") {
// convert to string version addSearchParams can handle
let url = this.resolve(location).href;
url = addSearchParams(url, { __vkey__: Date.now() });
// convert back to object version
location = this.resolve(url).route;
}
}
// verify if confirmation is required
if (this.confirmation) {
if (confirm("There are unsaved changes which will be lost.")) {
this.confirmation = undefined;
} else {
return;
}
}
// show location in window manager
const Galaxy = getGalaxyInstance();
if (title && !preventWindowManager && Galaxy.frame && Galaxy.frame.active) {
Galaxy.frame.add({ title: title, url: location });
return;
}
// always emit event, even when a duplicate route is pushed
this.app.$emit("router-push");
// avoid console warning when user clicks to revisit same route
return originalPush.call(this, location).catch((err) => {
if (err.name !== "NavigationDuplicated") {
throw err;
}
});
};
}