forked from webduinoio/webduino-blockly
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage-sim.js
More file actions
279 lines (242 loc) · 8.17 KB
/
Copy pathstorage-sim.js
File metadata and controls
279 lines (242 loc) · 8.17 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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
'use strict';
// Create a namespace.
var BlocklySimStorage = {};
BlocklySimStorage.STORAGE_URL = 'https://glowing-fire-4998.firebaseio.com';
BlocklySimStorage.LOCALSTORAGE_KEY = 'simulator';
BlocklySimStorage.isLinkUrl = function () {
var op = ['openModal', 'close'];
return window.location.hash.length > 1 && op.indexOf(window.location.hash.substring(1)) === -1;
};
/**
* Backup simulator config to localStorage.
* @param {Element} area - Dom Element
* @param {Element} frame - Dom Element
* @private
*/
BlocklySimStorage.backupBlocks_ = function (area, frame) {
if ('localStorage' in window) {
var storage = BlocklySimStorage.getConfig_(area, frame);
window.localStorage.setItem(BlocklySimStorage.LOCALSTORAGE_KEY, JSON.stringify(storage));
}
};
/**
* Get simulator config
* @param {element} area - DOM element
* @param {element} frame - DOM Element
* @return {object} storage - Simulator config
* @private
*/
BlocklySimStorage.getConfig_ = function (area, frame) {
var storage = {};
storage.config = frame.contentWindow.blockly.getConfig();
storage.opened = area.classList.contains('show');
storage.isFull = area.classList.contains('full');
storage.width = area.dataset.width;
storage.height = area.dataset.height;
return storage;
};
/**
* Save simulator config to localStorage when window unloaded.
* @param {element} area - DOM element
* @param {element} frame - DOM Element
*/
BlocklySimStorage.backupOnUnload = function (area, frame) {
window.addEventListener('unload',
function () {
if (!BlocklySimStorage.isLinkUrl()) {
BlocklySimStorage.backupBlocks_(area, frame);
}
}, false);
};
/**
* Set config to frame
* @param {element} area - DOM element
* @param {element} frame - DOM Element
* @param {element} toggleBtn - DOM element
* @param {object} storage - Simulator config
* @param {function} cb - callback function
* @private
*/
BlocklySimStorage.setConfig_ = function (area, frame, toggleBtn, storage, cb) {
if (!frame.contentWindow.blockly) {
frame.contentWindow.addEventListener('load', function aa() {
frame.contentWindow.removeEventListener('load', aa);
BlocklySimStorage.setConfig_(area, frame, toggleBtn, storage, cb);
});
return;
}
cb = cb || function() {};
var api = frame.contentWindow.blockly;
// 寬、高值儲存在元素屬性上,全畫面放大/縮小的依據
storage.width && (area.dataset.width = storage.width);
storage.height && (area.dataset.height = storage.height);
// 依 blockly 目前的語系
api.lang(Code.getLang());
// 設定 area 寬高
if (storage.isFull) {
area.classList.add('full');
} else {
storage.width && (area.style.width = storage.width);
storage.height && (area.style.height = storage.height);
}
// 設定 area 顯示/隱藏
if (storage.opened) {
area.classList.add('show');
toggleBtn.classList.add('opened');
} else {
area.classList.remove('show');
toggleBtn.classList.remove('opened');
}
// 之前存檔的回復
api.setConfig(storage.config, cb);
};
/**
* Restore simulator config from localStorage
* @param {Element} area - DOM Element
* @param {Element} frame - DOM Element
* @param {Element} toggleBtn - DOM Element
*/
BlocklySimStorage.restoreBlocks = function (area, frame, toggleBtn) {
if ('localStorage' in window && window.localStorage[BlocklySimStorage.LOCALSTORAGE_KEY]) {
var storage = JSON.parse(window.localStorage[BlocklySimStorage.LOCALSTORAGE_KEY]);
BlocklySimStorage.setConfig_(area, frame, toggleBtn, storage);
}
};
/**
* Save simulator config to database
* @param {Element} area - DOM element
* @param {element} frame - DOM Element
* @param {element} toggleBtn - DOM element
*/
BlocklySimStorage.link = function (area, frame, toggleBtn) {
window.addEventListener('hashchange', hashChange, false);
function hashChange() {
window.removeEventListener('hashchange', hashChange);
var key = window.location.hash.substring(1);
var data = JSON.stringify(BlocklySimStorage.getConfig_(area, frame));
var workInfo = {
area: area,
frame: frame,
toggleBtn: toggleBtn,
key: key
};
BlocklySimStorage.makeRequest_('/blockly', 'simulator', data, workInfo);
}
};
/**
* Retrieve Simulator text from database using given key.
* @param {string} key - Key to Simulator
* @param {Element} area - DOM element
* @param {element} frame - DOM Element
* @param {element} toggleBtn - DOM element
*/
BlocklySimStorage.retrieveXml = function (key, area, frame, toggleBtn) {
var workInfo = {
area: area,
frame: frame,
toggleBtn: toggleBtn
};
BlocklySimStorage.makeRequest_('/blockly', 'key', key, workInfo);
};
/**
* Global reference to current AJAX request.
* @type {XMLHttpRequest}
* @private
*/
BlocklySimStorage.httpRequest_ = null;
/**
* Fire a new AJAX request.
* @param {string} url - URL to fetch.
* @param {string} name - Name of parameter.
* @param {string} content - Content of parameter.
* @param {object} workInfo - {area, frame, toggleBtn}
* @private
*/
BlocklySimStorage.makeRequest_ = function (url, name, content, workInfo) {
BlocklySimStorage.httpRequest_ = new XMLHttpRequest();
BlocklySimStorage.httpRequest_.name = name;
BlocklySimStorage.httpRequest_.onreadystatechange =
BlocklySimStorage.handleRequest_;
switch (name) {
case 'simulator':
BlocklySimStorage.httpRequest_.open('PATCH',
BlocklySimStorage.STORAGE_URL + url + '/' + workInfo.key + '.json');
BlocklySimStorage.httpRequest_.setRequestHeader('Content-Type',
'application/x-www-form-urlencoded');
BlocklySimStorage.httpRequest_.send(JSON.stringify({
simulator: content
}));
break;
case 'key':
BlocklySimStorage.httpRequest_.open('GET',
BlocklySimStorage.STORAGE_URL + url + '/' + content + '.json');
BlocklySimStorage.httpRequest_.send();
break;
}
BlocklySimStorage.httpRequest_.workInfo = workInfo;
};
/**
* Callback function for AJAX call.
* @private
*/
BlocklySimStorage.handleRequest_ = function () {
if (BlocklySimStorage.httpRequest_.readyState == 4) {
if (BlocklySimStorage.httpRequest_.status != 200) {
BlocklySimStorage.alert(BlocklySimStorage.HTTPREQUEST_ERROR + '\n' +
'httpRequest_.status: ' + BlocklySimStorage.httpRequest_.status);
} else {
var data = BlocklySimStorage.httpRequest_.responseText.trim();
var workInfo = BlocklySimStorage.httpRequest_.workInfo;
var area = workInfo.area;
var frame = workInfo.frame;
var toggleBtn = workInfo.toggleBtn;
var storage;
if (BlocklySimStorage.httpRequest_.name == 'simulator') {
BlocklySimStorage.monitorChanges_(workInfo);
} else if (BlocklySimStorage.httpRequest_.name == 'key') {
if (!data.length) return;
data = JSON.parse(data).simulator;
// 相容舊的情況
if (data) {
storage = JSON.parse(data);
BlocklySimStorage.setConfig_(area, frame, toggleBtn, storage, function () {
BlocklySimStorage.monitorChanges_(workInfo);
});
}
}
}
BlocklySimStorage.httpRequest_ = null;
}
};
/**
* Start monitoring the iframe. If a change is made that changes the iframe,
* clear the key from the URL. Stop monitoring the iframe once such a
* change is detected.
* @param {object} workInfo - {area, frame, toggleBtn}
* @private
*/
BlocklySimStorage.monitorChanges_ = function (workInfo) {
var frame = workInfo.frame;
var win = frame.contentWindow;
var MutationObserver = win.MutationObserver
|| win.WebKitMutationObserver
|| win.MozMutationObserver;
var observeMutationSupport = !!MutationObserver;
if (!observeMutationSupport) {
console.warn('No support MutationObserver!');
return;
}
function change() {
observer.disconnect();
observer = null;
history.pushState('', document.title, window.location.pathname + window.location.search);
}
var observer = new MutationObserver(change);
var option = {
'childList': true,
'attributes': true,
'characterData': true,
'subtree': true
};
observer.observe(win.document.body, option);
};