storage.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /**
  2. * @license
  3. * Visual Blocks Editor
  4. *
  5. * Copyright 2012 Google Inc.
  6. * https://developers.google.com/blockly/
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. /**
  21. * @fileoverview Loading and saving blocks with localStorage and cloud storage.
  22. * @author q.neutron@gmail.com (Quynh Neutron)
  23. */
  24. 'use strict';
  25. // Create a namespace.
  26. var BlocklyStorage = {};
  27. /**
  28. * Backup code blocks to localStorage.
  29. * @param {!Blockly.WorkspaceSvg} workspace Workspace.
  30. * @private
  31. */
  32. BlocklyStorage.backupBlocks_ = function(workspace) {
  33. if ('localStorage' in window) {
  34. var xml = Blockly.Xml.workspaceToDom(workspace);
  35. // Gets the current URL, not including the hash.
  36. var url = window.location.href.split('#')[0];
  37. window.localStorage.setItem(url, Blockly.Xml.domToText(xml));
  38. }
  39. };
  40. /**
  41. * Bind the localStorage backup function to the unload event.
  42. * @param {Blockly.WorkspaceSvg=} opt_workspace Workspace.
  43. */
  44. BlocklyStorage.backupOnUnload = function(opt_workspace) {
  45. var workspace = opt_workspace || Blockly.getMainWorkspace();
  46. window.addEventListener('unload',
  47. function() {BlocklyStorage.backupBlocks_(workspace);}, false);
  48. };
  49. /**
  50. * Restore code blocks from localStorage.
  51. * @param {Blockly.WorkspaceSvg=} opt_workspace Workspace.
  52. */
  53. BlocklyStorage.restoreBlocks = function(opt_workspace) {
  54. var url = window.location.href.split('#')[0];
  55. if ('localStorage' in window && window.localStorage[url]) {
  56. var workspace = opt_workspace || Blockly.getMainWorkspace();
  57. var xml = Blockly.Xml.textToDom(window.localStorage[url]);
  58. Blockly.Xml.domToWorkspace(xml, workspace);
  59. }
  60. };
  61. /**
  62. * Save blocks to database and return a link containing key to XML.
  63. * @param {Blockly.WorkspaceSvg=} opt_workspace Workspace.
  64. */
  65. BlocklyStorage.link = function(opt_workspace) {
  66. var workspace = opt_workspace || Blockly.getMainWorkspace();
  67. var xml = Blockly.Xml.workspaceToDom(workspace);
  68. var data = Blockly.Xml.domToText(xml);
  69. BlocklyStorage.makeRequest_('/storage', 'xml', data, workspace);
  70. };
  71. /**
  72. * Retrieve XML text from database using given key.
  73. * @param {string} key Key to XML, obtained from href.
  74. * @param {Blockly.WorkspaceSvg=} opt_workspace Workspace.
  75. */
  76. BlocklyStorage.retrieveXml = function(key, opt_workspace) {
  77. var workspace = opt_workspace || Blockly.getMainWorkspace();
  78. BlocklyStorage.makeRequest_('/storage', 'key', key, workspace);
  79. };
  80. /**
  81. * Global reference to current AJAX request.
  82. * @type {XMLHttpRequest}
  83. * @private
  84. */
  85. BlocklyStorage.httpRequest_ = null;
  86. /**
  87. * Fire a new AJAX request.
  88. * @param {string} url URL to fetch.
  89. * @param {string} name Name of parameter.
  90. * @param {string} content Content of parameter.
  91. * @param {!Blockly.WorkspaceSvg} workspace Workspace.
  92. * @private
  93. */
  94. BlocklyStorage.makeRequest_ = function(url, name, content, workspace) {
  95. if (BlocklyStorage.httpRequest_) {
  96. // AJAX call is in-flight.
  97. BlocklyStorage.httpRequest_.abort();
  98. }
  99. BlocklyStorage.httpRequest_ = new XMLHttpRequest();
  100. BlocklyStorage.httpRequest_.name = name;
  101. BlocklyStorage.httpRequest_.onreadystatechange =
  102. BlocklyStorage.handleRequest_;
  103. BlocklyStorage.httpRequest_.open('POST', url);
  104. BlocklyStorage.httpRequest_.setRequestHeader('Content-Type',
  105. 'application/x-www-form-urlencoded');
  106. BlocklyStorage.httpRequest_.send(name + '=' + encodeURIComponent(content));
  107. BlocklyStorage.httpRequest_.workspace = workspace;
  108. };
  109. /**
  110. * Callback function for AJAX call.
  111. * @private
  112. */
  113. BlocklyStorage.handleRequest_ = function() {
  114. if (BlocklyStorage.httpRequest_.readyState == 4) {
  115. if (BlocklyStorage.httpRequest_.status != 200) {
  116. BlocklyStorage.alert(BlocklyStorage.HTTPREQUEST_ERROR + '\n' +
  117. 'httpRequest_.status: ' + BlocklyStorage.httpRequest_.status);
  118. } else {
  119. var data = BlocklyStorage.httpRequest_.responseText.trim();
  120. if (BlocklyStorage.httpRequest_.name == 'xml') {
  121. window.location.hash = data;
  122. BlocklyStorage.alert(BlocklyStorage.LINK_ALERT.replace('%1',
  123. window.location.href));
  124. } else if (BlocklyStorage.httpRequest_.name == 'key') {
  125. if (!data.length) {
  126. BlocklyStorage.alert(BlocklyStorage.HASH_ERROR.replace('%1',
  127. window.location.hash));
  128. } else {
  129. BlocklyStorage.loadXml_(data, BlocklyStorage.httpRequest_.workspace);
  130. }
  131. }
  132. BlocklyStorage.monitorChanges_(BlocklyStorage.httpRequest_.workspace);
  133. }
  134. BlocklyStorage.httpRequest_ = null;
  135. }
  136. };
  137. /**
  138. * Start monitoring the workspace. If a change is made that changes the XML,
  139. * clear the key from the URL. Stop monitoring the workspace once such a
  140. * change is detected.
  141. * @param {!Blockly.WorkspaceSvg} workspace Workspace.
  142. * @private
  143. */
  144. BlocklyStorage.monitorChanges_ = function(workspace) {
  145. var startXmlDom = Blockly.Xml.workspaceToDom(workspace);
  146. var startXmlText = Blockly.Xml.domToText(startXmlDom);
  147. function change() {
  148. var xmlDom = Blockly.Xml.workspaceToDom(workspace);
  149. var xmlText = Blockly.Xml.domToText(xmlDom);
  150. if (startXmlText != xmlText) {
  151. window.location.hash = '';
  152. workspace.removeChangeListener(bindData);
  153. }
  154. }
  155. var bindData = workspace.addChangeListener(change);
  156. };
  157. /**
  158. * Load blocks from XML.
  159. * @param {string} xml Text representation of XML.
  160. * @param {!Blockly.WorkspaceSvg} workspace Workspace.
  161. * @private
  162. */
  163. BlocklyStorage.loadXml_ = function(xml, workspace) {
  164. try {
  165. xml = Blockly.Xml.textToDom(xml);
  166. } catch (e) {
  167. BlocklyStorage.alert(BlocklyStorage.XML_ERROR + '\nXML: ' + xml);
  168. return;
  169. }
  170. // Clear the workspace to avoid merge.
  171. workspace.clear();
  172. Blockly.Xml.domToWorkspace(xml, workspace);
  173. };
  174. /**
  175. * Present a text message to the user.
  176. * Designed to be overridden if an app has custom dialogs, or a butter bar.
  177. * @param {string} message Text to alert.
  178. */
  179. BlocklyStorage.alert = function(message) {
  180. window.alert(message);
  181. };