forked from mattias-persson/url-parameters
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
175 lines (132 loc) · 3.41 KB
/
index.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import LocationBar from "location-bar";
const locationBarInstance = new LocationBar();
function Url() {}
Url.prototype.enable = function (onChangeCallback, pushState) {
var self = this;
// When the URL changes, run provided callback.
if (onChangeCallback) {
locationBarInstance.onChange(function (queryString) {
onChangeCallback({
queryParams: self.getParams(),
queryString: queryString,
});
});
}
// Push state defaults to true but can be disabled.
if (typeof pushState !== "boolean") {
pushState = true;
}
// Start the URL listener.
locationBarInstance.start({ pushState: pushState });
};
Url.prototype.apply = function (params) {
if (typeof params === "object") {
params = this.objToQueryString(params);
}
if (params.substring(0, 1) !== "?" && params.length > 0) {
params = "?" + params;
}
locationBarInstance.update(params, { trigger: true });
};
Url.prototype.set = function (param, value) {
var params = this.getParams();
params[param] = value;
this.apply(params);
};
Url.prototype.get = function (param, fallback) {
if (typeof fallback === "undefined" || fallback === null) {
fallback = null;
}
var params = this.getParams();
if (params[param]) {
return params[param];
}
return fallback;
};
Url.prototype.getQueryString = function () {
return this.objToQueryString(this.getParams());
};
Url.prototype.toggle = function (param, value) {
var params = this.getParams();
if (params[param]) {
delete params[param];
} else {
params[param] = value;
}
this.apply(params);
};
Url.prototype.toggleValue = function (param, value) {
var params = this.getParams();
if (params[param]) {
var values = params[param].split(",");
var valuePosition = values.indexOf(value.toString());
if (valuePosition > -1) {
values.splice(valuePosition, 1);
} else {
values.push(value);
}
if (!values.length) {
delete params[param];
} else {
params[param] = values.join(",");
}
} else {
params[param] = value;
}
this.apply(params);
};
Url.prototype.replace = function (values) {
var params = this.getParams();
Object.keys(values).map(function (key) {
params[key] = values[key];
});
this.apply(params);
};
Url.prototype.remove = function (param) {
var params = this.getParams();
if (params[param]) {
delete params[param];
}
this.apply(params);
};
Url.prototype.clear = function () {
this.apply("");
};
Url.prototype.containsValue = function (param, value) {
var params = this.getParams();
if (!params[param]) {
return false;
}
return params[param].split(",").indexOf(value.toString()) > -1;
};
Url.prototype.objToQueryString = function (obj) {
var str = [];
for (var p in obj) {
if (obj.hasOwnProperty(p)) {
str.push(encodeURIComponent(p) + "=" + obj[p]);
}
}
return str.join("&");
};
Url.prototype.getParams = function () {
var search;
if (location.hash) {
search = location.hash.substring(2);
} else if (location.search) {
search = location.search.substring(1);
}
if (!search) {
return {};
}
// Handle any backslashes in the string to avoid parsing errors.
search = search.replace("\\", "\\\\");
return JSON.parse(
'{"' +
decodeURI(search)
.replace(/"/g, '\\"')
.replace(/&/g, '","')
.replace(/=/g, '":"') +
'"}'
);
};
export default new Url();