actionbar.ts 19.9 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!./actionbar';
9 10
import nls = require('vs/nls');
import lifecycle = require('vs/base/common/lifecycle');
11
import { TPromise } from 'vs/base/common/winjs.base';
J
Johannes Rieken 已提交
12 13
import { Builder, $ } from 'vs/base/browser/builder';
import { SelectBox } from 'vs/base/browser/ui/selectBox/selectBox';
I
isidor 已提交
14
import { IAction, IActionRunner, Action, IActionChangeEvent, ActionRunner, IRunEvent } from 'vs/base/common/actions';
15 16
import DOM = require('vs/base/browser/dom');
import types = require('vs/base/common/types');
17
import { EventType, Gesture } from 'vs/base/browser/touch';
J
Johannes Rieken 已提交
18 19
import { StandardKeyboardEvent } from 'vs/base/browser/keyboardEvent';
import { KeyCode, KeyMod } from 'vs/base/common/keyCodes';
20
import { IContextViewProvider } from 'vs/base/browser/ui/contextview/contextview';
M
Matt Bierner 已提交
21
import { Event, Emitter } from 'vs/base/common/event';
E
Erich Gamma 已提交
22

I
isidor 已提交
23
export interface IActionItem {
B
Benjamin Pasero 已提交
24
	actionRunner: IActionRunner;
B
Benjamin Pasero 已提交
25 26 27
	setActionContext(context: any): void;
	render(element: HTMLElement): void;
	isEnabled(): boolean;
I
isidor 已提交
28
	focus(fromRight?: boolean): void;
B
Benjamin Pasero 已提交
29 30
	blur(): void;
	dispose(): void;
E
Erich Gamma 已提交
31 32
}

33 34
export interface IBaseActionItemOptions {
	draggable?: boolean;
35
	isMenu?: boolean;
36 37
}

I
isidor 已提交
38
export class BaseActionItem implements IActionItem {
E
Erich Gamma 已提交
39

B
Benjamin Pasero 已提交
40
	public builder: Builder;
A
Alex Dima 已提交
41
	public _callOnDispose: lifecycle.IDisposable[];
B
Benjamin Pasero 已提交
42
	public _context: any;
B
Benjamin Pasero 已提交
43 44 45
	public _action: IAction;

	private _actionRunner: IActionRunner;
E
Erich Gamma 已提交
46

47
	constructor(context: any, action: IAction, protected options?: IBaseActionItemOptions) {
E
Erich Gamma 已提交
48 49 50 51
		this._callOnDispose = [];
		this._context = context || this;
		this._action = action;

B
Benjamin Pasero 已提交
52
		if (action instanceof Action) {
53
			this._callOnDispose.push(action.onDidChange(event => {
B
Benjamin Pasero 已提交
54
				if (!this.builder) {
E
Erich Gamma 已提交
55 56 57 58
					// we have not been rendered yet, so there
					// is no point in updating the UI
					return;
				}
59 60 61 62
				this._handleActionChangeEvent(event);
			}));
		}
	}
E
Erich Gamma 已提交
63

64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
	protected _handleActionChangeEvent(event: IActionChangeEvent): void {
		if (event.enabled !== void 0) {
			this._updateEnabled();
		}
		if (event.checked !== void 0) {
			this._updateChecked();
		}
		if (event.class !== void 0) {
			this._updateClass();
		}
		if (event.label !== void 0) {
			this._updateLabel();
			this._updateTooltip();
		}
		if (event.tooltip !== void 0) {
			this._updateTooltip();
E
Erich Gamma 已提交
80 81 82 83 84 85 86
		}
	}

	public get callOnDispose() {
		return this._callOnDispose;
	}

B
Benjamin Pasero 已提交
87
	public set actionRunner(actionRunner: IActionRunner) {
E
Erich Gamma 已提交
88 89 90
		this._actionRunner = actionRunner;
	}

B
Benjamin Pasero 已提交
91
	public get actionRunner(): IActionRunner {
E
Erich Gamma 已提交
92 93 94
		return this._actionRunner;
	}

B
Benjamin Pasero 已提交
95
	public getAction(): IAction {
E
Erich Gamma 已提交
96 97 98
		return this._action;
	}

B
Benjamin Pasero 已提交
99
	public isEnabled(): boolean {
E
Erich Gamma 已提交
100 101 102
		return this._action.enabled;
	}

B
Benjamin Pasero 已提交
103
	public setActionContext(newContext: any): void {
E
Erich Gamma 已提交
104 105 106
		this._context = newContext;
	}

B
Benjamin Pasero 已提交
107
	public render(container: HTMLElement): void {
E
Erich Gamma 已提交
108
		this.builder = $(container);
109
		Gesture.addTarget(container);
E
Erich Gamma 已提交
110

111 112 113 114 115
		const enableDragging = this.options && this.options.draggable;
		if (enableDragging) {
			container.draggable = true;
		}

I
isidor 已提交
116
		this.builder.on(EventType.Tap, e => this.onClick(e));
E
Erich Gamma 已提交
117

118
		this.builder.on(DOM.EventType.MOUSE_DOWN, (e) => {
119
			if (!enableDragging) {
J
Joao Moreno 已提交
120
				DOM.EventHelper.stop(e, true); // do not run when dragging is on because that would disable it
121 122
			}

123 124
			const mouseEvent = e as MouseEvent;
			if (this._action.enabled && mouseEvent.button === 0) {
E
Erich Gamma 已提交
125 126 127
				this.builder.addClass('active');
			}
		});
128

129
		this.builder.on(DOM.EventType.CLICK, (e) => {
J
Johannes Rieken 已提交
130
			DOM.EventHelper.stop(e, true);
131 132 133 134 135 136 137 138 139 140 141
			// See https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Interact_with_the_clipboard
			// > Writing to the clipboard
			// > You can use the "cut" and "copy" commands without any special
			// permission if you are using them in a short-lived event handler
			// for a user action (for example, a click handler).

			// => to get the Copy and Paste context menu actions working on Firefox,
			// there should be no timeout here
			if (this.options && this.options.isMenu) {
				this.onClick(e);
			} else {
142
				setImmediate(() => this.onClick(e));
143
			}
J
Johannes Rieken 已提交
144
		});
E
Erich Gamma 已提交
145

146
		this.builder.on([DOM.EventType.MOUSE_UP, DOM.EventType.MOUSE_OUT], (e) => {
B
Benjamin Pasero 已提交
147
			DOM.EventHelper.stop(e);
J
Johannes Rieken 已提交
148
			this.builder.removeClass('active');
E
Erich Gamma 已提交
149 150 151
		});
	}

I
isidor 已提交
152
	public onClick(event: DOM.EventLike): void {
B
Benjamin Pasero 已提交
153
		DOM.EventHelper.stop(event, true);
J
Joao Moreno 已提交
154

155 156 157 158 159 160 161 162
		let context: any;
		if (types.isUndefinedOrNull(this._context)) {
			context = event;
		} else {
			context = this._context;
			context.event = event;
		}

163
		this._actionRunner.run(this._action, context);
E
Erich Gamma 已提交
164 165
	}

B
Benjamin Pasero 已提交
166
	public focus(): void {
167 168 169
		if (this.builder) {
			this.builder.domFocus();
		}
E
Erich Gamma 已提交
170 171
	}

B
Benjamin Pasero 已提交
172
	public blur(): void {
173
		if (this.builder) {
174
			this.builder.domBlur();
175
		}
E
Erich Gamma 已提交
176 177
	}

178
	protected _updateEnabled(): void {
E
Erich Gamma 已提交
179 180 181
		// implement in subclass
	}

182
	protected _updateLabel(): void {
E
Erich Gamma 已提交
183 184 185
		// implement in subclass
	}

186
	protected _updateTooltip(): void {
E
Erich Gamma 已提交
187 188 189
		// implement in subclass
	}

190
	protected _updateClass(): void {
E
Erich Gamma 已提交
191 192 193
		// implement in subclass
	}

194
	protected _updateChecked(): void {
E
Erich Gamma 已提交
195 196 197
		// implement in subclass
	}

B
Benjamin Pasero 已提交
198
	public dispose(): void {
E
Erich Gamma 已提交
199 200 201 202 203
		if (this.builder) {
			this.builder.destroy();
			this.builder = null;
		}

A
Alex Dima 已提交
204
		this._callOnDispose = lifecycle.dispose(this._callOnDispose);
E
Erich Gamma 已提交
205 206 207
	}
}

B
Benjamin Pasero 已提交
208
export class Separator extends Action {
E
Erich Gamma 已提交
209

M
Matt Bierner 已提交
210
	public static readonly ID = 'vs.actions.separator';
E
Erich Gamma 已提交
211

B
Benjamin Pasero 已提交
212
	constructor(label?: string, order?: number) {
E
Erich Gamma 已提交
213 214
		super(Separator.ID, label, label ? 'separator text' : 'separator');
		this.checked = false;
215
		this.radio = false;
E
Erich Gamma 已提交
216 217 218 219 220
		this.enabled = false;
		this.order = order;
	}
}

221
export interface IActionItemOptions extends IBaseActionItemOptions {
B
Benjamin Pasero 已提交
222 223 224
	icon?: boolean;
	label?: boolean;
	keybinding?: string;
E
Erich Gamma 已提交
225 226 227 228
}

export class ActionItem extends BaseActionItem {

J
Johannes Rieken 已提交
229 230
	protected $e: Builder;
	protected options: IActionItemOptions;
B
Benjamin Pasero 已提交
231
	private cssClass: string;
E
Erich Gamma 已提交
232

B
Benjamin Pasero 已提交
233
	constructor(context: any, action: IAction, options: IActionItemOptions = {}) {
234
		super(context, action, options);
E
Erich Gamma 已提交
235 236 237 238 239 240 241

		this.options = options;
		this.options.icon = options.icon !== undefined ? options.icon : false;
		this.options.label = options.label !== undefined ? options.label : true;
		this.cssClass = '';
	}

B
Benjamin Pasero 已提交
242
	public render(container: HTMLElement): void {
E
Erich Gamma 已提交
243 244
		super.render(container);

245
		this.$e = $('a.action-label').appendTo(this.builder);
246 247 248
		if (this._action.id === Separator.ID) {
			// A separator is a presentation item
			this.$e.attr({ role: 'presentation' });
A
Alex Dima 已提交
249
		} else {
250 251 252 253 254
			if (this.options.isMenu) {
				this.$e.attr({ role: 'menuitem' });
			} else {
				this.$e.attr({ role: 'button' });
			}
A
Alex Dima 已提交
255
		}
E
Erich Gamma 已提交
256 257 258 259 260 261 262 263 264 265 266 267

		if (this.options.label && this.options.keybinding) {
			$('span.keybinding').text(this.options.keybinding).appendTo(this.builder);
		}

		this._updateClass();
		this._updateLabel();
		this._updateTooltip();
		this._updateEnabled();
		this._updateChecked();
	}

B
Benjamin Pasero 已提交
268
	public focus(): void {
E
Erich Gamma 已提交
269 270 271 272
		super.focus();
		this.$e.domFocus();
	}

B
Benjamin Pasero 已提交
273
	public _updateLabel(): void {
E
Erich Gamma 已提交
274 275 276 277 278
		if (this.options.label) {
			this.$e.text(this.getAction().label);
		}
	}

B
Benjamin Pasero 已提交
279
	public _updateTooltip(): void {
B
Benjamin Pasero 已提交
280
		let title: string = null;
E
Erich Gamma 已提交
281 282 283 284 285 286 287 288

		if (this.getAction().tooltip) {
			title = this.getAction().tooltip;

		} else if (!this.options.label && this.getAction().label && this.options.icon) {
			title = this.getAction().label;

			if (this.options.keybinding) {
B
Benjamin Pasero 已提交
289
				title = nls.localize({ key: 'titleLabel', comment: ['action title', 'action keybinding'] }, "{0} ({1})", title, this.options.keybinding);
E
Erich Gamma 已提交
290 291 292 293 294 295 296 297
			}
		}

		if (title) {
			this.$e.attr({ title: title });
		}
	}

B
Benjamin Pasero 已提交
298
	public _updateClass(): void {
E
Erich Gamma 已提交
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
		if (this.cssClass) {
			this.$e.removeClass(this.cssClass);
		}
		if (this.options.icon) {
			this.cssClass = this.getAction().class;
			this.$e.addClass('icon');
			if (this.cssClass) {
				this.$e.addClass(this.cssClass);
			}
			this._updateEnabled();
		} else {
			this.$e.removeClass('icon');
		}
	}

B
Benjamin Pasero 已提交
314 315
	public _updateEnabled(): void {
		if (this.getAction().enabled) {
E
Erich Gamma 已提交
316 317
			this.builder.removeClass('disabled');
			this.$e.removeClass('disabled');
318
			this.$e.attr({ tabindex: 0 });
E
Erich Gamma 已提交
319 320 321
		} else {
			this.builder.addClass('disabled');
			this.$e.addClass('disabled');
322
			DOM.removeTabIndexAndUpdateFocus(this.$e.getHTMLElement());
E
Erich Gamma 已提交
323 324 325
		}
	}

B
Benjamin Pasero 已提交
326 327
	public _updateChecked(): void {
		if (this.getAction().checked) {
E
Erich Gamma 已提交
328 329 330 331 332 333 334 335
			this.$e.addClass('checked');
		} else {
			this.$e.removeClass('checked');
		}
	}
}

export enum ActionsOrientation {
J
Joao Moreno 已提交
336 337 338 339
	HORIZONTAL,
	HORIZONTAL_REVERSE,
	VERTICAL,
	VERTICAL_REVERSE,
E
Erich Gamma 已提交
340 341 342
}

export interface IActionItemProvider {
B
Benjamin Pasero 已提交
343
	(action: IAction): IActionItem;
E
Erich Gamma 已提交
344 345 346
}

export interface IActionBarOptions {
B
Benjamin Pasero 已提交
347 348 349
	orientation?: ActionsOrientation;
	context?: any;
	actionItemProvider?: IActionItemProvider;
B
Benjamin Pasero 已提交
350
	actionRunner?: IActionRunner;
351
	ariaLabel?: string;
J
Joao Moreno 已提交
352
	animated?: boolean;
A
Alex Dima 已提交
353
	isMenu?: boolean;
E
Erich Gamma 已提交
354 355
}

B
Benjamin Pasero 已提交
356
let defaultOptions: IActionBarOptions = {
E
Erich Gamma 已提交
357 358 359 360 361
	orientation: ActionsOrientation.HORIZONTAL,
	context: null
};

export interface IActionOptions extends IActionItemOptions {
B
Benjamin Pasero 已提交
362
	index?: number;
E
Erich Gamma 已提交
363 364
}

I
isidor 已提交
365
export class ActionBar implements IActionRunner {
E
Erich Gamma 已提交
366

B
Benjamin Pasero 已提交
367
	public options: IActionBarOptions;
368

B
Benjamin Pasero 已提交
369
	private _actionRunner: IActionRunner;
E
Erich Gamma 已提交
370 371 372
	private _context: any;

	// Items
B
Benjamin Pasero 已提交
373
	public items: IActionItem[];
374

B
Benjamin Pasero 已提交
375
	private focusedItem: number;
376
	private focusTracker: DOM.IFocusTracker;
E
Erich Gamma 已提交
377 378

	// Elements
B
Benjamin Pasero 已提交
379 380
	public domNode: HTMLElement;
	private actionsList: HTMLElement;
E
Erich Gamma 已提交
381

B
Benjamin Pasero 已提交
382
	private toDispose: lifecycle.IDisposable[];
E
Erich Gamma 已提交
383

I
isidor 已提交
384 385 386 387 388
	private _onDidBlur = new Emitter<void>();
	private _onDidCancel = new Emitter<void>();
	private _onDidRun = new Emitter<IRunEvent>();
	private _onDidBeforeRun = new Emitter<IRunEvent>();

389
	constructor(container: HTMLElement | Builder, options: IActionBarOptions = defaultOptions) {
E
Erich Gamma 已提交
390 391 392 393 394 395
		this.options = options;
		this._context = options.context;
		this.toDispose = [];
		this._actionRunner = this.options.actionRunner;

		if (!this._actionRunner) {
B
Benjamin Pasero 已提交
396
			this._actionRunner = new ActionRunner();
E
Erich Gamma 已提交
397 398 399
			this.toDispose.push(this._actionRunner);
		}

I
isidor 已提交
400 401
		this.toDispose.push(this._actionRunner.onDidRun(e => this._onDidRun.fire(e)));
		this.toDispose.push(this._actionRunner.onDidBeforeRun(e => this._onDidBeforeRun.fire(e)));
E
Erich Gamma 已提交
402 403 404 405 406 407 408

		this.items = [];
		this.focusedItem = undefined;

		this.domNode = document.createElement('div');
		this.domNode.className = 'monaco-action-bar';

J
Joao Moreno 已提交
409 410 411 412
		if (options.animated !== false) {
			DOM.addClass(this.domNode, 'animated');
		}

J
Joao Moreno 已提交
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
		let previousKey: KeyCode;
		let nextKey: KeyCode;

		switch (this.options.orientation) {
			case ActionsOrientation.HORIZONTAL:
				previousKey = KeyCode.LeftArrow;
				nextKey = KeyCode.RightArrow;
				break;
			case ActionsOrientation.HORIZONTAL_REVERSE:
				previousKey = KeyCode.RightArrow;
				nextKey = KeyCode.LeftArrow;
				this.domNode.className += ' reverse';
				break;
			case ActionsOrientation.VERTICAL:
				previousKey = KeyCode.UpArrow;
				nextKey = KeyCode.DownArrow;
				this.domNode.className += ' vertical';
				break;
			case ActionsOrientation.VERTICAL_REVERSE:
				previousKey = KeyCode.DownArrow;
				nextKey = KeyCode.UpArrow;
				this.domNode.className += ' vertical reverse';
				break;
E
Erich Gamma 已提交
436 437
		}

438 439
		$(this.domNode).on(DOM.EventType.KEY_DOWN, (e) => {
			let event = new StandardKeyboardEvent(e as KeyboardEvent);
B
Benjamin Pasero 已提交
440
			let eventHandled = true;
E
Erich Gamma 已提交
441

J
Joao Moreno 已提交
442
			if (event.equals(previousKey)) {
E
Erich Gamma 已提交
443
				this.focusPrevious();
J
Joao Moreno 已提交
444
			} else if (event.equals(nextKey)) {
E
Erich Gamma 已提交
445
				this.focusNext();
A
Alexandru Dima 已提交
446
			} else if (event.equals(KeyCode.Escape)) {
E
Erich Gamma 已提交
447
				this.cancel();
A
Alexandru Dima 已提交
448
			} else if (event.equals(KeyCode.Enter) || event.equals(KeyCode.Space)) {
E
Erich Gamma 已提交
449 450 451 452 453
				// Nothing, just staying out of the else branch
			} else {
				eventHandled = false;
			}

B
Benjamin Pasero 已提交
454
			if (eventHandled) {
E
Erich Gamma 已提交
455 456 457 458 459
				event.preventDefault();
				event.stopPropagation();
			}
		});

460 461
		$(this.domNode).on(DOM.EventType.KEY_UP, (e) => {
			let event = new StandardKeyboardEvent(e as KeyboardEvent);
E
Erich Gamma 已提交
462

463
			// Run action on Enter/Space
A
Alexandru Dima 已提交
464
			if (event.equals(KeyCode.Enter) || event.equals(KeyCode.Space)) {
E
Erich Gamma 已提交
465 466 467 468
				this.doTrigger(event);
				event.preventDefault();
				event.stopPropagation();
			}
469 470

			// Recompute focused item
A
Alexandru Dima 已提交
471
			else if (event.equals(KeyCode.Tab) || event.equals(KeyMod.Shift | KeyCode.Tab)) {
472 473
				this.updateFocusedItem();
			}
E
Erich Gamma 已提交
474 475
		});

476
		this.focusTracker = DOM.trackFocus(this.domNode);
477
		this.toDispose.push(this.focusTracker.onDidBlur(() => {
B
Benjamin Pasero 已提交
478
			if (document.activeElement === this.domNode || !DOM.isAncestor(document.activeElement, this.domNode)) {
I
isidor 已提交
479
				this._onDidBlur.fire();
480 481
				this.focusedItem = undefined;
			}
482
		}));
483

484
		this.toDispose.push(this.focusTracker.onDidFocus(() => this.updateFocusedItem()));
E
Erich Gamma 已提交
485 486 487

		this.actionsList = document.createElement('ul');
		this.actionsList.className = 'actions-container';
A
Alex Dima 已提交
488 489 490 491 492
		if (this.options.isMenu) {
			this.actionsList.setAttribute('role', 'menubar');
		} else {
			this.actionsList.setAttribute('role', 'toolbar');
		}
493 494 495 496
		if (this.options.ariaLabel) {
			this.actionsList.setAttribute('aria-label', this.options.ariaLabel);
		}

E
Erich Gamma 已提交
497 498
		this.domNode.appendChild(this.actionsList);

499
		((container instanceof Builder) ? container.getHTMLElement() : container).appendChild(this.domNode);
E
Erich Gamma 已提交
500 501
	}

I
isidor 已提交
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
	public get onDidBlur(): Event<void> {
		return this._onDidBlur.event;
	}

	public get onDidCancel(): Event<void> {
		return this._onDidCancel.event;
	}

	public get onDidRun(): Event<IRunEvent> {
		return this._onDidRun.event;
	}

	public get onDidBeforeRun(): Event<IRunEvent> {
		return this._onDidBeforeRun.event;
	}

518 519 520
	public setAriaLabel(label: string): void {
		if (label) {
			this.actionsList.setAttribute('aria-label', label);
B
Benjamin Pasero 已提交
521
		} else {
522 523 524 525
			this.actionsList.removeAttribute('aria-label');
		}
	}

526 527 528 529 530 531 532 533 534 535
	private updateFocusedItem(): void {
		for (let i = 0; i < this.actionsList.children.length; i++) {
			let elem = this.actionsList.children[i];
			if (DOM.isAncestor(document.activeElement, elem)) {
				this.focusedItem = i;
				break;
			}
		}
	}

E
Erich Gamma 已提交
536 537 538 539 540 541 542 543 544
	public get context(): any {
		return this._context;
	}

	public set context(context: any) {
		this._context = context;
		this.items.forEach(i => i.setActionContext(context));
	}

B
Benjamin Pasero 已提交
545
	public get actionRunner(): IActionRunner {
E
Erich Gamma 已提交
546 547 548
		return this._actionRunner;
	}

B
Benjamin Pasero 已提交
549
	public set actionRunner(actionRunner: IActionRunner) {
E
Erich Gamma 已提交
550 551 552 553 554 555
		if (actionRunner) {
			this._actionRunner = actionRunner;
			this.items.forEach(item => item.actionRunner = actionRunner);
		}
	}

B
Benjamin Pasero 已提交
556
	public getContainer(): Builder {
E
Erich Gamma 已提交
557 558 559
		return $(this.domNode);
	}

560 561 562
	public push(arg: IAction | IAction[], options: IActionOptions = {}): void {

		const actions: IAction[] = !Array.isArray(arg) ? [arg] : arg;
E
Erich Gamma 已提交
563

B
Benjamin Pasero 已提交
564
		let index = types.isNumber(options.index) ? options.index : null;
E
Erich Gamma 已提交
565

B
Benjamin Pasero 已提交
566
		actions.forEach((action: IAction) => {
567
			const actionItemElement = document.createElement('li');
E
Erich Gamma 已提交
568 569 570
			actionItemElement.className = 'action-item';
			actionItemElement.setAttribute('role', 'presentation');

571
			// Prevent native context menu on actions
I
isidor 已提交
572
			$(actionItemElement).on(DOM.EventType.CONTEXT_MENU, (e: DOM.EventLike) => {
573 574 575 576
				e.preventDefault();
				e.stopPropagation();
			});

B
Benjamin Pasero 已提交
577
			let item: IActionItem = null;
E
Erich Gamma 已提交
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600

			if (this.options.actionItemProvider) {
				item = this.options.actionItemProvider(action);
			}

			if (!item) {
				item = new ActionItem(this.context, action, options);
			}

			item.actionRunner = this._actionRunner;
			item.setActionContext(this.context);
			item.render(actionItemElement);

			if (index === null || index < 0 || index >= this.actionsList.children.length) {
				this.actionsList.appendChild(actionItemElement);
			} else {
				this.actionsList.insertBefore(actionItemElement, this.actionsList.children[index++]);
			}

			this.items.push(item);
		});
	}

601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
	public getWidth(index: number): number {
		if (index >= 0 && index < this.actionsList.children.length) {
			return this.actionsList.children.item(index).clientWidth;
		}

		return 0;
	}

	public getHeight(index: number): number {
		if (index >= 0 && index < this.actionsList.children.length) {
			return this.actionsList.children.item(index).clientHeight;
		}

		return 0;
	}

P
Pine Wu 已提交
617
	public pull(index: number): void {
P
Pine Wu 已提交
618
		if (index >= 0 && index < this.items.length) {
P
Pine Wu 已提交
619 620 621 622 623
			this.items.splice(index, 1);
			this.actionsList.removeChild(this.actionsList.childNodes[index]);
		}
	}

B
Benjamin Pasero 已提交
624
	public clear(): void {
625
		this.items = lifecycle.dispose(this.items);
E
Erich Gamma 已提交
626 627 628
		$(this.actionsList).empty();
	}

B
Benjamin Pasero 已提交
629
	public length(): number {
E
Erich Gamma 已提交
630 631 632
		return this.items.length;
	}

B
Benjamin Pasero 已提交
633
	public isEmpty(): boolean {
E
Erich Gamma 已提交
634 635 636
		return this.items.length === 0;
	}

B
Benjamin Pasero 已提交
637
	public focus(selectFirst?: boolean): void {
E
Erich Gamma 已提交
638 639 640 641 642 643 644
		if (selectFirst && typeof this.focusedItem === 'undefined') {
			this.focusedItem = 0;
		}

		this.updateFocus();
	}

B
Benjamin Pasero 已提交
645
	private focusNext(): void {
E
Erich Gamma 已提交
646 647 648 649
		if (typeof this.focusedItem === 'undefined') {
			this.focusedItem = this.items.length - 1;
		}

B
Benjamin Pasero 已提交
650 651
		let startIndex = this.focusedItem;
		let item: IActionItem;
E
Erich Gamma 已提交
652 653 654 655 656 657 658 659 660 661 662 663 664

		do {
			this.focusedItem = (this.focusedItem + 1) % this.items.length;
			item = this.items[this.focusedItem];
		} while (this.focusedItem !== startIndex && !item.isEnabled());

		if (this.focusedItem === startIndex && !item.isEnabled()) {
			this.focusedItem = undefined;
		}

		this.updateFocus();
	}

B
Benjamin Pasero 已提交
665
	private focusPrevious(): void {
E
Erich Gamma 已提交
666 667 668 669
		if (typeof this.focusedItem === 'undefined') {
			this.focusedItem = 0;
		}

B
Benjamin Pasero 已提交
670 671
		let startIndex = this.focusedItem;
		let item: IActionItem;
E
Erich Gamma 已提交
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686

		do {
			this.focusedItem = this.focusedItem - 1;

			if (this.focusedItem < 0) {
				this.focusedItem = this.items.length - 1;
			}

			item = this.items[this.focusedItem];
		} while (this.focusedItem !== startIndex && !item.isEnabled());

		if (this.focusedItem === startIndex && !item.isEnabled()) {
			this.focusedItem = undefined;
		}

I
isidor 已提交
687
		this.updateFocus(true);
E
Erich Gamma 已提交
688 689
	}

I
isidor 已提交
690
	private updateFocus(fromRight?: boolean): void {
E
Erich Gamma 已提交
691 692 693 694 695
		if (typeof this.focusedItem === 'undefined') {
			this.domNode.focus();
			return;
		}

B
Benjamin Pasero 已提交
696 697
		for (let i = 0; i < this.items.length; i++) {
			let item = this.items[i];
E
Erich Gamma 已提交
698

B
Benjamin Pasero 已提交
699
			let actionItem = <any>item;
E
Erich Gamma 已提交
700

B
Benjamin Pasero 已提交
701 702
			if (i === this.focusedItem) {
				if (types.isFunction(actionItem.focus)) {
I
isidor 已提交
703
					actionItem.focus(fromRight);
E
Erich Gamma 已提交
704 705
				}
			} else {
B
Benjamin Pasero 已提交
706
				if (types.isFunction(actionItem.blur)) {
E
Erich Gamma 已提交
707 708 709 710 711 712
					actionItem.blur();
				}
			}
		}
	}

B
Benjamin Pasero 已提交
713
	private doTrigger(event: StandardKeyboardEvent): void {
B
Benjamin Pasero 已提交
714
		if (typeof this.focusedItem === 'undefined') {
715
			return; //nothing to focus
E
Erich Gamma 已提交
716 717 718
		}

		// trigger action
I
isidor 已提交
719 720 721 722 723
		let actionItem = this.items[this.focusedItem];
		if (actionItem instanceof BaseActionItem) {
			const context = (actionItem._context === null || actionItem._context === undefined) ? event : actionItem._context;
			this.run(actionItem._action, context).done();
		}
E
Erich Gamma 已提交
724 725
	}

B
Benjamin Pasero 已提交
726
	private cancel(): void {
B
Benjamin Pasero 已提交
727
		if (document.activeElement instanceof HTMLElement) {
728
			(<HTMLElement>document.activeElement).blur(); // remove focus from focused action
B
Benjamin Pasero 已提交
729 730
		}

I
isidor 已提交
731
		this._onDidCancel.fire();
E
Erich Gamma 已提交
732 733
	}

734
	public run(action: IAction, context?: any): TPromise<void> {
E
Erich Gamma 已提交
735 736 737
		return this._actionRunner.run(action, context);
	}

B
Benjamin Pasero 已提交
738
	public dispose(): void {
E
Erich Gamma 已提交
739
		if (this.items !== null) {
740
			lifecycle.dispose(this.items);
E
Erich Gamma 已提交
741 742 743
		}
		this.items = null;

744 745 746 747 748
		if (this.focusTracker) {
			this.focusTracker.dispose();
			this.focusTracker = null;
		}

J
Joao Moreno 已提交
749
		this.toDispose = lifecycle.dispose(this.toDispose);
E
Erich Gamma 已提交
750 751 752 753 754 755

		this.getContainer().destroy();
	}
}

export class SelectActionItem extends BaseActionItem {
756
	protected selectBox: SelectBox;
I
isidor 已提交
757
	protected toDispose: lifecycle.IDisposable[];
E
Erich Gamma 已提交
758

759 760
	constructor(ctx: any, action: IAction, options: string[], selected: number, contextViewProvider: IContextViewProvider
	) {
E
Erich Gamma 已提交
761
		super(ctx, action);
762
		this.selectBox = new SelectBox(options, selected, contextViewProvider);
E
Erich Gamma 已提交
763 764

		this.toDispose = [];
I
isidor 已提交
765
		this.toDispose.push(this.selectBox);
E
Erich Gamma 已提交
766 767 768
		this.registerListeners();
	}

S
Sandeep Somavarapu 已提交
769 770
	public setOptions(options: string[], selected?: number, disabled?: number): void {
		this.selectBox.setOptions(options, selected, disabled);
E
Erich Gamma 已提交
771 772
	}

I
isidor 已提交
773 774 775 776
	public select(index: number): void {
		this.selectBox.select(index);
	}

E
Erich Gamma 已提交
777
	private registerListeners(): void {
778 779
		this.toDispose.push(this.selectBox.onDidSelect(e => {
			this.actionRunner.run(this._action, this.getActionContext(e.selected)).done();
E
Erich Gamma 已提交
780 781 782
		}));
	}

I
isidor 已提交
783 784 785 786
	protected getActionContext(option: string) {
		return option;
	}

787
	public focus(): void {
I
isidor 已提交
788 789
		if (this.selectBox) {
			this.selectBox.focus();
790 791 792 793
		}
	}

	public blur(): void {
I
isidor 已提交
794 795
		if (this.selectBox) {
			this.selectBox.blur();
796 797 798
		}
	}

B
Benjamin Pasero 已提交
799
	public render(container: HTMLElement): void {
I
isidor 已提交
800
		this.selectBox.render(container);
E
Erich Gamma 已提交
801 802 803
	}

	public dispose(): void {
J
Joao Moreno 已提交
804
		this.toDispose = lifecycle.dispose(this.toDispose);
E
Erich Gamma 已提交
805 806 807

		super.dispose();
	}
808
}