welcomePage.ts 5.8 KB
Newer Older
C
Christof Marti 已提交
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';

7
import 'vs/css!./welcomePage';
C
Christof Marti 已提交
8 9
import URI from 'vs/base/common/uri';
import * as path from 'path';
C
Christof Marti 已提交
10
import { WalkThroughInput } from 'vs/workbench/parts/walkThrough/node/walkThroughInput';
C
Christof Marti 已提交
11 12 13 14 15 16 17 18 19 20 21 22 23
import { IWorkbenchContribution } from 'vs/workbench/common/contributions';
import { IPartService } from 'vs/workbench/services/part/common/partService';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { IWorkbenchEditorService } from 'vs/workbench/services/editor/common/editorService';
import { Position } from 'vs/platform/editor/common/editor';
import { onUnexpectedError } from 'vs/base/common/errors';
import { IWindowService, IWindowsService } from 'vs/platform/windows/common/windows';
import { TPromise } from 'vs/base/common/winjs.base';
import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { IConfigurationEditingService, ConfigurationTarget } from 'vs/workbench/services/configuration/common/configurationEditing';
import { localize } from 'vs/nls';
import { Action } from 'vs/base/common/actions';
C
Christof Marti 已提交
24
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
25
import { WALK_THROUGH_SCHEME } from 'vs/workbench/parts/walkThrough/node/walkThroughContentProvider';
C
Christof Marti 已提交
26 27 28 29 30 31 32 33 34

const enabledKey = 'workbench.welcome.enabled';

export class WelcomePageContribution implements IWorkbenchContribution {

	constructor(
		@IPartService partService: IPartService,
		@IInstantiationService instantiationService: IInstantiationService,
		@IConfigurationService configurationService: IConfigurationService,
C
Christof Marti 已提交
35
		@ITelemetryService telemetryService: ITelemetryService
C
Christof Marti 已提交
36
	) {
C
Christof Marti 已提交
37 38
		const configured = configurationService.lookup<boolean>(enabledKey).value;
		const enabled = typeof configured === 'boolean' ? configured : telemetryService.getExperiments().enableWelcomePage;
C
Christof Marti 已提交
39 40 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 66 67 68 69 70 71 72 73 74 75 76 77 78
		if (enabled) {
			partService.joinCreation().then(() => {
				instantiationService.createInstance(WelcomePage);
			}).then(null, onUnexpectedError);
		}
	}

	public getId() {
		return 'vs.welcomePage';
	}
}

export class WelcomePageAction extends Action {

	public static ID = 'workbench.action.welcomePage';
	public static LABEL = localize('welcomePage', "Welcome");

	constructor(
		id: string,
		label: string,
		@IInstantiationService private instantiationService: IInstantiationService
	) {
		super(id, label);
	}

	public run(): TPromise<void> {
		this.instantiationService.createInstance(WelcomePage);
		return null;
	}
}

class WelcomePage {

	constructor(
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService,
		@IInstantiationService private instantiationService: IInstantiationService,
		@IWindowService private windowService: IWindowService,
		@IWindowsService private windowsService: IWindowsService,
		@IWorkspaceContextService private contextService: IWorkspaceContextService,
		@IConfigurationService private configurationService: IConfigurationService,
C
Christof Marti 已提交
79 80
		@IConfigurationEditingService private configurationEditingService: IConfigurationEditingService,
		@ITelemetryService private telemetryService: ITelemetryService
C
Christof Marti 已提交
81 82 83 84 85 86
	) {
		this.create();
	}

	private create() {
		const recentlyOpened = this.windowService.getRecentlyOpen();
C
Christof Marti 已提交
87 88
		const uri = URI.parse(require.toUrl('./welcomePage.html'))
			.with({ scheme: WALK_THROUGH_SCHEME });
C
Christof Marti 已提交
89
		const input = this.instantiationService.createInstance(WalkThroughInput, localize('welcome.title', "Welcome"), '', uri, 'welcomePage', container => this.onReady(container, recentlyOpened));
C
Christof Marti 已提交
90 91 92 93 94
		this.editorService.openEditor(input, { pinned: true }, Position.ONE)
			.then(null, onUnexpectedError);
	}

	private onReady(container: HTMLElement, recentlyOpened: TPromise<{ files: string[]; folders: string[]; }>): void {
C
Christof Marti 已提交
95 96
		const configured = this.configurationService.lookup<boolean>(enabledKey).value;
		const enabled = typeof configured === 'boolean' ? configured : this.telemetryService.getExperiments().enableWelcomePage;
C
Christof Marti 已提交
97 98 99 100 101 102 103 104 105 106
		const showOnStartup = <HTMLInputElement>container.querySelector('#showOnStartup');
		if (enabled) {
			showOnStartup.setAttribute('checked', 'checked');
		}
		showOnStartup.addEventListener('click', e => {
			this.configurationEditingService.writeConfiguration(ConfigurationTarget.USER, { key: enabledKey, value: showOnStartup.checked })
				.then(null, onUnexpectedError);
		});

		recentlyOpened.then(({folders}) => {
C
Christof Marti 已提交
107 108 109 110 111
			if (!folders.length) {
				const recent = container.querySelector('.recent') as HTMLElement;
				recent.classList.add('empty');
				return;
			}
C
Christof Marti 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
			const ul = container.querySelector('.recent ul');
			if (this.contextService.hasWorkspace()) {
				const current = this.contextService.getWorkspace().resource.fsPath;
				folders = folders.filter(folder => folder !== current);
			}
			folders.slice(0, 5).forEach(folder => {
				const li = document.createElement('li');

				const a = document.createElement('a');
				const name = path.basename(folder);
				a.innerText = name;
				a.title = folder;
				a.href = 'javascript:void(0)';
				a.addEventListener('click', e => {
					this.windowsService.openWindow([folder]);
					e.preventDefault();
C
Christof Marti 已提交
128
					e.stopPropagation();
C
Christof Marti 已提交
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
				});
				li.appendChild(a);

				const span = document.createElement('span');
				span.classList.add('path');
				const parentFolder = path.dirname(folder);
				span.innerText = parentFolder;
				span.title = folder;
				li.appendChild(span);

				ul.appendChild(li);
			});
		}).then(null, onUnexpectedError);
	}
}