-
Notifications
You must be signed in to change notification settings - Fork 1
/
router.js
140 lines (126 loc) · 3.75 KB
/
router.js
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import VueRouter from "vue-router";
import store from "@/store/index";
import AnalyserWarnings from "@/views/AnalyserWarnings.vue";
import ConfirmAutoAnalyser from "@/views/ConfirmAutoAnalyser.vue";
import ConfirmNewUpdate from "@/views/ConfirmNewUpdate.vue";
import ConfirmSelectedShortcuts from "@/views/ConfirmSelectedShortcuts.vue";
import FoundInserts from "@/views/FoundInserts.vue";
import FoundSnippets from "@/views/FoundSnippets.vue";
import HelpBugReport from "@/views/HelpBugReport.vue";
import HelpDocumentation from "@/views/HelpDocumentation.vue";
import HelpFAQ from "@/views/HelpFAQ.vue";
import HelpGetStarted from "@/views/HelpGetStarted.vue";
import HelpKeyboardShortcuts from "@/views/HelpKeyboardShortcuts.vue";
import HelpMenu from "@/views/HelpMenu.vue";
import ImportShortcuts from "@/views/ImportShortcuts.vue";
import ListiCloudUrls from "@/views/ListiCloudUrls.vue";
import ListSnippets from "@/views/ListSnippets.vue";
import MainMenu from "@/views/MainMenu.vue";
import MergeSnippetsIntoShortcut from "@/views/MergeSnippetsIntoShortcut.vue";
import OpenApp from "@/views/OpenApp.vue";
import PrefAutoLoadShortcuts from "@/views/PrefAutoLoadShortcuts.vue";
import Preferences from "@/views/Preferences.vue";
import PrefLanguage from "@/views/PrefLanguage.vue";
import ProcessShortcuts from "@/views/ProcessShortcuts.vue";
import SelectShortcuts from "@/views/SelectShortcuts.vue";
import SnippetActions from "@/views/SnippetActions.vue";
/** @type { import("vue").ComponentOptions[] } */
const comps = [
AnalyserWarnings,
ConfirmAutoAnalyser,
ConfirmNewUpdate,
ConfirmSelectedShortcuts,
FoundInserts,
FoundSnippets,
HelpBugReport,
HelpDocumentation,
HelpFAQ,
HelpGetStarted,
HelpKeyboardShortcuts,
HelpMenu,
ImportShortcuts,
ListiCloudUrls,
ListSnippets,
MainMenu,
MergeSnippetsIntoShortcut,
OpenApp,
PrefAutoLoadShortcuts,
Preferences,
PrefLanguage,
ProcessShortcuts,
SelectShortcuts,
SnippetActions,
];
const compNames = comps.map((c) => c.name);
const router = new VueRouter({
routes: comps.map((comp) => {
const name = comp.name;
return {
path: name === "MainMenu" ? "/" : "/" + name,
name: name,
component: comp,
props: true,
};
}),
fallback: false,
mode: "abstract",
});
/** @type {Map<string, {scrollPos: {x: number, y: number}}>} */
const compSettings = new Map();
// ========= intercept navigation ==========
let historyNavigation = false;
/**
* @param {Function} callback
*/
function historyHandler(callback) {
return function(...args) {
historyNavigation = true;
callback(...args);
};
}
const go = router.go.bind(router);
router.go = historyHandler(go);
// ===== /intercept navigation ======
router.beforeEach((to, from, next) => {
if (compNames.includes(from.name)) {
compSettings.set(from.name, {
scrollPos: {
x: window.scrollX,
y: window.scrollY,
},
});
}
next();
});
/**
* @param {import("vue-router").Route} to
* @param {import("vue-router").Route} from
*/
function scrollBehavior(to, from) {
let position = {
x: 0,
y: 0,
};
if (historyNavigation && compSettings.has(to.name)) {
position = compSettings.get(to.name).scrollPos;
}
return position;
}
/** @param {import("vue-router").Route} route */
export function routeToTitle(route) {
const compLang =
store.state.language[route.name[0].toLowerCase() + route.name.substr(1)];
return compLang && compLang.title;
}
export const titlePrefix = "CopyPaste Actions - ";
router.afterEach((to, from) => {
document.title = titlePrefix + (routeToTitle(to) || to.name);
const pos = scrollBehavior(to, from);
if (pos) {
setTimeout(
() => window.scrollTo({ left: pos.x, top: pos.y, behavior: "auto" }),
0,
);
}
});
export default router;