| 1 | /** |
| 2 | * @fileoverview Textbutton field. |
| 3 | * @author Leo Gordon |
| 4 | */ |
| 5 | 'use strict'; |
| 6 | |
| 7 | goog.provide('Blockly.FieldTextbutton'); |
| 8 | |
| 9 | goog.require('Blockly.Field'); |
| 10 | |
| 11 | |
| 12 | Blockly.FieldTextbutton = function(buttontext, changeHandler) { |
| 13 | Blockly.FieldTextbutton.superClass_.constructor.call(this, ''); |
| 14 | |
| 15 | this.buttontext_ = buttontext; |
| 16 | this.changeHandler_ = changeHandler; |
| 17 | this.setText(buttontext); |
| 18 | }; |
| 19 | goog.inherits(Blockly.FieldTextbutton, Blockly.Field); |
| 20 | |
| 21 | |
| 22 | Blockly.FieldTextbutton.prototype.clone = function() { |
| 23 | return new Blockly.FieldTextbutton(this.buttontext_, this.changeHandler_); |
| 24 | }; |
| 25 | |
| 26 | |
| 27 | Blockly.FieldTextbutton.prototype.CURSOR = 'default'; |
| 28 | |
| 29 | |
| 30 | Blockly.FieldTextbutton.prototype.showEditor_ = function() { |
| 31 | if (this.changeHandler_) { |
| 32 | this.changeHandler_(); |
| 33 | } |
| 34 | }; |
| 35 | |
| 36 | |
| 37 | /* |
| 38 | |
| 39 | // An example of how to use FieldTextbutton: implementation of a simple register with limiters linked to "-" and "+" buttons |
| 40 | |
| 41 | Blockly.Block.appendMinusPlusCounter = function(block, name, startValue, lowerLimit, upperLimit) { |
| 42 | block.appendDummyInput(name+'_input') |
| 43 | .appendField(name+':', name+'_label') |
| 44 | .appendField(String(startValue || '0'), name) |
| 45 | .appendField(new Blockly.FieldTextbutton('–', function() { var counter_=parseInt(this.sourceBlock_.getFieldValue(name))-1; if((lowerLimit===undefined) || counter_>=lowerLimit) { this.sourceBlock_.setFieldValue(String(counter_), name); } }), name+'_minus') |
| 46 | .appendField(new Blockly.FieldTextbutton('+', function() { var counter_=parseInt(this.sourceBlock_.getFieldValue(name))+1; if((upperLimit===undefined) || counter_<=upperLimit) { this.sourceBlock_.setFieldValue(String(counter_), name); } }), name+'_plus'); |
| 47 | } |
| 48 | |
| 49 | // A usage example of the above. You can add two independent MinusPlusCounters to a block by saying: |
| 50 | |
| 51 | Blockly.Block.appendMinusPlusCounter(this, 'age', 0, 0 ); |
| 52 | Blockly.Block.appendMinusPlusCounter(this, 'temperature', 37, 34, 42 ); |
| 53 | |
| 54 | */ |