|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
|
|
goog.provide('Blockly.Events');
|
|
|
|
goog.require('goog.array');
|
|
goog.require('goog.math.Coordinate');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Events.group_ = '';
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Events.recordUndo = true;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Events.disabled_ = 0;
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Events.CREATE = 'create';
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Events.BLOCK_CREATE = Blockly.Events.CREATE;
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Events.DELETE = 'delete';
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Events.BLOCK_DELETE = Blockly.Events.DELETE;
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Events.CHANGE = 'change';
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Events.BLOCK_CHANGE = Blockly.Events.CHANGE;
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Events.MOVE = 'move';
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Events.DRAG_OUTSIDE = 'dragOutside';
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Events.END_DRAG = 'endDrag';
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Events.BLOCK_MOVE = Blockly.Events.MOVE;
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Events.VAR_CREATE = 'var_create';
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Events.VAR_DELETE = 'var_delete';
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Events.VAR_RENAME = 'var_rename';
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Events.COMMENT_CREATE = 'comment_create';
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Events.COMMENT_MOVE = 'comment_move';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Events.COMMENT_CHANGE = 'comment_change';
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Events.COMMENT_DELETE = 'comment_delete';
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Events.UI = 'ui';
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Events.FIRE_QUEUE_ = [];
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Events.fire = function(event) {
|
|
if (!Blockly.Events.isEnabled()) {
|
|
return;
|
|
}
|
|
if (!Blockly.Events.FIRE_QUEUE_.length) {
|
|
|
|
setTimeout(Blockly.Events.fireNow_, 0);
|
|
}
|
|
Blockly.Events.FIRE_QUEUE_.push(event);
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Events.fireNow_ = function() {
|
|
var queue = Blockly.Events.filter(Blockly.Events.FIRE_QUEUE_, true);
|
|
Blockly.Events.FIRE_QUEUE_.length = 0;
|
|
for (var i = 0, event; event = queue[i]; i++) {
|
|
var workspace = Blockly.Workspace.getById(event.workspaceId);
|
|
if (workspace) {
|
|
workspace.fireChangeListener(event);
|
|
}
|
|
}
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Events.filter = function(queueIn, forward) {
|
|
var queue = goog.array.clone(queueIn);
|
|
if (!forward) {
|
|
|
|
queue.reverse();
|
|
}
|
|
var mergedQueue = [];
|
|
var hash = Object.create(null);
|
|
|
|
for (var i = 0, event; event = queue[i]; i++) {
|
|
if (!event.isNull()) {
|
|
var key = [event.type, event.blockId, event.workspaceId].join(' ');
|
|
|
|
var lastEntry = hash[key];
|
|
var lastEvent = lastEntry ? lastEntry.event : null;
|
|
if (!lastEntry) {
|
|
|
|
|
|
|
|
hash[key] = {event: event, index: i};
|
|
mergedQueue.push(event);
|
|
} else if (event.type == Blockly.Events.MOVE &&
|
|
lastEntry.index == i - 1) {
|
|
|
|
lastEvent.newParentId = event.newParentId;
|
|
lastEvent.newInputName = event.newInputName;
|
|
lastEvent.newCoordinate = event.newCoordinate;
|
|
lastEntry.index = i;
|
|
} else if (event.type == Blockly.Events.CHANGE &&
|
|
event.element == lastEvent.element &&
|
|
event.name == lastEvent.name) {
|
|
|
|
lastEvent.newValue = event.newValue;
|
|
} else if (event.type == Blockly.Events.UI &&
|
|
event.element == 'click' &&
|
|
(lastEvent.element == 'commentOpen' ||
|
|
lastEvent.element == 'mutatorOpen' ||
|
|
lastEvent.element == 'warningOpen')) {
|
|
|
|
lastEvent.newValue = event.newValue;
|
|
} else {
|
|
|
|
hash[key] = {event: event, index: 1};
|
|
mergedQueue.push(event);
|
|
}
|
|
}
|
|
}
|
|
|
|
queue = mergedQueue.filter(function(e) { return !e.isNull(); });
|
|
if (!forward) {
|
|
|
|
queue.reverse();
|
|
}
|
|
|
|
|
|
for (var i = 1, event; event = queue[i]; i++) {
|
|
if (event.type == Blockly.Events.CHANGE &&
|
|
event.element == 'mutation') {
|
|
queue.unshift(queue.splice(i, 1)[0]);
|
|
}
|
|
}
|
|
return queue;
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Events.clearPendingUndo = function() {
|
|
for (var i = 0, event; event = Blockly.Events.FIRE_QUEUE_[i]; i++) {
|
|
event.recordUndo = false;
|
|
}
|
|
};
|
|
|
|
|
|
|
|
|
|
Blockly.Events.disable = function() {
|
|
Blockly.Events.disabled_++;
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Events.enable = function() {
|
|
Blockly.Events.disabled_--;
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Events.isEnabled = function() {
|
|
return Blockly.Events.disabled_ == 0;
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Events.getGroup = function() {
|
|
return Blockly.Events.group_;
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Events.setGroup = function(state) {
|
|
if (typeof state == 'boolean') {
|
|
Blockly.Events.group_ = state ? Blockly.utils.genUid() : '';
|
|
} else {
|
|
Blockly.Events.group_ = state;
|
|
}
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Events.getDescendantIds_ = function(block) {
|
|
var ids = [];
|
|
var descendants = block.getDescendants(false);
|
|
for (var i = 0, descendant; descendant = descendants[i]; i++) {
|
|
ids[i] = descendant.id;
|
|
}
|
|
return ids;
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Events.fromJson = function(json, workspace) {
|
|
var event;
|
|
switch (json.type) {
|
|
case Blockly.Events.CREATE:
|
|
event = new Blockly.Events.Create(null);
|
|
break;
|
|
case Blockly.Events.DELETE:
|
|
event = new Blockly.Events.Delete(null);
|
|
break;
|
|
case Blockly.Events.CHANGE:
|
|
event = new Blockly.Events.Change(null);
|
|
break;
|
|
case Blockly.Events.MOVE:
|
|
event = new Blockly.Events.Move(null);
|
|
break;
|
|
case Blockly.Events.VAR_CREATE:
|
|
event = new Blockly.Events.VarCreate(null);
|
|
break;
|
|
case Blockly.Events.VAR_DELETE:
|
|
event = new Blockly.Events.VarDelete(null);
|
|
break;
|
|
case Blockly.Events.VAR_RENAME:
|
|
event = new Blockly.Events.VarRename(null);
|
|
break;
|
|
case Blockly.Events.COMMENT_CREATE:
|
|
event = new Blockly.Events.CommentCreate(null);
|
|
break;
|
|
case Blockly.Events.COMMENT_CHANGE:
|
|
event = new Blockly.Events.CommentChange(null);
|
|
break;
|
|
case Blockly.Events.COMMENT_MOVE:
|
|
event = new Blockly.Events.CommentMove(null);
|
|
break;
|
|
case Blockly.Events.COMMENT_DELETE:
|
|
event = new Blockly.Events.CommentDelete(null);
|
|
break;
|
|
case Blockly.Events.UI:
|
|
event = new Blockly.Events.Ui(null);
|
|
break;
|
|
case Blockly.Events.DRAG_OUTSIDE:
|
|
event = new Blockly.Events.DragBlockOutside(null);
|
|
break;
|
|
case Blockly.Events.END_DRAG:
|
|
event = new Blockly.Events.EndBlockDrag(null, false);
|
|
break;
|
|
default:
|
|
throw 'Unknown event type.';
|
|
}
|
|
event.fromJson(json);
|
|
event.workspaceId = workspace.id;
|
|
return event;
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Events.disableOrphans = function(event) {
|
|
if (event.type == Blockly.Events.MOVE ||
|
|
event.type == Blockly.Events.CREATE) {
|
|
Blockly.Events.disable();
|
|
var workspace = Blockly.Workspace.getById(event.workspaceId);
|
|
var block = workspace.getBlockById(event.blockId);
|
|
if (block) {
|
|
if (block.getParent() && !block.getParent().disabled) {
|
|
var children = block.getDescendants(false);
|
|
for (var i = 0, child; child = children[i]; i++) {
|
|
child.setDisabled(false);
|
|
}
|
|
} else if ((block.outputConnection || block.previousConnection) &&
|
|
!workspace.isDragging()) {
|
|
do {
|
|
block.setDisabled(true);
|
|
block = block.getNextBlock();
|
|
} while (block);
|
|
}
|
|
}
|
|
Blockly.Events.enable();
|
|
}
|
|
};
|
|
|