-
Notifications
You must be signed in to change notification settings - Fork 17
/
urlParamService.js
127 lines (108 loc) · 3.9 KB
/
urlParamService.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
let urlParamService = {};
urlParamService.params = {
PARAM_DEMO_MODE: 'demo',
PARAM_SCANNING: 'scanning',
PARAM_HUFFMAN: 'huffman',
PARAM_DIR_INPUT: 'direction',
PARAM_RESET_DATABASE: 'reset',
PARAM_USE_GRIDSET_FILENAME: 'gridset_filename',
PARAM_USE_GRIDSET_URL: 'gridset_url',
PARAM_ELEMENT_HIGHLIGHT_IDS: "highlightIds"
};
let _demoMode = false;
let _alreadyResetted = false;
/**
* sets many params to search params in the current URL
* @param params an object containing key/value pairs to add
*/
urlParamService.setParamsToSearchQuery = function(params = {}) {
let queryParams = new URLSearchParams(location.search);
for (let key of Object.keys(params)) {
if (params[key]) {
queryParams.set(key, JSON.stringify(params[key]));
} else {
queryParams.delete(key);
}
}
setURLSearchParamsToURL(queryParams);
}
/**
* gets an object with keys/values of the search query params of the current url
* @additionalParams object with additional params, which are added to the search query params
*/
urlParamService.getSearchQueryParams = function(additionalParams = {}) {
let params = new URLSearchParams(location.search);
let paramObject = Object.fromEntries(params);
for (let key of Object.keys(paramObject)) {
try {
paramObject[key] = JSON.parse(paramObject[key]);
} catch (e) {
}
}
return Object.assign(paramObject, additionalParams);
};
urlParamService.isDemoMode = function () {
_demoMode = _demoMode || hasParam(urlParamService.params.PARAM_DEMO_MODE);
removeParam(urlParamService.params.PARAM_DEMO_MODE);
return _demoMode;
};
urlParamService.isScanningEnabled = function () {
return hasParam(urlParamService.params.PARAM_SCANNING) && !isParamFalse(urlParamService.params.PARAM_SCANNING);
};
urlParamService.isDirectionEnabled = function () {
return hasParam(urlParamService.params.PARAM_DIR_INPUT) && !isParamFalse(urlParamService.params.PARAM_DIR_INPUT);
};
urlParamService.isHuffmanEnabled = function () {
return hasParam(urlParamService.params.PARAM_HUFFMAN) && !isParamFalse(urlParamService.params.PARAM_HUFFMAN);
};
urlParamService.shouldResetDatabase = function () {
let shouldReset =
!_alreadyResetted && (urlParamService.isDemoMode() || isParamTrue(urlParamService.params.PARAM_RESET_DATABASE));
_alreadyResetted = true;
return shouldReset;
};
urlParamService.getParam = function (name) {
return getParam(name);
}
urlParamService.removeParam = function(name) {
let searchParams = new URLSearchParams(window.location.search);
searchParams.delete(name);
setURLSearchParamsToURL(searchParams);
}
function setURLSearchParamsToURL (searchParams) {
let questionMark = searchParams.size > 0 ? "?" : "";
history.replaceState(null, null, `${location.pathname}${questionMark}${searchParams.toString()}${location.hash}`);
}
function hasParam(name) {
let searchParams = new URLSearchParams(window.location.search);
return searchParams.has(name);
}
function getParam(name) {
let searchParams = new URLSearchParams(window.location.search);
return searchParams.get(name);
}
function isParamTrue(name) {
return getParam(name) === 'true';
}
function isParamFalse(name) {
return getParam(name) === 'false';
}
function removeParam(paramName) {
if (!hasParam(paramName)) {
return;
}
let searchParams = new URLSearchParams(window.location.search);
searchParams.delete(paramName);
if (history.replaceState) {
let searchString = searchParams.toString().length > 0 ? '?' + searchParams.toString() : '';
let newUrl =
window.location.protocol +
'//' +
window.location.host +
window.location.pathname +
searchString +
window.location.hash;
history.replaceState(null, '', newUrl);
}
}
export { urlParamService };