e233129379e2fa3f864401a86307c241dea29b276bef7d9233868bdff37edf9d139c400ae2e5846d44bbce4ed150bf3b9ef522c3e287865bcdaf7c2a49eb56 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*---------------------------------------------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Licensed under the MIT License. See License.txt in the project root for license information.
  4. *--------------------------------------------------------------------------------------------*/
  5. import { $, append } from '../../dom.js';
  6. import { BaseActionViewItem } from '../actionbar/actionViewItems.js';
  7. import { DropdownMenu } from './dropdown.js';
  8. import { Emitter } from '../../../common/event.js';
  9. import './dropdown.css';
  10. export class DropdownMenuActionViewItem extends BaseActionViewItem {
  11. constructor(action, menuActionsOrProvider, contextMenuProvider, options = Object.create(null)) {
  12. super(null, action, options);
  13. this.actionItem = null;
  14. this._onDidChangeVisibility = this._register(new Emitter());
  15. this.menuActionsOrProvider = menuActionsOrProvider;
  16. this.contextMenuProvider = contextMenuProvider;
  17. this.options = options;
  18. if (this.options.actionRunner) {
  19. this.actionRunner = this.options.actionRunner;
  20. }
  21. }
  22. render(container) {
  23. this.actionItem = container;
  24. const labelRenderer = (el) => {
  25. this.element = append(el, $('a.action-label'));
  26. let classNames = [];
  27. if (typeof this.options.classNames === 'string') {
  28. classNames = this.options.classNames.split(/\s+/g).filter(s => !!s);
  29. }
  30. else if (this.options.classNames) {
  31. classNames = this.options.classNames;
  32. }
  33. // todo@aeschli: remove codicon, should come through `this.options.classNames`
  34. if (!classNames.find(c => c === 'icon')) {
  35. classNames.push('codicon');
  36. }
  37. this.element.classList.add(...classNames);
  38. this.element.setAttribute('role', 'button');
  39. this.element.setAttribute('aria-haspopup', 'true');
  40. this.element.setAttribute('aria-expanded', 'false');
  41. this.element.title = this._action.label || '';
  42. this.element.ariaLabel = this._action.label || '';
  43. return null;
  44. };
  45. const isActionsArray = Array.isArray(this.menuActionsOrProvider);
  46. const options = {
  47. contextMenuProvider: this.contextMenuProvider,
  48. labelRenderer: labelRenderer,
  49. menuAsChild: this.options.menuAsChild,
  50. actions: isActionsArray ? this.menuActionsOrProvider : undefined,
  51. actionProvider: isActionsArray ? undefined : this.menuActionsOrProvider
  52. };
  53. this.dropdownMenu = this._register(new DropdownMenu(container, options));
  54. this._register(this.dropdownMenu.onDidChangeVisibility(visible => {
  55. var _a;
  56. (_a = this.element) === null || _a === void 0 ? void 0 : _a.setAttribute('aria-expanded', `${visible}`);
  57. this._onDidChangeVisibility.fire(visible);
  58. }));
  59. this.dropdownMenu.menuOptions = {
  60. actionViewItemProvider: this.options.actionViewItemProvider,
  61. actionRunner: this.actionRunner,
  62. getKeyBinding: this.options.keybindingProvider,
  63. context: this._context
  64. };
  65. if (this.options.anchorAlignmentProvider) {
  66. const that = this;
  67. this.dropdownMenu.menuOptions = Object.assign(Object.assign({}, this.dropdownMenu.menuOptions), { get anchorAlignment() {
  68. return that.options.anchorAlignmentProvider();
  69. } });
  70. }
  71. this.updateTooltip();
  72. this.updateEnabled();
  73. }
  74. getTooltip() {
  75. let title = null;
  76. if (this.getAction().tooltip) {
  77. title = this.getAction().tooltip;
  78. }
  79. else if (this.getAction().label) {
  80. title = this.getAction().label;
  81. }
  82. return title !== null && title !== void 0 ? title : undefined;
  83. }
  84. setActionContext(newContext) {
  85. super.setActionContext(newContext);
  86. if (this.dropdownMenu) {
  87. if (this.dropdownMenu.menuOptions) {
  88. this.dropdownMenu.menuOptions.context = newContext;
  89. }
  90. else {
  91. this.dropdownMenu.menuOptions = { context: newContext };
  92. }
  93. }
  94. }
  95. updateEnabled() {
  96. var _a, _b;
  97. const disabled = !this.getAction().enabled;
  98. (_a = this.actionItem) === null || _a === void 0 ? void 0 : _a.classList.toggle('disabled', disabled);
  99. (_b = this.element) === null || _b === void 0 ? void 0 : _b.classList.toggle('disabled', disabled);
  100. }
  101. }