forked from mixtern/3k-project
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpersist.js
192 lines (134 loc) · 5.19 KB
/
persist.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;
var observer = new MutationObserver(function(mutations) {
saveState();
});
const storagePrefix = "state.";
const propertyPrefix = "$property.";
var watchedElements ={
'#chord-color':propertyPrefix+'value',
'#tonic-enabled':propertyPrefix+'checked',
'#tonic-degree-enabled':propertyPrefix+'checked',
'#alt-color':propertyPrefix+'value',
'#alt-enabled':propertyPrefix+'checked',
'#tonic-color':propertyPrefix+'value',
'#label-size':propertyPrefix+'value',
'#label-color':propertyPrefix+'value',
'#overall-label-size':propertyPrefix+'value',
'#overall-label-placement':propertyPrefix+'value',
'#arrow-color':propertyPrefix+'value',
'#arrow-thickness':propertyPrefix+'value',
'#arrow-simplification':propertyPrefix+'value',
'#arrow-transparency':propertyPrefix+'value',
'#arrow-snap':propertyPrefix+'checked',
'#fill-circlefill':propertyPrefix+'checked',
'#fill-bgcolor':propertyPrefix+'value',
'#fill-margin':propertyPrefix+'value',
'#settings-width':propertyPrefix+'value',
};
var persistedObjects = new Object();
var persistedObjectsCallbacks = new Object();
var persistedObjectsBeforeRestoreCallbacks = {};
window.addEventListener('load', function () {
restoreState();
//Start observing attribute changes on persisted elements
for (const selector in watchedElements) {
var target = document.querySelector(selector);
if(null == target)
continue;
target.addEventListener('input', function(){saveState();});
observer.observe(target,{attributes:true});
}
redraw();
});
const fallback = a => a;
function persistObject(key, target, onRestored, onBeforeRestored)
{
persistedObjects[key] = target;
persistedObjectsCallbacks[key] = onRestored;
persistedObjectsBeforeRestoreCallbacks[key] = onBeforeRestored || fallback;
}
function createStateSnapshot(){
var snapshot = {
objects:JSON.stringify(persistedObjects),
elements:{},
arrows:Array.from(activeArrows),
labels:Array.from(activeLabels),
keys: JSON.stringify(keys),
circle: JSON.stringify(chordDefinitions),
};
for (const selector in watchedElements) {
var attributeOrProperty = watchedElements[selector];
var target = document.querySelector(selector);
if(null == target)
continue;
var value = target.getAttribute(attributeOrProperty);
if(attributeOrProperty.startsWith(propertyPrefix))
value = target[attributeOrProperty.substring(propertyPrefix.length)];
snapshot.elements[storagePrefix+selector] =JSON.stringify(value);
}
return snapshot;
}
function restoreStateSnapshot(snapshot){
for (const selector in watchedElements) {
var attributeOrProperty = watchedElements[selector];
var value = snapshot.elements[storagePrefix+selector];
if(undefined === value)
continue;
value = JSON.parse(value);
var target = document.querySelector(selector);
if(null == target)
continue;
if(attributeOrProperty.startsWith(propertyPrefix))
target[attributeOrProperty.substring(propertyPrefix.length)] = value;
else
target.setAttribute(attributeOrProperty,value);
}
const oldStates = snapshot.objects;
if(undefined === oldStates)
return;
const restored = JSON.parse(oldStates);
for (var key in persistedObjects) {
if(!restored.hasOwnProperty(key))
continue;
let restoredItem = restored[key];
if(persistedObjectsBeforeRestoreCallbacks.hasOwnProperty(key))
restoredItem = persistedObjectsBeforeRestoreCallbacks[key](restoredItem);
Object.assign(persistedObjects[key],restoredItem);
}
if(Array.isArray(snapshot.arrows))
activeArrows = Array.from(snapshot.arrows);
if(Array.isArray(snapshot.labels))
activeLabels = Array.from(snapshot.labels);
if(typeof snapshot.keys!= 'undefined' && snapshot.keys!=null)
keys = JSON.parse(snapshot.keys);
if(typeof snapshot.circle!= 'undefined' && snapshot.circle!=null)
{
const restoredCircle = JSON.parse(snapshot.circle);
for (let i = 0; i < chordDefinitions.length; i++) {
Object.assign(chordDefinitions[i],restoredCircle[i]);
}
}
window.setTimeout(function() {
for (var key in persistedObjects) {
persistedObjectsCallbacks[key]();
}
return false;
},200); //hack
}
function saveState()
{
localStorage.setItem('lastState',JSON.stringify(createStateSnapshot()));
}
function restoreState()
{
var value = localStorage.getItem('lastState');
if(undefined === value)
return;
var snapshot = JSON.parse(value);
//Dont restore arrow state on regular persist event
snapshot.arrows= [];
snapshot.labels=[];
snapshot.keys = null;
snapshot.circle = null;
restoreStateSnapshot(snapshot);
}