| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- /**
- * @license
- * Visual Blocks Editor
- *
- * Copyright 2012 Google Inc.
- * https://developers.google.com/blockly/
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /**
- * @fileoverview Non-editable text field. Used for titles, labels, etc.
- * @author fraser@google.com (Neil Fraser)
- */
- 'use strict';
- goog.provide('Blockly.FieldLabel');
- goog.require('Blockly.Field');
- goog.require('Blockly.Tooltip');
- goog.require('goog.dom');
- goog.require('goog.math.Size');
- var Blockscad = Blockscad || {};
- /**
- * Class for a non-editable field.
- * @param {string} text The initial content of the field.
- * @param {string=} opt_class Optional CSS class for the field's text.
- * @extends {Blockly.Field}
- * @constructor
- */
- Blockly.FieldLabel = function(text, opt_class) {
- this.size_ = new goog.math.Size(0, 17.5);
- this.class_ = opt_class;
- this.setValue(text);
- };
- goog.inherits(Blockly.FieldLabel, Blockly.Field);
- /**
- * Editable fields are saved by the XML renderer, non-editable fields are not.
- */
- Blockly.FieldLabel.prototype.EDITABLE = false;
- /**
- * Install this text on a block.
- */
- Blockly.FieldLabel.prototype.init = function() {
- if (this.textElement_) {
- // Text has already been initialized once.
- return;
- }
- // Build the DOM.
- this.textElement_ = Blockly.createSvgElement('text',
- {'class': 'blocklyText', 'y': this.size_.height - 5}, null);
- if (this.class_) {
- Blockly.addClass_(this.textElement_, this.class_);
- }
- if (!this.visible_) {
- this.textElement_.style.display = 'none';
- }
- this.sourceBlock_.getSvgRoot().appendChild(this.textElement_);
- // Configure the field to be transparent with respect to tooltips.
- this.textElement_.tooltip = this.sourceBlock_;
- Blockly.Tooltip.bindMouseEvents(this.textElement_);
- // Force a render.
- this.updateTextNode_();
- };
- /**
- * Dispose of all DOM objects belonging to this text.
- */
- Blockly.FieldLabel.prototype.dispose = function() {
- goog.dom.removeNode(this.textElement_);
- this.textElement_ = null;
- };
- /**
- * Gets the group element for this field.
- * Used for measuring the size and for positioning.
- * @return {!Element} The group element.
- */
- Blockly.FieldLabel.prototype.getSvgRoot = function() {
- return /** @type {!Element} */ (this.textElement_);
- };
- /**
- * Change the tooltip text for this field.
- * @param {string|!Element} newTip Text for tooltip or a parent element to
- * link to for its tooltip.
- */
- Blockly.FieldLabel.prototype.setTooltip = function(newTip) {
- this.textElement_.tooltip = newTip;
- };
- // lets try adding in a button, which will be a text element that when you click on it,
- // the "editor" is to do the file choose menu.
- goog.provide('Blockly.FieldButton');
- goog.require('Blockly.Field');
- goog.require('Blockly.Msg');
- goog.require('goog.asserts');
- goog.require('goog.dom');
- goog.require('goog.userAgent');
- /**
- * Class for an editable text field.
- * @param {string} text The initial content of the field.
- * @param {Function=} opt_changeHandler An optional function that is called
- * to validate any constraints on what the user entered. Takes the new
- * text as an argument and returns either the accepted text, a replacement
- * text, or null to abort the change.
- * @extends {Blockly.Field}
- * @constructor
- */
- Blockly.FieldButton = function(text, opt_validator) {
- Blockly.FieldTextInput.superClass_.constructor.call(this, text,
- opt_validator);
- };
- goog.inherits(Blockly.FieldButton, Blockly.Field);
- /**
- * Mouse cursor style when over the hotspot that initiates the editor.
- */
- Blockly.FieldButton.prototype.CURSOR = 'default';
- /**
- * Editable fields are saved by the XML renderer, non-editable fields are not.
- */
- Blockly.FieldButton.prototype.EDITABLE = true;
- /**
- * Close the input widget if this input is being deleted.
- */
- Blockly.FieldButton.prototype.dispose = function() {
- Blockly.WidgetDiv.hideIfOwner(this);
- Blockly.FieldButton.superClass_.dispose.call(this);
- };
- /**
- * Set the text in this field.
- * @param {?string} text New text.
- * @override
- */
- Blockly.FieldButton.prototype.setValue = function(text) {
- if (text === null) {
- return; // No change if null.
- }
- if (this.sourceBlock_) {
- var validated = this.callValidator(text);
- // If the new text is invalid, validation returns null.
- // In this case we still want to display the illegal result.
- if (validated !== null) {
- text = validated;
- }
- }
- Blockly.Field.prototype.setValue.call(this, text);
- };
- /**
- * Show the inline free-text editor on top of the text.
- * @param {boolean=} opt_quietInput True if editor should be created without
- * focus. Defaults to false.
- * @private
- */
- Blockly.FieldButton.prototype.showEditor_ = function(opt_quietInput) {
- // console.log("editor activated");
- Blockscad.currentInterestingBlock = this.sourceBlock_;
- $('#importStl').click();
- };
- /**
- * Close the editor, save the results, and dispose of the editable
- * text field's elements.
- * @return {!Function} Closure to call on destruction of the WidgetDiv.
- * @private
- */
- Blockly.FieldButton.prototype.widgetDispose_ = function() {
- var thisField = this;
- return function() {
- var htmlInput = Blockly.FieldButton.htmlInput_;
- // Save the edit (if it validates).
- var text = htmlInput.value;
- if (thisField.sourceBlock_) {
- var text1 = text;
- if (text1 === null) {
- // Invalid edit.
- text = htmlInput.defaultValue;
- } else if (text1 !== undefined) {
- // Change handler has changed the text.
- text = text1;
- }
- }
- thisField.setValue(text);
- thisField.sourceBlock_.rendered && thisField.sourceBlock_.render();
- Blockly.unbindEvent_(htmlInput.onKeyDownWrapper_);
- Blockly.unbindEvent_(htmlInput.onKeyUpWrapper_);
- Blockly.unbindEvent_(htmlInput.onKeyPressWrapper_);
- thisField.workspace_.removeChangeListener(
- htmlInput.onWorkspaceChangeWrapper_);
- Blockly.FieldTextInput.htmlInput_ = null;
- // Delete style properties.
- var style = Blockly.WidgetDiv.DIV.style;
- style.width = 'auto';
- style.height = 'auto';
- style.fontSize = '';
- };
- };
|