workspace.component.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. <div class="blocklyWorkspaceColumn">
  29. <h3 #workspaceTitle id="blockly-workspace-title">{{'WORKSPACE'|translate}}</h3>
  30. <div *ngIf="workspace" class="blocklyWorkspace">
  31. <ol #tree *ngFor="#block of workspace.topBlocks_; #i = index"
  32. tabindex="0" role="tree" class="blocklyTree blocklyWorkspaceTree"
  33. [attr.aria-activedescendant]="getActiveDescId(tree.id)"
  34. [attr.aria-labelledby]="workspaceTitle.id"
  35. (keydown)="onKeypress($event, tree)">
  36. <blockly-workspace-tree [level]="0" [block]="block" [tree]="tree" [isTopLevel]="true">
  37. </blockly-workspace-tree>
  38. </ol>
  39. <span *ngIf="workspace.topBlocks_.length === 0">
  40. <i>Workspace is empty.</i>
  41. </span>
  42. </div>
  43. </div>
  44. <div class="blocklyToolbarColumn">
  45. <div id="blockly-workspace-toolbar" (keydown)="onWorkspaceToolbarKeypress($event)">
  46. <span *ngFor="#buttonConfig of toolbarButtonConfig">
  47. <button *ngIf="!buttonConfig.isHidden()"
  48. (click)="handleButtonClick(buttonConfig)"
  49. [attr.aria-describedby]="buttonConfig.ariaDescribedBy"
  50. class="blocklyTree blocklyWorkspaceToolbarButton">
  51. {{buttonConfig.text}}
  52. </button>
  53. </span>
  54. <button id="clear-workspace" (click)="clearWorkspace()"
  55. [attr.aria-disabled]="isWorkspaceEmpty()"
  56. class="blocklyTree blocklyWorkspaceToolbarButton">
  57. {{'CLEAR_WORKSPACE'|translate}}
  58. </button>
  59. </div>
  60. </div>
  61. `,
  62. directives: [blocklyApp.WorkspaceTreeComponent],
  63. pipes: [blocklyApp.TranslatePipe]
  64. })
  65. .Class({
  66. constructor: [
  67. blocklyApp.NotificationsService, blocklyApp.TreeService,
  68. blocklyApp.UtilsService,
  69. function(_notificationsService, _treeService, _utilsService) {
  70. // ACCESSIBLE_GLOBALS is a global variable defined by the containing
  71. // page. It should contain a key, toolbarButtonConfig, whose
  72. // corresponding value is an Array with two keys: 'text' and 'action'.
  73. // The first is the text to display on the button, and the second is the
  74. // function that gets run when the button is clicked.
  75. this.toolbarButtonConfig =
  76. ACCESSIBLE_GLOBALS && ACCESSIBLE_GLOBALS.toolbarButtonConfig ?
  77. ACCESSIBLE_GLOBALS.toolbarButtonConfig : [];
  78. this.workspace = blocklyApp.workspace;
  79. this.notificationsService = _notificationsService;
  80. this.treeService = _treeService;
  81. this.utilsService = _utilsService;
  82. }],
  83. clearWorkspace: function() {
  84. this.workspace.clear();
  85. },
  86. getActiveDescId: function(treeId) {
  87. return this.treeService.getActiveDescId(treeId);
  88. },
  89. handleButtonClick: function(buttonConfig) {
  90. buttonConfig.action();
  91. if (buttonConfig.onClickNotification) {
  92. this.notificationsService.setStatusMessage(
  93. buttonConfig.onClickNotification);
  94. }
  95. },
  96. onWorkspaceToolbarKeypress: function(e) {
  97. this.treeService.onWorkspaceToolbarKeypress(
  98. e, document.activeElement.id);
  99. },
  100. onKeypress: function(e, tree) {
  101. this.treeService.onKeypress(e, tree);
  102. },
  103. isWorkspaceEmpty: function() {
  104. return this.utilsService.isWorkspaceEmpty();
  105. }
  106. });