field_label.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 Non-editable text field. Used for titles, labels, etc.
  22. * @author fraser@google.com (Neil Fraser)
  23. */
  24. 'use strict';
  25. goog.provide('Blockly.FieldLabel');
  26. goog.require('Blockly.Field');
  27. goog.require('Blockly.Tooltip');
  28. goog.require('goog.dom');
  29. goog.require('goog.math.Size');
  30. var Blockscad = Blockscad || {};
  31. /**
  32. * Class for a non-editable field.
  33. * @param {string} text The initial content of the field.
  34. * @param {string=} opt_class Optional CSS class for the field's text.
  35. * @extends {Blockly.Field}
  36. * @constructor
  37. */
  38. Blockly.FieldLabel = function(text, opt_class) {
  39. this.size_ = new goog.math.Size(0, 17.5);
  40. this.class_ = opt_class;
  41. this.setValue(text);
  42. };
  43. goog.inherits(Blockly.FieldLabel, Blockly.Field);
  44. /**
  45. * Editable fields are saved by the XML renderer, non-editable fields are not.
  46. */
  47. Blockly.FieldLabel.prototype.EDITABLE = false;
  48. /**
  49. * Install this text on a block.
  50. */
  51. Blockly.FieldLabel.prototype.init = function() {
  52. if (this.textElement_) {
  53. // Text has already been initialized once.
  54. return;
  55. }
  56. // Build the DOM.
  57. this.textElement_ = Blockly.createSvgElement('text',
  58. {'class': 'blocklyText', 'y': this.size_.height - 5}, null);
  59. if (this.class_) {
  60. Blockly.addClass_(this.textElement_, this.class_);
  61. }
  62. if (!this.visible_) {
  63. this.textElement_.style.display = 'none';
  64. }
  65. this.sourceBlock_.getSvgRoot().appendChild(this.textElement_);
  66. // Configure the field to be transparent with respect to tooltips.
  67. this.textElement_.tooltip = this.sourceBlock_;
  68. Blockly.Tooltip.bindMouseEvents(this.textElement_);
  69. // Force a render.
  70. this.updateTextNode_();
  71. };
  72. /**
  73. * Dispose of all DOM objects belonging to this text.
  74. */
  75. Blockly.FieldLabel.prototype.dispose = function() {
  76. goog.dom.removeNode(this.textElement_);
  77. this.textElement_ = null;
  78. };
  79. /**
  80. * Gets the group element for this field.
  81. * Used for measuring the size and for positioning.
  82. * @return {!Element} The group element.
  83. */
  84. Blockly.FieldLabel.prototype.getSvgRoot = function() {
  85. return /** @type {!Element} */ (this.textElement_);
  86. };
  87. /**
  88. * Change the tooltip text for this field.
  89. * @param {string|!Element} newTip Text for tooltip or a parent element to
  90. * link to for its tooltip.
  91. */
  92. Blockly.FieldLabel.prototype.setTooltip = function(newTip) {
  93. this.textElement_.tooltip = newTip;
  94. };
  95. // lets try adding in a button, which will be a text element that when you click on it,
  96. // the "editor" is to do the file choose menu.
  97. goog.provide('Blockly.FieldButton');
  98. goog.require('Blockly.Field');
  99. goog.require('Blockly.Msg');
  100. goog.require('goog.asserts');
  101. goog.require('goog.dom');
  102. goog.require('goog.userAgent');
  103. /**
  104. * Class for an editable text field.
  105. * @param {string} text The initial content of the field.
  106. * @param {Function=} opt_changeHandler An optional function that is called
  107. * to validate any constraints on what the user entered. Takes the new
  108. * text as an argument and returns either the accepted text, a replacement
  109. * text, or null to abort the change.
  110. * @extends {Blockly.Field}
  111. * @constructor
  112. */
  113. Blockly.FieldButton = function(text, opt_validator) {
  114. Blockly.FieldTextInput.superClass_.constructor.call(this, text,
  115. opt_validator);
  116. };
  117. goog.inherits(Blockly.FieldButton, Blockly.Field);
  118. /**
  119. * Mouse cursor style when over the hotspot that initiates the editor.
  120. */
  121. Blockly.FieldButton.prototype.CURSOR = 'default';
  122. /**
  123. * Editable fields are saved by the XML renderer, non-editable fields are not.
  124. */
  125. Blockly.FieldButton.prototype.EDITABLE = true;
  126. /**
  127. * Close the input widget if this input is being deleted.
  128. */
  129. Blockly.FieldButton.prototype.dispose = function() {
  130. Blockly.WidgetDiv.hideIfOwner(this);
  131. Blockly.FieldButton.superClass_.dispose.call(this);
  132. };
  133. /**
  134. * Set the text in this field.
  135. * @param {?string} text New text.
  136. * @override
  137. */
  138. Blockly.FieldButton.prototype.setValue = function(text) {
  139. if (text === null) {
  140. return; // No change if null.
  141. }
  142. if (this.sourceBlock_) {
  143. var validated = this.callValidator(text);
  144. // If the new text is invalid, validation returns null.
  145. // In this case we still want to display the illegal result.
  146. if (validated !== null) {
  147. text = validated;
  148. }
  149. }
  150. Blockly.Field.prototype.setValue.call(this, text);
  151. };
  152. /**
  153. * Show the inline free-text editor on top of the text.
  154. * @param {boolean=} opt_quietInput True if editor should be created without
  155. * focus. Defaults to false.
  156. * @private
  157. */
  158. Blockly.FieldButton.prototype.showEditor_ = function(opt_quietInput) {
  159. // console.log("editor activated");
  160. Blockscad.currentInterestingBlock = this.sourceBlock_;
  161. $('#importStl').click();
  162. };
  163. /**
  164. * Close the editor, save the results, and dispose of the editable
  165. * text field's elements.
  166. * @return {!Function} Closure to call on destruction of the WidgetDiv.
  167. * @private
  168. */
  169. Blockly.FieldButton.prototype.widgetDispose_ = function() {
  170. var thisField = this;
  171. return function() {
  172. var htmlInput = Blockly.FieldButton.htmlInput_;
  173. // Save the edit (if it validates).
  174. var text = htmlInput.value;
  175. if (thisField.sourceBlock_) {
  176. var text1 = text;
  177. if (text1 === null) {
  178. // Invalid edit.
  179. text = htmlInput.defaultValue;
  180. } else if (text1 !== undefined) {
  181. // Change handler has changed the text.
  182. text = text1;
  183. }
  184. }
  185. thisField.setValue(text);
  186. thisField.sourceBlock_.rendered && thisField.sourceBlock_.render();
  187. Blockly.unbindEvent_(htmlInput.onKeyDownWrapper_);
  188. Blockly.unbindEvent_(htmlInput.onKeyUpWrapper_);
  189. Blockly.unbindEvent_(htmlInput.onKeyPressWrapper_);
  190. thisField.workspace_.removeChangeListener(
  191. htmlInput.onWorkspaceChangeWrapper_);
  192. Blockly.FieldTextInput.htmlInput_ = null;
  193. // Delete style properties.
  194. var style = Blockly.WidgetDiv.DIV.style;
  195. style.width = 'auto';
  196. style.height = 'auto';
  197. style.fontSize = '';
  198. };
  199. };