This repository was archived by the owner on Feb 17, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathurlParameterHandler.js
More file actions
83 lines (74 loc) · 2.21 KB
/
Copy pathurlParameterHandler.js
File metadata and controls
83 lines (74 loc) · 2.21 KB
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
'use strict';
/* Problem:
* A bunch of different components want to update the url parameters
* But they don't know about each other.
* They add, they remove, they do it at crazy times.
* So we get a long tail of url manipulation.
*
* Solution:
* hold a 'virtual' query string.
* let the various components update it as much as they want - go mad.
* Try to push it to history but ... debounce it :D
*/
const urlParameter = require('./urlParameter');
var virtualQueryString = '';
var liveQueryString = '';
var debounceTime = 500;
if (typeof window != 'undefined'){
var windowRef = window;
virtualQueryString = windowRef.location.search;
virtualQueryString = windowRef.location.search;
}
//Replace me with an import if you have debounce already!
function debounce(func, wait, immediate) {
var timeout;
return function() {
var context = this,
args = arguments;
var later = function() {
timeout = null;
if ( !immediate ) {
func.apply(context, args);
}
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait || 200);
if ( callNow ) {
func.apply(context, args);
}
};
};
var setHistroy = function(){
if (typeof windowRef != 'undefined'){
if (windowRef.history) {
windowRef.history.pushState(null, '', windowRef.pathname + virtualQueryString);
liveQueryString = virtualQueryString;
} else {
console.log('No window.history :(');
}
} else {
console.log('No window to set histroy on :(');
}
};
var updateUrl = debounce(setHistroy, debounceTime);
module.exports = {
get(paramName, isEncoded){
return urlParameter.get(paramName, virtualQueryString, isEncoded);
},
set(paramName, value, isEncoded){
var newQueryString = urlParameter.set(paramName, value, virtualQueryString, isEncoded);
virtualQueryString = newQueryString;
updateUrl();
return newQueryString;
},
config(options){
if (options.hasOwnProperty('debounce')) { debounce = options.debounce; };
if (options.hasOwnProperty('debounceTime')) { debounceTime = options.debounceTime; };
if (options.hasOwnProperty('windowReplacement')) { windowRef = options.windowReplacement; };
return true; //return false for err?
},
getLiveQueryString(){
return liveQueryString;
}
}