Add Create, Delete, and Change events.

Not counting change for mutators.
This commit is contained in:
Neil Fraser
2016-01-20 19:11:03 -08:00
parent 36fe3994c9
commit c429949a4e
17 changed files with 209 additions and 78 deletions

View File

@@ -29,16 +29,39 @@ goog.provide('Blockly.Events');
/**
* Allow change events to be created and fired.
* @type {boolean}
* @type {number}
* @private
*/
Blockly.Events.enabled = true;
Blockly.Events.disabled_ = 0;
/**
* Name of event that creates a block.
* @const
*/
Blockly.Events.CREATE = 'create';
/**
* Name of event that deletes a block.
* @const
*/
Blockly.Events.DELETE = 'delete';
/**
* Name of event that changes a block.
* @const
*/
Blockly.Events.CHANGE = 'change';
/**
* Create a custom event and fire it.
* @param {Object} detail Custom data for event.
*/
Blockly.Events.fire = function(detail) {
var workspace = Blockly.Workspace.getById(detail.workspace);
if (!Blockly.Events.isEnabled()) {
return; // No events allowed.
}
console.log(detail);
var workspace = Blockly.Workspace.getById(detail.workspaceId);
if (workspace.rendered) {
// Create a custom event in a browser-compatible way.
if (typeof CustomEvent == 'function') {
@@ -52,3 +75,91 @@ Blockly.Events.fire = function(detail) {
workspace.getCanvas().dispatchEvent(evt);
}
};
/**
* Stop sending events. Every call to this function MUST also call enable.
*/
Blockly.Events.disable = function() {
Blockly.Events.disabled_++;
};
/**
* Start sending events. Unless events were already disabled when the
* corresponding call to disable was made.
*/
Blockly.Events.enable = function() {
Blockly.Events.disabled_--;
};
/**
* Returns whether events may be fired or not.
* @return {boolean} True if enabled.
*/
Blockly.Events.isEnabled = function() {
return Blockly.Events.disabled_ == 0;
};
/**
* Abstract class for a change event.
* @constructor
*/
Blockly.Events.Abstract = function() {};
/**
* Class for a block creation event.
* @param {!Blockly.Workspace} workspace The workspace.
* @param {!Element} xml XML DOM.
* @extends {Blockly.Events.Abstract}
* @constructor
*/
Blockly.Events.Create = function(workspace, xml) {
this.type = Blockly.Events.CREATE;
this.workspaceId = workspace.id;
this.xml = xml;
};
goog.inherits(Blockly.Events.Create, Blockly.Events.Abstract);
/**
* Class for a block deletion event.
* @param {!Blockly.Block} block The deleted block.
* @extends {Blockly.Events.Abstract}
* @constructor
*/
Blockly.Events.Delete = function(block) {
this.type = Blockly.Events.DELETE;
this.workspaceId = block.workspace.id;
this.blockId = block.id;
this.oldXml = Blockly.Xml.blockToDom(block);
var parent = block.getParent();
if (parent) {
this.oldParentId = parent.id;
for (var i = 0, input; input = parent.inputList[i]; i++) {
if (input.connection && input.connection.targetBlock() == block) {
this.oldInput = input.name;
break;
}
}
}
};
goog.inherits(Blockly.Events.Delete, Blockly.Events.Abstract);
/**
* Class for a block change event.
* @param {!Blockly.Block} block The deleted block.
* @param {string} element One of 'field', 'comment', 'disabled', etc.
* @param {?string} name Name of input or field affected, or null.
* @param {string} oldValue Previous value of element.
* @param {string} newValue New value of element.
* @extends {Blockly.Events.Abstract}
* @constructor
*/
Blockly.Events.Change = function(block, element, name, oldValue, newValue) {
this.type = Blockly.Events.CHANGE;
this.workspaceId = block.workspace.id;
this.blockId = block.id;
this.element = element;
this.name = name;
this.oldValue = oldValue;
this.newValue = newValue;
};
goog.inherits(Blockly.Events.Create, Blockly.Events.Abstract);