field.component.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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.Field is
  21. * rendered in the toolbox in AccessibleBlockly. Also handles any interactions
  22. * with the field.
  23. * @author madeeha@google.com (Madeeha Ghori)
  24. */
  25. blocklyApp.FieldComponent = ng.core
  26. .Component({
  27. selector: 'blockly-field',
  28. template: `
  29. <li [id]="idMap['listItem']" role="treeitem" *ngIf="isTextInput()"
  30. [attr.aria-labelledBy]="generateAriaLabelledByAttr('blockly-argument-input', idMap['input'])"
  31. [attr.aria-level]="level" aria-selected=false>
  32. <input [id]="idMap['input']" [ngModel]="field.getValue()" (ngModelChange)="field.setValue($event)">
  33. </li>
  34. <li [id]="idMap['listItem']" role="treeitem" *ngIf="isDropdown()"
  35. [attr.aria-labelledBy]="generateAriaLabelledByAttr('blockly-argument-menu', idMap['label'])"
  36. [attr.aria-level]="level" aria-selected=false>
  37. <label [id]="idMap['label']">{{'CURRENT_ARGUMENT_VALUE'|translate}} {{field.getText()}}</label>
  38. <ol role="group" [attr.aria-level]="level+1">
  39. <li [id]="idMap[optionValue]" role="treeitem" *ngFor="#optionValue of getOptions()"
  40. [attr.aria-labelledBy]="generateAriaLabelledByAttr(idMap[optionValue + 'Button'], 'blockly-button')"
  41. [attr.aria-level]="level+1" aria-selected=false>
  42. <button [id]="idMap[optionValue + 'Button']" (click)="handleDropdownChange(field, optionValue)">
  43. {{optionText[optionValue]}}
  44. </button>
  45. </li>
  46. </ol>
  47. </li>
  48. <li [id]="idMap['listItem']" role="treeitem" *ngIf="isCheckbox()"
  49. [attr.aria-level]="level" aria-selected=false>
  50. // Checkboxes are not currently supported.
  51. </li>
  52. <li [id]="idMap['listItem']" role="treeitem" *ngIf="isTextField() && hasVisibleText()"
  53. [attr.aria-labelledBy]="utilsService.generateAriaLabelledByAttr('blockly-argument-text', idMap['label'])"
  54. [attr.aria-level]="level" aria-selected=false>
  55. <label [id]="idMap['label']">
  56. {{field.getText()}}
  57. </label>
  58. </li>
  59. `,
  60. inputs: ['field', 'level', 'index', 'parentId'],
  61. pipes: [blocklyApp.TranslatePipe]
  62. })
  63. .Class({
  64. constructor: [blocklyApp.UtilsService, function(_utilsService) {
  65. this.optionText = {
  66. keys: []
  67. };
  68. this.utilsService = _utilsService;
  69. }],
  70. ngOnInit: function() {
  71. var elementsNeedingIds = this.generateElementNames(this.field);
  72. // Warning: this assumes that the elements returned by
  73. // this.generateElementNames() are unique.
  74. this.idMap = this.utilsService.generateIds(elementsNeedingIds);
  75. },
  76. generateAriaLabelledByAttr: function(mainLabel, secondLabel) {
  77. return mainLabel + ' ' + secondLabel;
  78. },
  79. generateElementNames: function() {
  80. var elementNames = ['listItem'];
  81. if (this.isTextInput()) {
  82. elementNames.push('input');
  83. } else if (this.isDropdown()) {
  84. elementNames.push('label');
  85. var keys = this.getOptions();
  86. for (var i = 0; i < keys.length; i++){
  87. elementNames.push(keys[i], keys[i] + 'Button');
  88. }
  89. } else if (this.isTextField() && this.hasVisibleText()) {
  90. elementNames.push('label');
  91. }
  92. return elementNames;
  93. },
  94. isTextInput: function() {
  95. return this.field instanceof Blockly.FieldTextInput;
  96. },
  97. isDropdown: function() {
  98. return this.field instanceof Blockly.FieldDropdown;
  99. },
  100. isCheckbox: function() {
  101. return this.field instanceof Blockly.FieldCheckbox;
  102. },
  103. isTextField: function() {
  104. return !(this.field instanceof Blockly.FieldTextInput) &&
  105. !(this.field instanceof Blockly.FieldDropdown) &&
  106. !(this.field instanceof Blockly.FieldCheckbox);
  107. },
  108. hasVisibleText: function() {
  109. var text = this.field.getText().trim();
  110. return !!text;
  111. },
  112. getOptions: function() {
  113. if (this.optionText.keys.length) {
  114. return this.optionText.keys;
  115. }
  116. var options = this.field.getOptions_();
  117. for (var i = 0; i < options.length; i++) {
  118. var tuple = options[i];
  119. this.optionText[tuple[1]] = tuple[0];
  120. this.optionText.keys.push(tuple[1]);
  121. }
  122. return this.optionText.keys;
  123. },
  124. handleDropdownChange: function(field, text) {
  125. if (text == 'NO_ACTION') {
  126. return;
  127. }
  128. if (this.field instanceof Blockly.FieldVariable) {
  129. Blockly.FieldVariable.dropdownChange.call(this.field, text);
  130. } else {
  131. this.field.setValue(text);
  132. }
  133. }
  134. });