dropdown.ts 9.1 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6 7 8
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

'use strict';

import 'vs/css!./dropdown';
J
Johannes Rieken 已提交
9 10
import { Builder, $ } from 'vs/base/browser/builder';
import { TPromise } from 'vs/base/common/winjs.base';
11
import { Gesture, EventType as GestureEventType } from 'vs/base/browser/touch';
12
import { ActionRunner, IAction, IActionRunner } from 'vs/base/common/actions';
13
import { BaseActionItem, IActionItemProvider } from 'vs/base/browser/ui/actionbar/actionbar';
J
Johannes Rieken 已提交
14
import { IDisposable, dispose } from 'vs/base/common/lifecycle';
15
import { IContextViewProvider, IAnchor } from 'vs/base/browser/ui/contextview/contextview';
J
Johannes Rieken 已提交
16
import { IMenuOptions } from 'vs/base/browser/ui/menu/menu';
17
import { ResolvedKeybinding } from 'vs/base/common/keyCodes';
B
Benjamin Pasero 已提交
18
import { EventHelper, EventType } from 'vs/base/browser/dom';
19
import { IContextMenuDelegate } from 'vs/base/browser/contextmenu';
E
Erich Gamma 已提交
20 21

export interface ILabelRenderer {
B
Benjamin Pasero 已提交
22
	(container: HTMLElement): IDisposable;
E
Erich Gamma 已提交
23 24 25 26 27 28 29
}

export interface IBaseDropdownOptions {
	label?: string;
	labelRenderer?: ILabelRenderer;
}

B
Benjamin Pasero 已提交
30
export class BaseDropdown extends ActionRunner {
31 32
	private _toDispose: IDisposable[];
	private $el: Builder;
B
Benjamin Pasero 已提交
33 34 35
	private $boxContainer: Builder;
	private $label: Builder;
	private $contents: Builder;
36
	private visible: boolean;
E
Erich Gamma 已提交
37

B
Benjamin Pasero 已提交
38
	constructor(container: HTMLElement, options: IBaseDropdownOptions) {
E
Erich Gamma 已提交
39 40
		super();

41
		this._toDispose = [];
E
Erich Gamma 已提交
42

43
		this.$el = $('.monaco-dropdown').appendTo(container);
E
Erich Gamma 已提交
44 45 46

		this.$label = $('.dropdown-label');

B
Benjamin Pasero 已提交
47
		let labelRenderer = options.labelRenderer;
E
Erich Gamma 已提交
48
		if (!labelRenderer) {
B
Benjamin Pasero 已提交
49
			labelRenderer = (container: HTMLElement): IDisposable => {
E
Erich Gamma 已提交
50 51 52 53 54
				$(container).text(options.label || '');
				return null;
			};
		}

55 56 57
		this.$label.on([EventType.CLICK, EventType.MOUSE_DOWN, GestureEventType.Tap], (e: Event) => {
			EventHelper.stop(e, true); // prevent default click behaviour to trigger
		}).on([EventType.MOUSE_DOWN, GestureEventType.Tap], (e: Event) => {
B
Benjamin Pasero 已提交
58 59 60 61
			if (e instanceof MouseEvent && e.detail > 1) {
				return; // prevent multiple clicks to open multiple context menus (https://github.com/Microsoft/vscode/issues/41363)
			}

62 63 64 65 66
			if (this.visible) {
				this.hide();
			} else {
				this.show();
			}
E
Erich Gamma 已提交
67 68
		}).appendTo(this.$el);

B
Benjamin Pasero 已提交
69
		let cleanupFn = labelRenderer(this.$label.getHTMLElement());
E
Erich Gamma 已提交
70 71

		if (cleanupFn) {
72
			this._toDispose.push(cleanupFn);
E
Erich Gamma 已提交
73 74
		}

75
		Gesture.addTarget(this.$label.getHTMLElement());
76 77 78 79 80 81 82 83 84 85 86 87
	}

	public get toDispose(): IDisposable[] {
		return this._toDispose;
	}

	public get element(): Builder {
		return this.$el;
	}

	public get label(): Builder {
		return this.$label;
E
Erich Gamma 已提交
88 89 90 91 92 93
	}

	public set tooltip(tooltip: string) {
		this.$label.title(tooltip);
	}

94
	public show(): void {
95
		this.visible = true;
E
Erich Gamma 已提交
96 97
	}

98
	public hide(): void {
99
		this.visible = false;
E
Erich Gamma 已提交
100 101
	}

102
	protected onEvent(e: Event, activeElement: HTMLElement): void {
E
Erich Gamma 已提交
103 104 105 106 107 108 109
		this.hide();
	}

	public dispose(): void {
		super.dispose();
		this.hide();

110
		this._toDispose = dispose(this.toDispose);
E
Erich Gamma 已提交
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129

		if (this.$boxContainer) {
			this.$boxContainer.destroy();
			this.$boxContainer = null;
		}

		if (this.$contents) {
			this.$contents.destroy();
			this.$contents = null;
		}

		if (this.$label) {
			this.$label.destroy();
			this.$label = null;
		}
	}
}

export interface IDropdownOptions extends IBaseDropdownOptions {
B
Benjamin Pasero 已提交
130
	contextViewProvider: IContextViewProvider;
E
Erich Gamma 已提交
131 132 133
}

export class Dropdown extends BaseDropdown {
134
	private contextViewProvider: IContextViewProvider;
E
Erich Gamma 已提交
135

B
Benjamin Pasero 已提交
136
	constructor(container: HTMLElement, options: IDropdownOptions) {
E
Erich Gamma 已提交
137 138
		super(container, options);

139
		this.contextViewProvider = options.contextViewProvider;
E
Erich Gamma 已提交
140 141
	}

142
	public show(): void {
143 144
		super.show();

145
		this.element.addClass('active');
E
Erich Gamma 已提交
146

147
		this.contextViewProvider.showContextView({
148
			getAnchor: () => this.getAnchor(),
E
Erich Gamma 已提交
149 150 151 152 153 154 155 156 157

			render: (container) => {
				return this.renderContents(container);
			},

			onDOMEvent: (e, activeElement) => {
				this.onEvent(e, activeElement);
			},

158
			onHide: () => this.onHide()
E
Erich Gamma 已提交
159 160 161
		});
	}

162 163 164 165 166 167 168 169
	protected getAnchor(): HTMLElement | IAnchor {
		return this.element.getHTMLElement();
	}

	protected onHide(): void {
		this.element.removeClass('active');
	}

170
	public hide(): void {
171 172
		super.hide();

173 174
		if (this.contextViewProvider) {
			this.contextViewProvider.hideContextView();
E
Erich Gamma 已提交
175 176 177
		}
	}

178
	protected renderContents(container: HTMLElement): IDisposable {
E
Erich Gamma 已提交
179 180 181 182 183 184 185 186 187
		return null;
	}
}

export interface IContextMenuProvider {
	showContextMenu(delegate: IContextMenuDelegate): void;
}

export interface IActionProvider {
B
Benjamin Pasero 已提交
188
	getActions(): IAction[];
E
Erich Gamma 已提交
189 190 191 192
}

export interface IDropdownMenuOptions extends IBaseDropdownOptions {
	contextMenuProvider: IContextMenuProvider;
B
Benjamin Pasero 已提交
193
	actions?: IAction[];
E
Erich Gamma 已提交
194 195 196 197 198
	actionProvider?: IActionProvider;
	menuClassName?: string;
}

export class DropdownMenu extends BaseDropdown {
199
	private _contextMenuProvider: IContextMenuProvider;
B
Benjamin Pasero 已提交
200
	private _menuOptions: IMenuOptions;
201 202
	private _actions: IAction[];
	private actionProvider: IActionProvider;
E
Erich Gamma 已提交
203 204
	private menuClassName: string;

B
Benjamin Pasero 已提交
205
	constructor(container: HTMLElement, options: IDropdownMenuOptions) {
E
Erich Gamma 已提交
206 207 208 209 210 211 212 213
		super(container, options);

		this._contextMenuProvider = options.contextMenuProvider;
		this.actions = options.actions || [];
		this.actionProvider = options.actionProvider;
		this.menuClassName = options.menuClassName || '';
	}

B
Benjamin Pasero 已提交
214
	public set menuOptions(options: IMenuOptions) {
E
Erich Gamma 已提交
215 216 217
		this._menuOptions = options;
	}

B
Benjamin Pasero 已提交
218
	public get menuOptions(): IMenuOptions {
E
Erich Gamma 已提交
219 220 221
		return this._menuOptions;
	}

222
	private get actions(): IAction[] {
E
Erich Gamma 已提交
223 224 225 226 227 228 229
		if (this.actionProvider) {
			return this.actionProvider.getActions();
		}

		return this._actions;
	}

230
	private set actions(actions: IAction[]) {
E
Erich Gamma 已提交
231 232 233
		this._actions = actions;
	}

234
	public show(): void {
235 236
		super.show();

237
		this.element.addClass('active');
E
Erich Gamma 已提交
238 239

		this._contextMenuProvider.showContextMenu({
240
			getAnchor: () => this.element.getHTMLElement(),
A
Alex Dima 已提交
241
			getActions: () => TPromise.as(this.actions),
E
Erich Gamma 已提交
242 243
			getActionsContext: () => this.menuOptions ? this.menuOptions.context : null,
			getActionItem: (action) => this.menuOptions && this.menuOptions.actionItemProvider ? this.menuOptions.actionItemProvider(action) : null,
J
Johannes Rieken 已提交
244
			getKeyBinding: (action: IAction) => this.menuOptions && this.menuOptions.getKeyBinding ? this.menuOptions.getKeyBinding(action) : null,
E
Erich Gamma 已提交
245
			getMenuClassName: () => this.menuClassName,
246 247
			onHide: () => this.element.removeClass('active'),
			actionRunner: this.menuOptions ? this.menuOptions.actionRunner : null
E
Erich Gamma 已提交
248 249 250
		});
	}

251
	public hide(): void {
252
		super.hide();
E
Erich Gamma 已提交
253
	}
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
}

export class DropdownMenuActionItem extends BaseActionItem {
	private menuActionsOrProvider: any;
	private dropdownMenu: DropdownMenu;
	private contextMenuProvider: IContextMenuProvider;
	private actionItemProvider: IActionItemProvider;
	private keybindings: (action: IAction) => ResolvedKeybinding;
	private clazz: string;

	constructor(action: IAction, menuActions: IAction[], contextMenuProvider: IContextMenuProvider, actionItemProvider: IActionItemProvider, actionRunner: IActionRunner, keybindings: (action: IAction) => ResolvedKeybinding, clazz: string);
	constructor(action: IAction, actionProvider: IActionProvider, contextMenuProvider: IContextMenuProvider, actionItemProvider: IActionItemProvider, actionRunner: IActionRunner, keybindings: (action: IAction) => ResolvedKeybinding, clazz: string);
	constructor(action: IAction, menuActionsOrProvider: any, contextMenuProvider: IContextMenuProvider, actionItemProvider: IActionItemProvider, actionRunner: IActionRunner, keybindings: (action: IAction) => ResolvedKeybinding, clazz: string) {
		super(null, action);

		this.menuActionsOrProvider = menuActionsOrProvider;
		this.contextMenuProvider = contextMenuProvider;
		this.actionItemProvider = actionItemProvider;
		this.actionRunner = actionRunner;
		this.keybindings = keybindings;
		this.clazz = clazz;
	}

	public render(container: HTMLElement): void {
		let labelRenderer: ILabelRenderer = (el: HTMLElement): IDisposable => {
			this.builder = $('a.action-label').attr({
				tabIndex: '0',
				role: 'button',
				'aria-haspopup': 'true',
				title: this._action.label || '',
				class: this.clazz
			});

			this.builder.appendTo(el);

			return null;
		};

		let options: IDropdownMenuOptions = {
			contextMenuProvider: this.contextMenuProvider,
			labelRenderer: labelRenderer
		};

		// Render the DropdownMenu around a simple action to toggle it
		if (Array.isArray(this.menuActionsOrProvider)) {
			options.actions = this.menuActionsOrProvider;
		} else {
			options.actionProvider = this.menuActionsOrProvider;
		}

		this.dropdownMenu = new DropdownMenu(container, options);

		this.dropdownMenu.menuOptions = {
			actionItemProvider: this.actionItemProvider,
			actionRunner: this.actionRunner,
			getKeyBinding: this.keybindings,
			context: this._context
		};
	}

	public setActionContext(newContext: any): void {
		super.setActionContext(newContext);

		if (this.dropdownMenu) {
			this.dropdownMenu.menuOptions.context = newContext;
		}
	}

	public show(): void {
		if (this.dropdownMenu) {
			this.dropdownMenu.show();
		}
	}

	public dispose(): void {
		this.dropdownMenu.dispose();

		super.dispose();
	}
B
Benjamin Pasero 已提交
333
}