123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377 |
- 'use strict';
- goog.provide('Blockly.Block');
- goog.require('Blockly.Blocks');
- goog.require('Blockly.Comment');
- goog.require('Blockly.Connection');
- goog.require('Blockly.Input');
- goog.require('Blockly.Mutator');
- goog.require('Blockly.Warning');
- goog.require('Blockly.Workspace');
- goog.require('Blockly.Xml');
- goog.require('goog.array');
- goog.require('goog.asserts');
- goog.require('goog.math.Coordinate');
- goog.require('goog.string');
- Blockly.Block = function(workspace, prototypeName, opt_id) {
-
- this.id = (opt_id && !workspace.getBlockById(opt_id)) ?
- opt_id : Blockly.genUid();
- workspace.blockDB_[this.id] = this;
-
- this.outputConnection = null;
-
- this.nextConnection = null;
-
- this.previousConnection = null;
-
- this.inputList = [];
-
- this.inputsInline = undefined;
-
- this.disabled = false;
-
- this.tooltip = '';
-
- this.contextMenu = true;
-
- this.parentBlock_ = null;
-
- this.childBlocks_ = [];
-
- this.deletable_ = true;
-
- this.movable_ = true;
-
- this.editable_ = true;
-
- this.isShadow_ = false;
-
- this.collapsed_ = false;
-
- this.comment = null;
-
- this.xy_ = new goog.math.Coordinate(0, 0);
-
- this.workspace = workspace;
-
- this.isInFlyout = workspace.isFlyout;
-
- this.isInMutator = workspace.isMutator;
-
- this.RTL = workspace.RTL;
-
- if (prototypeName) {
-
- this.type = prototypeName;
- var prototype = Blockly.Blocks[prototypeName];
- goog.asserts.assertObject(prototype,
- 'Error: "%s" is an unknown language block.', prototypeName);
- goog.mixin(this, prototype);
- }
- workspace.addTopBlock(this);
-
- if (goog.isFunction(this.init)) {
- this.init();
- }
-
-
- this.inputsInlineDefault = this.inputsInline;
-
- this.lineNumber = undefined;
- if (Blockly.Events.isEnabled()) {
- Blockly.Events.fire(new Blockly.Events.Create(this));
- }
-
- if (goog.isFunction(this.onchange)) {
- this.onchangeWrapper_ = this.onchange.bind(this);
- this.workspace.addChangeListener(this.onchangeWrapper_);
- }
- };
- Blockly.Block.obtain = function(workspace, prototypeName) {
- console.warn('Deprecated call to Blockly.Block.obtain, ' +
- 'use workspace.newBlock instead.');
- return workspace.newBlock(prototypeName);
- };
- Blockly.Block.prototype.data = null;
- Blockly.Block.prototype.colour_ = '#000000';
- Blockly.Block.prototype.dispose = function(healStack) {
- if (!this.workspace) {
-
- return;
- }
-
- if (this.onchangeWrapper_) {
- this.workspace.removeChangeListener(this.onchangeWrapper_);
- }
- this.unplug(healStack);
- if (Blockly.Events.isEnabled()) {
- Blockly.Events.fire(new Blockly.Events.Delete(this));
- }
- Blockly.Events.disable();
- try {
-
-
- if (this.workspace) {
- this.workspace.removeTopBlock(this);
-
- delete this.workspace.blockDB_[this.id];
- this.workspace = null;
- }
-
-
-
-
- for (var i = this.childBlocks_.length - 1; i >= 0; i--) {
- this.childBlocks_[i].dispose(false);
- }
-
-
- for (var i = 0, input; input = this.inputList[i]; i++) {
- input.dispose();
- }
- this.inputList.length = 0;
-
- var connections = this.getConnections_(true);
- for (var i = 0; i < connections.length; i++) {
- var connection = connections[i];
- if (connection.isConnected()) {
- connection.disconnect();
- }
- connections[i].dispose();
- }
- } finally {
- Blockly.Events.enable();
- }
- };
- Blockly.Block.prototype.unplug = function(opt_healStack) {
- if (this.outputConnection) {
- if (this.outputConnection.isConnected()) {
-
- this.outputConnection.disconnect();
- }
- } else if (this.previousConnection) {
- var previousTarget = null;
- if (this.previousConnection.isConnected()) {
-
- previousTarget = this.previousConnection.targetConnection;
-
- this.previousConnection.disconnect();
- }
- var nextBlock = this.getNextBlock();
- if (opt_healStack && nextBlock) {
-
- var nextTarget = this.nextConnection.targetConnection;
- nextTarget.disconnect();
- if (previousTarget && previousTarget.checkType_(nextTarget)) {
-
- previousTarget.connect(nextTarget);
- }
- }
- }
- };
- Blockly.Block.prototype.getConnections_ = function() {
- var myConnections = [];
- if (this.outputConnection) {
- myConnections.push(this.outputConnection);
- }
- if (this.previousConnection) {
- myConnections.push(this.previousConnection);
- }
- if (this.nextConnection) {
- myConnections.push(this.nextConnection);
- }
- for (var i = 0, input; input = this.inputList[i]; i++) {
- if (input.connection) {
- myConnections.push(input.connection);
- }
- }
- return myConnections;
- };
- Blockly.Block.prototype.lastConnectionInStack_ = function() {
- var nextConnection = this.nextConnection;
- while (nextConnection) {
- var nextBlock = nextConnection.targetBlock();
- if (!nextBlock) {
-
- return nextConnection;
- }
- nextConnection = nextBlock.nextConnection;
- }
-
- return null;
- };
- Blockly.Block.prototype.bumpNeighbours_ = function() {
- if (!this.workspace) {
- return;
- }
- if (Blockly.dragMode_ != Blockly.DRAG_NONE) {
- return;
- }
- var rootBlock = this.getRootBlock();
- if (rootBlock.isInFlyout) {
- return;
- }
-
- var myConnections = this.getConnections_(false);
- for (var i = 0, connection; connection = myConnections[i]; i++) {
-
- if (connection.isConnected() && connection.isSuperior()) {
- connection.targetBlock().bumpNeighbours_();
- }
- var neighbours = connection.neighbours_(Blockly.SNAP_RADIUS);
- for (var j = 0, otherConnection; otherConnection = neighbours[j]; j++) {
-
-
- if (!connection.isConnected() || !otherConnection.isConnected()) {
-
- if (otherConnection.getSourceBlock().getRootBlock() != rootBlock) {
-
- if (connection.isSuperior()) {
- otherConnection.bumpAwayFrom_(connection);
- } else {
- connection.bumpAwayFrom_(otherConnection);
- }
- }
- }
- }
- }
- };
- Blockly.Block.prototype.getParent = function() {
-
- return this.parentBlock_;
- };
- Blockly.Block.prototype.getInputWithBlock = function(block) {
- for (var i = 0, input; input = this.inputList[i]; i++) {
- if (input.connection && input.connection.targetBlock() == block) {
- return input;
- }
- }
- return null;
- };
- Blockly.Block.prototype.getSurroundParent = function() {
- var block = this;
- do {
- var prevBlock = block;
- block = block.getParent();
- if (!block) {
-
- return null;
- }
- } while (block.getNextBlock() == prevBlock);
-
- return block;
- };
- Blockly.Block.prototype.getNextBlock = function() {
- return this.nextConnection && this.nextConnection.targetBlock();
- };
- Blockly.Block.prototype.getRootBlock = function() {
- var rootBlock;
- var block = this;
- do {
- rootBlock = block;
- block = rootBlock.parentBlock_;
- } while (block);
- return rootBlock;
- };
- Blockly.Block.prototype.getChildren = function() {
- return this.childBlocks_;
- };
- Blockly.Block.prototype.setParent = function(newParent) {
- if (newParent == this.parentBlock_) {
- return;
- }
- if (this.parentBlock_) {
-
- goog.array.remove(this.parentBlock_.childBlocks_, this);
-
- if (this.previousConnection && this.previousConnection.isConnected()) {
- throw 'Still connected to previous block.';
- }
- if (this.outputConnection && this.outputConnection.isConnected()) {
- throw 'Still connected to parent block.';
- }
- this.parentBlock_ = null;
-
-
- } else {
-
- this.workspace.removeTopBlock(this);
- }
- this.parentBlock_ = newParent;
- if (newParent) {
-
- newParent.childBlocks_.push(this);
- } else {
- this.workspace.addTopBlock(this);
- }
- };
- Blockly.Block.prototype.getDescendants = function() {
- var blocks = [this];
- for (var child, x = 0; child = this.childBlocks_[x]; x++) {
- blocks.push.apply(blocks, child.getDescendants());
- }
- return blocks;
- };
- Blockly.Block.prototype.isDeletable = function() {
- return this.deletable_ && !this.isShadow_ &&
- !(this.workspace && this.workspace.options.readOnly);
- };
- Blockly.Block.prototype.setDeletable = function(deletable) {
- this.deletable_ = deletable;
- };
- Blockly.Block.prototype.setLineNumber = function(lineNumber) {
- this.lineNumber = lineNumber;
- };
- Blockly.Block.prototype.getLineNumber = function() {
- return this.lineNumber;
- };
- Blockly.Block.prototype.isMovable = function() {
- return this.movable_ && !this.isShadow_ &&
- !(this.workspace && this.workspace.options.readOnly);
- };
- Blockly.Block.prototype.setMovable = function(movable) {
- this.movable_ = movable;
- };
- Blockly.Block.prototype.isShadow = function() {
- return this.isShadow_;
- };
- Blockly.Block.prototype.setShadow = function(shadow) {
- this.isShadow_ = shadow;
- };
- Blockly.Block.prototype.isEditable = function() {
- return this.editable_ && !(this.workspace && this.workspace.options.readOnly);
- };
- Blockly.Block.prototype.setEditable = function(editable) {
- this.editable_ = editable;
- for (var i = 0, input; input = this.inputList[i]; i++) {
- for (var j = 0, field; field = input.fieldRow[j]; j++) {
- field.updateEditable();
- }
- }
- };
- Blockly.Block.prototype.setConnectionsHidden = function(hidden) {
- if (!hidden && this.isCollapsed()) {
- if (this.outputConnection) {
- this.outputConnection.setHidden(hidden);
- }
- if (this.previousConnection) {
- this.previousConnection.setHidden(hidden);
- }
- if (this.nextConnection) {
- this.nextConnection.setHidden(hidden);
- var child = this.nextConnection.targetBlock();
- if (child) {
- child.setConnectionsHidden(hidden);
- }
- }
- } else {
- var myConnections = this.getConnections_(true);
- for (var i = 0, connection; connection = myConnections[i]; i++) {
- connection.setHidden(hidden);
- if (connection.isSuperior()) {
- var child = connection.targetBlock();
- if (child) {
- child.setConnectionsHidden(hidden);
- }
- }
- }
- }
- };
- Blockly.Block.prototype.setHelpUrl = function(url) {
- this.helpUrl = url;
- };
- Blockly.Block.prototype.setTooltip = function(newTip) {
- this.tooltip = newTip;
- };
- Blockly.Block.prototype.getColour = function() {
- return this.colour_;
- };
- Blockly.Block.prototype.setColour = function(colour) {
- var hue = parseFloat(colour);
- if (!isNaN(hue)) {
- this.colour_ = Blockly.hueToRgb(hue);
- } else if (goog.isString(colour) && colour.match(/^#[0-9a-fA-F]{6}$/)) {
- this.colour_ = colour;
- } else {
- throw 'Invalid colour: ' + colour;
- }
- };
- Blockly.Block.prototype.getField = function(name) {
- for (var i = 0, input; input = this.inputList[i]; i++) {
- for (var j = 0, field; field = input.fieldRow[j]; j++) {
- if (field.name === name) {
- return field;
- }
- }
- }
- return null;
- };
- Blockly.Block.prototype.getVars = function() {
- var vars = [];
- for (var i = 0, input; input = this.inputList[i]; i++) {
- for (var j = 0, field; field = input.fieldRow[j]; j++) {
- if (field instanceof Blockly.FieldVariable) {
- vars.push(field.getValue());
- }
- }
- }
- return vars;
- };
- Blockly.Block.prototype.renameVar = function(oldName, newName) {
- for (var i = 0, input; input = this.inputList[i]; i++) {
- for (var j = 0, field; field = input.fieldRow[j]; j++) {
- if (field instanceof Blockly.FieldVariable &&
- Blockly.Names.equals(oldName, field.getValue())) {
- field.setValue(newName);
- }
- }
- }
- };
- Blockly.Block.prototype.getFieldValue = function(name) {
- var field = this.getField(name);
- if (field) {
- return field.getValue();
- }
- return null;
- };
- Blockly.Block.prototype.getTitleValue = function(name) {
- console.warn('Deprecated call to getTitleValue, use getFieldValue instead.');
- return this.getFieldValue(name);
- };
- Blockly.Block.prototype.setFieldValue = function(newValue, name) {
- var field = this.getField(name);
- goog.asserts.assertObject(field, 'Field "%s" not found.', name);
- field.setValue(newValue);
- };
- Blockly.Block.prototype.setTitleValue = function(newValue, name) {
- console.warn('Deprecated call to setTitleValue, use setFieldValue instead.');
- this.setFieldValue(newValue, name);
- };
- Blockly.Block.prototype.setPreviousStatement = function(newBoolean, opt_check) {
- if (newBoolean) {
- if (opt_check === undefined) {
- opt_check = null;
- }
- if (!this.previousConnection) {
- goog.asserts.assert(!this.outputConnection,
- 'Remove output connection prior to adding previous connection.');
- this.previousConnection =
- this.makeConnection_(Blockly.PREVIOUS_STATEMENT);
- }
- this.previousConnection.setCheck(opt_check);
- } else {
- if (this.previousConnection) {
- goog.asserts.assert(!this.previousConnection.isConnected(),
- 'Must disconnect previous statement before removing connection.');
- this.previousConnection.dispose();
- this.previousConnection = null;
- }
- }
- };
- Blockly.Block.prototype.setNextStatement = function(newBoolean, opt_check) {
- if (newBoolean) {
- if (opt_check === undefined) {
- opt_check = null;
- }
- if (!this.nextConnection) {
- this.nextConnection = this.makeConnection_(Blockly.NEXT_STATEMENT);
- }
- this.nextConnection.setCheck(opt_check);
- } else {
- if (this.nextConnection) {
- goog.asserts.assert(!this.nextConnection.isConnected(),
- 'Must disconnect next statement before removing connection.');
- this.nextConnection.dispose();
- this.nextConnection = null;
- }
- }
- };
- Blockly.Block.prototype.setOutput = function(newBoolean, opt_check) {
- if (newBoolean) {
- if (opt_check === undefined) {
- opt_check = null;
- }
- if (!this.outputConnection) {
- goog.asserts.assert(!this.previousConnection,
- 'Remove previous connection prior to adding output connection.');
- this.outputConnection = this.makeConnection_(Blockly.OUTPUT_VALUE);
- }
- this.outputConnection.setCheck(opt_check);
- } else {
- if (this.outputConnection) {
- goog.asserts.assert(!this.outputConnection.isConnected(),
- 'Must disconnect output value before removing connection.');
- this.outputConnection.dispose();
- this.outputConnection = null;
- }
- }
- };
- Blockly.Block.prototype.setInputsInline = function(newBoolean) {
- if (this.inputsInline != newBoolean) {
- Blockly.Events.fire(new Blockly.Events.Change(
- this, 'inline', null, this.inputsInline, newBoolean));
- this.inputsInline = newBoolean;
- }
- };
- Blockly.Block.prototype.getInputsInline = function() {
- if (this.inputsInline != undefined) {
-
- return this.inputsInline;
- }
-
- for (var i = 1; i < this.inputList.length; i++) {
- if (this.inputList[i - 1].type == Blockly.DUMMY_INPUT &&
- this.inputList[i].type == Blockly.DUMMY_INPUT) {
-
- return false;
- }
- }
- for (var i = 1; i < this.inputList.length; i++) {
- if (this.inputList[i - 1].type == Blockly.INPUT_VALUE &&
- this.inputList[i].type == Blockly.DUMMY_INPUT) {
-
- return true;
- }
- }
- return false;
- };
- Blockly.Block.prototype.setDisabled = function(disabled) {
- if (this.disabled != disabled) {
- Blockly.Events.fire(new Blockly.Events.Change(
- this, 'disabled', null, this.disabled, disabled));
- this.disabled = disabled;
- }
- };
- Blockly.Block.prototype.getInheritedDisabled = function() {
- var block = this;
- while (true) {
- block = block.getSurroundParent();
- if (!block) {
-
- return false;
- } else if (block.disabled) {
- return true;
- }
- }
- };
- Blockly.Block.prototype.isCollapsed = function() {
- return this.collapsed_;
- };
- Blockly.Block.prototype.setCollapsed = function(collapsed) {
- if (this.collapsed_ != collapsed) {
- Blockly.Events.fire(new Blockly.Events.Change(
- this, 'collapsed', null, this.collapsed_, collapsed));
- this.collapsed_ = collapsed;
- }
- };
- Blockly.Block.prototype.toString = function(opt_maxLength, opt_emptyToken) {
- var text = [];
- var emptyFieldPlaceholder = opt_emptyToken || '?';
- if (this.collapsed_) {
- text.push(this.getInput('_TEMP_COLLAPSED_INPUT').fieldRow[0].text_);
- } else {
- for (var i = 0, input; input = this.inputList[i]; i++) {
- for (var j = 0, field; field = input.fieldRow[j]; j++) {
- text.push(field.getText());
- }
- if (input.connection) {
- var child = input.connection.targetBlock();
- if (child) {
- text.push(child.toString(undefined, opt_emptyToken));
- } else {
- text.push(emptyFieldPlaceholder);
- }
- }
- }
- }
- text = goog.string.trim(text.join(' ')) || '???';
- if (opt_maxLength) {
-
-
-
- text = goog.string.truncate(text, opt_maxLength);
- }
- return text;
- };
- Blockly.Block.prototype.appendValueInput = function(name) {
- return this.appendInput_(Blockly.INPUT_VALUE, name);
- };
- Blockly.Block.prototype.appendStatementInput = function(name) {
- return this.appendInput_(Blockly.NEXT_STATEMENT, name);
- };
- Blockly.Block.prototype.appendDummyInput = function(opt_name) {
- return this.appendInput_(Blockly.DUMMY_INPUT, opt_name || '');
- };
- Blockly.Block.prototype.jsonInit = function(json) {
-
- goog.asserts.assert(json['output'] == undefined ||
- json['previousStatement'] == undefined,
- 'Must not have both an output and a previousStatement.');
-
- if (json['colour'] !== undefined) {
- this.setColour(json['colour']);
- }
-
- var i = 0;
- while (json['message' + i] !== undefined) {
- this.interpolate_(json['message' + i], json['args' + i] || [],
- json['lastDummyAlign' + i]);
- i++;
- }
- if (json['inputsInline'] !== undefined) {
- this.setInputsInline(json['inputsInline']);
- }
-
- if (json['output'] !== undefined) {
- this.setOutput(true, json['output']);
- }
- if (json['previousStatement'] !== undefined) {
- this.setPreviousStatement(true, json['previousStatement']);
- }
- if (json['nextStatement'] !== undefined) {
- this.setNextStatement(true, json['nextStatement']);
- }
- if (json['tooltip'] !== undefined) {
- this.setTooltip(json['tooltip']);
- }
- if (json['helpUrl'] !== undefined) {
- this.setHelpUrl(json['helpUrl']);
- }
- };
- Blockly.Block.prototype.interpolate_ = function(message, args, lastDummyAlign) {
- var tokens = Blockly.utils.tokenizeInterpolation(message);
-
- var indexDup = [];
- var indexCount = 0;
- var elements = [];
- for (var i = 0; i < tokens.length; i++) {
- var token = tokens[i];
- if (typeof token == 'number') {
- goog.asserts.assert(token > 0 && token <= args.length,
- 'Message index "%s" out of range.', token);
- goog.asserts.assert(!indexDup[token],
- 'Message index "%s" duplicated.', token);
- indexDup[token] = true;
- indexCount++;
- elements.push(args[token - 1]);
- } else {
- token = token.trim();
- if (token) {
- elements.push(token);
- }
- }
- }
- goog.asserts.assert(indexCount == args.length,
- 'Message does not reference all %s arg(s).', args.length);
-
- if (elements.length && (typeof elements[elements.length - 1] == 'string' ||
- goog.string.startsWith(elements[elements.length - 1]['type'],
- 'field_'))) {
- var dummyInput = {type: 'input_dummy'};
- if (lastDummyAlign) {
- dummyInput['align'] = lastDummyAlign;
- }
- elements.push(dummyInput);
- }
-
- var alignmentLookup = {
- 'LEFT': Blockly.ALIGN_LEFT,
- 'RIGHT': Blockly.ALIGN_RIGHT,
- 'CENTRE': Blockly.ALIGN_CENTRE
- };
-
- var fieldStack = [];
- for (var i = 0; i < elements.length; i++) {
- var element = elements[i];
- if (typeof element == 'string') {
- fieldStack.push([element, undefined]);
- } else {
- var field = null;
- var input = null;
- do {
- var altRepeat = false;
- if (typeof element == 'string') {
- field = new Blockly.FieldLabel(element);
- } else {
- switch (element['type']) {
- case 'input_value':
- input = this.appendValueInput(element['name']);
- break;
- case 'input_statement':
- input = this.appendStatementInput(element['name']);
- break;
- case 'input_dummy':
- input = this.appendDummyInput(element['name']);
- break;
- case 'field_label':
- field = new Blockly.FieldLabel(element['text'], element['class']);
- break;
- case 'field_input':
- field = new Blockly.FieldTextInput(element['text']);
- if (typeof element['spellcheck'] == 'boolean') {
- field.setSpellcheck(element['spellcheck']);
- }
- break;
- case 'field_angle':
- field = new Blockly.FieldAngle(element['angle']);
- break;
- case 'field_checkbox':
- field = new Blockly.FieldCheckbox(
- element['checked'] ? 'TRUE' : 'FALSE');
- break;
- case 'field_colour':
- field = new Blockly.FieldColour(element['colour']);
- break;
- case 'field_variable':
- field = new Blockly.FieldVariable(element['variable']);
- break;
- case 'field_dropdown':
- field = new Blockly.FieldDropdown(element['options']);
- break;
- case 'field_image':
- field = new Blockly.FieldImage(element['src'],
- element['width'], element['height'], element['alt']);
- break;
- case 'field_number':
- field = new Blockly.FieldNumber(element['value'],
- element['min'], element['max'], element['precision']);
- break;
- case 'field_date':
- if (Blockly.FieldDate) {
- field = new Blockly.FieldDate(element['date']);
- break;
- }
-
- default:
-
- if (element['alt']) {
- element = element['alt'];
- altRepeat = true;
- }
- }
- }
- } while (altRepeat);
- if (field) {
- fieldStack.push([field, element['name']]);
- } else if (input) {
- if (element['check']) {
- input.setCheck(element['check']);
- }
- if (element['align']) {
- input.setAlign(alignmentLookup[element['align']]);
- }
- for (var j = 0; j < fieldStack.length; j++) {
- input.appendField(fieldStack[j][0], fieldStack[j][1]);
- }
- fieldStack.length = 0;
- }
- }
- }
- };
- Blockly.Block.prototype.appendInput_ = function(type, name) {
- var connection = null;
- if (type == Blockly.INPUT_VALUE || type == Blockly.NEXT_STATEMENT) {
- connection = this.makeConnection_(type);
- }
- var input = new Blockly.Input(type, name, this, connection);
-
- this.inputList.push(input);
- return input;
- };
- Blockly.Block.prototype.moveInputBefore = function(name, refName) {
- if (name == refName) {
- return;
- }
-
- var inputIndex = -1;
- var refIndex = refName ? -1 : this.inputList.length;
- for (var i = 0, input; input = this.inputList[i]; i++) {
- if (input.name == name) {
- inputIndex = i;
- if (refIndex != -1) {
- break;
- }
- } else if (refName && input.name == refName) {
- refIndex = i;
- if (inputIndex != -1) {
- break;
- }
- }
- }
- goog.asserts.assert(inputIndex != -1, 'Named input "%s" not found.', name);
- goog.asserts.assert(refIndex != -1, 'Reference input "%s" not found.',
- refName);
- this.moveNumberedInputBefore(inputIndex, refIndex);
- };
- Blockly.Block.prototype.moveNumberedInputBefore = function(
- inputIndex, refIndex) {
-
- goog.asserts.assert(inputIndex != refIndex, 'Can\'t move input to itself.');
- goog.asserts.assert(inputIndex < this.inputList.length,
- 'Input index ' + inputIndex + ' out of bounds.');
- goog.asserts.assert(refIndex <= this.inputList.length,
- 'Reference input ' + refIndex + ' out of bounds.');
-
- var input = this.inputList[inputIndex];
- this.inputList.splice(inputIndex, 1);
- if (inputIndex < refIndex) {
- refIndex--;
- }
-
- this.inputList.splice(refIndex, 0, input);
- };
- Blockly.Block.prototype.removeInput = function(name, opt_quiet) {
- for (var i = 0, input; input = this.inputList[i]; i++) {
- if (input.name == name) {
- if (input.connection && input.connection.isConnected()) {
- input.connection.setShadowDom(null);
- var block = input.connection.targetBlock();
- if (block.isShadow()) {
-
- block.dispose();
- } else {
-
- block.unplug();
- }
- }
- input.dispose();
- this.inputList.splice(i, 1);
- return;
- }
- }
- if (!opt_quiet) {
- goog.asserts.fail('Input "%s" not found.', name);
- }
- };
- Blockly.Block.prototype.getInput = function(name) {
- for (var i = 0, input; input = this.inputList[i]; i++) {
- if (input.name == name) {
- return input;
- }
- }
-
- return null;
- };
- Blockly.Block.prototype.getInputTargetBlock = function(name) {
- var input = this.getInput(name);
- return input && input.connection && input.connection.targetBlock();
- };
- Blockly.Block.prototype.getCommentText = function() {
- return this.comment || '';
- };
- Blockly.Block.prototype.setCommentText = function(text) {
- if (this.comment != text) {
- Blockly.Events.fire(new Blockly.Events.Change(
- this, 'comment', null, this.comment, text || ''));
- this.comment = text;
- }
- };
- Blockly.Block.prototype.setWarningText = function(text) {
-
- };
- Blockly.Block.prototype.setMutator = function(mutator) {
-
- };
- Blockly.Block.prototype.getRelativeToSurfaceXY = function() {
- return this.xy_;
- };
- Blockly.Block.prototype.moveBy = function(dx, dy) {
- goog.asserts.assert(!this.parentBlock_, 'Block has parent.');
- var event = new Blockly.Events.Move(this);
- this.xy_.translate(dx, dy);
- event.recordNew();
- Blockly.Events.fire(event);
- };
- Blockly.Block.prototype.makeConnection_ = function(type) {
- return new Blockly.Connection(this, type);
- };
|