actionbar.ts 18.9 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';
B
Benjamin Pasero 已提交
13
import {IAction, IActionRunner, Action, ActionRunner} from 'vs/base/common/actions';
B
Benjamin Pasero 已提交
14 15 16 17 18 19
import DOM = require('vs/base/browser/dom');
import {EventType as CommonEventType} from 'vs/base/common/events';
import types = require('vs/base/common/types');
import {IEventEmitter, EventEmitter, IEmitterEvent} from 'vs/base/common/eventEmitter';
import {Gesture, EventType} from 'vs/base/browser/touch';
import {StandardKeyboardEvent} from 'vs/base/browser/keyboardEvent';
E
Erich Gamma 已提交
20 21
import {CommonKeybindings} from 'vs/base/common/keyCodes';

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

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

B
Benjamin Pasero 已提交
34 35 36
	public builder: Builder;
	public _callOnDispose: Function[];
	public _context: any;
B
Benjamin Pasero 已提交
37 38 39 40
	public _action: IAction;

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

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

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

B
Benjamin Pasero 已提交
49 50
		if (action instanceof Action) {
			let l = (<Action>action).addBulkListener((events: IEmitterEvent[]) => {
E
Erich Gamma 已提交
51

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

B
Benjamin Pasero 已提交
58
				events.forEach((event: IEmitterEvent) => {
E
Erich Gamma 已提交
59

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

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

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

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

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

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

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

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

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

B
Benjamin Pasero 已提交
118
		this.builder.on('mousedown', (e: MouseEvent) => {
E
Erich Gamma 已提交
119 120 121 122 123
			if (e.button === 0 && this._action.enabled) {
				this.builder.addClass('active');
			}
		});

B
Benjamin Pasero 已提交
124
		this.builder.on(['mouseup', 'mouseout'], (e: MouseEvent) => {
E
Erich Gamma 已提交
125 126 127 128 129 130
			if (e.button === 0 && this._action.enabled) {
				this.builder.removeClass('active');
			}
		});
	}

B
Benjamin Pasero 已提交
131 132
	public onClick(event: Event): void {
		DOM.EventHelper.stop(event, true);
E
Erich Gamma 已提交
133 134 135
		this._actionRunner.run(this._action, this._context || event);
	}

B
Benjamin Pasero 已提交
136
	public focus(): void {
137 138 139
		if (this.builder) {
			this.builder.domFocus();
		}
E
Erich Gamma 已提交
140 141
	}

B
Benjamin Pasero 已提交
142
	public blur(): void {
143
		if (this.builder) {
144
			this.builder.domBlur();
145
		}
E
Erich Gamma 已提交
146 147
	}

B
Benjamin Pasero 已提交
148
	public _updateEnabled(): void {
E
Erich Gamma 已提交
149 150 151
		// implement in subclass
	}

B
Benjamin Pasero 已提交
152
	public _updateLabel(): void {
E
Erich Gamma 已提交
153 154 155
		// implement in subclass
	}

B
Benjamin Pasero 已提交
156
	public _updateTooltip(): void {
E
Erich Gamma 已提交
157 158 159
		// implement in subclass
	}

B
Benjamin Pasero 已提交
160
	public _updateClass(): void {
E
Erich Gamma 已提交
161 162 163
		// implement in subclass
	}

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

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

B
Benjamin Pasero 已提交
172
	public dispose(): void {
E
Erich Gamma 已提交
173 174 175 176 177 178 179 180 181 182 183 184
		super.dispose();

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

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

B
Benjamin Pasero 已提交
185
		lifecycle.cAll(this._callOnDispose);
E
Erich Gamma 已提交
186 187 188
	}
}

B
Benjamin Pasero 已提交
189
export class Separator extends Action {
E
Erich Gamma 已提交
190

B
Benjamin Pasero 已提交
191
	public static ID = 'actions.monaco.separator';
E
Erich Gamma 已提交
192

B
Benjamin Pasero 已提交
193
	constructor(label?: string, order?) {
E
Erich Gamma 已提交
194 195 196 197 198 199 200 201
		super(Separator.ID, label, label ? 'separator text' : 'separator');
		this.checked = false;
		this.enabled = false;
		this.order = order;
	}
}

export interface IActionItemOptions {
B
Benjamin Pasero 已提交
202 203 204
	icon?: boolean;
	label?: boolean;
	keybinding?: string;
E
Erich Gamma 已提交
205 206 207 208
}

export class ActionItem extends BaseActionItem {

B
Benjamin Pasero 已提交
209 210 211
	$e: Builder;
	private cssClass: string;
	private options: IActionItemOptions;
E
Erich Gamma 已提交
212

B
Benjamin Pasero 已提交
213
	constructor(context: any, action: IAction, options: IActionItemOptions = {}) {
E
Erich Gamma 已提交
214 215 216 217 218 219 220 221
		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 已提交
222
	public render(container: HTMLElement): void {
E
Erich Gamma 已提交
223 224
		super.render(container);

225
		this.$e = $('a.action-label').appendTo(this.builder);
B
Benjamin Pasero 已提交
226
		this.$e.attr({ role: 'menuitem' });
E
Erich Gamma 已提交
227 228 229 230 231 232 233 234 235 236 237 238

		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 已提交
239
	public focus(): void {
E
Erich Gamma 已提交
240 241 242 243
		super.focus();
		this.$e.domFocus();
	}

B
Benjamin Pasero 已提交
244
	public _updateLabel(): void {
E
Erich Gamma 已提交
245 246 247 248 249
		if (this.options.label) {
			this.$e.text(this.getAction().label);
		}
	}

B
Benjamin Pasero 已提交
250
	public _updateTooltip(): void {
B
Benjamin Pasero 已提交
251
		let title: string = null;
E
Erich Gamma 已提交
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268

		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) {
				title = nls.localize('titleLabel', "{0} ({1})", title, this.options.keybinding);
			}
		}

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

B
Benjamin Pasero 已提交
269
	public _updateClass(): void {
E
Erich Gamma 已提交
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
		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 已提交
285 286
	public _updateEnabled(): void {
		if (this.getAction().enabled) {
E
Erich Gamma 已提交
287 288
			this.builder.removeClass('disabled');
			this.$e.removeClass('disabled');
289
			this.$e.attr({ tabindex: 0 });
E
Erich Gamma 已提交
290 291 292
		} else {
			this.builder.addClass('disabled');
			this.$e.addClass('disabled');
293
			this.$e.removeAttribute('tabindex');
E
Erich Gamma 已提交
294 295 296
		}
	}

B
Benjamin Pasero 已提交
297 298
	public _updateChecked(): void {
		if (this.getAction().checked) {
E
Erich Gamma 已提交
299 300 301 302 303 304 305 306 307
			this.$e.addClass('checked');
		} else {
			this.$e.removeClass('checked');
		}
	}
}

export class ProgressItem extends BaseActionItem {

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

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

B
Benjamin Pasero 已提交
313
		let label = document.createElement('div');
E
Erich Gamma 已提交
314 315 316 317 318
		$(label).addClass('label');
		label.textContent = this.getAction().label;
		label.title = this.getAction().label;
		super.render(label);

B
Benjamin Pasero 已提交
319
		let progress = document.createElement('div');
E
Erich Gamma 已提交
320 321 322
		progress.textContent = '\u2026';
		$(progress).addClass('tag', 'progress');

B
Benjamin Pasero 已提交
323
		let done = document.createElement('div');
E
Erich Gamma 已提交
324 325 326
		done.textContent = '\u2713';
		$(done).addClass('tag', 'done');

B
Benjamin Pasero 已提交
327
		let error = document.createElement('div');
E
Erich Gamma 已提交
328 329 330
		error.textContent = '!';
		$(error).addClass('tag', 'error');

B
Benjamin Pasero 已提交
331
		this.callOnDispose.push(this.addListener(CommonEventType.BEFORE_RUN, () => {
E
Erich Gamma 已提交
332 333 334 335 336
			$(progress).addClass('active');
			$(done).removeClass('active');
			$(error).removeClass('active');
		}));

B
Benjamin Pasero 已提交
337
		this.callOnDispose.push(this.addListener(CommonEventType.RUN, (result) => {
E
Erich Gamma 已提交
338
			$(progress).removeClass('active');
B
Benjamin Pasero 已提交
339
			if (result.error) {
E
Erich Gamma 已提交
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
				$(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 已提交
355 356
	public dispose(): void {
		lifecycle.cAll(this.callOnDispose);
E
Erich Gamma 已提交
357 358 359 360 361 362 363 364 365 366
		super.dispose();
	}
}

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

export interface IActionItemProvider {
B
Benjamin Pasero 已提交
367
	(action: IAction): IActionItem;
E
Erich Gamma 已提交
368 369 370
}

export interface IActionBarOptions {
B
Benjamin Pasero 已提交
371 372 373
	orientation?: ActionsOrientation;
	context?: any;
	actionItemProvider?: IActionItemProvider;
B
Benjamin Pasero 已提交
374
	actionRunner?: IActionRunner;
E
Erich Gamma 已提交
375 376
}

B
Benjamin Pasero 已提交
377
let defaultOptions: IActionBarOptions = {
E
Erich Gamma 已提交
378 379 380 381 382
	orientation: ActionsOrientation.HORIZONTAL,
	context: null
};

export interface IActionOptions extends IActionItemOptions {
B
Benjamin Pasero 已提交
383
	index?: number;
E
Erich Gamma 已提交
384 385
}

B
Benjamin Pasero 已提交
386
export class ActionBar extends EventEmitter implements IActionRunner {
E
Erich Gamma 已提交
387 388 389

	private static nlsActionBarAccessibleLabel = nls.localize('actionBarAccessibleLabel', "Action Bar");

B
Benjamin Pasero 已提交
390
	static DEFAULT_OPTIONS: IActionBarOptions = {
E
Erich Gamma 已提交
391 392 393
		orientation: ActionsOrientation.HORIZONTAL
	};

B
Benjamin Pasero 已提交
394
	public options: IActionBarOptions;
B
Benjamin Pasero 已提交
395
	private _actionRunner: IActionRunner;
E
Erich Gamma 已提交
396 397 398
	private _context: any;

	// Items
B
Benjamin Pasero 已提交
399
	public items: IActionItem[];
400

B
Benjamin Pasero 已提交
401
	private focusedItem: number;
402
	private focusTracker: DOM.IFocusTracker;
E
Erich Gamma 已提交
403 404

	// Elements
B
Benjamin Pasero 已提交
405 406
	public domNode: HTMLElement;
	private actionsList: HTMLElement;
E
Erich Gamma 已提交
407

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

B
Benjamin Pasero 已提交
410 411 412
	constructor(container: HTMLElement, options?: IActionBarOptions);
	constructor(container: Builder, options?: IActionBarOptions);
	constructor(container: any, options: IActionBarOptions = defaultOptions) {
E
Erich Gamma 已提交
413 414 415 416 417 418 419
		super();
		this.options = options;
		this._context = options.context;
		this.toDispose = [];
		this._actionRunner = this.options.actionRunner;

		if (!this._actionRunner) {
B
Benjamin Pasero 已提交
420
			this._actionRunner = new ActionRunner();
E
Erich Gamma 已提交
421 422 423 424 425 426 427 428 429 430 431
			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';

B
Benjamin Pasero 已提交
432
		let isVertical = this.options.orientation === ActionsOrientation.VERTICAL;
E
Erich Gamma 已提交
433 434 435 436
		if (isVertical) {
			this.domNode.className += ' vertical';
		}

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

B
Benjamin Pasero 已提交
441
			if (event.equals(isVertical ? CommonKeybindings.UP_ARROW : CommonKeybindings.LEFT_ARROW)) {
E
Erich Gamma 已提交
442
				this.focusPrevious();
B
Benjamin Pasero 已提交
443
			} else if (event.equals(isVertical ? CommonKeybindings.DOWN_ARROW : CommonKeybindings.RIGHT_ARROW)) {
E
Erich Gamma 已提交
444 445 446
				this.focusNext();
			} else if (event.equals(CommonKeybindings.ESCAPE)) {
				this.cancel();
447
			} else if (event.equals(CommonKeybindings.ENTER) || event.equals(CommonKeybindings.SPACE)) {
E
Erich Gamma 已提交
448 449 450 451 452
				// Nothing, just staying out of the else branch
			} else {
				eventHandled = false;
			}

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

		// Prevent native context menu on actions
B
Benjamin Pasero 已提交
460
		$(this.domNode).on(DOM.EventType.CONTEXT_MENU, (e: Event) => {
E
Erich Gamma 已提交
461 462 463 464
			e.preventDefault();
			e.stopPropagation();
		});

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

468
			// Run action on Enter/Space
469
			if (event.equals(CommonKeybindings.ENTER) || event.equals(CommonKeybindings.SPACE)) {
E
Erich Gamma 已提交
470 471 472 473
				this.doTrigger(event);
				event.preventDefault();
				event.stopPropagation();
			}
474 475

			// Recompute focused item
476
			else if (event.equals(CommonKeybindings.TAB) || event.equals(CommonKeybindings.SHIFT_TAB)) {
477 478
				this.updateFocusedItem();
			}
E
Erich Gamma 已提交
479 480
		});

481 482
		this.focusTracker = DOM.trackFocus(this.domNode);
		this.focusTracker.addBlurListener((e: Event) => {
B
Benjamin Pasero 已提交
483
			if (document.activeElement === this.domNode || !DOM.isAncestor(document.activeElement, this.domNode)) {
E
Erich Gamma 已提交
484
				this.emit('blur', e);
485 486 487 488
				this.focusedItem = undefined;
			}
		});

489
		this.focusTracker.addFocusListener(() => this.updateFocusedItem());
E
Erich Gamma 已提交
490 491 492 493 494 495 496

		this.actionsList = document.createElement('ul');
		this.actionsList.className = 'actions-container';
		this.actionsList.setAttribute('role', 'menu');
		this.actionsList.setAttribute('aria-label', ActionBar.nlsActionBarAccessibleLabel);
		this.domNode.appendChild(this.actionsList);

B
Benjamin Pasero 已提交
497
		container = (container instanceof Builder) ? container.getHTMLElement() : container;
E
Erich Gamma 已提交
498 499 500
		container.appendChild(this.domNode);
	}

501 502 503 504 505 506 507 508 509 510
	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 已提交
511 512 513 514 515 516 517 518 519
	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 已提交
520
	public get actionRunner(): IActionRunner {
E
Erich Gamma 已提交
521 522 523
		return this._actionRunner;
	}

B
Benjamin Pasero 已提交
524
	public set actionRunner(actionRunner: IActionRunner) {
E
Erich Gamma 已提交
525 526 527 528 529 530
		if (actionRunner) {
			this._actionRunner = actionRunner;
			this.items.forEach(item => item.actionRunner = actionRunner);
		}
	}

B
Benjamin Pasero 已提交
531
	public getContainer(): Builder {
E
Erich Gamma 已提交
532 533 534
		return $(this.domNode);
	}

B
Benjamin Pasero 已提交
535 536
	public push(actions: IAction, options?: IActionOptions): void;
	public push(actions: IAction[], options?: IActionOptions): void;
B
Benjamin Pasero 已提交
537 538
	public push(actions: any, options: IActionOptions = {}): void {
		if (!Array.isArray(actions)) {
E
Erich Gamma 已提交
539 540 541
			actions = [actions];
		}

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

B
Benjamin Pasero 已提交
544 545
		actions.forEach((action: IAction) => {
			let actionItemElement = document.createElement('li');
E
Erich Gamma 已提交
546 547 548
			actionItemElement.className = 'action-item';
			actionItemElement.setAttribute('role', 'presentation');

B
Benjamin Pasero 已提交
549
			let item: IActionItem = null;
E
Erich Gamma 已提交
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573

			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);
			this.addEmitter(item);
			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 已提交
574
	public clear(): void {
B
Benjamin Pasero 已提交
575
		let item: IActionItem;
E
Erich Gamma 已提交
576 577 578 579 580 581
		while (item = this.items.pop()) {
			item.dispose();
		}
		$(this.actionsList).empty();
	}

B
Benjamin Pasero 已提交
582
	public length(): number {
E
Erich Gamma 已提交
583 584 585
		return this.items.length;
	}

B
Benjamin Pasero 已提交
586
	public isEmpty(): boolean {
E
Erich Gamma 已提交
587 588 589
		return this.items.length === 0;
	}

B
Benjamin Pasero 已提交
590 591
	public onContentsChange(): void {
		this.emit(CommonEventType.CONTENTS_CHANGED);
E
Erich Gamma 已提交
592 593
	}

B
Benjamin Pasero 已提交
594
	public focus(selectFirst?: boolean): void {
E
Erich Gamma 已提交
595 596 597 598 599 600 601
		if (selectFirst && typeof this.focusedItem === 'undefined') {
			this.focusedItem = 0;
		}

		this.updateFocus();
	}

B
Benjamin Pasero 已提交
602
	private focusNext(): void {
E
Erich Gamma 已提交
603 604 605 606
		if (typeof this.focusedItem === 'undefined') {
			this.focusedItem = this.items.length - 1;
		}

B
Benjamin Pasero 已提交
607 608
		let startIndex = this.focusedItem;
		let item: IActionItem;
E
Erich Gamma 已提交
609 610 611 612 613 614 615 616 617 618 619 620 621

		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 已提交
622
	private focusPrevious(): void {
E
Erich Gamma 已提交
623 624 625 626
		if (typeof this.focusedItem === 'undefined') {
			this.focusedItem = 0;
		}

B
Benjamin Pasero 已提交
627 628
		let startIndex = this.focusedItem;
		let item: IActionItem;
E
Erich Gamma 已提交
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646

		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 已提交
647
	private updateFocus(): void {
E
Erich Gamma 已提交
648 649 650 651 652
		if (typeof this.focusedItem === 'undefined') {
			this.domNode.focus();
			return;
		}

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

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

B
Benjamin Pasero 已提交
658 659
			if (i === this.focusedItem) {
				if (types.isFunction(actionItem.focus)) {
E
Erich Gamma 已提交
660 661 662
					actionItem.focus();
				}
			} else {
B
Benjamin Pasero 已提交
663
				if (types.isFunction(actionItem.blur)) {
E
Erich Gamma 已提交
664 665 666 667 668 669 670
					actionItem.blur();
				}
			}
		}
	}

	private doTrigger(event): void {
B
Benjamin Pasero 已提交
671
		if (typeof this.focusedItem === 'undefined') {
672
			return; //nothing to focus
E
Erich Gamma 已提交
673 674 675
		}

		// trigger action
B
Benjamin Pasero 已提交
676
		let actionItem = (<BaseActionItem>this.items[this.focusedItem]);
E
Erich Gamma 已提交
677 678 679
		this.run(actionItem._action, actionItem._context || event).done();
	}

B
Benjamin Pasero 已提交
680
	private cancel(): void {
B
Benjamin Pasero 已提交
681 682 683 684
		if (document.activeElement instanceof HTMLElement) {
			(<HTMLElement>document.activeElement).blur(); // remove focus from focussed action
		}

B
Benjamin Pasero 已提交
685
		this.emit(CommonEventType.CANCEL);
E
Erich Gamma 已提交
686 687
	}

B
Benjamin Pasero 已提交
688
	public run(action: IAction, context?: any): Promise {
E
Erich Gamma 已提交
689 690 691
		return this._actionRunner.run(action, context);
	}

B
Benjamin Pasero 已提交
692
	public dispose(): void {
E
Erich Gamma 已提交
693 694 695 696 697
		if (this.items !== null) {
			this.clear();
		}
		this.items = null;

698 699 700 701 702
		if (this.focusTracker) {
			this.focusTracker.dispose();
			this.focusTracker = null;
		}

B
Benjamin Pasero 已提交
703
		this.toDispose = lifecycle.disposeAll(this.toDispose);
E
Erich Gamma 已提交
704 705 706 707 708 709 710 711 712 713 714

		this.getContainer().destroy();

		super.dispose();
	}
}

export class SelectActionItem extends BaseActionItem {
	private select: HTMLSelectElement;
	private options: string[];
	private selected: number;
B
Benjamin Pasero 已提交
715
	private toDispose: lifecycle.IDisposable[];
E
Erich Gamma 已提交
716

B
Benjamin Pasero 已提交
717
	constructor(ctx: any, action: IAction, options: string[], selected: number) {
E
Erich Gamma 已提交
718 719 720 721 722 723 724 725 726 727 728 729 730
		super(ctx, action);

		this.select = document.createElement('select');
		this.select.className = 'action-bar-select';

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

		this.toDispose = [];

		this.registerListeners();
	}

B
Benjamin Pasero 已提交
731
	public setOptions(options: string[], selected: number): void {
E
Erich Gamma 已提交
732 733 734 735 736 737 738
		this.options = options;
		this.selected = selected;

		this.doSetOptions();
	}

	private registerListeners(): void {
B
Benjamin Pasero 已提交
739
		this.toDispose.push(DOM.addStandardDisposableListener(this.select, 'change', (e) => {
E
Erich Gamma 已提交
740 741 742 743
			this.actionRunner.run(this._action, e.target.value).done();
		}));
	}

744 745 746 747 748 749 750 751 752 753 754 755
	public focus(): void {
		if (this.select) {
			this.select.focus();
		}
	}

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

B
Benjamin Pasero 已提交
756 757
	public render(container: HTMLElement): void {
		DOM.addClass(container, 'select-container');
E
Erich Gamma 已提交
758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774
		container.appendChild(this.select);
		this.doSetOptions();
	}

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

		this.options.forEach((option) => {
			this.select.options.add(this.createOption(option));
		});

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

	private createOption(value: string): HTMLOptionElement {
B
Benjamin Pasero 已提交
775
		let option = document.createElement('option');
E
Erich Gamma 已提交
776 777 778 779 780 781 782
		option.value = value;
		option.text = value;

		return option;
	}

	public dispose(): void {
B
Benjamin Pasero 已提交
783
		this.toDispose = lifecycle.disposeAll(this.toDispose);
E
Erich Gamma 已提交
784 785 786 787

		super.dispose();
	}
}