-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
storage.js
205 lines (192 loc) · 6.33 KB
/
storage.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
193
194
195
196
197
198
199
200
201
202
203
204
205
/* jshint esversion: 6 */
/**
* @license
* Copyright 2012 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @fileoverview Loading and saving blocks with localStorage and cloud storage.
* @author [email protected] (Quynh Neutron)
* @author [email protected] (Greg Dyke) -- modified to store multiple blocklys locally
*/
"use strict";
// Create a namespace.
var BlocklyStorage = {};
/**
* Backup code blocks to localStorage.
* @param {!Blockly.WorkspaceSvg} workspace Workspace.
* @private
*/
BlocklyStorage.backupBlocks_ = function (workspace, id) {
if ("localStorage" in window) {
var xml = Blockly.Xml.workspaceToDom(workspace);
// Gets the current URL, not including the hash.
var url = window.location.href.split("#")[0];
window.localStorage.setItem(url + id, Blockly.Xml.domToText(xml));
}
};
/**
* Bind the localStorage backup function to the unload event.
* @param {Blockly.WorkspaceSvg=} opt_workspace Workspace.
*/
BlocklyStorage.backupOnUnload = function (opt_workspace, id) {
var workspace = opt_workspace || Blockly.getMainWorkspace();
window.addEventListener(
"unload",
function () {
BlocklyStorage.backupBlocks_(workspace, id);
},
false
);
};
/**
* Restore code blocks from localStorage.
* @param {Blockly.WorkspaceSvg=} opt_workspace Workspace.
* @param {String} id unique id, must be stable across page reloads
*/
BlocklyStorage.restoreBlocks = function (opt_workspace, id) {
var url = window.location.href.split("#")[0];
if ("localStorage" in window && window.localStorage[url + id]) {
var workspace = opt_workspace || Blockly.getMainWorkspace();
var xml = Blockly.Xml.textToDom(window.localStorage[url + id]);
Blockly.Xml.domToWorkspace(xml, workspace);
}
};
/**
* Save blocks to database and return a link containing key to XML.
* @param {Blockly.WorkspaceSvg=} opt_workspace Workspace.
*/
BlocklyStorage.link = function (opt_workspace) {
var workspace = opt_workspace || Blockly.getMainWorkspace();
var xml = Blockly.Xml.workspaceToDom(workspace, true);
// Remove x/y coordinates from XML if there's only one block stack.
// There's no reason to store this, removing it helps with anonymity.
if (workspace.getTopBlocks(false).length == 1 && xml.querySelector) {
var block = xml.querySelector("block");
if (block) {
block.removeAttribute("x");
block.removeAttribute("y");
}
}
var data = Blockly.Xml.domToText(xml);
BlocklyStorage.makeRequest_("/storage", "xml", data, workspace);
};
/**
* Retrieve XML text from database using given key.
* @param {string} key Key to XML, obtained from href.
* @param {Blockly.WorkspaceSvg=} opt_workspace Workspace.
*/
BlocklyStorage.retrieveXml = function (key, opt_workspace) {
var workspace = opt_workspace || Blockly.getMainWorkspace();
BlocklyStorage.makeRequest_("/storage", "key", key, workspace);
};
/**
* Global reference to current AJAX request.
* @type {XMLHttpRequest}
* @private
*/
BlocklyStorage.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 {!Blockly.WorkspaceSvg} workspace Workspace.
* @private
*/
BlocklyStorage.makeRequest_ = function (url, name, content, workspace) {
if (BlocklyStorage.httpRequest_) {
// AJAX call is in-flight.
BlocklyStorage.httpRequest_.abort();
}
BlocklyStorage.httpRequest_ = new XMLHttpRequest();
BlocklyStorage.httpRequest_.name = name;
BlocklyStorage.httpRequest_.onreadystatechange =
BlocklyStorage.handleRequest_;
BlocklyStorage.httpRequest_.open("POST", url);
BlocklyStorage.httpRequest_.setRequestHeader(
"Content-Type",
"application/x-www-form-urlencoded"
);
BlocklyStorage.httpRequest_.send(name + "=" + encodeURIComponent(content));
BlocklyStorage.httpRequest_.workspace = workspace;
};
/**
* Callback function for AJAX call.
* @private
*/
BlocklyStorage.handleRequest_ = function () {
if (BlocklyStorage.httpRequest_.readyState == 4) {
if (BlocklyStorage.httpRequest_.status != 200) {
BlocklyStorage.alert(
BlocklyStorage.HTTPREQUEST_ERROR +
"\n" +
"httpRequest_.status: " +
BlocklyStorage.httpRequest_.status
);
} else {
var data = BlocklyStorage.httpRequest_.responseText.trim();
if (BlocklyStorage.httpRequest_.name == "xml") {
window.location.hash = data;
BlocklyStorage.alert(
BlocklyStorage.LINK_ALERT.replace("%1", window.location.href)
);
} else if (BlocklyStorage.httpRequest_.name == "key") {
if (!data.length) {
BlocklyStorage.alert(
BlocklyStorage.HASH_ERROR.replace("%1", window.location.hash)
);
} else {
BlocklyStorage.loadXml_(data, BlocklyStorage.httpRequest_.workspace);
}
}
BlocklyStorage.monitorChanges_(BlocklyStorage.httpRequest_.workspace);
}
BlocklyStorage.httpRequest_ = null;
}
};
/**
* Start monitoring the workspace. If a change is made that changes the XML,
* clear the key from the URL. Stop monitoring the workspace once such a
* change is detected.
* @param {!Blockly.WorkspaceSvg} workspace Workspace.
* @private
*/
BlocklyStorage.monitorChanges_ = function (workspace) {
var startXmlDom = Blockly.Xml.workspaceToDom(workspace);
var startXmlText = Blockly.Xml.domToText(startXmlDom);
function change() {
var xmlDom = Blockly.Xml.workspaceToDom(workspace);
var xmlText = Blockly.Xml.domToText(xmlDom);
if (startXmlText != xmlText) {
window.location.hash = "";
workspace.removeChangeListener(change);
}
}
workspace.addChangeListener(change);
};
/**
* Load blocks from XML.
* @param {string} xml Text representation of XML.
* @param {!Blockly.WorkspaceSvg} workspace Workspace.
* @private
*/
BlocklyStorage.loadXml_ = function (xml, workspace) {
try {
xml = Blockly.Xml.textToDom(xml);
} catch (e) {
BlocklyStorage.alert(BlocklyStorage.XML_ERROR + "\nXML: " + xml);
return;
}
// Clear the workspace to avoid merge.
workspace.clear();
Blockly.Xml.domToWorkspace(xml, workspace);
};
/**
* Present a text message to the user.
* Designed to be overridden if an app has custom dialogs, or a butter bar.
* @param {string} message Text to alert.
*/
BlocklyStorage.alert = function (message) {
window.alert(message);
};