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

'use strict';

B
Benjamin Pasero 已提交
8
import {$} from 'vs/base/browser/builder';
E
Erich Gamma 已提交
9 10 11 12 13 14 15 16 17

/**
 * A helper that will execute a provided function when the provided HTMLElement receives
 *  dragover event for 800ms. If the drag is aborted before, the callback will not be triggered.
 */
export class DelayedDragHandler {

	private timeout: number;

B
Benjamin Pasero 已提交
18
	constructor(container: HTMLElement, callback: () => void) {
E
Erich Gamma 已提交
19 20 21 22 23
		$(container).on('dragover', () => {
			if (!this.timeout) {
				this.timeout = setTimeout(() => {
					callback();

B
Benjamin Pasero 已提交
24
					this.timeout = null;
E
Erich Gamma 已提交
25 26 27 28 29 30 31 32 33 34
				}, 800);
			}
		});

		$(container).on(['dragleave', 'drop', 'dragend'], () => this.clearDragTimeout());
	}

	private clearDragTimeout(): void {
		if (this.timeout) {
			clearTimeout(this.timeout);
B
Benjamin Pasero 已提交
35
			this.timeout = null;
E
Erich Gamma 已提交
36 37 38 39 40 41 42
		}
	}

	public dispose(): void {
		this.clearDragTimeout();
	}
}