forked from algorithm-visualizer/algorithm-visualizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.js
104 lines (87 loc) · 2.26 KB
/
helpers.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
'use strict';
const app = require('../app');
const Toast = require('../dom/toast');
const checkLoading = () => {
if (app.getIsLoading()) {
Toast.showErrorToast('Wait until it completes loading of previous file.');
return true;
}
return false;
};
const getParameterByName = (name) => {
const url = window.location.href;
const regex = new RegExp(`[?&]${name}(=([^&#]*)|&|#|$)`);
const results = regex.exec(url);
if (!results || results.length !== 3) {
return null;
}
const [, , id] = results;
return id;
};
const getHashValue = (key)=> {
if (!key) return null;
const hash = window.location.hash.substr(1);
const params = hash ? hash.split('&') : [];
for (let i = 0; i < params.length; i++) {
const pair = params[i].split('=');
if (pair[0] === key) {
return pair[1];
}
}
return null;
};
const setHashValue = (key, value)=> {
if (!key || !value) return;
const hash = window.location.hash.substr(1);
const params = hash ? hash.split('&') : [];
let found = false;
for (let i = 0; i < params.length && !found; i++) {
const pair = params[i].split('=');
if (pair[0] === key) {
pair[1] = value;
params[i] = pair.join('=');
found = true;
}
}
if (!found) {
params.push([key, value].join('='));
}
const newHash = params.join('&');
window.location.hash = `#${newHash}`;
};
const removeHashValue = (key) => {
if (!key) return;
const hash = window.location.hash.substr(1);
const params = hash ? hash.split('&') : [];
for (let i = 0; i < params.length; i++) {
const pair = params[i].split('=');
if (pair[0] === key) {
params.splice(i, 1);
break;
}
}
const newHash = params.join('&');
window.location.hash = `#${newHash}`;
};
const setPath = (category, algorithm, file) => {
const path = category ? category + (algorithm ? `/${algorithm}` + (file ? `/${file}` : '') : '') : '';
setHashValue('path', path);
};
const getPath = () => {
const hash = getHashValue('path');
if (hash) {
const [ category, algorithm, file ] = hash.split('/');
return { category, algorithm, file };
} else {
return false;
}
};
module.exports = {
checkLoading,
getParameterByName,
getHashValue,
setHashValue,
removeHashValue,
setPath,
getPath
};