options.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. this.RTL = rtl;
  102. this.collapse = hasCollapse;
  103. this.comments = hasComments;
  104. this.disable = hasDisable;
  105. this.readOnly = readOnly;
  106. this.maxBlocks = options['maxBlocks'] || Infinity;
  107. this.pathToMedia = pathToMedia;
  108. this.hasCategories = hasCategories;
  109. this.hasScrollbars = hasScrollbars;
  110. this.hasTrashcan = hasTrashcan;
  111. this.hasSounds = hasSounds;
  112. this.hasCss = hasCss;
  113. this.horizontalLayout = horizontalLayout;
  114. this.languageTree = languageTree;
  115. this.gridOptions = Blockly.Options.parseGridOptions_(options);
  116. this.zoomOptions = Blockly.Options.parseZoomOptions_(options);
  117. this.toolboxPosition = toolboxPosition;
  118. };
  119. /**
  120. * @type {Blockly.Workspace} the parent of the current workspace, or null if
  121. * there is no parent workspace.
  122. **/
  123. Blockly.Options.prototype.parentWorkspace = null;
  124. /**
  125. * If set, sets the translation of the workspace to match the scrollbars.
  126. * No-op if unset.
  127. */
  128. Blockly.Options.prototype.setMetrics = function() { return; };
  129. /**
  130. * Return an object with the metrics required to size the workspace, or null
  131. * if unset.
  132. * @return {Object} Contains size an position metrics, or null.
  133. */
  134. Blockly.Options.prototype.getMetrics = function() { return null; };
  135. /**
  136. * Parse the user-specified zoom options, using reasonable defaults where
  137. * behaviour is unspecified. See zoom documentation:
  138. * https://developers.google.com/blockly/guides/configure/web/zoom
  139. * @param {!Object} options Dictionary of options.
  140. * @return {!Object} A dictionary of normalized options.
  141. * @private
  142. */
  143. Blockly.Options.parseZoomOptions_ = function(options) {
  144. var zoom = options['zoom'] || {};
  145. var zoomOptions = {};
  146. if (zoom['controls'] === undefined) {
  147. zoomOptions.controls = false;
  148. } else {
  149. zoomOptions.controls = !!zoom['controls'];
  150. }
  151. if (zoom['wheel'] === undefined) {
  152. zoomOptions.wheel = false;
  153. } else {
  154. zoomOptions.wheel = !!zoom['wheel'];
  155. }
  156. if (zoom['startScale'] === undefined) {
  157. zoomOptions.startScale = 1;
  158. } else {
  159. zoomOptions.startScale = parseFloat(zoom['startScale']);
  160. }
  161. if (zoom['maxScale'] === undefined) {
  162. zoomOptions.maxScale = 3;
  163. } else {
  164. zoomOptions.maxScale = parseFloat(zoom['maxScale']);
  165. }
  166. if (zoom['minScale'] === undefined) {
  167. zoomOptions.minScale = 0.3;
  168. } else {
  169. zoomOptions.minScale = parseFloat(zoom['minScale']);
  170. }
  171. if (zoom['scaleSpeed'] === undefined) {
  172. zoomOptions.scaleSpeed = 1.2;
  173. } else {
  174. zoomOptions.scaleSpeed = parseFloat(zoom['scaleSpeed']);
  175. }
  176. return zoomOptions;
  177. };
  178. /**
  179. * Parse the user-specified grid options, using reasonable defaults where
  180. * behaviour is unspecified. See grid documentation:
  181. * https://developers.google.com/blockly/guides/configure/web/grid
  182. * @param {!Object} options Dictionary of options.
  183. * @return {!Object} A dictionary of normalized options.
  184. * @private
  185. */
  186. Blockly.Options.parseGridOptions_ = function(options) {
  187. var grid = options['grid'] || {};
  188. var gridOptions = {};
  189. gridOptions.spacing = parseFloat(grid['spacing']) || 0;
  190. gridOptions.colour = grid['colour'] || '#888';
  191. gridOptions.length = parseFloat(grid['length']) || 1;
  192. gridOptions.snap = gridOptions.spacing > 0 && !!grid['snap'];
  193. return gridOptions;
  194. };
  195. /**
  196. * Parse the provided toolbox tree into a consistent DOM format.
  197. * @param {Node|string} tree DOM tree of blocks, or text representation of same.
  198. * @return {Node} DOM tree of blocks, or null.
  199. */
  200. Blockly.Options.parseToolboxTree = function(tree) {
  201. if (tree) {
  202. if (typeof tree != 'string') {
  203. if (typeof XSLTProcessor == 'undefined' && tree.outerHTML) {
  204. // In this case the tree will not have been properly built by the
  205. // browser. The HTML will be contained in the element, but it will
  206. // not have the proper DOM structure since the browser doesn't support
  207. // XSLTProcessor (XML -> HTML). This is the case in IE 9+.
  208. tree = tree.outerHTML;
  209. } else if (!(tree instanceof Element)) {
  210. tree = null;
  211. }
  212. }
  213. if (typeof tree == 'string') {
  214. tree = Blockly.Xml.textToDom(tree);
  215. }
  216. } else {
  217. tree = null;
  218. }
  219. return tree;
  220. };