options.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /**
  2. * @license
  3. * Visual Blocks Editor
  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 Object that controls settings for the workspace.
  22. * @author fenichel@google.com (Rachel Fenichel)
  23. */
  24. 'use strict';
  25. goog.provide('Blockly.Options');
  26. /**
  27. * Parse the user-specified options, using reasonable defaults where behaviour
  28. * is unspecified.
  29. * @param {!Object} options Dictionary of options. Specification:
  30. * https://developers.google.com/blockly/guides/get-started/web#configuration
  31. * @constructor
  32. */
  33. Blockly.Options = function(options) {
  34. var readOnly = !!options['readOnly'];
  35. if (readOnly) {
  36. var languageTree = null;
  37. var hasCategories = false;
  38. var hasTrashcan = false;
  39. var hasCollapse = false;
  40. var hasComments = false;
  41. var hasDisable = false;
  42. var hasSounds = false;
  43. } else {
  44. var languageTree = Blockly.Options.parseToolboxTree(options['toolbox']);
  45. var hasCategories = Boolean(languageTree &&
  46. languageTree.getElementsByTagName('category').length);
  47. var hasTrashcan = options['trashcan'];
  48. if (hasTrashcan === undefined) {
  49. hasTrashcan = hasCategories;
  50. }
  51. var hasCollapse = options['collapse'];
  52. if (hasCollapse === undefined) {
  53. hasCollapse = hasCategories;
  54. }
  55. var hasComments = options['comments'];
  56. if (hasComments === undefined) {
  57. hasComments = hasCategories;
  58. }
  59. var hasDisable = options['disable'];
  60. if (hasDisable === undefined) {
  61. hasDisable = hasCategories;
  62. }
  63. var hasSounds = options['sounds'];
  64. if (hasSounds === undefined) {
  65. hasSounds = true;
  66. }
  67. }
  68. var rtl = !!options['rtl'];
  69. var horizontalLayout = options['horizontalLayout'];
  70. if (horizontalLayout === undefined) {
  71. horizontalLayout = false;
  72. }
  73. var toolboxAtStart = options['toolboxPosition'];
  74. if (toolboxAtStart === 'end') {
  75. toolboxAtStart = false;
  76. } else {
  77. toolboxAtStart = true;
  78. }
  79. if (horizontalLayout) {
  80. var toolboxPosition = toolboxAtStart ?
  81. Blockly.TOOLBOX_AT_TOP : Blockly.TOOLBOX_AT_BOTTOM;
  82. } else {
  83. var toolboxPosition = (toolboxAtStart == rtl) ?
  84. Blockly.TOOLBOX_AT_RIGHT : Blockly.TOOLBOX_AT_LEFT;
  85. }
  86. var hasScrollbars = options['scrollbars'];
  87. if (hasScrollbars === undefined) {
  88. hasScrollbars = hasCategories;
  89. }
  90. var hasCss = options['css'];
  91. if (hasCss === undefined) {
  92. hasCss = true;
  93. }
  94. var pathToMedia = 'https://blockly-demo.appspot.com/static/media/';
  95. if (options['media']) {
  96. pathToMedia = options['media'];
  97. } else if (options['path']) {
  98. // 'path' is a deprecated option which has been replaced by 'media'.
  99. pathToMedia = options['path'] + 'media/';
  100. }
  101. if (options['oneBasedIndex'] === undefined) {
  102. var oneBasedIndex = true;
  103. } else {
  104. var oneBasedIndex = !!options['oneBasedIndex'];
  105. }
  106. this.RTL = rtl;
  107. this.oneBasedIndex = oneBasedIndex;
  108. this.collapse = hasCollapse;
  109. this.comments = hasComments;
  110. this.disable = hasDisable;
  111. this.readOnly = readOnly;
  112. this.maxBlocks = options['maxBlocks'] || Infinity;
  113. this.pathToMedia = pathToMedia;
  114. this.hasCategories = hasCategories;
  115. this.hasScrollbars = hasScrollbars;
  116. this.hasTrashcan = hasTrashcan;
  117. this.hasSounds = hasSounds;
  118. this.hasCss = hasCss;
  119. this.horizontalLayout = horizontalLayout;
  120. this.languageTree = languageTree;
  121. this.gridOptions = Blockly.Options.parseGridOptions_(options);
  122. this.zoomOptions = Blockly.Options.parseZoomOptions_(options);
  123. this.toolboxPosition = toolboxPosition;
  124. };
  125. /**
  126. * The parent of the current workspace, or null if there is no parent workspace.
  127. * @type {Blockly.Workspace}
  128. **/
  129. Blockly.Options.prototype.parentWorkspace = null;
  130. /**
  131. * If set, sets the translation of the workspace to match the scrollbars.
  132. */
  133. Blockly.Options.prototype.setMetrics = null;
  134. /**
  135. * Return an object with the metrics required to size the workspace.
  136. * @return {Object} Contains size and position metrics, or null.
  137. */
  138. Blockly.Options.prototype.getMetrics = null;
  139. /**
  140. * Parse the user-specified zoom options, using reasonable defaults where
  141. * behaviour is unspecified. See zoom documentation:
  142. * https://developers.google.com/blockly/guides/configure/web/zoom
  143. * @param {!Object} options Dictionary of options.
  144. * @return {!Object} A dictionary of normalized options.
  145. * @private
  146. */
  147. Blockly.Options.parseZoomOptions_ = function(options) {
  148. var zoom = options['zoom'] || {};
  149. var zoomOptions = {};
  150. if (zoom['controls'] === undefined) {
  151. zoomOptions.controls = false;
  152. } else {
  153. zoomOptions.controls = !!zoom['controls'];
  154. }
  155. if (zoom['wheel'] === undefined) {
  156. zoomOptions.wheel = false;
  157. } else {
  158. zoomOptions.wheel = !!zoom['wheel'];
  159. }
  160. if (zoom['startScale'] === undefined) {
  161. zoomOptions.startScale = 1;
  162. } else {
  163. zoomOptions.startScale = parseFloat(zoom['startScale']);
  164. }
  165. if (zoom['maxScale'] === undefined) {
  166. zoomOptions.maxScale = 3;
  167. } else {
  168. zoomOptions.maxScale = parseFloat(zoom['maxScale']);
  169. }
  170. if (zoom['minScale'] === undefined) {
  171. zoomOptions.minScale = 0.3;
  172. } else {
  173. zoomOptions.minScale = parseFloat(zoom['minScale']);
  174. }
  175. if (zoom['scaleSpeed'] === undefined) {
  176. zoomOptions.scaleSpeed = 1.2;
  177. } else {
  178. zoomOptions.scaleSpeed = parseFloat(zoom['scaleSpeed']);
  179. }
  180. return zoomOptions;
  181. };
  182. /**
  183. * Parse the user-specified grid options, using reasonable defaults where
  184. * behaviour is unspecified. See grid documentation:
  185. * https://developers.google.com/blockly/guides/configure/web/grid
  186. * @param {!Object} options Dictionary of options.
  187. * @return {!Object} A dictionary of normalized options.
  188. * @private
  189. */
  190. Blockly.Options.parseGridOptions_ = function(options) {
  191. var grid = options['grid'] || {};
  192. var gridOptions = {};
  193. gridOptions.spacing = parseFloat(grid['spacing']) || 0;
  194. gridOptions.colour = grid['colour'] || '#888';
  195. gridOptions.length = parseFloat(grid['length']) || 1;
  196. gridOptions.snap = gridOptions.spacing > 0 && !!grid['snap'];
  197. return gridOptions;
  198. };
  199. /**
  200. * Parse the provided toolbox tree into a consistent DOM format.
  201. * @param {Node|string} tree DOM tree of blocks, or text representation of same.
  202. * @return {Node} DOM tree of blocks, or null.
  203. */
  204. Blockly.Options.parseToolboxTree = function(tree) {
  205. if (tree) {
  206. if (typeof tree != 'string') {
  207. if (typeof XSLTProcessor == 'undefined' && tree.outerHTML) {
  208. // In this case the tree will not have been properly built by the
  209. // browser. The HTML will be contained in the element, but it will
  210. // not have the proper DOM structure since the browser doesn't support
  211. // XSLTProcessor (XML -> HTML). This is the case in IE 9+.
  212. tree = tree.outerHTML;
  213. } else if (!(tree instanceof Element)) {
  214. tree = null;
  215. }
  216. }
  217. if (typeof tree == 'string') {
  218. tree = Blockly.Xml.textToDom(tree);
  219. }
  220. } else {
  221. tree = null;
  222. }
  223. return tree;
  224. };