-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstate.js
80 lines (61 loc) · 1.92 KB
/
state.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
/**
* Utilities for keeping GLOBAL_PROPERTIES in sync with localStore and the url hash.
*/
let GLOBAL_PROPERTIES = {
'locus': 'chr15:92835700-93031800',
'tracks': '',
}
const initGlobalProperties = () => {
// get properties from localStorage
const propertiesFromLocalStore = Object.keys(GLOBAL_PROPERTIES).reduce((result, key) => {
const value = localStorage.getItem(key)
if (value !== null) {
result[key] = value
}
return result
}, {})
// get properties from url hash
const hash = window.location.hash.substr(1)
const propertiesFromUrl = hash.split('&').reduce((result, item) => {
if (item) {
const [key, value] = item.split('=')
result[decodeURIComponent(key)] = decodeURIComponent(value)
}
return result
}, {})
GLOBAL_PROPERTIES = {...GLOBAL_PROPERTIES, ...propertiesFromLocalStore, ...propertiesFromUrl}
updateUrlHash()
console.log('GLOBAL_PROPERTIES:', GLOBAL_PROPERTIES)
}
const updateUrlHash = () => {
const updatedUrlHash = Object.entries(GLOBAL_PROPERTIES).map(
([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`
).join('&')
window.location.hash = updatedUrlHash
}
const getGlobalProperty = key => GLOBAL_PROPERTIES[key]
const setGlobalProperty = (key, value) => {
localStorage.setItem(key, value)
//update url hash
GLOBAL_PROPERTIES[key] = value
updateUrlHash()
}
const getLocus = () => getGlobalProperty('locus')
const updateLocus = (locus) => {
setGlobalProperty('locus', locus)
}
const getTrackList = () => {
return GLOBAL_PROPERTIES.tracks ? GLOBAL_PROPERTIES.tracks.split(',') : []
}
const isTrackShown = (name) => {
return getTrackList().indexOf(name) !== -1
}
const updateTrack = (name, isSelected) => {
let trackList = getTrackList()
if (isSelected) {
trackList.push(name)
} else {
trackList = trackList.filter(item => item !== name)
}
setGlobalProperty('tracks', trackList.join(','))
}