toolbox.component.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. <div class="blocklyToolboxColumn">
  29. <h3 #toolboxTitle id="blockly-toolbox-title">{{'TOOLBOX'|translate}}</h3>
  30. <ol #tree
  31. id="blockly-toolbox-tree" role="tree" class="blocklyTree"
  32. *ngIf="toolboxCategories && toolboxCategories.length > 0" tabindex="0"
  33. [attr.aria-labelledby]="toolboxTitle.id"
  34. [attr.aria-activedescendant]="getActiveDescId()"
  35. (keydown)="treeService.onKeypress($event, tree)">
  36. <template [ngIf]="xmlHasCategories">
  37. <li #parent
  38. [id]="idMap['Parent' + i]" role="treeitem"
  39. [ngClass]="{blocklyHasChildren: true, blocklyActiveDescendant: tree.getAttribute('aria-activedescendant') == idMap['Parent' + i]}"
  40. *ngFor="#category of toolboxCategories; #i=index"
  41. aria-level="0"
  42. [attr.aria-label]="getCategoryAriaLabel(category)">
  43. <div *ngIf="category && category.attributes">
  44. <label [id]="idMap['Label' + i]" #name>
  45. {{category.attributes.name.value}}
  46. </label>
  47. <ol role="group" *ngIf="getToolboxWorkspace(category).topBlocks_.length > 0">
  48. <blockly-toolbox-tree *ngFor="#block of getToolboxWorkspace(category).topBlocks_"
  49. [level]="1" [block]="block"
  50. [displayBlockMenu]="true"
  51. [tree]="tree">
  52. </blockly-toolbox-tree>
  53. </ol>
  54. </div>
  55. </li>
  56. </template>
  57. <div *ngIf="!xmlHasCategories">
  58. <blockly-toolbox-tree *ngFor="#block of getToolboxWorkspace(toolboxCategories[0]).topBlocks_; #i=index"
  59. role="treeitem" [level]="0" [block]="block"
  60. [tree]="tree" [displayBlockMenu]="true"
  61. [isFirstToolboxTree]="i === 0">
  62. </blockly-toolbox-tree>
  63. </div>
  64. </ol>
  65. </div>
  66. `,
  67. directives: [blocklyApp.ToolboxTreeComponent],
  68. pipes: [blocklyApp.TranslatePipe]
  69. })
  70. .Class({
  71. constructor: [
  72. blocklyApp.TreeService, blocklyApp.UtilsService,
  73. function(_treeService, _utilsService) {
  74. this.toolboxCategories = [];
  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. // Note that a timeout is needed here in order to trigger Angular
  106. // change detection.
  107. if (this.xmlHasCategories) {
  108. var that = this;
  109. setTimeout(function() {
  110. that.treeService.setActiveDesc(
  111. 'blockly-toolbox-tree-node0', 'blockly-toolbox-tree');
  112. });
  113. }
  114. },
  115. getActiveDescId: function() {
  116. return this.treeService.getActiveDescId('blockly-toolbox-tree');
  117. },
  118. getCategoryAriaLabel: function(category) {
  119. var numBlocks = this.getToolboxWorkspace(category).topBlocks_.length;
  120. return category.attributes.name.value + ' category. ' +
  121. 'Move right to access ' + numBlocks + ' blocks in this category.';
  122. },
  123. getToolboxWorkspace: function(categoryNode) {
  124. return this.treeService.getToolboxWorkspace(categoryNode);
  125. }
  126. });