|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
goog.provide('Blockly.Events.BlockBase');
|
|
goog.provide('Blockly.Events.BlockChange');
|
|
goog.provide('Blockly.Events.BlockCreate');
|
|
goog.provide('Blockly.Events.BlockDelete');
|
|
goog.provide('Blockly.Events.BlockMove');
|
|
goog.provide('Blockly.Events.Change');
|
|
goog.provide('Blockly.Events.Create');
|
|
goog.provide('Blockly.Events.Delete');
|
|
goog.provide('Blockly.Events.Move');
|
|
|
|
goog.require('Blockly.Events');
|
|
goog.require('Blockly.Events.Abstract');
|
|
|
|
goog.require('goog.array');
|
|
goog.require('goog.math.Coordinate');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Events.BlockBase = function(block) {
|
|
Blockly.Events.BlockBase.superClass_.constructor.call(this);
|
|
|
|
|
|
|
|
|
|
|
|
this.blockId = block.id;
|
|
this.workspaceId = block.workspace.id;
|
|
};
|
|
goog.inherits(Blockly.Events.BlockBase, Blockly.Events.Abstract);
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Events.BlockBase.prototype.toJson = function() {
|
|
var json = Blockly.Events.BlockBase.superClass_.toJson.call(this);
|
|
json['blockId'] = this.blockId;
|
|
return json;
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Events.BlockBase.prototype.fromJson = function(json) {
|
|
Blockly.Events.BlockBase.superClass_.toJson.call(this);
|
|
this.blockId = json['blockId'];
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Events.Change = function(block, element, name, oldValue, newValue) {
|
|
if (!block) {
|
|
return;
|
|
}
|
|
Blockly.Events.Change.superClass_.constructor.call(this, block);
|
|
this.element = element;
|
|
this.name = name;
|
|
this.oldValue = oldValue;
|
|
this.newValue = newValue;
|
|
};
|
|
goog.inherits(Blockly.Events.Change, Blockly.Events.BlockBase);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Events.BlockChange = Blockly.Events.Change;
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Events.Change.prototype.type = Blockly.Events.CHANGE;
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Events.Change.prototype.toJson = function() {
|
|
var json = Blockly.Events.Change.superClass_.toJson.call(this);
|
|
json['element'] = this.element;
|
|
if (this.name) {
|
|
json['name'] = this.name;
|
|
}
|
|
json['newValue'] = this.newValue;
|
|
return json;
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Events.Change.prototype.fromJson = function(json) {
|
|
Blockly.Events.Change.superClass_.fromJson.call(this, json);
|
|
this.element = json['element'];
|
|
this.name = json['name'];
|
|
this.newValue = json['newValue'];
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Events.Change.prototype.isNull = function() {
|
|
return this.oldValue == this.newValue;
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Events.Change.prototype.run = function(forward) {
|
|
var workspace = this.getEventWorkspace_();
|
|
var block = workspace.getBlockById(this.blockId);
|
|
if (!block) {
|
|
console.warn("Can't change non-existent block: " + this.blockId);
|
|
return;
|
|
}
|
|
if (block.mutator) {
|
|
|
|
block.mutator.setVisible(false);
|
|
}
|
|
var value = forward ? this.newValue : this.oldValue;
|
|
switch (this.element) {
|
|
case 'field':
|
|
var field = block.getField(this.name);
|
|
if (field) {
|
|
|
|
|
|
field.callValidator(value);
|
|
field.setValue(value);
|
|
} else {
|
|
console.warn("Can't set non-existent field: " + this.name);
|
|
}
|
|
break;
|
|
case 'comment':
|
|
block.setCommentText(value || null);
|
|
break;
|
|
case 'collapsed':
|
|
block.setCollapsed(value);
|
|
break;
|
|
case 'disabled':
|
|
block.setDisabled(value);
|
|
break;
|
|
case 'inline':
|
|
block.setInputsInline(value);
|
|
break;
|
|
case 'mutation':
|
|
var oldMutation = '';
|
|
if (block.mutationToDom) {
|
|
var oldMutationDom = block.mutationToDom();
|
|
oldMutation = oldMutationDom && Blockly.Xml.domToText(oldMutationDom);
|
|
}
|
|
if (block.domToMutation) {
|
|
value = value || '<mutation></mutation>';
|
|
var dom = Blockly.Xml.textToDom('<xml>' + value + '</xml>');
|
|
block.domToMutation(dom.firstChild);
|
|
}
|
|
Blockly.Events.fire(new Blockly.Events.Change(
|
|
block, 'mutation', null, oldMutation, value));
|
|
break;
|
|
default:
|
|
console.warn('Unknown change type: ' + this.element);
|
|
}
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Events.Create = function(block) {
|
|
if (!block) {
|
|
return;
|
|
}
|
|
Blockly.Events.Create.superClass_.constructor.call(this, block);
|
|
|
|
if (block.workspace.rendered) {
|
|
this.xml = Blockly.Xml.blockToDomWithXY(block);
|
|
} else {
|
|
this.xml = Blockly.Xml.blockToDom(block);
|
|
}
|
|
this.ids = Blockly.Events.getDescendantIds_(block);
|
|
};
|
|
goog.inherits(Blockly.Events.Create, Blockly.Events.BlockBase);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Events.BlockCreate = Blockly.Events.Create;
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Events.Create.prototype.type = Blockly.Events.CREATE;
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Events.Create.prototype.toJson = function() {
|
|
var json = Blockly.Events.Create.superClass_.toJson.call(this);
|
|
json['xml'] = Blockly.Xml.domToText(this.xml);
|
|
json['ids'] = this.ids;
|
|
return json;
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Events.Create.prototype.fromJson = function(json) {
|
|
Blockly.Events.Create.superClass_.fromJson.call(this, json);
|
|
this.xml = Blockly.Xml.textToDom('<xml>' + json['xml'] + '</xml>').firstChild;
|
|
this.ids = json['ids'];
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Events.Create.prototype.run = function(forward) {
|
|
var workspace = this.getEventWorkspace_();
|
|
if (forward) {
|
|
var xml = goog.dom.createDom('xml');
|
|
xml.appendChild(this.xml);
|
|
Blockly.Xml.domToWorkspace(xml, workspace);
|
|
} else {
|
|
for (var i = 0, id; id = this.ids[i]; i++) {
|
|
var block = workspace.getBlockById(id);
|
|
if (block) {
|
|
block.dispose(false, false);
|
|
} else if (id == this.blockId) {
|
|
|
|
console.warn("Can't uncreate non-existent block: " + id);
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Events.Delete = function(block) {
|
|
if (!block) {
|
|
return;
|
|
}
|
|
if (block.getParent()) {
|
|
throw 'Connected blocks cannot be deleted.';
|
|
}
|
|
Blockly.Events.Delete.superClass_.constructor.call(this, block);
|
|
|
|
if (block.workspace.rendered) {
|
|
this.oldXml = Blockly.Xml.blockToDomWithXY(block);
|
|
} else {
|
|
this.oldXml = Blockly.Xml.blockToDom(block);
|
|
}
|
|
this.ids = Blockly.Events.getDescendantIds_(block);
|
|
};
|
|
goog.inherits(Blockly.Events.Delete, Blockly.Events.BlockBase);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Events.BlockDelete = Blockly.Events.Delete;
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Events.Delete.prototype.type = Blockly.Events.DELETE;
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Events.Delete.prototype.toJson = function() {
|
|
var json = Blockly.Events.Delete.superClass_.toJson.call(this);
|
|
json['ids'] = this.ids;
|
|
return json;
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Events.Delete.prototype.fromJson = function(json) {
|
|
Blockly.Events.Delete.superClass_.fromJson.call(this, json);
|
|
this.ids = json['ids'];
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Events.Delete.prototype.run = function(forward) {
|
|
var workspace = this.getEventWorkspace_();
|
|
if (forward) {
|
|
for (var i = 0, id; id = this.ids[i]; i++) {
|
|
var block = workspace.getBlockById(id);
|
|
if (block) {
|
|
block.dispose(false, false);
|
|
} else if (id == this.blockId) {
|
|
|
|
console.warn("Can't delete non-existent block: " + id);
|
|
}
|
|
}
|
|
} else {
|
|
var xml = goog.dom.createDom('xml');
|
|
xml.appendChild(this.oldXml);
|
|
Blockly.Xml.domToWorkspace(xml, workspace);
|
|
}
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Events.Move = function(block) {
|
|
if (!block) {
|
|
return;
|
|
}
|
|
Blockly.Events.Move.superClass_.constructor.call(this, block);
|
|
var location = this.currentLocation_();
|
|
this.oldParentId = location.parentId;
|
|
this.oldInputName = location.inputName;
|
|
this.oldCoordinate = location.coordinate;
|
|
};
|
|
goog.inherits(Blockly.Events.Move, Blockly.Events.BlockBase);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Events.BlockMove = Blockly.Events.Move;
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Events.Move.prototype.type = Blockly.Events.MOVE;
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Events.Move.prototype.toJson = function() {
|
|
var json = Blockly.Events.Move.superClass_.toJson.call(this);
|
|
if (this.newParentId) {
|
|
json['newParentId'] = this.newParentId;
|
|
}
|
|
if (this.newInputName) {
|
|
json['newInputName'] = this.newInputName;
|
|
}
|
|
if (this.newCoordinate) {
|
|
json['newCoordinate'] = Math.round(this.newCoordinate.x) + ',' +
|
|
Math.round(this.newCoordinate.y);
|
|
}
|
|
return json;
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Events.Move.prototype.fromJson = function(json) {
|
|
Blockly.Events.Move.superClass_.fromJson.call(this, json);
|
|
this.newParentId = json['newParentId'];
|
|
this.newInputName = json['newInputName'];
|
|
if (json['newCoordinate']) {
|
|
var xy = json['newCoordinate'].split(',');
|
|
this.newCoordinate =
|
|
new goog.math.Coordinate(parseFloat(xy[0]), parseFloat(xy[1]));
|
|
}
|
|
};
|
|
|
|
|
|
|
|
|
|
Blockly.Events.Move.prototype.recordNew = function() {
|
|
var location = this.currentLocation_();
|
|
this.newParentId = location.parentId;
|
|
this.newInputName = location.inputName;
|
|
this.newCoordinate = location.coordinate;
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Events.Move.prototype.currentLocation_ = function() {
|
|
var workspace = Blockly.Workspace.getById(this.workspaceId);
|
|
var block = workspace.getBlockById(this.blockId);
|
|
var location = {};
|
|
var parent = block.getParent();
|
|
if (parent) {
|
|
location.parentId = parent.id;
|
|
var input = parent.getInputWithBlock(block);
|
|
if (input) {
|
|
location.inputName = input.name;
|
|
}
|
|
} else {
|
|
var blockXY = block.getRelativeToSurfaceXY();
|
|
|
|
|
|
var rtlAwareX = workspace.RTL ? workspace.getWidth() - blockXY.x : blockXY.x;
|
|
location.coordinate = new goog.math.Coordinate(rtlAwareX, blockXY.y);
|
|
}
|
|
return location;
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Events.Move.prototype.isNull = function() {
|
|
return this.oldParentId == this.newParentId &&
|
|
this.oldInputName == this.newInputName &&
|
|
goog.math.Coordinate.equals(this.oldCoordinate, this.newCoordinate);
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Events.Move.prototype.run = function(forward) {
|
|
var workspace = this.getEventWorkspace_();
|
|
var block = workspace.getBlockById(this.blockId);
|
|
if (!block) {
|
|
console.warn("Can't move non-existent block: " + this.blockId);
|
|
return;
|
|
}
|
|
var parentId = forward ? this.newParentId : this.oldParentId;
|
|
var inputName = forward ? this.newInputName : this.oldInputName;
|
|
var coordinate = forward ? this.newCoordinate : this.oldCoordinate;
|
|
var parentBlock = null;
|
|
if (parentId) {
|
|
parentBlock = workspace.getBlockById(parentId);
|
|
if (!parentBlock) {
|
|
console.warn("Can't connect to non-existent block: " + parentId);
|
|
return;
|
|
}
|
|
}
|
|
if (block.getParent()) {
|
|
block.unplug();
|
|
}
|
|
if (coordinate) {
|
|
var xy = block.getRelativeToSurfaceXY();
|
|
var rtlAwareX = workspace.RTL ? workspace.getWidth() - coordinate.x : coordinate.x;
|
|
block.moveBy(rtlAwareX - xy.x, coordinate.y - xy.y);
|
|
} else {
|
|
var blockConnection = block.outputConnection || block.previousConnection;
|
|
var parentConnection;
|
|
if (inputName) {
|
|
var input = parentBlock.getInput(inputName);
|
|
if (input) {
|
|
parentConnection = input.connection;
|
|
}
|
|
} else if (blockConnection.type == Blockly.PREVIOUS_STATEMENT) {
|
|
parentConnection = parentBlock.nextConnection;
|
|
}
|
|
if (parentConnection) {
|
|
blockConnection.connect(parentConnection);
|
|
} else {
|
|
console.warn("Can't connect to non-existent input: " + inputName);
|
|
}
|
|
}
|
|
};
|
|
|