actionbar.ts 17.8 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6 7 8 9
/*---------------------------------------------------------------------------------------------
 *  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';
import nls = require('vs/nls');
B
Benjamin Pasero 已提交
10
import lifecycle = require('vs/base/common/lifecycle');
J
Johannes Rieken 已提交
11 12 13 14
import { Promise } from 'vs/base/common/winjs.base';
import { Builder, $ } from 'vs/base/browser/builder';
import { SelectBox } from 'vs/base/browser/ui/selectBox/selectBox';
import { IAction, IActionRunner, Action, IActionChangeEvent, ActionRunner } from 'vs/base/common/actions';
B
Benjamin Pasero 已提交
15
import DOM = require('vs/base/browser/dom');
J
Johannes Rieken 已提交
16
import { EventType as CommonEventType } from 'vs/base/common/events';
B
Benjamin Pasero 已提交
17
import types = require('vs/base/common/types');
J
Johannes Rieken 已提交
18 19 20 21
import { IEventEmitter, EventEmitter } from 'vs/base/common/eventEmitter';
import { Gesture, EventType } from 'vs/base/browser/touch';
import { StandardKeyboardEvent } from 'vs/base/browser/keyboardEvent';
import { KeyCode, KeyMod } from 'vs/base/common/keyCodes';
E
Erich Gamma 已提交
22

B
Benjamin Pasero 已提交
23
export interface IActionItem extends IEventEmitter {
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 35 36
export interface IBaseActionItemOptions {
	draggable?: boolean;
}

B
Benjamin Pasero 已提交
37
export class BaseActionItem extends EventEmitter implements IActionItem {
E
Erich Gamma 已提交
38

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

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

47
	constructor(context: any, action: IAction, protected options?: IBaseActionItemOptions) {
E
Erich Gamma 已提交
48 49 50 51 52 53
		super();

		this._callOnDispose = [];
		this._context = context || this;
		this._action = action;

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

66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
	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 已提交
82 83 84 85 86 87 88
		}
	}

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

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

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

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

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

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

B
Benjamin Pasero 已提交
109
	public render(container: HTMLElement): void {
E
Erich Gamma 已提交
110
		this.builder = $(container);
B
Benjamin Pasero 已提交
111
		this.gesture = new Gesture(container);
E
Erich Gamma 已提交
112

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

B
Benjamin Pasero 已提交
118
		this.builder.on(EventType.Tap, e => this.onClick(e));
E
Erich Gamma 已提交
119

B
Benjamin Pasero 已提交
120
		this.builder.on(DOM.EventType.MOUSE_DOWN, (e: MouseEvent) => {
121 122 123 124
			if (!enableDragging) {
				DOM.EventHelper.stop(e); // do not run when dragging is on because that would disable it
			}

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

J
Johannes Rieken 已提交
130
		this.builder.on(DOM.EventType.CLICK, (e: MouseEvent) => {
J
Johannes Rieken 已提交
131
			DOM.EventHelper.stop(e, true);
J
Johannes Rieken 已提交
132 133
			setTimeout(() => this.onClick(e), 50);
		});
E
Erich Gamma 已提交
134

B
Benjamin Pasero 已提交
135 136
		this.builder.on([DOM.EventType.MOUSE_UP, DOM.EventType.MOUSE_OUT], (e: MouseEvent) => {
			DOM.EventHelper.stop(e);
J
Johannes Rieken 已提交
137
			this.builder.removeClass('active');
E
Erich Gamma 已提交
138 139 140
		});
	}

B
Benjamin Pasero 已提交
141 142
	public onClick(event: Event): void {
		DOM.EventHelper.stop(event, true);
J
Joao Moreno 已提交
143

144 145 146 147 148 149 150 151
		let context: any;
		if (types.isUndefinedOrNull(this._context)) {
			context = event;
		} else {
			context = this._context;
			context.event = event;
		}

152
		this._actionRunner.run(this._action, context);
E
Erich Gamma 已提交
153 154
	}

B
Benjamin Pasero 已提交
155
	public focus(): void {
156 157 158
		if (this.builder) {
			this.builder.domFocus();
		}
E
Erich Gamma 已提交
159 160
	}

B
Benjamin Pasero 已提交
161
	public blur(): void {
162
		if (this.builder) {
163
			this.builder.domBlur();
164
		}
E
Erich Gamma 已提交
165 166
	}

167
	protected _updateEnabled(): void {
E
Erich Gamma 已提交
168 169 170
		// implement in subclass
	}

171
	protected _updateLabel(): void {
E
Erich Gamma 已提交
172 173 174
		// implement in subclass
	}

175
	protected _updateTooltip(): void {
E
Erich Gamma 已提交
176 177 178
		// implement in subclass
	}

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

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

B
Benjamin Pasero 已提交
187
	public dispose(): void {
E
Erich Gamma 已提交
188 189 190 191 192 193 194 195 196 197 198 199
		super.dispose();

		if (this.builder) {
			this.builder.destroy();
			this.builder = null;
		}

		if (this.gesture) {
			this.gesture.dispose();
			this.gesture = null;
		}

A
Alex Dima 已提交
200
		this._callOnDispose = lifecycle.dispose(this._callOnDispose);
E
Erich Gamma 已提交
201 202 203
	}
}

B
Benjamin Pasero 已提交
204
export class Separator extends Action {
E
Erich Gamma 已提交
205

B
Benjamin Pasero 已提交
206
	public static ID = 'vs.actions.separator';
E
Erich Gamma 已提交
207

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

217
export interface IActionItemOptions extends IBaseActionItemOptions {
B
Benjamin Pasero 已提交
218 219 220
	icon?: boolean;
	label?: boolean;
	keybinding?: string;
E
Erich Gamma 已提交
221 222 223 224
}

export class ActionItem extends BaseActionItem {

J
Johannes Rieken 已提交
225 226
	protected $e: Builder;
	protected options: IActionItemOptions;
B
Benjamin Pasero 已提交
227
	private cssClass: string;
E
Erich Gamma 已提交
228

B
Benjamin Pasero 已提交
229
	constructor(context: any, action: IAction, options: IActionItemOptions = {}) {
230
		super(context, action, options);
E
Erich Gamma 已提交
231 232 233 234 235 236 237

		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 已提交
238
	public render(container: HTMLElement): void {
E
Erich Gamma 已提交
239 240
		super.render(container);

241
		this.$e = $('a.action-label').appendTo(this.builder);
242
		this.$e.attr({ role: 'button' });
E
Erich Gamma 已提交
243 244 245 246 247 248 249 250 251 252 253 254

		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 已提交
255
	public focus(): void {
E
Erich Gamma 已提交
256 257 258 259
		super.focus();
		this.$e.domFocus();
	}

B
Benjamin Pasero 已提交
260
	public _updateLabel(): void {
E
Erich Gamma 已提交
261 262 263 264 265
		if (this.options.label) {
			this.$e.text(this.getAction().label);
		}
	}

B
Benjamin Pasero 已提交
266
	public _updateTooltip(): void {
B
Benjamin Pasero 已提交
267
		let title: string = null;
E
Erich Gamma 已提交
268 269 270 271 272 273 274 275

		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 已提交
276
				title = nls.localize({ key: 'titleLabel', comment: ['action title', 'action keybinding'] }, "{0} ({1})", title, this.options.keybinding);
E
Erich Gamma 已提交
277 278 279 280 281 282 283 284
			}
		}

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

B
Benjamin Pasero 已提交
285
	public _updateClass(): void {
E
Erich Gamma 已提交
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
		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 已提交
301 302
	public _updateEnabled(): void {
		if (this.getAction().enabled) {
E
Erich Gamma 已提交
303 304
			this.builder.removeClass('disabled');
			this.$e.removeClass('disabled');
305
			this.$e.attr({ tabindex: 0 });
E
Erich Gamma 已提交
306 307 308
		} else {
			this.builder.addClass('disabled');
			this.$e.addClass('disabled');
309
			DOM.removeTabIndexAndUpdateFocus(this.$e.getHTMLElement());
E
Erich Gamma 已提交
310 311 312
		}
	}

B
Benjamin Pasero 已提交
313 314
	public _updateChecked(): void {
		if (this.getAction().checked) {
E
Erich Gamma 已提交
315 316 317 318 319
			this.$e.addClass('checked');
		} else {
			this.$e.removeClass('checked');
		}
	}
320 321 322 323 324 325 326 327

	public _updateRadio(): void {
		if (this.getAction().radio) {
			this.$e.addClass('radio');
		} else {
			this.$e.removeClass('radio');
		}
	}
E
Erich Gamma 已提交
328 329 330 331 332 333 334 335
}

export enum ActionsOrientation {
	HORIZONTAL = 1,
	VERTICAL = 2
}

export interface IActionItemProvider {
B
Benjamin Pasero 已提交
336
	(action: IAction): IActionItem;
E
Erich Gamma 已提交
337 338 339
}

export interface IActionBarOptions {
B
Benjamin Pasero 已提交
340 341 342
	orientation?: ActionsOrientation;
	context?: any;
	actionItemProvider?: IActionItemProvider;
B
Benjamin Pasero 已提交
343
	actionRunner?: IActionRunner;
344
	ariaLabel?: string;
J
Joao Moreno 已提交
345
	animated?: boolean;
E
Erich Gamma 已提交
346 347
}

B
Benjamin Pasero 已提交
348
let defaultOptions: IActionBarOptions = {
E
Erich Gamma 已提交
349 350 351 352 353
	orientation: ActionsOrientation.HORIZONTAL,
	context: null
};

export interface IActionOptions extends IActionItemOptions {
B
Benjamin Pasero 已提交
354
	index?: number;
E
Erich Gamma 已提交
355 356
}

B
Benjamin Pasero 已提交
357
export class ActionBar extends EventEmitter implements IActionRunner {
E
Erich Gamma 已提交
358

B
Benjamin Pasero 已提交
359
	public options: IActionBarOptions;
360

B
Benjamin Pasero 已提交
361
	private _actionRunner: IActionRunner;
E
Erich Gamma 已提交
362 363 364
	private _context: any;

	// Items
B
Benjamin Pasero 已提交
365
	public items: IActionItem[];
366

B
Benjamin Pasero 已提交
367
	private focusedItem: number;
368
	private focusTracker: DOM.IFocusTracker;
E
Erich Gamma 已提交
369 370

	// Elements
B
Benjamin Pasero 已提交
371 372
	public domNode: HTMLElement;
	private actionsList: HTMLElement;
E
Erich Gamma 已提交
373

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

376
	constructor(container: HTMLElement | Builder, options: IActionBarOptions = defaultOptions) {
E
Erich Gamma 已提交
377 378 379 380 381 382 383
		super();
		this.options = options;
		this._context = options.context;
		this.toDispose = [];
		this._actionRunner = this.options.actionRunner;

		if (!this._actionRunner) {
B
Benjamin Pasero 已提交
384
			this._actionRunner = new ActionRunner();
E
Erich Gamma 已提交
385 386 387
			this.toDispose.push(this._actionRunner);
		}

A
Alex Dima 已提交
388
		this.toDispose.push(this.addEmitter(this._actionRunner));
E
Erich Gamma 已提交
389 390 391 392 393 394 395

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

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

J
Joao Moreno 已提交
396 397 398 399
		if (options.animated !== false) {
			DOM.addClass(this.domNode, 'animated');
		}

B
Benjamin Pasero 已提交
400
		let isVertical = this.options.orientation === ActionsOrientation.VERTICAL;
E
Erich Gamma 已提交
401 402 403 404
		if (isVertical) {
			this.domNode.className += ' vertical';
		}

B
Benjamin Pasero 已提交
405
		$(this.domNode).on(DOM.EventType.KEY_DOWN, (e: KeyboardEvent) => {
B
Benjamin Pasero 已提交
406 407
			let event = new StandardKeyboardEvent(e);
			let eventHandled = true;
E
Erich Gamma 已提交
408

A
Alexandru Dima 已提交
409
			if (event.equals(isVertical ? KeyCode.UpArrow : KeyCode.LeftArrow)) {
E
Erich Gamma 已提交
410
				this.focusPrevious();
A
Alexandru Dima 已提交
411
			} else if (event.equals(isVertical ? KeyCode.DownArrow : KeyCode.RightArrow)) {
E
Erich Gamma 已提交
412
				this.focusNext();
A
Alexandru Dima 已提交
413
			} else if (event.equals(KeyCode.Escape)) {
E
Erich Gamma 已提交
414
				this.cancel();
A
Alexandru Dima 已提交
415
			} else if (event.equals(KeyCode.Enter) || event.equals(KeyCode.Space)) {
E
Erich Gamma 已提交
416 417 418 419 420
				// Nothing, just staying out of the else branch
			} else {
				eventHandled = false;
			}

B
Benjamin Pasero 已提交
421
			if (eventHandled) {
E
Erich Gamma 已提交
422 423 424 425 426 427
				event.preventDefault();
				event.stopPropagation();
			}
		});

		// Prevent native context menu on actions
B
Benjamin Pasero 已提交
428
		$(this.domNode).on(DOM.EventType.CONTEXT_MENU, (e: Event) => {
E
Erich Gamma 已提交
429 430 431 432
			e.preventDefault();
			e.stopPropagation();
		});

B
Benjamin Pasero 已提交
433
		$(this.domNode).on(DOM.EventType.KEY_UP, (e: KeyboardEvent) => {
B
Benjamin Pasero 已提交
434
			let event = new StandardKeyboardEvent(e);
E
Erich Gamma 已提交
435

436
			// Run action on Enter/Space
A
Alexandru Dima 已提交
437
			if (event.equals(KeyCode.Enter) || event.equals(KeyCode.Space)) {
E
Erich Gamma 已提交
438 439 440 441
				this.doTrigger(event);
				event.preventDefault();
				event.stopPropagation();
			}
442 443

			// Recompute focused item
A
Alexandru Dima 已提交
444
			else if (event.equals(KeyCode.Tab) || event.equals(KeyMod.Shift | KeyCode.Tab)) {
445 446
				this.updateFocusedItem();
			}
E
Erich Gamma 已提交
447 448
		});

449
		this.focusTracker = DOM.trackFocus(this.domNode);
450
		this.focusTracker.addBlurListener(() => {
B
Benjamin Pasero 已提交
451
			if (document.activeElement === this.domNode || !DOM.isAncestor(document.activeElement, this.domNode)) {
452
				this.emit(DOM.EventType.BLUR, {});
453 454 455 456
				this.focusedItem = undefined;
			}
		});

457
		this.focusTracker.addFocusListener(() => this.updateFocusedItem());
E
Erich Gamma 已提交
458 459 460

		this.actionsList = document.createElement('ul');
		this.actionsList.className = 'actions-container';
461 462 463 464 465
		this.actionsList.setAttribute('role', 'toolbar');
		if (this.options.ariaLabel) {
			this.actionsList.setAttribute('aria-label', this.options.ariaLabel);
		}

E
Erich Gamma 已提交
466 467
		this.domNode.appendChild(this.actionsList);

468
		((container instanceof Builder) ? container.getHTMLElement() : container).appendChild(this.domNode);
E
Erich Gamma 已提交
469 470
	}

471 472 473
	public setAriaLabel(label: string): void {
		if (label) {
			this.actionsList.setAttribute('aria-label', label);
B
Benjamin Pasero 已提交
474
		} else {
475 476 477 478
			this.actionsList.removeAttribute('aria-label');
		}
	}

479 480 481 482 483 484 485 486 487 488
	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 已提交
489 490 491 492 493 494 495 496 497
	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 已提交
498
	public get actionRunner(): IActionRunner {
E
Erich Gamma 已提交
499 500 501
		return this._actionRunner;
	}

B
Benjamin Pasero 已提交
502
	public set actionRunner(actionRunner: IActionRunner) {
E
Erich Gamma 已提交
503 504 505 506 507 508
		if (actionRunner) {
			this._actionRunner = actionRunner;
			this.items.forEach(item => item.actionRunner = actionRunner);
		}
	}

B
Benjamin Pasero 已提交
509
	public getContainer(): Builder {
E
Erich Gamma 已提交
510 511 512
		return $(this.domNode);
	}

513 514 515
	public push(arg: IAction | IAction[], options: IActionOptions = {}): void {

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

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

B
Benjamin Pasero 已提交
519
		actions.forEach((action: IAction) => {
520
			const actionItemElement = document.createElement('li');
E
Erich Gamma 已提交
521 522 523
			actionItemElement.className = 'action-item';
			actionItemElement.setAttribute('role', 'presentation');

B
Benjamin Pasero 已提交
524
			let item: IActionItem = null;
E
Erich Gamma 已提交
525 526 527 528 529 530 531 532 533 534 535

			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);
A
Alex Dima 已提交
536
			this.addEmitter(item);
E
Erich Gamma 已提交
537 538 539 540 541 542 543 544 545 546 547 548
			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);
		});
	}

P
Pine Wu 已提交
549
	public pull(index: number): void {
P
Pine Wu 已提交
550
		if (index >= 0 && index < this.items.length) {
P
Pine Wu 已提交
551 552 553 554 555
			this.items.splice(index, 1);
			this.actionsList.removeChild(this.actionsList.childNodes[index]);
		}
	}

B
Benjamin Pasero 已提交
556
	public clear(): void {
557
		this.items = lifecycle.dispose(this.items);
E
Erich Gamma 已提交
558 559 560
		$(this.actionsList).empty();
	}

B
Benjamin Pasero 已提交
561
	public length(): number {
E
Erich Gamma 已提交
562 563 564
		return this.items.length;
	}

B
Benjamin Pasero 已提交
565
	public isEmpty(): boolean {
E
Erich Gamma 已提交
566 567 568
		return this.items.length === 0;
	}

B
Benjamin Pasero 已提交
569
	public focus(selectFirst?: boolean): void {
E
Erich Gamma 已提交
570 571 572 573 574 575 576
		if (selectFirst && typeof this.focusedItem === 'undefined') {
			this.focusedItem = 0;
		}

		this.updateFocus();
	}

B
Benjamin Pasero 已提交
577
	private focusNext(): void {
E
Erich Gamma 已提交
578 579 580 581
		if (typeof this.focusedItem === 'undefined') {
			this.focusedItem = this.items.length - 1;
		}

B
Benjamin Pasero 已提交
582 583
		let startIndex = this.focusedItem;
		let item: IActionItem;
E
Erich Gamma 已提交
584 585 586 587 588 589 590 591 592 593 594 595 596

		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 已提交
597
	private focusPrevious(): void {
E
Erich Gamma 已提交
598 599 600 601
		if (typeof this.focusedItem === 'undefined') {
			this.focusedItem = 0;
		}

B
Benjamin Pasero 已提交
602 603
		let startIndex = this.focusedItem;
		let item: IActionItem;
E
Erich Gamma 已提交
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618

		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 已提交
619
		this.updateFocus(true);
E
Erich Gamma 已提交
620 621
	}

I
isidor 已提交
622
	private updateFocus(fromRight?: boolean): void {
E
Erich Gamma 已提交
623 624 625 626 627
		if (typeof this.focusedItem === 'undefined') {
			this.domNode.focus();
			return;
		}

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

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

B
Benjamin Pasero 已提交
633 634
			if (i === this.focusedItem) {
				if (types.isFunction(actionItem.focus)) {
I
isidor 已提交
635
					actionItem.focus(fromRight);
E
Erich Gamma 已提交
636 637
				}
			} else {
B
Benjamin Pasero 已提交
638
				if (types.isFunction(actionItem.blur)) {
E
Erich Gamma 已提交
639 640 641 642 643 644
					actionItem.blur();
				}
			}
		}
	}

B
Benjamin Pasero 已提交
645
	private doTrigger(event: StandardKeyboardEvent): void {
B
Benjamin Pasero 已提交
646
		if (typeof this.focusedItem === 'undefined') {
647
			return; //nothing to focus
E
Erich Gamma 已提交
648 649 650
		}

		// trigger action
I
isidor 已提交
651 652 653 654 655
		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 已提交
656 657
	}

B
Benjamin Pasero 已提交
658
	private cancel(): void {
B
Benjamin Pasero 已提交
659 660 661 662
		if (document.activeElement instanceof HTMLElement) {
			(<HTMLElement>document.activeElement).blur(); // remove focus from focussed action
		}

B
Benjamin Pasero 已提交
663
		this.emit(CommonEventType.CANCEL);
E
Erich Gamma 已提交
664 665
	}

B
Benjamin Pasero 已提交
666
	public run(action: IAction, context?: any): Promise {
E
Erich Gamma 已提交
667 668 669
		return this._actionRunner.run(action, context);
	}

B
Benjamin Pasero 已提交
670
	public dispose(): void {
E
Erich Gamma 已提交
671
		if (this.items !== null) {
672
			lifecycle.dispose(this.items);
E
Erich Gamma 已提交
673 674 675
		}
		this.items = null;

676 677 678 679 680
		if (this.focusTracker) {
			this.focusTracker.dispose();
			this.focusTracker = null;
		}

J
Joao Moreno 已提交
681
		this.toDispose = lifecycle.dispose(this.toDispose);
E
Erich Gamma 已提交
682 683 684 685 686 687 688 689

		this.getContainer().destroy();

		super.dispose();
	}
}

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

B
Benjamin Pasero 已提交
693
	constructor(ctx: any, action: IAction, options: string[], selected: number) {
E
Erich Gamma 已提交
694
		super(ctx, action);
I
isidor 已提交
695
		this.selectBox = new SelectBox(options, selected);
E
Erich Gamma 已提交
696 697

		this.toDispose = [];
I
isidor 已提交
698
		this.toDispose.push(this.selectBox);
E
Erich Gamma 已提交
699 700 701
		this.registerListeners();
	}

702
	public setOptions(options: string[], selected?: number): void {
I
isidor 已提交
703
		this.selectBox.setOptions(options, selected);
E
Erich Gamma 已提交
704 705
	}

I
isidor 已提交
706 707 708 709
	public select(index: number): void {
		this.selectBox.select(index);
	}

E
Erich Gamma 已提交
710
	private registerListeners(): void {
I
isidor 已提交
711 712
		this.toDispose.push(this.selectBox.onDidSelect(selected => {
			this.actionRunner.run(this._action, this.getActionContext(selected)).done();
E
Erich Gamma 已提交
713 714 715
		}));
	}

I
isidor 已提交
716 717 718 719
	protected getActionContext(option: string) {
		return option;
	}

720
	public focus(): void {
I
isidor 已提交
721 722
		if (this.selectBox) {
			this.selectBox.focus();
723 724 725 726
		}
	}

	public blur(): void {
I
isidor 已提交
727 728
		if (this.selectBox) {
			this.selectBox.blur();
729 730 731
		}
	}

B
Benjamin Pasero 已提交
732
	public render(container: HTMLElement): void {
I
isidor 已提交
733
		this.selectBox.render(container);
E
Erich Gamma 已提交
734 735 736
	}

	public dispose(): void {
J
Joao Moreno 已提交
737
		this.toDispose = lifecycle.dispose(this.toDispose);
E
Erich Gamma 已提交
738 739 740 741

		super.dispose();
	}
}