/**
* AccessibleBlockly
*
* Copyright 2016 Google Inc.
* https://developers.google.com/blockly/
*
* Licensed under the Apache License, Version 2.0 (the 'License');
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an 'AS IS' BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @fileoverview Angular2 Component that details how Blockly.Block's are
* rendered in the workspace in AccessibleBlockly. Also handles any
* interactions with the blocks.
* @author madeeha@google.com (Madeeha Ghori)
*/
blocklyApp.WorkspaceTreeComponent = ng.core
.Component({
selector: 'blockly-workspace-tree',
template: `
`,
directives: [blocklyApp.FieldComponent, ng.core.forwardRef(function() {
return blocklyApp.WorkspaceTreeComponent;
})],
inputs: ['block', 'level', 'tree', 'isTopLevel'],
pipes: [blocklyApp.TranslatePipe]
})
.Class({
constructor: [
blocklyApp.ClipboardService, blocklyApp.TreeService, blocklyApp.UtilsService,
function(_clipboardService, _treeService, _utilsService) {
this.infoBlocks = Object.create(null);
this.clipboardService = _clipboardService;
this.treeService = _treeService;
this.utilsService = _utilsService;
}],
getElementsNeedingIds_: function() {
var elementsNeedingIds = ['blockSummary', 'listItem', 'label',
'cutListItem', 'cutButton', 'copyListItem', 'copyButton',
'pasteBelow', 'pasteBelowButton', 'pasteAbove', 'pasteAboveButton',
'markBelow', 'markBelowButton', 'markAbove', 'markAboveButton',
'sendToSelectedListItem', 'sendToSelectedButton', 'delete',
'deleteButton'];
for (var i = 0; i < this.block.inputList.length; i++) {
var inputBlock = this.block.inputList[i];
if (inputBlock.connection && !inputBlock.connection.targetBlock()) {
elementsNeedingIds = elementsNeedingIds.concat(
['inputList' + i, 'inputMenuLabel' + i, 'markSpot' + i,
'markSpotButton' + i, 'paste' + i, 'pasteButton' + i]);
}
}
return elementsNeedingIds;
},
ngOnInit: function() {
var elementsNeedingIds = this.getElementsNeedingIds_();
this.idMap = {}
this.idMap['parentList'] = this.utilsService.generateUniqueId();
for (var i = 0; i < elementsNeedingIds.length; i++) {
this.idMap[elementsNeedingIds[i]] =
this.block.id + elementsNeedingIds[i];
}
},
ngAfterViewInit: function() {
// If this is a top-level tree in the workspace, set its id and active
// descendant.
if (this.tree && this.isTopLevel && !this.tree.id) {
this.tree.id = this.utilsService.generateUniqueId();
}
if (this.tree && this.isTopLevel &&
!this.treeService.getActiveDescId(this.tree.id)) {
this.treeService.setActiveDesc(this.idMap['parentList'], this.tree.id);
}
},
hasPreviousConnection: function(block) {
return Boolean(block.previousConnection);
},
hasNextConnection: function(block) {
return Boolean(block.nextConnection);
},
isCompatibleWithClipboard: function(connection) {
return this.clipboardService.isClipboardCompatibleWithConnection(
connection);
},
isTopLevelBlock: function(block) {
return blocklyApp.workspace.topBlocks_.some(function(topBlock) {
return topBlock.id == block.id;
});
},
pasteAbove: function(block) {
var that = this;
this.treeService.runWhilePreservingFocus(function() {
that.clipboardService.pasteFromClipboard(block.previousConnection);
}, this.tree.id);
},
pasteBelow: function(block) {
var that = this;
this.treeService.runWhilePreservingFocus(function() {
that.clipboardService.pasteFromClipboard(block.nextConnection);
}, this.tree.id);
},
cutToClipboard: function(block) {
if (this.isTopLevelBlock(block)) {
nextNodeToFocusOn = this.treeService.getNodeToFocusOnWhenTreeIsDeleted(
this.tree.id);
this.clipboardService.cut(block);
nextNodeToFocusOn.focus();
} else {
// TODO(sll): Change the active descendant of the tree.
this.clipboardService.cut(block);
}
},
deleteBlock: function(block) {
if (this.isTopLevelBlock(block)) {
nextNodeToFocusOn = this.treeService.getNodeToFocusOnWhenTreeIsDeleted(
this.tree.id);
block.dispose(true);
nextNodeToFocusOn.focus();
} else {
// TODO(sll): Change the active descendant of the tree.
block.dispose(true);
}
},
generateAriaLabelledByAttr: function(mainLabel, secondLabel, isDisabled) {
return this.utilsService.generateAriaLabelledByAttr(
mainLabel, secondLabel, isDisabled);
},
sendToMarkedSpot: function(block) {
this.clipboardService.pasteToMarkedConnection(block, false);
if (this.isTopLevelBlock(block)) {
nextNodeToFocusOn = this.treeService.getNodeToFocusOnWhenTreeIsDeleted(
this.tree.id);
block.dispose(true);
nextNodeToFocusOn.focus();
} else {
// TODO(sll): Change the active descendant of the tree.
block.dispose(true);
}
alert('Block moved to marked spot: ' + block.toString());
}
});