1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | export const BunnyURL = {
|
9 |
|
10 | |
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | getParam(get_param, url = window.location.href) {
|
21 | const params = this.getParams(url);
|
22 | if (params === undefined) {
|
23 | return undefined;
|
24 | }
|
25 | return params[get_param];
|
26 | },
|
27 |
|
28 |
|
29 |
|
30 | |
31 |
|
32 |
|
33 |
|
34 |
|
35 | getQueryString(url = window.location.href) {
|
36 | const pos = url.indexOf('?');
|
37 | let hashPos = url.indexOf('#');
|
38 | if (hashPos > -1) {
|
39 | return pos > -1 ? decodeURI(url.slice(pos + 1, hashPos)) : undefined;
|
40 | }
|
41 | return pos > -1 ? decodeURI(url.slice(pos + 1)) : undefined;
|
42 | },
|
43 |
|
44 |
|
45 |
|
46 | getHash(url = window.location.href) {
|
47 | const pos = url.indexOf('#');
|
48 | return pos > -1 ? decodeURI(url.slice(pos + 1)) : undefined;
|
49 | },
|
50 |
|
51 |
|
52 |
|
53 | getURLWithoutQueryString(url = window.location.href) {
|
54 | const pos = url.indexOf('?');
|
55 | return pos > -1 ? decodeURI(url.slice(0, pos)) : url;
|
56 | },
|
57 |
|
58 |
|
59 |
|
60 | |
61 |
|
62 |
|
63 |
|
64 |
|
65 | getParams(url = window.location.href) {
|
66 | const query = this.getQueryString(url);
|
67 | if (query === undefined) {
|
68 | return undefined;
|
69 | }
|
70 | const params = {};
|
71 | const hashes = query.split('&');
|
72 | for(let i = 0; i < hashes.length; i++) {
|
73 | let hash = hashes[i].split('=');
|
74 | if (hash[0].indexOf('[]') !== -1) {
|
75 |
|
76 | if (params[hash[0]] === undefined) {
|
77 | params[hash[0]] = [];
|
78 | }
|
79 | params[hash[0]].push(hash[1]);
|
80 | } else {
|
81 | params[hash[0]] = hash[1];
|
82 | }
|
83 | }
|
84 | return params;
|
85 | },
|
86 |
|
87 |
|
88 | |
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 | hasParam(param, url = window.location.href) {
|
96 | return this.getParam(param, url) !== undefined;
|
97 | },
|
98 |
|
99 | setParam(param, value, url = window.location.href) {
|
100 | let params = this.getParams(url);
|
101 | if (params === undefined) {
|
102 | params = {};
|
103 | }
|
104 | params[param] = value;
|
105 | return this.replaceParams(params, url);
|
106 | },
|
107 |
|
108 | generateQueryString(params) {
|
109 | let i = 0;
|
110 | let queryStr = '';
|
111 | for(let k in params) {
|
112 | queryStr += i === 0 ? '?' : '&';
|
113 | queryStr += k + '=' + params[k];
|
114 | i++;
|
115 | }
|
116 | return queryStr;
|
117 | },
|
118 |
|
119 | replaceParams(params, url = window.location.href) {
|
120 | let newUrl = this.getURLWithoutQueryString(url) + this.generateQueryString(params);
|
121 | const hash = this.getHash(url);
|
122 | if (hash !== undefined) {
|
123 | newUrl += '#' + hash;
|
124 | }
|
125 | return newUrl;
|
126 | },
|
127 |
|
128 | setParams(params, url = window.location.href) {
|
129 | const urlParams = this.getParams(url);
|
130 | for (let k in params) {
|
131 | urlParams[k] = params[k];
|
132 | }
|
133 | return this.replaceParams(urlParams, url);
|
134 | },
|
135 |
|
136 | removeParam(param, url = window.location.href) {
|
137 | const params = this.getParams(url);
|
138 | if (params[param] !== undefined) {
|
139 | delete params[param];
|
140 | }
|
141 | return this.replaceParams(params, url);
|
142 | }
|
143 |
|
144 | };
|