ToolbarAuralView.es6.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /**
  2. * @file
  3. * A Backbone view for the aural feedback of the toolbar.
  4. */
  5. (function (Backbone, Drupal) {
  6. Drupal.toolbar.ToolbarAuralView = Backbone.View.extend(/** @lends Drupal.toolbar.ToolbarAuralView# */{
  7. /**
  8. * Backbone view for the aural feedback of the toolbar.
  9. *
  10. * @constructs
  11. *
  12. * @augments Backbone.View
  13. *
  14. * @param {object} options
  15. * Options for the view.
  16. * @param {object} options.strings
  17. * Various strings to use in the view.
  18. */
  19. initialize(options) {
  20. this.strings = options.strings;
  21. this.listenTo(this.model, 'change:orientation', this.onOrientationChange);
  22. this.listenTo(this.model, 'change:activeTray', this.onActiveTrayChange);
  23. },
  24. /**
  25. * Announces an orientation change.
  26. *
  27. * @param {Drupal.toolbar.ToolbarModel} model
  28. * The toolbar model in question.
  29. * @param {string} orientation
  30. * The new value of the orientation attribute in the model.
  31. */
  32. onOrientationChange(model, orientation) {
  33. Drupal.announce(Drupal.t('Tray orientation changed to @orientation.', {
  34. '@orientation': orientation,
  35. }));
  36. },
  37. /**
  38. * Announces a changed active tray.
  39. *
  40. * @param {Drupal.toolbar.ToolbarModel} model
  41. * The toolbar model in question.
  42. * @param {HTMLElement} tray
  43. * The new value of the tray attribute in the model.
  44. */
  45. onActiveTrayChange(model, tray) {
  46. const relevantTray = (tray === null) ? model.previous('activeTray') : tray;
  47. // Current activeTray and previous activeTray are empty, no state change
  48. // to announce.
  49. if (!relevantTray) {
  50. return;
  51. }
  52. const action = (tray === null) ? Drupal.t('closed') : Drupal.t('opened');
  53. const trayNameElement = relevantTray.querySelector('.toolbar-tray-name');
  54. let text;
  55. if (trayNameElement !== null) {
  56. text = Drupal.t('Tray "@tray" @action.', {
  57. '@tray': trayNameElement.textContent, '@action': action,
  58. });
  59. }
  60. else {
  61. text = Drupal.t('Tray @action.', { '@action': action });
  62. }
  63. Drupal.announce(text);
  64. },
  65. });
  66. }(Backbone, Drupal));