block_library_storage.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /**
  2. * @license
  3. * Blockly Demos: Block Factory
  4. *
  5. * Copyright 2016 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 Javascript for Block Library's Storage Class.
  22. * Depends on Block Library for its namespace.
  23. *
  24. * @author quachtina96 (Tina Quach)
  25. */
  26. 'use strict';
  27. goog.provide('BlockLibraryStorage');
  28. /**
  29. * Represents a block library's storage.
  30. * @param {string} blockLibraryName Desired name of Block Library, also used
  31. * to create the key for where it's stored in local storage.
  32. * @param {Object} opt_blocks Object mapping block type to XML.
  33. * @constructor
  34. */
  35. BlockLibraryStorage = function(blockLibraryName, opt_blocks) {
  36. // Add prefix to this.name to avoid collisions in local storage.
  37. this.name = 'BlockLibraryStorage.' + blockLibraryName;
  38. if (!opt_blocks) {
  39. // Initialize this.blocks by loading from local storage.
  40. this.loadFromLocalStorage();
  41. if (this.blocks == null) {
  42. this.blocks = Object.create(null);
  43. // The line above is equivalent of {} except that this object is TRULY
  44. // empty. It doesn't have built-in attributes/functions such as length or
  45. // toString.
  46. this.saveToLocalStorage();
  47. }
  48. } else {
  49. this.blocks = opt_blocks;
  50. this.saveToLocalStorage();
  51. }
  52. };
  53. /**
  54. * Reads the named block library from local storage and saves it in this.blocks.
  55. */
  56. BlockLibraryStorage.prototype.loadFromLocalStorage = function() {
  57. // goog.global is synonymous to window, and allows for flexibility
  58. // between browsers.
  59. var object = goog.global.localStorage[this.name];
  60. this.blocks = object ? JSON.parse(object) : null;
  61. };
  62. /**
  63. * Writes the current block library (this.blocks) to local storage.
  64. */
  65. BlockLibraryStorage.prototype.saveToLocalStorage = function() {
  66. goog.global.localStorage[this.name] = JSON.stringify(this.blocks);
  67. };
  68. /**
  69. * Clears the current block library.
  70. */
  71. BlockLibraryStorage.prototype.clear = function() {
  72. this.blocks = Object.create(null);
  73. // The line above is equivalent of {} except that this object is TRULY
  74. // empty. It doesn't have built-in attributes/functions such as length or
  75. // toString.
  76. };
  77. /**
  78. * Saves block to block library.
  79. * @param {string} blockType Type of block.
  80. * @param {Element} blockXML The block's XML pulled from workspace.
  81. */
  82. BlockLibraryStorage.prototype.addBlock = function(blockType, blockXML) {
  83. var prettyXml = Blockly.Xml.domToPrettyText(blockXML);
  84. this.blocks[blockType] = prettyXml;
  85. };
  86. /**
  87. * Removes block from current block library (this.blocks).
  88. * @param {string} blockType Type of block.
  89. */
  90. BlockLibraryStorage.prototype.removeBlock = function(blockType) {
  91. delete this.blocks[blockType];
  92. };
  93. /**
  94. * Returns the XML of given block type stored in current block library
  95. * (this.blocks).
  96. * @param {string} blockType Type of block.
  97. * @return {Element} The XML that represents the block type or null.
  98. */
  99. BlockLibraryStorage.prototype.getBlockXml = function(blockType) {
  100. var xml = this.blocks[blockType] || null;
  101. if (xml) {
  102. var xml = Blockly.Xml.textToDom(xml);
  103. }
  104. return xml;
  105. };
  106. /**
  107. * Returns map of each block type to its corresponding XML stored in current
  108. * block library (this.blocks).
  109. * @param {!Array.<string>} blockTypes Types of blocks.
  110. * @return {!Object} Map of block type to corresponding XML.
  111. */
  112. BlockLibraryStorage.prototype.getBlockXmlMap = function(blockTypes) {
  113. var blockXmlMap = {};
  114. for (var i = 0; i < blockTypes.length; i++) {
  115. var blockType = blockTypes[i];
  116. var xml = this.getBlockXml(blockType);
  117. blockXmlMap[blockType] = xml;
  118. }
  119. return blockXmlMap;
  120. };
  121. /**
  122. * Returns array of all block types stored in current block library.
  123. * @return {!Array.<string>} Array of block types stored in library.
  124. */
  125. BlockLibraryStorage.prototype.getBlockTypes = function() {
  126. return Object.keys(this.blocks);
  127. };
  128. /**
  129. * Checks to see if block library is empty.
  130. * @return {boolean} True if empty, false otherwise.
  131. */
  132. BlockLibraryStorage.prototype.isEmpty = function() {
  133. for (var blockType in this.blocks) {
  134. return false;
  135. }
  136. return true;
  137. };
  138. /**
  139. * Returns array of all block types stored in current block library.
  140. * @return {!Array.<string>} Map of block type to corresponding XML text.
  141. */
  142. BlockLibraryStorage.prototype.getBlockXmlTextMap = function() {
  143. return this.blocks;
  144. };
  145. /**
  146. * Returns boolean of whether or not a given blockType is stored in block
  147. * library.
  148. * @param {string} blockType Type of block.
  149. * @return {boolean} Whether or not blockType is stored in block library.
  150. */
  151. BlockLibraryStorage.prototype.has = function(blockType) {
  152. return !!this.blocks[blockType];
  153. };