actionbar.ts 19.7 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 11 12
import lifecycle = require('vs/base/common/lifecycle');
import {Promise} from 'vs/base/common/winjs.base';
import {Builder, $} from 'vs/base/browser/builder';
13
import platform = require('vs/base/common/platform');
B
Benjamin Pasero 已提交
14
import {IAction, IActionRunner, Action, ActionRunner} from 'vs/base/common/actions';
B
Benjamin Pasero 已提交
15 16 17
import DOM = require('vs/base/browser/dom');
import {EventType as CommonEventType} from 'vs/base/common/events';
import types = require('vs/base/common/types');
A
Alex Dima 已提交
18
import {IEventEmitter, EventEmitter, EmitterEvent} from 'vs/base/common/eventEmitter';
B
Benjamin Pasero 已提交
19 20
import {Gesture, EventType} from 'vs/base/browser/touch';
import {StandardKeyboardEvent} from 'vs/base/browser/keyboardEvent';
E
Erich Gamma 已提交
21 22
import {CommonKeybindings} from 'vs/base/common/keyCodes';

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

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

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

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

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

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

B
Benjamin Pasero 已提交
50
		if (action instanceof Action) {
A
Alex Dima 已提交
51
			let l = (<Action>action).addBulkListener2((events: EmitterEvent[]) => {
B
Benjamin Pasero 已提交
52
				if (!this.builder) {
E
Erich Gamma 已提交
53 54 55 56 57
					// we have not been rendered yet, so there
					// is no point in updating the UI
					return;
				}

A
Alex Dima 已提交
58
				events.forEach((event: EmitterEvent) => {
B
Benjamin Pasero 已提交
59
					switch (event.getType()) {
B
Benjamin Pasero 已提交
60
						case Action.ENABLED:
E
Erich Gamma 已提交
61 62
							this._updateEnabled();
							break;
B
Benjamin Pasero 已提交
63
						case Action.LABEL:
E
Erich Gamma 已提交
64 65 66
							this._updateLabel();
							this._updateTooltip();
							break;
B
Benjamin Pasero 已提交
67
						case Action.TOOLTIP:
E
Erich Gamma 已提交
68 69
							this._updateTooltip();
							break;
B
Benjamin Pasero 已提交
70
						case Action.CLASS:
E
Erich Gamma 已提交
71 72
							this._updateClass();
							break;
B
Benjamin Pasero 已提交
73
						case Action.CHECKED:
E
Erich Gamma 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
							this._updateChecked();
							break;
						default:
							this._updateUnknown(event);
							break;
					}
				});
			});
			this._callOnDispose.push(l);
		}
	}

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

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

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

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

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

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

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

B
Benjamin Pasero 已提交
114 115
		this.builder.on(DOM.EventType.CLICK, (event: Event) => this.onClick(event));
		this.builder.on(EventType.Tap, e => this.onClick(e));
E
Erich Gamma 已提交
116

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

B
Benjamin Pasero 已提交
121 122 123
		this.builder.on(DOM.EventType.MOUSE_DOWN, (e: MouseEvent) => {
			DOM.EventHelper.stop(e);

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

B
Benjamin Pasero 已提交
129 130 131
		this.builder.on([DOM.EventType.MOUSE_UP, DOM.EventType.MOUSE_OUT], (e: MouseEvent) => {
			DOM.EventHelper.stop(e);

E
Erich Gamma 已提交
132 133 134 135 136 137
			if (e.button === 0 && this._action.enabled) {
				this.builder.removeClass('active');
			}
		});
	}

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

141 142 143 144 145 146 147 148
		let context: any;
		if (types.isUndefinedOrNull(this._context)) {
			context = event;
		} else {
			context = this._context;
			context.event = event;
		}

149
		this._actionRunner.run(this._action, context);
E
Erich Gamma 已提交
150 151
	}

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

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

B
Benjamin Pasero 已提交
164
	public _updateEnabled(): void {
E
Erich Gamma 已提交
165 166 167
		// implement in subclass
	}

B
Benjamin Pasero 已提交
168
	public _updateLabel(): void {
E
Erich Gamma 已提交
169 170 171
		// implement in subclass
	}

B
Benjamin Pasero 已提交
172
	public _updateTooltip(): void {
E
Erich Gamma 已提交
173 174 175
		// implement in subclass
	}

B
Benjamin Pasero 已提交
176
	public _updateClass(): void {
E
Erich Gamma 已提交
177 178 179
		// implement in subclass
	}

B
Benjamin Pasero 已提交
180
	public _updateChecked(): void {
E
Erich Gamma 已提交
181 182 183
		// implement in subclass
	}

A
Alex Dima 已提交
184
	public _updateUnknown(event: EmitterEvent): void {
E
Erich Gamma 已提交
185 186 187
		// can implement in subclass
	}

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

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

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

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

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

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

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

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

export class ActionItem extends BaseActionItem {

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

B
Benjamin Pasero 已提交
229
	constructor(context: any, action: IAction, options: IActionItemOptions = {}) {
E
Erich Gamma 已提交
230 231 232 233 234 235 236 237
		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 已提交
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 320 321 322 323
			this.$e.addClass('checked');
		} else {
			this.$e.removeClass('checked');
		}
	}
}

export class ProgressItem extends BaseActionItem {

B
Benjamin Pasero 已提交
324
	public render(parent: HTMLElement): void {
E
Erich Gamma 已提交
325

B
Benjamin Pasero 已提交
326
		let container = document.createElement('div');
E
Erich Gamma 已提交
327 328
		$(container).addClass('progress-item');

B
Benjamin Pasero 已提交
329
		let label = document.createElement('div');
E
Erich Gamma 已提交
330 331 332 333 334
		$(label).addClass('label');
		label.textContent = this.getAction().label;
		label.title = this.getAction().label;
		super.render(label);

B
Benjamin Pasero 已提交
335
		let progress = document.createElement('div');
E
Erich Gamma 已提交
336 337 338
		progress.textContent = '\u2026';
		$(progress).addClass('tag', 'progress');

B
Benjamin Pasero 已提交
339
		let done = document.createElement('div');
E
Erich Gamma 已提交
340 341 342
		done.textContent = '\u2713';
		$(done).addClass('tag', 'done');

B
Benjamin Pasero 已提交
343
		let error = document.createElement('div');
E
Erich Gamma 已提交
344 345 346
		error.textContent = '!';
		$(error).addClass('tag', 'error');

A
Alex Dima 已提交
347
		this.callOnDispose.push(this.addListener2(CommonEventType.BEFORE_RUN, () => {
E
Erich Gamma 已提交
348 349 350 351 352
			$(progress).addClass('active');
			$(done).removeClass('active');
			$(error).removeClass('active');
		}));

A
Alex Dima 已提交
353
		this.callOnDispose.push(this.addListener2(CommonEventType.RUN, (result) => {
E
Erich Gamma 已提交
354
			$(progress).removeClass('active');
B
Benjamin Pasero 已提交
355
			if (result.error) {
E
Erich Gamma 已提交
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
				$(done).removeClass('active');
				$(error).addClass('active');
			} else {
				$(error).removeClass('active');
				$(done).addClass('active');
			}
		}));

		container.appendChild(label);
		container.appendChild(progress);
		container.appendChild(done);
		container.appendChild(error);
		parent.appendChild(container);
	}

B
Benjamin Pasero 已提交
371
	public dispose(): void {
E
Erich Gamma 已提交
372 373 374 375 376 377 378 379 380 381
		super.dispose();
	}
}

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

export interface IActionItemProvider {
B
Benjamin Pasero 已提交
382
	(action: IAction): IActionItem;
E
Erich Gamma 已提交
383 384 385
}

export interface IActionBarOptions {
B
Benjamin Pasero 已提交
386 387 388
	orientation?: ActionsOrientation;
	context?: any;
	actionItemProvider?: IActionItemProvider;
B
Benjamin Pasero 已提交
389
	actionRunner?: IActionRunner;
390
	ariaLabel?: string;
J
Joao Moreno 已提交
391
	animated?: boolean;
E
Erich Gamma 已提交
392 393
}

B
Benjamin Pasero 已提交
394
let defaultOptions: IActionBarOptions = {
E
Erich Gamma 已提交
395 396 397 398 399
	orientation: ActionsOrientation.HORIZONTAL,
	context: null
};

export interface IActionOptions extends IActionItemOptions {
B
Benjamin Pasero 已提交
400
	index?: number;
E
Erich Gamma 已提交
401 402
}

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

B
Benjamin Pasero 已提交
405
	public options: IActionBarOptions;
406

B
Benjamin Pasero 已提交
407
	private _actionRunner: IActionRunner;
E
Erich Gamma 已提交
408 409 410
	private _context: any;

	// Items
B
Benjamin Pasero 已提交
411
	public items: IActionItem[];
412

B
Benjamin Pasero 已提交
413
	private focusedItem: number;
414
	private focusTracker: DOM.IFocusTracker;
E
Erich Gamma 已提交
415 416

	// Elements
B
Benjamin Pasero 已提交
417 418
	public domNode: HTMLElement;
	private actionsList: HTMLElement;
E
Erich Gamma 已提交
419

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

B
Benjamin Pasero 已提交
422 423 424
	constructor(container: HTMLElement, options?: IActionBarOptions);
	constructor(container: Builder, options?: IActionBarOptions);
	constructor(container: any, options: IActionBarOptions = defaultOptions) {
E
Erich Gamma 已提交
425 426 427 428 429 430 431
		super();
		this.options = options;
		this._context = options.context;
		this.toDispose = [];
		this._actionRunner = this.options.actionRunner;

		if (!this._actionRunner) {
B
Benjamin Pasero 已提交
432
			this._actionRunner = new ActionRunner();
E
Erich Gamma 已提交
433 434 435 436 437 438 439 440 441 442 443
			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 已提交
444 445 446 447
		if (options.animated !== false) {
			DOM.addClass(this.domNode, 'animated');
		}

B
Benjamin Pasero 已提交
448
		let isVertical = this.options.orientation === ActionsOrientation.VERTICAL;
E
Erich Gamma 已提交
449 450 451 452
		if (isVertical) {
			this.domNode.className += ' vertical';
		}

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

B
Benjamin Pasero 已提交
457
			if (event.equals(isVertical ? CommonKeybindings.UP_ARROW : CommonKeybindings.LEFT_ARROW)) {
E
Erich Gamma 已提交
458
				this.focusPrevious();
B
Benjamin Pasero 已提交
459
			} else if (event.equals(isVertical ? CommonKeybindings.DOWN_ARROW : CommonKeybindings.RIGHT_ARROW)) {
E
Erich Gamma 已提交
460 461 462
				this.focusNext();
			} else if (event.equals(CommonKeybindings.ESCAPE)) {
				this.cancel();
463
			} else if (event.equals(CommonKeybindings.ENTER) || event.equals(CommonKeybindings.SPACE)) {
E
Erich Gamma 已提交
464 465 466 467 468
				// Nothing, just staying out of the else branch
			} else {
				eventHandled = false;
			}

B
Benjamin Pasero 已提交
469
			if (eventHandled) {
E
Erich Gamma 已提交
470 471 472 473 474 475
				event.preventDefault();
				event.stopPropagation();
			}
		});

		// Prevent native context menu on actions
B
Benjamin Pasero 已提交
476
		$(this.domNode).on(DOM.EventType.CONTEXT_MENU, (e: Event) => {
E
Erich Gamma 已提交
477 478 479 480
			e.preventDefault();
			e.stopPropagation();
		});

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

484
			// Run action on Enter/Space
485
			if (event.equals(CommonKeybindings.ENTER) || event.equals(CommonKeybindings.SPACE)) {
E
Erich Gamma 已提交
486 487 488 489
				this.doTrigger(event);
				event.preventDefault();
				event.stopPropagation();
			}
490 491

			// Recompute focused item
492
			else if (event.equals(CommonKeybindings.TAB) || event.equals(CommonKeybindings.SHIFT_TAB)) {
493 494
				this.updateFocusedItem();
			}
E
Erich Gamma 已提交
495 496
		});

497
		this.focusTracker = DOM.trackFocus(this.domNode);
498
		this.focusTracker.addBlurListener(() => {
B
Benjamin Pasero 已提交
499
			if (document.activeElement === this.domNode || !DOM.isAncestor(document.activeElement, this.domNode)) {
500
				this.emit(DOM.EventType.BLUR, {});
501 502 503 504
				this.focusedItem = undefined;
			}
		});

505
		this.focusTracker.addFocusListener(() => this.updateFocusedItem());
E
Erich Gamma 已提交
506 507 508

		this.actionsList = document.createElement('ul');
		this.actionsList.className = 'actions-container';
509 510 511 512 513
		this.actionsList.setAttribute('role', 'toolbar');
		if (this.options.ariaLabel) {
			this.actionsList.setAttribute('aria-label', this.options.ariaLabel);
		}

E
Erich Gamma 已提交
514 515
		this.domNode.appendChild(this.actionsList);

B
Benjamin Pasero 已提交
516
		container = (container instanceof Builder) ? container.getHTMLElement() : container;
E
Erich Gamma 已提交
517 518 519
		container.appendChild(this.domNode);
	}

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

528 529 530 531 532 533 534 535 536 537
	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 已提交
538 539 540 541 542 543 544 545 546
	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 已提交
547
	public get actionRunner(): IActionRunner {
E
Erich Gamma 已提交
548 549 550
		return this._actionRunner;
	}

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

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

B
Benjamin Pasero 已提交
562 563
	public push(actions: IAction, options?: IActionOptions): void;
	public push(actions: IAction[], options?: IActionOptions): void;
B
Benjamin Pasero 已提交
564 565
	public push(actions: any, options: IActionOptions = {}): void {
		if (!Array.isArray(actions)) {
E
Erich Gamma 已提交
566 567 568
			actions = [actions];
		}

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

B
Benjamin Pasero 已提交
571 572
		actions.forEach((action: IAction) => {
			let actionItemElement = document.createElement('li');
E
Erich Gamma 已提交
573 574 575
			actionItemElement.className = 'action-item';
			actionItemElement.setAttribute('role', 'presentation');

B
Benjamin Pasero 已提交
576
			let item: IActionItem = null;
E
Erich Gamma 已提交
577 578 579 580 581 582 583 584 585 586 587

			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 已提交
588
			this.addEmitter2(item);
E
Erich Gamma 已提交
589 590 591 592 593 594 595 596 597 598 599 600
			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 已提交
601
	public clear(): void {
B
Benjamin Pasero 已提交
602
		let item: IActionItem;
E
Erich Gamma 已提交
603 604 605 606 607 608
		while (item = this.items.pop()) {
			item.dispose();
		}
		$(this.actionsList).empty();
	}

B
Benjamin Pasero 已提交
609
	public length(): number {
E
Erich Gamma 已提交
610 611 612
		return this.items.length;
	}

B
Benjamin Pasero 已提交
613
	public isEmpty(): boolean {
E
Erich Gamma 已提交
614 615 616
		return this.items.length === 0;
	}

B
Benjamin Pasero 已提交
617 618
	public onContentsChange(): void {
		this.emit(CommonEventType.CONTENTS_CHANGED);
E
Erich Gamma 已提交
619 620
	}

B
Benjamin Pasero 已提交
621
	public focus(selectFirst?: boolean): void {
E
Erich Gamma 已提交
622 623 624 625 626 627 628
		if (selectFirst && typeof this.focusedItem === 'undefined') {
			this.focusedItem = 0;
		}

		this.updateFocus();
	}

B
Benjamin Pasero 已提交
629
	private focusNext(): void {
E
Erich Gamma 已提交
630 631 632 633
		if (typeof this.focusedItem === 'undefined') {
			this.focusedItem = this.items.length - 1;
		}

B
Benjamin Pasero 已提交
634 635
		let startIndex = this.focusedItem;
		let item: IActionItem;
E
Erich Gamma 已提交
636 637 638 639 640 641 642 643 644 645 646 647 648

		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 已提交
649
	private focusPrevious(): void {
E
Erich Gamma 已提交
650 651 652 653
		if (typeof this.focusedItem === 'undefined') {
			this.focusedItem = 0;
		}

B
Benjamin Pasero 已提交
654 655
		let startIndex = this.focusedItem;
		let item: IActionItem;
E
Erich Gamma 已提交
656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673

		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 已提交
674
	private updateFocus(): void {
E
Erich Gamma 已提交
675 676 677 678 679
		if (typeof this.focusedItem === 'undefined') {
			this.domNode.focus();
			return;
		}

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

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

B
Benjamin Pasero 已提交
685 686
			if (i === this.focusedItem) {
				if (types.isFunction(actionItem.focus)) {
E
Erich Gamma 已提交
687 688 689
					actionItem.focus();
				}
			} else {
B
Benjamin Pasero 已提交
690
				if (types.isFunction(actionItem.blur)) {
E
Erich Gamma 已提交
691 692 693 694 695 696 697
					actionItem.blur();
				}
			}
		}
	}

	private doTrigger(event): void {
B
Benjamin Pasero 已提交
698
		if (typeof this.focusedItem === 'undefined') {
699
			return; //nothing to focus
E
Erich Gamma 已提交
700 701 702
		}

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

B
Benjamin Pasero 已提交
708
	private cancel(): void {
B
Benjamin Pasero 已提交
709 710 711 712
		if (document.activeElement instanceof HTMLElement) {
			(<HTMLElement>document.activeElement).blur(); // remove focus from focussed action
		}

B
Benjamin Pasero 已提交
713
		this.emit(CommonEventType.CANCEL);
E
Erich Gamma 已提交
714 715
	}

B
Benjamin Pasero 已提交
716
	public run(action: IAction, context?: any): Promise {
E
Erich Gamma 已提交
717 718 719
		return this._actionRunner.run(action, context);
	}

B
Benjamin Pasero 已提交
720
	public dispose(): void {
E
Erich Gamma 已提交
721 722 723 724 725
		if (this.items !== null) {
			this.clear();
		}
		this.items = null;

726 727 728 729 730
		if (this.focusTracker) {
			this.focusTracker.dispose();
			this.focusTracker = null;
		}

J
Joao Moreno 已提交
731
		this.toDispose = lifecycle.dispose(this.toDispose);
E
Erich Gamma 已提交
732 733 734 735 736 737 738 739 740 741 742

		this.getContainer().destroy();

		super.dispose();
	}
}

export class SelectActionItem extends BaseActionItem {
	private select: HTMLSelectElement;
	private options: string[];
	private selected: number;
I
isidor 已提交
743
	protected toDispose: lifecycle.IDisposable[];
E
Erich Gamma 已提交
744

B
Benjamin Pasero 已提交
745
	constructor(ctx: any, action: IAction, options: string[], selected: number) {
E
Erich Gamma 已提交
746 747 748
		super(ctx, action);

		this.select = document.createElement('select');
B
Benjamin Pasero 已提交
749
		this.select.className = `action-bar-select ${platform.isWindows ? 'windows' : ''}`;
E
Erich Gamma 已提交
750 751 752 753 754 755 756 757 758

		this.options = options;
		this.selected = selected;

		this.toDispose = [];

		this.registerListeners();
	}

B
Benjamin Pasero 已提交
759
	public setOptions(options: string[], selected: number): void {
E
Erich Gamma 已提交
760 761 762 763 764 765 766
		this.options = options;
		this.selected = selected;

		this.doSetOptions();
	}

	private registerListeners(): void {
B
Benjamin Pasero 已提交
767
		this.toDispose.push(DOM.addStandardDisposableListener(this.select, 'change', (e) => {
I
isidor 已提交
768
			this.actionRunner.run(this._action, this.getActionContext(e.target.value)).done();
E
Erich Gamma 已提交
769 770 771
		}));
	}

I
isidor 已提交
772 773 774 775
	protected getActionContext(option: string) {
		return option;
	}

776 777 778 779 780 781 782 783 784 785 786 787
	public focus(): void {
		if (this.select) {
			this.select.focus();
		}
	}

	public blur(): void {
		if (this.select) {
			this.select.blur();
		}
	}

B
Benjamin Pasero 已提交
788 789
	public render(container: HTMLElement): void {
		DOM.addClass(container, 'select-container');
E
Erich Gamma 已提交
790 791 792 793 794 795 796 797
		container.appendChild(this.select);
		this.doSetOptions();
	}

	private doSetOptions(): void {
		this.select.options.length = 0;

		this.options.forEach((option) => {
I
isidor 已提交
798
			this.select.add(this.createOption(option));
E
Erich Gamma 已提交
799 800 801 802 803 804 805 806
		});

		if (this.selected >= 0) {
			this.select.selectedIndex = this.selected;
		}
	}

	private createOption(value: string): HTMLOptionElement {
B
Benjamin Pasero 已提交
807
		let option = document.createElement('option');
E
Erich Gamma 已提交
808 809 810 811 812 813 814
		option.value = value;
		option.text = value;

		return option;
	}

	public dispose(): void {
J
Joao Moreno 已提交
815
		this.toDispose = lifecycle.dispose(this.toDispose);
E
Erich Gamma 已提交
816 817 818 819

		super.dispose();
	}
}