actionbar.ts 17.3 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
import { Promise } from 'vs/base/common/winjs.base';
import { Builder, $ } from 'vs/base/browser/builder';
import { SelectBox } from 'vs/base/browser/ui/selectBox/selectBox';
14
import platform = require('vs/base/common/platform');
J
Johannes Rieken 已提交
15
import { IAction, IActionRunner, Action, IActionChangeEvent, ActionRunner } from 'vs/base/common/actions';
B
Benjamin Pasero 已提交
16
import DOM = require('vs/base/browser/dom');
J
Johannes Rieken 已提交
17
import { EventType as CommonEventType } from 'vs/base/common/events';
B
Benjamin Pasero 已提交
18
import types = require('vs/base/common/types');
J
Johannes Rieken 已提交
19 20 21 22
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 已提交
23

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

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

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

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

B
Benjamin Pasero 已提交
44
	constructor(context: any, action: IAction) {
E
Erich Gamma 已提交
45 46 47 48 49 50
		super();

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

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

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

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

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

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

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

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

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

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

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

112
		if (platform.isMacintosh) {
B
Benjamin Pasero 已提交
113
			this.builder.on(DOM.EventType.CONTEXT_MENU, (event: Event) => this.onClick(event)); // https://github.com/Microsoft/vscode/issues/1011
114 115
		}

B
Benjamin Pasero 已提交
116 117
		this.builder.on(DOM.EventType.MOUSE_DOWN, (e: MouseEvent) => {
			DOM.EventHelper.stop(e);
J
Johannes Rieken 已提交
118
			if (this._action.enabled) {
E
Erich Gamma 已提交
119 120 121
				this.builder.addClass('active');
			}
		});
J
Johannes Rieken 已提交
122
		this.builder.on(DOM.EventType.CLICK, (e: MouseEvent) => {
J
Johannes Rieken 已提交
123
			DOM.EventHelper.stop(e, true);
J
Johannes Rieken 已提交
124 125
			setTimeout(() => this.onClick(e), 50);
		});
E
Erich Gamma 已提交
126

B
Benjamin Pasero 已提交
127 128
		this.builder.on([DOM.EventType.MOUSE_UP, DOM.EventType.MOUSE_OUT], (e: MouseEvent) => {
			DOM.EventHelper.stop(e);
J
Johannes Rieken 已提交
129
			this.builder.removeClass('active');
E
Erich Gamma 已提交
130 131 132
		});
	}

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

136 137 138 139 140 141 142 143
		let context: any;
		if (types.isUndefinedOrNull(this._context)) {
			context = event;
		} else {
			context = this._context;
			context.event = event;
		}

144
		this._actionRunner.run(this._action, context);
E
Erich Gamma 已提交
145 146
	}

B
Benjamin Pasero 已提交
147
	public focus(): void {
148 149 150
		if (this.builder) {
			this.builder.domFocus();
		}
E
Erich Gamma 已提交
151 152
	}

B
Benjamin Pasero 已提交
153
	public blur(): void {
154
		if (this.builder) {
155
			this.builder.domBlur();
156
		}
E
Erich Gamma 已提交
157 158
	}

159
	protected _updateEnabled(): void {
E
Erich Gamma 已提交
160 161 162
		// implement in subclass
	}

163
	protected _updateLabel(): void {
E
Erich Gamma 已提交
164 165 166
		// implement in subclass
	}

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

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

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

B
Benjamin Pasero 已提交
179
	public dispose(): void {
E
Erich Gamma 已提交
180 181 182 183 184 185 186 187 188 189 190 191
		super.dispose();

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

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

A
Alex Dima 已提交
192
		this._callOnDispose = lifecycle.dispose(this._callOnDispose);
E
Erich Gamma 已提交
193 194 195
	}
}

B
Benjamin Pasero 已提交
196
export class Separator extends Action {
E
Erich Gamma 已提交
197

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

B
Benjamin Pasero 已提交
200
	constructor(label?: string, order?) {
E
Erich Gamma 已提交
201 202 203 204 205 206 207 208
		super(Separator.ID, label, label ? 'separator text' : 'separator');
		this.checked = false;
		this.enabled = false;
		this.order = order;
	}
}

export interface IActionItemOptions {
B
Benjamin Pasero 已提交
209 210 211
	icon?: boolean;
	label?: boolean;
	keybinding?: string;
E
Erich Gamma 已提交
212 213 214 215
}

export class ActionItem extends BaseActionItem {

J
Johannes Rieken 已提交
216 217
	protected $e: Builder;
	protected options: IActionItemOptions;
B
Benjamin Pasero 已提交
218
	private cssClass: string;
E
Erich Gamma 已提交
219

B
Benjamin Pasero 已提交
220
	constructor(context: any, action: IAction, options: IActionItemOptions = {}) {
E
Erich Gamma 已提交
221 222 223 224 225 226 227 228
		super(context, action);

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

232
		this.$e = $('a.action-label').appendTo(this.builder);
233
		this.$e.attr({ role: 'button' });
E
Erich Gamma 已提交
234 235 236 237 238 239 240 241 242 243 244 245

		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 已提交
246
	public focus(): void {
E
Erich Gamma 已提交
247 248 249 250
		super.focus();
		this.$e.domFocus();
	}

B
Benjamin Pasero 已提交
251
	public _updateLabel(): void {
E
Erich Gamma 已提交
252 253 254 255 256
		if (this.options.label) {
			this.$e.text(this.getAction().label);
		}
	}

B
Benjamin Pasero 已提交
257
	public _updateTooltip(): void {
B
Benjamin Pasero 已提交
258
		let title: string = null;
E
Erich Gamma 已提交
259 260 261 262 263 264 265 266

		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 已提交
267
				title = nls.localize({ key: 'titleLabel', comment: ['action title', 'action keybinding'] }, "{0} ({1})", title, this.options.keybinding);
E
Erich Gamma 已提交
268 269 270 271 272 273 274 275
			}
		}

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

B
Benjamin Pasero 已提交
276
	public _updateClass(): void {
E
Erich Gamma 已提交
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
		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 已提交
292 293
	public _updateEnabled(): void {
		if (this.getAction().enabled) {
E
Erich Gamma 已提交
294 295
			this.builder.removeClass('disabled');
			this.$e.removeClass('disabled');
296
			this.$e.attr({ tabindex: 0 });
E
Erich Gamma 已提交
297 298 299
		} else {
			this.builder.addClass('disabled');
			this.$e.addClass('disabled');
300
			DOM.removeTabIndexAndUpdateFocus(this.$e.getHTMLElement());
E
Erich Gamma 已提交
301 302 303
		}
	}

B
Benjamin Pasero 已提交
304 305
	public _updateChecked(): void {
		if (this.getAction().checked) {
E
Erich Gamma 已提交
306 307 308 309 310 311 312 313 314 315 316 317 318
			this.$e.addClass('checked');
		} else {
			this.$e.removeClass('checked');
		}
	}
}

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

export interface IActionItemProvider {
B
Benjamin Pasero 已提交
319
	(action: IAction): IActionItem;
E
Erich Gamma 已提交
320 321 322
}

export interface IActionBarOptions {
B
Benjamin Pasero 已提交
323 324 325
	orientation?: ActionsOrientation;
	context?: any;
	actionItemProvider?: IActionItemProvider;
B
Benjamin Pasero 已提交
326
	actionRunner?: IActionRunner;
327
	ariaLabel?: string;
J
Joao Moreno 已提交
328
	animated?: boolean;
E
Erich Gamma 已提交
329 330
}

B
Benjamin Pasero 已提交
331
let defaultOptions: IActionBarOptions = {
E
Erich Gamma 已提交
332 333 334 335 336
	orientation: ActionsOrientation.HORIZONTAL,
	context: null
};

export interface IActionOptions extends IActionItemOptions {
B
Benjamin Pasero 已提交
337
	index?: number;
E
Erich Gamma 已提交
338 339
}

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

B
Benjamin Pasero 已提交
342
	public options: IActionBarOptions;
343

B
Benjamin Pasero 已提交
344
	private _actionRunner: IActionRunner;
E
Erich Gamma 已提交
345 346 347
	private _context: any;

	// Items
B
Benjamin Pasero 已提交
348
	public items: IActionItem[];
349

B
Benjamin Pasero 已提交
350
	private focusedItem: number;
351
	private focusTracker: DOM.IFocusTracker;
E
Erich Gamma 已提交
352 353

	// Elements
B
Benjamin Pasero 已提交
354 355
	public domNode: HTMLElement;
	private actionsList: HTMLElement;
E
Erich Gamma 已提交
356

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

359
	constructor(container: HTMLElement | Builder, options: IActionBarOptions = defaultOptions) {
E
Erich Gamma 已提交
360 361 362 363 364 365 366
		super();
		this.options = options;
		this._context = options.context;
		this.toDispose = [];
		this._actionRunner = this.options.actionRunner;

		if (!this._actionRunner) {
B
Benjamin Pasero 已提交
367
			this._actionRunner = new ActionRunner();
E
Erich Gamma 已提交
368 369 370 371 372 373 374 375 376 377 378
			this.toDispose.push(this._actionRunner);
		}

		this.toDispose.push(this.addEmitter2(this._actionRunner));

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

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

J
Joao Moreno 已提交
379 380 381 382
		if (options.animated !== false) {
			DOM.addClass(this.domNode, 'animated');
		}

B
Benjamin Pasero 已提交
383
		let isVertical = this.options.orientation === ActionsOrientation.VERTICAL;
E
Erich Gamma 已提交
384 385 386 387
		if (isVertical) {
			this.domNode.className += ' vertical';
		}

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

A
Alexandru Dima 已提交
392
			if (event.equals(isVertical ? KeyCode.UpArrow : KeyCode.LeftArrow)) {
E
Erich Gamma 已提交
393
				this.focusPrevious();
A
Alexandru Dima 已提交
394
			} else if (event.equals(isVertical ? KeyCode.DownArrow : KeyCode.RightArrow)) {
E
Erich Gamma 已提交
395
				this.focusNext();
A
Alexandru Dima 已提交
396
			} else if (event.equals(KeyCode.Escape)) {
E
Erich Gamma 已提交
397
				this.cancel();
A
Alexandru Dima 已提交
398
			} else if (event.equals(KeyCode.Enter) || event.equals(KeyCode.Space)) {
E
Erich Gamma 已提交
399 400 401 402 403
				// Nothing, just staying out of the else branch
			} else {
				eventHandled = false;
			}

B
Benjamin Pasero 已提交
404
			if (eventHandled) {
E
Erich Gamma 已提交
405 406 407 408 409 410
				event.preventDefault();
				event.stopPropagation();
			}
		});

		// Prevent native context menu on actions
B
Benjamin Pasero 已提交
411
		$(this.domNode).on(DOM.EventType.CONTEXT_MENU, (e: Event) => {
E
Erich Gamma 已提交
412 413 414 415
			e.preventDefault();
			e.stopPropagation();
		});

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

419
			// Run action on Enter/Space
A
Alexandru Dima 已提交
420
			if (event.equals(KeyCode.Enter) || event.equals(KeyCode.Space)) {
E
Erich Gamma 已提交
421 422 423 424
				this.doTrigger(event);
				event.preventDefault();
				event.stopPropagation();
			}
425 426

			// Recompute focused item
A
Alexandru Dima 已提交
427
			else if (event.equals(KeyCode.Tab) || event.equals(KeyMod.Shift | KeyCode.Tab)) {
428 429
				this.updateFocusedItem();
			}
E
Erich Gamma 已提交
430 431
		});

432
		this.focusTracker = DOM.trackFocus(this.domNode);
433
		this.focusTracker.addBlurListener(() => {
B
Benjamin Pasero 已提交
434
			if (document.activeElement === this.domNode || !DOM.isAncestor(document.activeElement, this.domNode)) {
435
				this.emit(DOM.EventType.BLUR, {});
436 437 438 439
				this.focusedItem = undefined;
			}
		});

440
		this.focusTracker.addFocusListener(() => this.updateFocusedItem());
E
Erich Gamma 已提交
441 442 443

		this.actionsList = document.createElement('ul');
		this.actionsList.className = 'actions-container';
444 445 446 447 448
		this.actionsList.setAttribute('role', 'toolbar');
		if (this.options.ariaLabel) {
			this.actionsList.setAttribute('aria-label', this.options.ariaLabel);
		}

E
Erich Gamma 已提交
449 450
		this.domNode.appendChild(this.actionsList);

451
		((container instanceof Builder) ? container.getHTMLElement() : container).appendChild(this.domNode);
E
Erich Gamma 已提交
452 453
	}

454 455 456
	public setAriaLabel(label: string): void {
		if (label) {
			this.actionsList.setAttribute('aria-label', label);
B
Benjamin Pasero 已提交
457
		} else {
458 459 460 461
			this.actionsList.removeAttribute('aria-label');
		}
	}

462 463 464 465 466 467 468 469 470 471
	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 已提交
472 473 474 475 476 477 478 479 480
	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 已提交
481
	public get actionRunner(): IActionRunner {
E
Erich Gamma 已提交
482 483 484
		return this._actionRunner;
	}

B
Benjamin Pasero 已提交
485
	public set actionRunner(actionRunner: IActionRunner) {
E
Erich Gamma 已提交
486 487 488 489 490 491
		if (actionRunner) {
			this._actionRunner = actionRunner;
			this.items.forEach(item => item.actionRunner = actionRunner);
		}
	}

B
Benjamin Pasero 已提交
492
	public getContainer(): Builder {
E
Erich Gamma 已提交
493 494 495
		return $(this.domNode);
	}

496 497 498
	public push(arg: IAction | IAction[], options: IActionOptions = {}): void {

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

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

B
Benjamin Pasero 已提交
502
		actions.forEach((action: IAction) => {
503
			const actionItemElement = document.createElement('li');
E
Erich Gamma 已提交
504 505 506
			actionItemElement.className = 'action-item';
			actionItemElement.setAttribute('role', 'presentation');

B
Benjamin Pasero 已提交
507
			let item: IActionItem = null;
E
Erich Gamma 已提交
508 509 510 511 512 513 514 515 516 517 518

			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 已提交
519
			this.addEmitter2(item);
E
Erich Gamma 已提交
520 521 522 523 524 525 526 527 528 529 530 531
			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);
		});
	}

B
Benjamin Pasero 已提交
532
	public clear(): void {
533 534
		// Do not dispose action items if they were provided from outside
		this.items = this.options.actionItemProvider ? [] : lifecycle.dispose(this.items);
E
Erich Gamma 已提交
535 536 537
		$(this.actionsList).empty();
	}

B
Benjamin Pasero 已提交
538
	public length(): number {
E
Erich Gamma 已提交
539 540 541
		return this.items.length;
	}

B
Benjamin Pasero 已提交
542
	public isEmpty(): boolean {
E
Erich Gamma 已提交
543 544 545
		return this.items.length === 0;
	}

B
Benjamin Pasero 已提交
546
	public focus(selectFirst?: boolean): void {
E
Erich Gamma 已提交
547 548 549 550 551 552 553
		if (selectFirst && typeof this.focusedItem === 'undefined') {
			this.focusedItem = 0;
		}

		this.updateFocus();
	}

B
Benjamin Pasero 已提交
554
	private focusNext(): void {
E
Erich Gamma 已提交
555 556 557 558
		if (typeof this.focusedItem === 'undefined') {
			this.focusedItem = this.items.length - 1;
		}

B
Benjamin Pasero 已提交
559 560
		let startIndex = this.focusedItem;
		let item: IActionItem;
E
Erich Gamma 已提交
561 562 563 564 565 566 567 568 569 570 571 572 573

		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 已提交
574
	private focusPrevious(): void {
E
Erich Gamma 已提交
575 576 577 578
		if (typeof this.focusedItem === 'undefined') {
			this.focusedItem = 0;
		}

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

		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;
		}

		this.updateFocus();
	}

B
Benjamin Pasero 已提交
599
	private updateFocus(): void {
E
Erich Gamma 已提交
600 601 602 603 604
		if (typeof this.focusedItem === 'undefined') {
			this.domNode.focus();
			return;
		}

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

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

B
Benjamin Pasero 已提交
610 611
			if (i === this.focusedItem) {
				if (types.isFunction(actionItem.focus)) {
E
Erich Gamma 已提交
612 613 614
					actionItem.focus();
				}
			} else {
B
Benjamin Pasero 已提交
615
				if (types.isFunction(actionItem.blur)) {
E
Erich Gamma 已提交
616 617 618 619 620 621 622
					actionItem.blur();
				}
			}
		}
	}

	private doTrigger(event): void {
B
Benjamin Pasero 已提交
623
		if (typeof this.focusedItem === 'undefined') {
624
			return; //nothing to focus
E
Erich Gamma 已提交
625 626 627
		}

		// trigger action
B
Benjamin Pasero 已提交
628
		let actionItem = (<BaseActionItem>this.items[this.focusedItem]);
629 630
		const context = (actionItem._context === null || actionItem._context === undefined) ? event : actionItem._context;
		this.run(actionItem._action, context).done();
E
Erich Gamma 已提交
631 632
	}

B
Benjamin Pasero 已提交
633
	private cancel(): void {
B
Benjamin Pasero 已提交
634 635 636 637
		if (document.activeElement instanceof HTMLElement) {
			(<HTMLElement>document.activeElement).blur(); // remove focus from focussed action
		}

B
Benjamin Pasero 已提交
638
		this.emit(CommonEventType.CANCEL);
E
Erich Gamma 已提交
639 640
	}

B
Benjamin Pasero 已提交
641
	public run(action: IAction, context?: any): Promise {
E
Erich Gamma 已提交
642 643 644
		return this._actionRunner.run(action, context);
	}

B
Benjamin Pasero 已提交
645
	public dispose(): void {
E
Erich Gamma 已提交
646
		if (this.items !== null) {
647
			lifecycle.dispose(this.items);
E
Erich Gamma 已提交
648 649 650
		}
		this.items = null;

651 652 653 654 655
		if (this.focusTracker) {
			this.focusTracker.dispose();
			this.focusTracker = null;
		}

J
Joao Moreno 已提交
656
		this.toDispose = lifecycle.dispose(this.toDispose);
E
Erich Gamma 已提交
657 658 659 660 661 662 663 664

		this.getContainer().destroy();

		super.dispose();
	}
}

export class SelectActionItem extends BaseActionItem {
I
isidor 已提交
665
	private selectBox: SelectBox;
I
isidor 已提交
666
	protected toDispose: lifecycle.IDisposable[];
E
Erich Gamma 已提交
667

B
Benjamin Pasero 已提交
668
	constructor(ctx: any, action: IAction, options: string[], selected: number) {
E
Erich Gamma 已提交
669
		super(ctx, action);
I
isidor 已提交
670
		this.selectBox = new SelectBox(options, selected);
E
Erich Gamma 已提交
671 672

		this.toDispose = [];
I
isidor 已提交
673
		this.toDispose.push(this.selectBox);
E
Erich Gamma 已提交
674 675 676
		this.registerListeners();
	}

B
Benjamin Pasero 已提交
677
	public setOptions(options: string[], selected: number): void {
I
isidor 已提交
678
		this.selectBox.setOptions(options, selected);
E
Erich Gamma 已提交
679 680 681
	}

	private registerListeners(): void {
I
isidor 已提交
682 683
		this.toDispose.push(this.selectBox.onDidSelect(selected => {
			this.actionRunner.run(this._action, this.getActionContext(selected)).done();
E
Erich Gamma 已提交
684 685 686
		}));
	}

I
isidor 已提交
687 688 689 690
	protected getActionContext(option: string) {
		return option;
	}

691
	public focus(): void {
I
isidor 已提交
692 693
		if (this.selectBox) {
			this.selectBox.focus();
694 695 696 697
		}
	}

	public blur(): void {
I
isidor 已提交
698 699
		if (this.selectBox) {
			this.selectBox.blur();
700 701 702
		}
	}

B
Benjamin Pasero 已提交
703
	public render(container: HTMLElement): void {
I
isidor 已提交
704
		this.selectBox.render(container);
E
Erich Gamma 已提交
705 706
	}

707
	protected getSelected(): string {
I
isidor 已提交
708
		return this.selectBox.getSelected();
E
Erich Gamma 已提交
709 710 711
	}

	public dispose(): void {
J
Joao Moreno 已提交
712
		this.toDispose = lifecycle.dispose(this.toDispose);
E
Erich Gamma 已提交
713 714 715 716

		super.dispose();
	}
}