2
* Copyright (C) 2019 Apple Inc. All rights reserved.
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions
7
* 1. Redistributions of source code must retain the above copyright
8
* notice, this list of conditions and the following disclaimer.
9
* 2. Redistributions in binary form must reproduce the above copyright
10
* notice, this list of conditions and the following disclaimer in the
11
* documentation and/or other materials provided with the distribution.
13
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23
* THE POSSIBILITY OF SUCH DAMAGE.
26
// DropZoneView creates an invisible drop zone when a drag enters a target element.
27
// There are delegate methods for deciding if the drop zone should appear, drag
28
// progress such as entering and leaving, and the drop itself. Clients should
29
// always initialize with a delegate and set a target element before showing.
31
WI.DropZoneView = class DropZoneView extends WI.View
35
console.assert(delegate);
36
console.assert(typeof delegate.dropZoneShouldAppearForDragEvent === "function");
40
this._delegate = delegate;
41
this._targetElement = null;
42
this._activelyHandlingDrag = false;
44
this.element.classList.add("drop-zone");
49
get delegate() { return this._delegate; }
53
return this._targetElement;
56
set targetElement(element)
58
console.assert(!this._activelyHandlingDrag);
60
if (this._targetElement === element)
63
if (!this._boundHandleDragEnter)
64
this._boundHandleDragEnter = this._handleDragEnter.bind(this);
66
if (this._targetElement)
67
this._targetElement.removeEventListener("dragenter", this._boundHandleDragEnter);
69
this._targetElement = element;
71
if (this._targetElement)
72
this._targetElement.addEventListener("dragenter", this._boundHandleDragEnter);
77
this.element.textContent = text;
84
super.initialLayout();
86
console.assert(this._targetElement);
88
this.element.addEventListener("dragover", this._handleDragOver.bind(this));
89
this.element.addEventListener("dragleave", this._handleDragLeave.bind(this));
90
this.element.addEventListener("drop", this._handleDrop.bind(this));
97
console.assert(!this._activelyHandlingDrag);
98
this._activelyHandlingDrag = true;
99
this.element.classList.add("visible");
104
console.assert(this._activelyHandlingDrag);
105
this._activelyHandlingDrag = false;
106
this.element.classList.remove("visible");
109
_handleDragEnter(event)
111
console.assert(this.isAttached);
112
if (this._activelyHandlingDrag)
115
if (!this._delegate.dropZoneShouldAppearForDragEvent(this, event))
118
this._startActiveDrag();
120
if (this._delegate.dropZoneHandleDragEnter)
121
this._delegate.dropZoneHandleDragEnter(this, event);
124
_handleDragLeave(event)
126
if (!this._activelyHandlingDrag)
129
this._stopActiveDrag();
131
if (this._delegate.dropZoneHandleDragLeave)
132
this._delegate.dropZoneHandleDragLeave(this, event);
135
_handleDragOver(event)
137
if (!this._activelyHandlingDrag)
140
event.preventDefault();
142
event.dataTransfer.dropEffect = "copy";
147
if (!this._activelyHandlingDrag)
150
event.preventDefault();
152
this._stopActiveDrag();
154
if (this._delegate.dropZoneHandleDrop)
155
this._delegate.dropZoneHandleDrop(this, event);