actions.ts 5.6 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
'use strict';

J
Johannes Rieken 已提交
7
import { TPromise } from 'vs/base/common/winjs.base';
A
Alex Dima 已提交
8
import { IEventEmitter, EventEmitter } from 'vs/base/common/eventEmitter';
J
Johannes Rieken 已提交
9
import { IDisposable } from 'vs/base/common/lifecycle';
J
Johannes Rieken 已提交
10
import * as Events from 'vs/base/common/events';
J
Johannes Rieken 已提交
11
import Event, { Emitter } from 'vs/base/common/event';
12
import { ITelemetryData } from 'vs/platform/telemetry/common/telemetry';
E
Erich Gamma 已提交
13

J
Johannes Rieken 已提交
14
export interface IAction extends IDisposable {
E
Erich Gamma 已提交
15 16 17 18 19 20
	id: string;
	label: string;
	tooltip: string;
	class: string;
	enabled: boolean;
	checked: boolean;
21
	radio: boolean;
A
Alex Dima 已提交
22
	run(event?: any): TPromise<any>;
E
Erich Gamma 已提交
23 24 25
}

export interface IActionRunner extends IEventEmitter {
A
Alex Dima 已提交
26
	run(action: IAction, context?: any): TPromise<any>;
E
Erich Gamma 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39
}

export interface IActionItem extends IEventEmitter {
	actionRunner: IActionRunner;
	setActionContext(context: any): void;
	render(element: any /* HTMLElement */): void;
	isEnabled(): boolean;
	focus(): void;
	blur(): void;
	dispose(): void;
}

/**
P
Pascal Borreli 已提交
40
 * Checks if the provided object is compatible
E
Erich Gamma 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
 * with the IAction interface.
 * @param thing an object
 */
export function isAction(thing: any): thing is IAction {
	if (!thing) {
		return false;
	} else if (thing instanceof Action) {
		return true;
	} else if (typeof thing.id !== 'string') {
		return false;
	} else if (typeof thing.label !== 'string') {
		return false;
	} else if (typeof thing.class !== 'string') {
		return false;
	} else if (typeof thing.enabled !== 'boolean') {
		return false;
	} else if (typeof thing.checked !== 'boolean') {
		return false;
	} else if (typeof thing.run !== 'function') {
		return false;
	} else {
		return true;
	}
}

66 67 68 69 70 71
export interface IActionChangeEvent {
	label?: string;
	tooltip?: string;
	class?: string;
	enabled?: boolean;
	checked?: boolean;
72
	radio?: boolean;
73
}
E
Erich Gamma 已提交
74

75
export class Action implements IAction {
E
Erich Gamma 已提交
76

77
	protected _onDidChange = new Emitter<IActionChangeEvent>();
78 79 80 81 82 83
	protected _id: string;
	protected _label: string;
	protected _tooltip: string;
	protected _cssClass: string;
	protected _enabled: boolean;
	protected _checked: boolean;
84
	protected _radio: boolean;
85
	protected _order: number;
86
	protected _actionCallback: (event?: any) => TPromise<any>;
87

88
	constructor(id: string, label: string = '', cssClass: string = '', enabled: boolean = true, actionCallback?: (event?: any) => TPromise<any>) {
E
Erich Gamma 已提交
89 90 91 92 93 94 95
		this._id = id;
		this._label = label;
		this._cssClass = cssClass;
		this._enabled = enabled;
		this._actionCallback = actionCallback;
	}

96 97 98 99 100 101 102 103
	public dispose() {
		this._onDidChange.dispose();
	}

	public get onDidChange(): Event<IActionChangeEvent> {
		return this._onDidChange.event;
	}

E
Erich Gamma 已提交
104 105 106 107 108 109 110 111 112 113 114 115
	public get id(): string {
		return this._id;
	}

	public get label(): string {
		return this._label;
	}

	public set label(value: string) {
		this._setLabel(value);
	}

116
	protected _setLabel(value: string): void {
E
Erich Gamma 已提交
117 118
		if (this._label !== value) {
			this._label = value;
119
			this._onDidChange.fire({ label: value });
E
Erich Gamma 已提交
120 121 122 123 124 125 126 127 128 129 130
		}
	}

	public get tooltip(): string {
		return this._tooltip;
	}

	public set tooltip(value: string) {
		this._setTooltip(value);
	}

131
	protected _setTooltip(value: string): void {
E
Erich Gamma 已提交
132 133
		if (this._tooltip !== value) {
			this._tooltip = value;
134
			this._onDidChange.fire({ tooltip: value });
E
Erich Gamma 已提交
135 136 137 138 139 140 141 142 143 144 145
		}
	}

	public get class(): string {
		return this._cssClass;
	}

	public set class(value: string) {
		this._setClass(value);
	}

146
	protected _setClass(value: string): void {
E
Erich Gamma 已提交
147 148
		if (this._cssClass !== value) {
			this._cssClass = value;
149
			this._onDidChange.fire({ class: value });
E
Erich Gamma 已提交
150 151 152 153 154 155 156 157 158 159 160
		}
	}

	public get enabled(): boolean {
		return this._enabled;
	}

	public set enabled(value: boolean) {
		this._setEnabled(value);
	}

161
	protected _setEnabled(value: boolean): void {
E
Erich Gamma 已提交
162 163
		if (this._enabled !== value) {
			this._enabled = value;
164
			this._onDidChange.fire({ enabled: value });
E
Erich Gamma 已提交
165 166 167 168 169 170 171 172 173 174 175
		}
	}

	public get checked(): boolean {
		return this._checked;
	}

	public set checked(value: boolean) {
		this._setChecked(value);
	}

176 177 178 179 180 181 182 183
	public get radio(): boolean {
		return this._radio;
	}

	public set radio(value: boolean) {
		this._setRadio(value);
	}

184
	protected _setChecked(value: boolean): void {
E
Erich Gamma 已提交
185 186
		if (this._checked !== value) {
			this._checked = value;
187
			this._onDidChange.fire({ checked: value });
E
Erich Gamma 已提交
188 189 190
		}
	}

191 192 193 194 195 196 197
	protected _setRadio(value: boolean): void {
		if (this._radio !== value) {
			this._radio = value;
			this._onDidChange.fire({ radio: value });
		}
	}

E
Erich Gamma 已提交
198 199 200 201 202 203 204 205
	public get order(): number {
		return this._order;
	}

	public set order(value: number) {
		this._order = value;
	}

206
	public run(event?: any, data?: ITelemetryData): TPromise<any> {
207
		if (this._actionCallback !== void 0) {
E
Erich Gamma 已提交
208 209
			return this._actionCallback(event);
		}
210
		return TPromise.as(true);
E
Erich Gamma 已提交
211 212 213 214 215 216 217 218 219 220 221
	}
}

export interface IRunEvent {
	action: IAction;
	result?: any;
	error?: any;
}

export class ActionRunner extends EventEmitter implements IActionRunner {

A
Alex Dima 已提交
222
	public run(action: IAction, context?: any): TPromise<any> {
E
Erich Gamma 已提交
223
		if (!action.enabled) {
A
Alex Dima 已提交
224
			return TPromise.as(null);
E
Erich Gamma 已提交
225 226 227 228
		}

		this.emit(Events.EventType.BEFORE_RUN, { action: action });

229
		return this.runAction(action, context).then((result: any) => {
E
Erich Gamma 已提交
230 231 232 233 234
			this.emit(Events.EventType.RUN, <IRunEvent>{ action: action, result: result });
		}, (error: any) => {
			this.emit(Events.EventType.RUN, <IRunEvent>{ action: action, error: error });
		});
	}
235 236

	protected runAction(action: IAction, context?: any): TPromise<any> {
237
		return TPromise.as(context ? action.run(context) : action.run());
238
	}
E
Erich Gamma 已提交
239
}