~mmach/netext73/webkit2gtk

« back to all changes in this revision

Viewing changes to Source/WebInspectorUI/UserInterface/Views/DropZoneView.js

  • Committer: mmach
  • Date: 2023-06-16 17:21:37 UTC
  • Revision ID: netbit73@gmail.com-20230616172137-2rqx6yr96ga9g3kp
1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2019 Apple Inc. All rights reserved.
 
3
 *
 
4
 * Redistribution and use in source and binary forms, with or without
 
5
 * modification, are permitted provided that the following conditions
 
6
 * are met:
 
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.
 
12
 *
 
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.
 
24
 */
 
25
 
 
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.
 
30
 
 
31
WI.DropZoneView = class DropZoneView extends WI.View
 
32
{
 
33
    constructor(delegate)
 
34
    {
 
35
        console.assert(delegate);
 
36
        console.assert(typeof delegate.dropZoneShouldAppearForDragEvent === "function");
 
37
 
 
38
        super();
 
39
 
 
40
        this._delegate = delegate;
 
41
        this._targetElement = null;
 
42
        this._activelyHandlingDrag = false;
 
43
 
 
44
        this.element.classList.add("drop-zone");
 
45
    }
 
46
 
 
47
    // Public
 
48
 
 
49
    get delegate() { return this._delegate; }
 
50
 
 
51
    get targetElement()
 
52
    {
 
53
        return this._targetElement;
 
54
    }
 
55
 
 
56
    set targetElement(element)
 
57
    {
 
58
        console.assert(!this._activelyHandlingDrag);
 
59
 
 
60
        if (this._targetElement === element)
 
61
            return;
 
62
 
 
63
        if (!this._boundHandleDragEnter)
 
64
            this._boundHandleDragEnter = this._handleDragEnter.bind(this);
 
65
 
 
66
        if (this._targetElement)
 
67
            this._targetElement.removeEventListener("dragenter", this._boundHandleDragEnter);
 
68
 
 
69
        this._targetElement = element;
 
70
 
 
71
        if (this._targetElement)
 
72
            this._targetElement.addEventListener("dragenter", this._boundHandleDragEnter);
 
73
    }
 
74
 
 
75
    set text(text)
 
76
    {
 
77
        this.element.textContent = text;
 
78
    }
 
79
 
 
80
    // Protected
 
81
 
 
82
    initialLayout()
 
83
    {
 
84
        super.initialLayout();
 
85
 
 
86
        console.assert(this._targetElement);
 
87
 
 
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));
 
91
    }
 
92
 
 
93
    // Private
 
94
 
 
95
    _startActiveDrag()
 
96
    {
 
97
        console.assert(!this._activelyHandlingDrag);
 
98
        this._activelyHandlingDrag = true;
 
99
        this.element.classList.add("visible");
 
100
    }
 
101
 
 
102
    _stopActiveDrag()
 
103
    {
 
104
        console.assert(this._activelyHandlingDrag);
 
105
        this._activelyHandlingDrag = false;
 
106
        this.element.classList.remove("visible");
 
107
    }
 
108
 
 
109
    _handleDragEnter(event)
 
110
    {
 
111
        console.assert(this.isAttached);
 
112
        if (this._activelyHandlingDrag)
 
113
            return;
 
114
 
 
115
        if (!this._delegate.dropZoneShouldAppearForDragEvent(this, event))
 
116
            return;
 
117
 
 
118
        this._startActiveDrag();
 
119
 
 
120
        if (this._delegate.dropZoneHandleDragEnter)
 
121
            this._delegate.dropZoneHandleDragEnter(this, event);
 
122
    }
 
123
 
 
124
    _handleDragLeave(event)
 
125
    {
 
126
        if (!this._activelyHandlingDrag)
 
127
            return;
 
128
 
 
129
        this._stopActiveDrag();
 
130
 
 
131
        if (this._delegate.dropZoneHandleDragLeave)
 
132
            this._delegate.dropZoneHandleDragLeave(this, event);
 
133
    }
 
134
 
 
135
    _handleDragOver(event)
 
136
    {
 
137
        if (!this._activelyHandlingDrag)
 
138
            return;
 
139
 
 
140
        event.preventDefault();
 
141
 
 
142
        event.dataTransfer.dropEffect = "copy";
 
143
    }
 
144
 
 
145
    _handleDrop(event)
 
146
    {
 
147
        if (!this._activelyHandlingDrag)
 
148
            return;
 
149
 
 
150
        event.preventDefault();
 
151
 
 
152
        this._stopActiveDrag();
 
153
 
 
154
        if (this._delegate.dropZoneHandleDrop)
 
155
            this._delegate.dropZoneHandleDrop(this, event);
 
156
    }
 
157
};