Move blockListener to blocks.js; combine flyout listener

This commit is contained in:
Tim Mickel 2016-06-08 13:44:09 -04:00
parent 8081ec3a16
commit 7a42e9ae68
3 changed files with 75 additions and 77 deletions
src/engine

View file

@ -1,3 +1,5 @@
var adapter = require('./adapter');
/**
* @fileoverview
* Store and mutate the VM block representation,
@ -80,6 +82,70 @@ Blocks.prototype.getOpcode = function (id) {
// ---------------------------------------------------------------------
/**
* Create event listener for blocks. Handles validation and serves as a generic
* adapter between the blocks and the runtime interface.
* @param {boolean} isFlyout If true, create a listener for flyout events.
* @param {?Runtime} opt_runtime Optional runtime to forward click events to.
* @return {Function} A generated listener to attach to Blockly instance.
*/
Blocks.prototype.generateBlockListener = function (isFlyout, opt_runtime) {
/**
* The actual generated block listener.
* @param {Object} Blockly "block" event
*/
var instance = this;
return function (e) {
// Validate event
if (typeof e !== 'object') return;
if (typeof e.blockId !== 'string') return;
// UI event: clicked stacks toggle in the runtime.
if (e.element === 'stackclick') {
if (opt_runtime) {
opt_runtime.toggleStack(e.blockId);
}
return;
}
// Block create/update/destroy
switch (e.type) {
case 'create':
var newBlocks = adapter(e);
// A create event can create many blocks. Add them all.
for (var i = 0; i < newBlocks.length; i++) {
instance.createBlock(newBlocks[i], isFlyout);
}
break;
case 'change':
instance.changeBlock({
id: e.blockId,
element: e.element,
name: e.name,
value: e.newValue
});
break;
case 'move':
instance.moveBlock({
id: e.blockId,
oldParent: e.oldParentId,
oldInput: e.oldInputName,
newParent: e.newParentId,
newInput: e.newInputName
});
break;
case 'delete':
instance.deleteBlock({
id: e.blockId
});
break;
}
};
};
// ---------------------------------------------------------------------
/**
* Block management: create blocks and stacks from a `create` event
* @param {!Object} block Blockly create event to be processed