2016-04-18 18:03:18 -04:00
|
|
|
var EventEmitter = require('events');
|
|
|
|
var util = require('util');
|
|
|
|
|
2016-04-18 17:20:30 -04:00
|
|
|
/**
|
|
|
|
* A simple runtime for blocks.
|
|
|
|
*/
|
|
|
|
function Runtime () {
|
2016-04-18 18:03:18 -04:00
|
|
|
// Bind event emitter
|
2016-04-26 09:49:52 -04:00
|
|
|
EventEmitter.call(this);
|
2016-04-18 17:20:30 -04:00
|
|
|
|
|
|
|
// State
|
|
|
|
this.blocks = {};
|
|
|
|
this.stacks = [];
|
2016-04-26 09:49:52 -04:00
|
|
|
|
|
|
|
window._BLOCKS = this.blocks;
|
|
|
|
window._STACKS = this.stacks;
|
2016-04-18 17:20:30 -04:00
|
|
|
}
|
|
|
|
|
2016-04-18 18:03:18 -04:00
|
|
|
/**
|
|
|
|
* Inherit from EventEmitter
|
|
|
|
*/
|
|
|
|
util.inherits(Runtime, EventEmitter);
|
|
|
|
|
2016-04-26 09:49:52 -04:00
|
|
|
Runtime.prototype.createBlock = function (block) {
|
2016-04-18 17:20:30 -04:00
|
|
|
// Create new block
|
2016-04-26 09:49:52 -04:00
|
|
|
this.blocks[block.id] = block;
|
|
|
|
|
|
|
|
// Walk each field and add any shadow blocks
|
|
|
|
// @todo Expand this to cover vertical / nested blocks
|
|
|
|
for (var i in block.fields) {
|
|
|
|
var shadows = block.fields[i].blocks;
|
|
|
|
for (var y in shadows) {
|
|
|
|
var shadow = shadows[y];
|
|
|
|
this.blocks[shadow.id] = shadow;
|
|
|
|
};
|
|
|
|
}
|
2016-04-18 17:20:30 -04:00
|
|
|
|
|
|
|
// Push block id to stacks array. New blocks are always a stack even if only
|
|
|
|
// momentary. If the new block is added to an existing stack this stack will
|
|
|
|
// be removed by the `moveBlock` method below.
|
2016-04-26 09:49:52 -04:00
|
|
|
this.stacks.push(block.id);
|
|
|
|
};
|
|
|
|
|
|
|
|
Runtime.prototype.changeBlock = function (args) {
|
|
|
|
// Validate
|
|
|
|
if (args.element !== 'field') return;
|
|
|
|
if (typeof this.blocks[args.id] === 'undefined') return;
|
|
|
|
if (typeof this.blocks[args.id].fields[args.name] === 'undefined') return;
|
|
|
|
|
|
|
|
// Update block value
|
|
|
|
this.blocks[args.id].fields[args.name].value = args.value;
|
2016-04-18 17:20:30 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
Runtime.prototype.moveBlock = function (e) {
|
|
|
|
var _this = this;
|
|
|
|
|
|
|
|
// Block has a new parent
|
|
|
|
if (e.oldParent === undefined && e.newParent !== undefined) {
|
|
|
|
// Remove stack
|
|
|
|
_this._deleteStack(e.id);
|
|
|
|
|
|
|
|
// Update new parent
|
2016-04-26 09:49:52 -04:00
|
|
|
if (e.newField === undefined) {
|
2016-04-18 17:20:30 -04:00
|
|
|
_this.blocks[e.newParent].next = e.id;
|
|
|
|
} else {
|
2016-04-26 09:49:52 -04:00
|
|
|
_this.blocks[e.newParent].fields[e.newField] = {
|
|
|
|
name: e.newField,
|
|
|
|
value: e.id,
|
|
|
|
blocks: {}
|
|
|
|
};
|
2016-04-18 17:20:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Block was removed from parent
|
|
|
|
if (e.newParentId === undefined && e.oldParent !== undefined) {
|
|
|
|
// Add stack
|
|
|
|
_this.stacks.push(e.id);
|
|
|
|
|
|
|
|
// Update old parent
|
2016-04-26 09:49:52 -04:00
|
|
|
if (e.oldField === undefined) {
|
2016-04-18 17:20:30 -04:00
|
|
|
_this.blocks[e.oldParent].next = null;
|
|
|
|
} else {
|
2016-04-26 09:49:52 -04:00
|
|
|
delete _this.blocks[e.oldParent].fields[e.oldField];
|
2016-04-18 17:20:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Runtime.prototype.deleteBlock = function (e) {
|
|
|
|
// @todo Stop threads running on this stack
|
|
|
|
|
2016-04-26 09:49:52 -04:00
|
|
|
// Get block
|
2016-04-18 17:20:30 -04:00
|
|
|
var block = this.blocks[e.id];
|
2016-04-26 09:49:52 -04:00
|
|
|
|
|
|
|
// Delete children
|
2016-04-18 17:20:30 -04:00
|
|
|
if (block.next !== null) {
|
|
|
|
this.deleteBlock({id: block.next});
|
|
|
|
}
|
|
|
|
|
2016-04-26 09:49:52 -04:00
|
|
|
// Delete substacks and fields
|
|
|
|
for (var field in block.fields) {
|
|
|
|
if (field === 'SUBSTACK') {
|
|
|
|
this.deleteBlock({id: block.fields[field].value});
|
|
|
|
} else {
|
|
|
|
for (var shadow in block.fields[field].blocks) {
|
|
|
|
this.deleteBlock({id: shadow});
|
|
|
|
}
|
|
|
|
}
|
2016-04-18 17:20:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete stack
|
|
|
|
this._deleteStack(e.id);
|
|
|
|
|
|
|
|
// Delete block
|
|
|
|
delete this.blocks[e.id];
|
|
|
|
};
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
Runtime.prototype._deleteStack = function (id) {
|
|
|
|
var i = this.stacks.indexOf(id);
|
|
|
|
if (i > -1) this.stacks.splice(i, 1);
|
|
|
|
};
|
|
|
|
|
|
|
|
Runtime.prototype._getNextBlock = function (id) {
|
|
|
|
if (typeof this.blocks[id] === 'undefined') return null;
|
|
|
|
return this.blocks[id].next;
|
|
|
|
};
|
|
|
|
|
|
|
|
Runtime.prototype._getSubstack = function (id) {
|
|
|
|
if (typeof this.blocks[id] === 'undefined') return null;
|
2016-04-26 09:49:52 -04:00
|
|
|
return this.blocks[id].fields['SUBSTACK'];
|
2016-04-18 17:20:30 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = Runtime;
|