123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500 |
- 'use strict';
- goog.provide('Blockly.Field');
- goog.require('goog.asserts');
- goog.require('goog.dom');
- goog.require('goog.math.Size');
- goog.require('goog.style');
- goog.require('goog.userAgent');
- Blockly.Field = function(text, opt_validator) {
- this.size_ = new goog.math.Size(0, 25);
- this.setValue(text);
- this.setValidator(opt_validator);
- };
- Blockly.Field.cacheWidths_ = null;
- Blockly.Field.cacheReference_ = 0;
- Blockly.Field.prototype.name = undefined;
- Blockly.Field.prototype.maxDisplayLength = 50;
- Blockly.Field.prototype.text_ = '';
- Blockly.Field.prototype.sourceBlock_ = null;
- Blockly.Field.prototype.visible_ = true;
- Blockly.Field.prototype.validator_ = null;
- Blockly.Field.NBSP = '\u00A0';
- Blockly.Field.prototype.EDITABLE = true;
- Blockly.Field.prototype.setSourceBlock = function(block) {
- goog.asserts.assert(!this.sourceBlock_, 'Field already bound to a block.');
- this.sourceBlock_ = block;
- };
- Blockly.Field.prototype.init = function() {
- if (this.fieldGroup_) {
-
- return;
- }
-
- this.fieldGroup_ = Blockly.createSvgElement('g', {}, null);
- if (!this.visible_) {
- this.fieldGroup_.style.display = 'none';
- }
- this.borderRect_ = Blockly.createSvgElement('rect',
- {'rx': 4,
- 'ry': 4,
- 'x': -Blockly.BlockSvg.SEP_SPACE_X / 2,
- 'y': 0,
- 'height': 16}, this.fieldGroup_, this.sourceBlock_.workspace);
-
- this.textElement_ = Blockly.createSvgElement('text',
- {'class': 'blocklyText', 'y': this.size_.height - 12.5},
- this.fieldGroup_);
- this.updateEditable();
- this.sourceBlock_.getSvgRoot().appendChild(this.fieldGroup_);
- this.mouseUpWrapper_ =
- Blockly.bindEventWithChecks_(this.fieldGroup_, 'mouseup', this,
- this.onMouseUp_);
-
- this.updateTextNode_();
- };
- Blockly.Field.prototype.dispose = function() {
- if (this.mouseUpWrapper_) {
- Blockly.unbindEvent_(this.mouseUpWrapper_);
- this.mouseUpWrapper_ = null;
- }
- this.sourceBlock_ = null;
- goog.dom.removeNode(this.fieldGroup_);
- this.fieldGroup_ = null;
- this.textElement_ = null;
- this.borderRect_ = null;
- this.validator_ = null;
- };
- Blockly.Field.prototype.updateEditable = function() {
- var group = this.fieldGroup_;
- if (!this.EDITABLE || !group) {
- return;
- }
- if (this.sourceBlock_.isEditable()) {
- Blockly.addClass_(group, 'blocklyEditableText');
- Blockly.removeClass_(group, 'blocklyNonEditableText');
- this.fieldGroup_.style.cursor = this.CURSOR;
- } else {
- Blockly.addClass_(group, 'blocklyNonEditableText');
- Blockly.removeClass_(group, 'blocklyEditableText');
- this.fieldGroup_.style.cursor = '';
- }
- };
- Blockly.Field.prototype.isVisible = function() {
- return this.visible_;
- };
- Blockly.Field.prototype.setVisible = function(visible) {
- if (this.visible_ == visible) {
- return;
- }
- this.visible_ = visible;
- var root = this.getSvgRoot();
- if (root) {
- root.style.display = visible ? 'block' : 'none';
- this.render_();
- }
- };
- Blockly.Field.prototype.setValidator = function(handler) {
- this.validator_ = handler;
- };
- Blockly.Field.prototype.getValidator = function() {
- return this.validator_;
- };
- Blockly.Field.prototype.classValidator = function(text) {
- return text;
- };
- Blockly.Field.prototype.callValidator = function(text) {
- var classResult = this.classValidator(text);
- if (classResult === null) {
-
- return null;
- } else if (classResult !== undefined) {
- text = classResult;
- }
- var userValidator = this.getValidator();
- if (userValidator) {
- var userResult = userValidator.call(this, text);
- if (userResult === null) {
-
- return null;
- } else if (userResult !== undefined) {
- text = userResult;
- }
- }
- return text;
- };
- Blockly.Field.prototype.getSvgRoot = function() {
- return (this.fieldGroup_);
- };
- Blockly.Field.prototype.render_ = function() {
- if (this.visible_ && this.textElement_) {
- var key = this.textElement_.textContent + '\n' +
- this.textElement_.className.baseVal;
- if (Blockly.Field.cacheWidths_ && Blockly.Field.cacheWidths_[key]) {
- var width = Blockly.Field.cacheWidths_[key];
- } else {
- try {
- var width = this.textElement_.getComputedTextLength();
- } catch (e) {
-
-
- var width = this.textElement_.textContent.length * 8;
- }
- if (Blockly.Field.cacheWidths_) {
- Blockly.Field.cacheWidths_[key] = width;
- }
- }
- if (this.borderRect_) {
- this.borderRect_.setAttribute('width',
- width + Blockly.BlockSvg.SEP_SPACE_X);
- }
- } else {
- var width = 0;
- }
- this.size_.width = width;
- };
- Blockly.Field.startCache = function() {
- Blockly.Field.cacheReference_++;
- if (!Blockly.Field.cacheWidths_) {
- Blockly.Field.cacheWidths_ = {};
- }
- };
- Blockly.Field.stopCache = function() {
- Blockly.Field.cacheReference_--;
- if (!Blockly.Field.cacheReference_) {
- Blockly.Field.cacheWidths_ = null;
- }
- };
- Blockly.Field.prototype.getSize = function() {
- if (!this.size_.width) {
- this.render_();
- }
- return this.size_;
- };
- Blockly.Field.prototype.getScaledBBox_ = function() {
- var bBox = this.borderRect_.getBBox();
-
- return new goog.math.Size(bBox.width * this.sourceBlock_.workspace.scale,
- bBox.height * this.sourceBlock_.workspace.scale);
- };
- Blockly.Field.prototype.getText = function() {
- return this.text_;
- };
- Blockly.Field.prototype.setText = function(text) {
- if (text === null) {
-
- return;
- }
- text = String(text);
- if (text === this.text_) {
-
- return;
- }
- this.text_ = text;
- this.updateTextNode_();
- if (this.sourceBlock_ && this.sourceBlock_.rendered) {
- this.sourceBlock_.render();
- this.sourceBlock_.bumpNeighbours_();
- }
- };
- Blockly.Field.prototype.updateTextNode_ = function() {
- if (!this.textElement_) {
-
- return;
- }
- var text = this.text_;
- if (text.length > this.maxDisplayLength) {
-
- text = text.substring(0, this.maxDisplayLength - 2) + '\u2026';
- }
-
- text = text.replace(/\s/g, Blockly.Field.NBSP);
- if (this.sourceBlock_.RTL && text) {
-
- text += '\u200F';
- }
- if (!text) {
-
- text = Blockly.Field.NBSP;
- }
-
- goog.dom.removeChildren( (this.textElement_));
- var textNode = document.createTextNode(text);
- this.textElement_.appendChild(textNode);
-
- this.size_.width = 0;
- };
- Blockly.Field.prototype.getValue = function() {
- return this.getText();
- };
- Blockly.Field.prototype.setValue = function(newText) {
- if (newText === null) {
-
- return;
- }
- var oldText = this.getValue();
- if (oldText == newText) {
- return;
- }
- if (this.sourceBlock_ && Blockly.Events.isEnabled()) {
- Blockly.Events.fire(new Blockly.Events.Change(
- this.sourceBlock_, 'field', this.name, oldText, newText));
- }
- this.setText(newText);
- };
- Blockly.Field.prototype.onMouseUp_ = function(e) {
- if ((goog.userAgent.IPHONE || goog.userAgent.IPAD) &&
- !goog.userAgent.isVersionOrHigher('537.51.2') &&
- e.layerX !== 0 && e.layerY !== 0) {
-
-
- return;
- } else if (Blockly.isRightButton(e)) {
-
- return;
- } else if (this.sourceBlock_.workspace.isDragging()) {
-
- return;
- } else if (this.sourceBlock_.isEditable()) {
-
- this.showEditor_(e);
-
-
-
- }
- };
- Blockly.Field.prototype.setTooltip = function(newTip) {
-
- };
- Blockly.Field.prototype.getAbsoluteXY_ = function() {
- return goog.style.getPageOffset(this.borderRect_);
- };
|