toolbox.component.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /**
  2. * AccessibleBlockly
  3. *
  4. * Copyright 2016 Google Inc.
  5. * https://developers.google.com/blockly/
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the 'License');
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an 'AS IS' BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. /**
  20. * @fileoverview Angular2 Component that details how a toolbox is rendered
  21. * in AccessibleBlockly. Also handles any interactions with the toolbox.
  22. * @author madeeha@google.com (Madeeha Ghori)
  23. */
  24. blocklyApp.ToolboxComponent = ng.core
  25. .Component({
  26. selector: 'blockly-toolbox',
  27. template: `
  28. <h3 #toolboxTitle id="blockly-toolbox-title">Toolbox</h3>
  29. <ol #tree
  30. id="blockly-toolbox-tree" role="group" class="blocklyTree"
  31. *ngIf="toolboxCategories && toolboxCategories.length > 0" tabIndex="0"
  32. [attr.aria-labelledby]="toolboxTitle.id"
  33. [attr.aria-activedescendant]="tree.getAttribute('aria-activedescendant') || tree.id + '-node0' "
  34. (keydown)="treeService.onKeypress($event, tree)">
  35. <template [ngIf]="xmlHasCategories">
  36. <li #parent
  37. [id]="idMap['Parent' + i]" role="treeitem"
  38. [ngClass]="{blocklyHasChildren: true, blocklyActiveDescendant: tree.getAttribute('aria-activedescendant') == idMap['Parent' + i]}"
  39. *ngFor="#category of toolboxCategories; #i=index"
  40. aria-level="1" aria-selected=false
  41. [attr.aria-label]="category.attributes.name.value">
  42. <div *ngIf="category && category.attributes">
  43. <label [id]="idMap['Label' + i]" #name>
  44. {{category.attributes.name.value}}
  45. </label>
  46. <ol role="group" *ngIf="getToolboxWorkspace(category).topBlocks_.length > 0">
  47. <blockly-toolbox-tree *ngFor="#block of getToolboxWorkspace(category).topBlocks_"
  48. [level]=2 [block]="block"
  49. [displayBlockMenu]="true"
  50. [tree]="tree">
  51. </blockly-toolbox-tree>
  52. </ol>
  53. </div>
  54. </li>
  55. </template>
  56. <div *ngIf="!xmlHasCategories">
  57. <blockly-toolbox-tree *ngFor="#block of getToolboxWorkspace(toolboxCategories[0]).topBlocks_; #i=index"
  58. [level]=1 [block]="block"
  59. [displayBlockMenu]="true"
  60. [index]="i" [tree]="tree"
  61. [noCategories]="true"
  62. [isTopLevel]="true">
  63. </blockly-toolbox-tree>
  64. </div>
  65. </ol>
  66. `,
  67. directives: [blocklyApp.ToolboxTreeComponent]
  68. })
  69. .Class({
  70. constructor: [
  71. blocklyApp.TreeService, blocklyApp.UtilsService,
  72. function(_treeService, _utilsService) {
  73. this.toolboxCategories = [];
  74. this.toolboxWorkspaces = Object.create(null);
  75. this.treeService = _treeService;
  76. this.utilsService = _utilsService;
  77. this.xmlHasCategories = false;
  78. }],
  79. ngOnInit: function() {
  80. // Note that sometimes the toolbox may not have categories; it may
  81. // display individual blocks directly (which is often the case in,
  82. // e.g., Blockly Games).
  83. var xmlToolboxElt = document.getElementById('blockly-toolbox-xml');
  84. var xmlCategoryElts = xmlToolboxElt.getElementsByTagName('category');
  85. if (xmlCategoryElts.length) {
  86. this.xmlHasCategories = true;
  87. this.toolboxCategories = Array.from(xmlCategoryElts);
  88. var elementsNeedingIds = [];
  89. for (var i = 0; i < this.toolboxCategories.length; i++) {
  90. elementsNeedingIds.push('Parent' + i, 'Label' + i);
  91. }
  92. this.idMap = this.utilsService.generateIds(elementsNeedingIds);
  93. for (var i = 0; i < this.toolboxCategories.length; i++) {
  94. this.idMap['Parent' + i] = 'blockly-toolbox-tree-node' + i;
  95. }
  96. } else {
  97. // Create a single category with all the top-level blocks.
  98. this.xmlHasCategories = false;
  99. this.toolboxCategories = [Array.from(xmlToolboxElt.children)];
  100. }
  101. },
  102. ngAfterViewInit: function() {
  103. // If this is a top-level tree in the toolbox, set its active
  104. // descendant after the ids have been computed.
  105. if (this.xmlHasCategories) {
  106. this.treeService.setActiveDesc(
  107. 'blockly-toolbox-tree-node0', 'blockly-toolbox-tree');
  108. }
  109. },
  110. getToolboxWorkspace: function(categoryNode) {
  111. if (categoryNode.attributes && categoryNode.attributes.name) {
  112. var categoryName = categoryNode.attributes.name.value;
  113. } else {
  114. var categoryName = 'no-category';
  115. }
  116. if (this.toolboxWorkspaces[categoryName]) {
  117. return this.toolboxWorkspaces[categoryName];
  118. } else {
  119. var categoryWorkspace = new Blockly.Workspace();
  120. if (categoryName == 'no-category') {
  121. for (var i = 0; i < categoryNode.length; i++) {
  122. Blockly.Xml.domToBlock(categoryWorkspace, categoryNode[i]);
  123. }
  124. } else {
  125. Blockly.Xml.domToWorkspace(categoryNode, categoryWorkspace);
  126. }
  127. this.toolboxWorkspaces[categoryName] = categoryWorkspace;
  128. return this.toolboxWorkspaces[categoryName];
  129. }
  130. }
  131. });