workspace.component.js 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 Blockly.Workspace is
  21. * rendered in AccessibleBlockly.
  22. * @author madeeha@google.com (Madeeha Ghori)
  23. */
  24. blocklyApp.WorkspaceComponent = ng.core
  25. .Component({
  26. selector: 'blockly-workspace',
  27. template: `
  28. <label>
  29. <h3 #workspaceTitle id="blockly-workspace-title">{{'WORKSPACE'|translate}}</h3>
  30. </label>
  31. <div id="blockly-workspace-toolbar" (keydown)="onWorkspaceToolbarKeypress($event)">
  32. <span *ngFor="#buttonConfig of toolbarButtonConfig">
  33. <button (click)="buttonConfig.action()"
  34. class="blocklyTree blocklyWorkspaceToolbarButton">
  35. {{buttonConfig.text}}
  36. </button>
  37. </span>
  38. <button id="clear-workspace" (click)="clearWorkspace()"
  39. [attr.aria-disabled]="isWorkspaceEmpty()"
  40. class="blocklyTree blocklyWorkspaceToolbarButton">
  41. {{'CLEAR_WORKSPACE'|translate}}
  42. </button>
  43. </div>
  44. <div *ngIf="workspace">
  45. <ol #tree *ngFor="#block of workspace.topBlocks_; #i = index"
  46. tabIndex="0" role="group" class="blocklyTree blocklyWorkspaceTree"
  47. [attr.aria-activedescendant]="getActiveDescId(tree.id)"
  48. [attr.aria-labelledby]="workspaceTitle.id"
  49. (keydown)="onKeypress($event, tree)">
  50. <blockly-workspace-tree [level]=1 [block]="block" [tree]="tree" [isTopLevel]="true">
  51. </blockly-workspace-tree>
  52. </ol>
  53. </div>
  54. `,
  55. directives: [blocklyApp.WorkspaceTreeComponent],
  56. pipes: [blocklyApp.TranslatePipe]
  57. })
  58. .Class({
  59. constructor: [blocklyApp.TreeService, function(_treeService) {
  60. // ACCESSIBLE_GLOBALS is a global variable defined by the containing
  61. // page. It should contain a key, toolbarButtonConfig, whose
  62. // corresponding value is an Array with two keys: 'text' and 'action'.
  63. // The first is the text to display on the button, and the second is the
  64. // function that gets run when the button is clicked.
  65. this.toolbarButtonConfig =
  66. ACCESSIBLE_GLOBALS && ACCESSIBLE_GLOBALS.toolbarButtonConfig ?
  67. ACCESSIBLE_GLOBALS.toolbarButtonConfig : [];
  68. this.workspace = blocklyApp.workspace;
  69. this.treeService = _treeService;
  70. }],
  71. clearWorkspace: function() {
  72. this.workspace.clear();
  73. },
  74. getActiveDescId: function(tree) {
  75. return this.treeService.getActiveDescId(tree.id);
  76. },
  77. onWorkspaceToolbarKeypress: function(e) {
  78. this.treeService.onWorkspaceToolbarKeypress(
  79. e, document.activeElement.id);
  80. },
  81. onKeypress: function(e, tree) {
  82. this.treeService.onKeypress(e, tree);
  83. },
  84. isWorkspaceEmpty: function() {
  85. return !this.workspace.topBlocks_.length;
  86. }
  87. });