|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Visual Blocks Editor |
| 4 | + * |
| 5 | + * Copyright 2013 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 | +/** |
| 22 | + * @fileoverview Range input field. |
| 23 | + * @author [email protected] (Neil Fraser) |
| 24 | + */ |
| 25 | +'use strict'; |
| 26 | +goog.provide('Blockly.FieldRange'); |
| 27 | +goog.require('Blockly.FieldTextInput'); |
| 28 | +goog.require('goog.math'); |
| 29 | +goog.require('goog.userAgent'); |
| 30 | +/** |
| 31 | + * Class for an editable range field. |
| 32 | + * @param {string} text The initial content of the field. |
| 33 | + * @param {string} opt_min The minimum content of the field. |
| 34 | + * @param {string} opt_max The maximum content of the field. |
| 35 | + * @param {Function} opt_changeHandler An optional function that is called |
| 36 | + * to validate any constraints on what the user entered. Takes the new |
| 37 | + * text as an argument and returns the accepted text or null to abort |
| 38 | + * the change. |
| 39 | + * @extends {Blockly.FieldTextInput} |
| 40 | + * @constructor |
| 41 | + */ |
| 42 | +Blockly.FieldRange = function (text, opt_min, opt_max, opt_changeHandler) { |
| 43 | + var changeHandler; |
| 44 | + if (opt_changeHandler) { |
| 45 | + // Wrap the user's change handler together with the range validator. |
| 46 | + var thisObj = this; |
| 47 | + changeHandler = function (value) { |
| 48 | + value = Blockly.FieldRange.rangeValidator.call(thisObj, value); |
| 49 | + if (value !== null) { |
| 50 | + opt_changeHandler.call(thisObj, value); |
| 51 | + } |
| 52 | + return value; |
| 53 | + }; |
| 54 | + } else { |
| 55 | + changeHandler = Blockly.FieldRange.rangeValidator; |
| 56 | + } |
| 57 | + |
| 58 | + Blockly.FieldRange.MIN = Number(opt_min); |
| 59 | + Blockly.FieldRange.MAX = Number(opt_max); |
| 60 | + Blockly.FieldRange.superClass_.constructor.call(this, |
| 61 | + text, changeHandler); |
| 62 | +}; |
| 63 | +goog.inherits(Blockly.FieldRange, Blockly.FieldTextInput); |
| 64 | +/** |
| 65 | + * Clone this FieldRange. |
| 66 | + * @return {!Blockly.FieldRange} The result of calling the constructor again |
| 67 | + * with the current values of the arguments used during construction. |
| 68 | + */ |
| 69 | +Blockly.FieldRange.prototype.clone = function () { |
| 70 | + return new Blockly.FieldRange(this.getText(), this.changeHandler_, |
| 71 | + Blockly.FieldRange.MIN, Blockly.FieldRange.MAX); |
| 72 | +}; |
| 73 | +/** |
| 74 | + * Clean up this FieldRange, as well as the inherited FieldTextInput. |
| 75 | + * @return {!Function} Closure to call on destruction of the WidgetDiv. |
| 76 | + * @private |
| 77 | + */ |
| 78 | +Blockly.FieldRange.prototype.dispose_ = function () { |
| 79 | + var thisField = this; |
| 80 | + return function () { |
| 81 | + Blockly.FieldRange.superClass_.dispose_.call(thisField)(); |
| 82 | + thisField.gauge_ = null; |
| 83 | + if (thisField.clickWrapper_) { |
| 84 | + Blockly.unbindEvent_(thisField.clickWrapper_); |
| 85 | + } |
| 86 | + if (thisField.moveWrapper1_) { |
| 87 | + Blockly.unbindEvent_(thisField.moveWrapper1_); |
| 88 | + } |
| 89 | + if (thisField.moveWrapper2_) { |
| 90 | + Blockly.unbindEvent_(thisField.moveWrapper2_); |
| 91 | + } |
| 92 | + }; |
| 93 | +}; |
| 94 | +/** |
| 95 | + * Show the inline free-text editor on top of the text. |
| 96 | + * @private |
| 97 | + */ |
| 98 | +Blockly.FieldRange.prototype.showEditor_ = function () { |
| 99 | + var noFocus = |
| 100 | + goog.userAgent.MOBILE || goog.userAgent.ANDROID || goog.userAgent.IPAD; |
| 101 | + // Mobile browsers have issues with in-line textareas (focus & keyboards). |
| 102 | + Blockly.FieldRange.superClass_.showEditor_.call(this, noFocus); |
| 103 | + var div = Blockly.WidgetDiv.DIV; |
| 104 | + if (!div.firstChild) { |
| 105 | + // Mobile interface uses window.prompt. |
| 106 | + return; |
| 107 | + } |
| 108 | + |
| 109 | + // Build the SVG DOM for the slider |
| 110 | + var svg = Blockly.createSvgElement('svg', { |
| 111 | + 'xmlns': 'http://www.w3.org/2000/svg', |
| 112 | + 'xmlns:html': 'http://www.w3.org/1999/xhtml', |
| 113 | + 'xmlns:xlink': 'http://www.w3.org/1999/xlink', |
| 114 | + 'version': '1.1', |
| 115 | + 'height': '44px', |
| 116 | + 'width': '200px' |
| 117 | + }, div); |
| 118 | + this.gauge_ = Blockly.createSvgElement('rect', { |
| 119 | + 'x': '10', 'y': '10', |
| 120 | + 'rx': '12', 'ry': '12', |
| 121 | + 'width': '180', 'height': '24', |
| 122 | + 'style': 'fill: #bbb;stroke: #000;stroke-width: .5;' |
| 123 | + }, svg); |
| 124 | + if (Blockly.FieldRange.MIN < 0) { |
| 125 | + Blockly.createSvgElement('line', { |
| 126 | + 'x1': '22', 'y1': '22', 'x2': '178', 'y2': '22', |
| 127 | + 'style': 'stroke:#888;stroke-width:10;stroke-linecap:round;' |
| 128 | + }, svg); |
| 129 | + } |
| 130 | + if (Blockly.FieldRange.MAX > 0 && Blockly.FieldRange.MIN < 0) { |
| 131 | + Blockly.createSvgElement('line', { |
| 132 | + 'x1': '125', 'y1': '22', 'x2': '178', 'y2': '22', |
| 133 | + 'style': 'stroke:#eee;stroke-width:10;stroke-linecap:round;' |
| 134 | + }, svg); |
| 135 | + Blockly.createSvgElement('line', { |
| 136 | + 'x1': '100', 'y1': '22', 'x2': '135', 'y2': '22', |
| 137 | + 'style': 'stroke:#eee;stroke-width:10;' |
| 138 | + }, svg); |
| 139 | + } else { |
| 140 | + Blockly.createSvgElement('line', { |
| 141 | + 'x1': '22', 'y1': '22', 'x2': '178', 'y2': '22', |
| 142 | + 'style': 'stroke:#eee;stroke-width:10;stroke-linecap:round;' |
| 143 | + }, svg); |
| 144 | + } |
| 145 | + this.circle_ = Blockly.createSvgElement('circle', { |
| 146 | + 'r': '9.5', |
| 147 | + 'style': 'fill:#fff;stroke:#000;fill-opacity:.6;stroke-width:2;' |
| 148 | + }, svg); |
| 149 | + svg.style.marginLeft = '-35px'; |
| 150 | + this.clickWrapper_ = |
| 151 | + Blockly.bindEvent_(svg, 'click', this, Blockly.WidgetDiv.hide); |
| 152 | + this.moveWrapper1_ = |
| 153 | + Blockly.bindEvent_(this.circle_, 'mousemove', this, this.onMouseMove); |
| 154 | + this.moveWrapper2_ = |
| 155 | + Blockly.bindEvent_(this.gauge_, 'mousemove', this, this.onMouseMove); |
| 156 | + this.updateGraph_(); |
| 157 | +}; |
| 158 | +/** |
| 159 | + * Set the range to match the mouse's position. |
| 160 | + * @param {!Event} e Mouse move event. |
| 161 | + */ |
| 162 | +Blockly.FieldRange.prototype.onMouseMove = function (e) { |
| 163 | + |
| 164 | + var bBox = this.gauge_.ownerSVGElement.getBoundingClientRect(); |
| 165 | + // set this values: |
| 166 | + //var steps = Blockly.FieldRange.ROUND; |
| 167 | + var min = Blockly.FieldRange.MIN; |
| 168 | + var max = Blockly.FieldRange.MAX; |
| 169 | + // calculate slider position: |
| 170 | + var m = max - min; |
| 171 | + var dx = e.clientX - bBox.left; |
| 172 | + var pos = dx / (bBox.width / 200); |
| 173 | + if (pos < 22) |
| 174 | + pos = 22; |
| 175 | + if (pos > 178) |
| 176 | + pos = 178; |
| 177 | + // draw slider and knob: |
| 178 | + var range = Math.round(((pos - 22) / (178 - 22)) * m + min); |
| 179 | + if (isNaN(range)) { |
| 180 | + // This shouldn't happen, but let's not let this error propogate further. |
| 181 | + return; |
| 182 | + } |
| 183 | + range = String(range); |
| 184 | + Blockly.FieldTextInput.htmlInput_.value = range; |
| 185 | + this.setText(range); |
| 186 | +}; |
| 187 | +/** |
| 188 | + * Insert a degree symbol. |
| 189 | + * @param {?string} text New text. |
| 190 | + */ |
| 191 | +Blockly.FieldRange.prototype.setText = function (text) { |
| 192 | + Blockly.FieldRange.superClass_.setText.call(this, text); |
| 193 | + if (!this.textElement_) { |
| 194 | + // Not rendered yet. |
| 195 | + return; |
| 196 | + } |
| 197 | + this.updateGraph_(); |
| 198 | + // Cached width is obsolete. Clear it. |
| 199 | + this.size_.width = 0; |
| 200 | +}; |
| 201 | +/** |
| 202 | + * Redraw the graph with the current range. |
| 203 | + * @private |
| 204 | + */ |
| 205 | +Blockly.FieldRange.prototype.updateGraph_ = function () { |
| 206 | + if (!this.gauge_) { |
| 207 | + return; |
| 208 | + } |
| 209 | + |
| 210 | + var rangeValue = Number(this.getText()); |
| 211 | + if (isNaN(rangeValue)) { |
| 212 | + this.circle_.setAttribute('cx', '100'); |
| 213 | + this.circle_.setAttribute('cy', '22'); |
| 214 | + } else { |
| 215 | + var min = Blockly.FieldRange.MIN; |
| 216 | + var max = Blockly.FieldRange.MAX; |
| 217 | + // calculate slider position: |
| 218 | + var m = max - min; |
| 219 | + if (rangeValue < min) |
| 220 | + rangeValue = min; |
| 221 | + if (rangeValue > max) |
| 222 | + rangeValue = max; |
| 223 | + var pos = Math.round(((rangeValue - min) / m) * 156 + 22); |
| 224 | + this.circle_.setAttribute('cx', pos); |
| 225 | + this.circle_.setAttribute('cy', '22'); |
| 226 | + } |
| 227 | +}; |
| 228 | +/** |
| 229 | + * Ensure that only a number may be entered. |
| 230 | + * @param {string} text The user's text. |
| 231 | + * @return {?string} A string representing a valid range, or null if invalid. |
| 232 | + */ |
| 233 | +Blockly.FieldRange.rangeValidator = function (text) { |
| 234 | + var n = Blockly.FieldTextInput.numberValidator(text); |
| 235 | + if (n < Blockly.FieldRange.MIN) |
| 236 | + n = Blockly.FieldRange.MIN; |
| 237 | + if (n > Blockly.FieldRange.MAX) |
| 238 | + n = Blockly.FieldRange.MAX; |
| 239 | + return n; |
| 240 | +}; |
0 commit comments