1 |
efrain |
1 |
// This file is part of Moodle - http://moodle.org/
|
|
|
2 |
//
|
|
|
3 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
4 |
// it under the terms of the GNU General Public License as published by
|
|
|
5 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
6 |
// (at your option) any later version.
|
|
|
7 |
//
|
|
|
8 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
9 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
10 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
11 |
// GNU General Public License for more details.
|
|
|
12 |
//
|
|
|
13 |
// You should have received a copy of the GNU General Public License
|
|
|
14 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
15 |
|
|
|
16 |
/**
|
|
|
17 |
* Drag and drop helper component.
|
|
|
18 |
*
|
|
|
19 |
* This component is used to delegate drag and drop handling.
|
|
|
20 |
*
|
|
|
21 |
* To delegate the logic to this particular element the component should create a new instance
|
|
|
22 |
* passing "this" as param. The component will use all the necessary callbacks and add all the
|
|
|
23 |
* necessary listeners to the component element.
|
|
|
24 |
*
|
|
|
25 |
* Component attributes used by dragdrop module:
|
|
|
26 |
* - element: the draggable or dropzone element.
|
|
|
27 |
* - (optional) classes: object with alternative CSS classes
|
|
|
28 |
* - (optional) fullregion: page element affeted by the elementy dragging. Use this attribute if
|
|
|
29 |
* the draggable element affects a bigger region (for example a draggable
|
|
|
30 |
* title).
|
|
|
31 |
* - (optional) autoconfigDraggable: by default, the component will be draggable if it has a
|
|
|
32 |
* getDraggableData method. If this value is false draggable
|
|
|
33 |
* property must be defined using setDraggable method.
|
|
|
34 |
* - (optional) relativeDrag: by default the drag image is located at point (0,0) relative to the
|
|
|
35 |
* mouse position to prevent the mouse from covering it. If this attribute
|
|
|
36 |
* is true the drag image will be located at the click offset.
|
|
|
37 |
*
|
|
|
38 |
* Methods the parent component should have for making it draggable:
|
|
|
39 |
*
|
|
|
40 |
* - getDraggableData(): Object|data
|
|
|
41 |
* Return the data that will be passed to any valid dropzone while it is dragged.
|
|
|
42 |
* If the component has this method, the dragdrop module will enable the dragging,
|
|
|
43 |
* this is the only required method for dragging.
|
|
|
44 |
* If at the dragging moment this method returns a false|null|undefined, the dragging
|
|
|
45 |
* actions won't be captured.
|
|
|
46 |
*
|
|
|
47 |
* - (optional) dragStart(Object dropdata, Event event): void
|
|
|
48 |
* - (optional) dragEnd(Object dropdata, Event event): void
|
|
|
49 |
* Callbacks dragdrop will call when the element is dragged and getDraggableData
|
|
|
50 |
* return some data.
|
|
|
51 |
*
|
|
|
52 |
* Methods the parent component should have for enabling it as a dropzone:
|
|
|
53 |
*
|
|
|
54 |
* - validateDropData(Object dropdata): boolean
|
|
|
55 |
* If that method exists, the dragdrop module will automathically configure the element as dropzone.
|
|
|
56 |
* This method will return true if the dropdata is accepted. In case it returns false, no drag and
|
|
|
57 |
* drop event will be listened for this specific dragged dropdata.
|
|
|
58 |
*
|
|
|
59 |
* - (Optional) showDropZone(Object dropdata, Event event): void
|
|
|
60 |
* - (Optional) hideDropZone(Object dropdata, Event event): void
|
|
|
61 |
* Methods called when a valid dragged data pass over the element.
|
|
|
62 |
*
|
|
|
63 |
* - (Optional) drop(Object dropdata, Event event): void
|
|
|
64 |
* Called when a valid dragged element is dropped over the element.
|
|
|
65 |
*
|
|
|
66 |
* Note that none of this methods will be called if validateDropData
|
|
|
67 |
* returns a false value.
|
|
|
68 |
*
|
|
|
69 |
* This module will also add or remove several CSS classes from both dragged elements and dropzones.
|
|
|
70 |
* See the "this.classes" in the create method for more details. In case the parent component wants
|
|
|
71 |
* to use the same classes, it can use the getClasses method. On the other hand, if the parent
|
|
|
72 |
* component has an alternative "classes" attribute, this will override the default drag and drop
|
|
|
73 |
* classes.
|
|
|
74 |
*
|
|
|
75 |
* @module core/local/reactive/dragdrop
|
|
|
76 |
* @class core/local/reactive/dragdrop
|
|
|
77 |
* @copyright 2021 Ferran Recio <ferran@moodle.com>
|
|
|
78 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
79 |
*/
|
|
|
80 |
|
|
|
81 |
import BaseComponent from 'core/local/reactive/basecomponent';
|
|
|
82 |
|
|
|
83 |
// Map with the dragged element generate by an specific reactive applications.
|
|
|
84 |
// Potentially, any component can generate a draggable element to interact with other
|
|
|
85 |
// page elements. However, the dragged data is specific and could only interact with
|
|
|
86 |
// components of the same reactive instance.
|
|
|
87 |
let activeDropData = new Map();
|
|
|
88 |
|
|
|
89 |
// Drag & Drop API provides the final drop point and incremental movements but we can
|
|
|
90 |
// provide also starting points and displacements. Absolute displacements simplifies
|
|
|
91 |
// moving components with aboslute position around the page.
|
|
|
92 |
let dragStartPoint = {};
|
|
|
93 |
|
|
|
94 |
export default class extends BaseComponent {
|
|
|
95 |
|
|
|
96 |
/**
|
|
|
97 |
* Constructor hook.
|
|
|
98 |
*
|
|
|
99 |
* @param {BaseComponent} parent the parent component.
|
|
|
100 |
*/
|
|
|
101 |
create(parent) {
|
|
|
102 |
// Optional component name for debugging.
|
|
|
103 |
this.name = `${parent.name ?? 'unkown'}_dragdrop`;
|
|
|
104 |
|
|
|
105 |
// Default drag and drop classes.
|
|
|
106 |
this.classes = Object.assign(
|
|
|
107 |
{
|
|
|
108 |
// This class indicate a dragging action is active at a page level.
|
|
|
109 |
BODYDRAGGING: 'dragging',
|
|
|
110 |
|
|
|
111 |
// Added when draggable and drop are ready.
|
|
|
112 |
DRAGGABLEREADY: 'draggable',
|
|
|
113 |
DROPREADY: 'dropready',
|
|
|
114 |
|
|
|
115 |
// When a valid drag element is over the element.
|
|
|
116 |
DRAGOVER: 'dragover',
|
|
|
117 |
// When a the component is dragged.
|
|
|
118 |
DRAGGING: 'dragging',
|
|
|
119 |
|
|
|
120 |
// Dropzones classes names.
|
|
|
121 |
DROPUP: 'drop-up',
|
|
|
122 |
DROPDOWN: 'drop-down',
|
|
|
123 |
DROPZONE: 'drop-zone',
|
|
|
124 |
|
|
|
125 |
// Drag icon class.
|
|
|
126 |
DRAGICON: 'dragicon',
|
|
|
127 |
},
|
|
|
128 |
parent?.classes ?? {}
|
|
|
129 |
);
|
|
|
130 |
|
|
|
131 |
// Add the affected region if any.
|
|
|
132 |
this.fullregion = parent.fullregion;
|
|
|
133 |
|
|
|
134 |
// Keep parent to execute drap and drop handlers.
|
|
|
135 |
this.parent = parent;
|
|
|
136 |
|
|
|
137 |
// Check if parent handle draggable manually.
|
|
|
138 |
this.autoconfigDraggable = this.parent.draggable ?? true;
|
|
|
139 |
|
|
|
140 |
// Drag image relative position.
|
|
|
141 |
this.relativeDrag = this.parent.relativeDrag ?? false;
|
|
|
142 |
|
|
|
143 |
// Sub HTML elements will trigger extra dragEnter and dragOver all the time.
|
|
|
144 |
// To prevent that from affecting dropzones, we need to count the enters and leaves.
|
|
|
145 |
this.entercount = 0;
|
|
|
146 |
|
|
|
147 |
// Stores if the droparea is shown or not.
|
|
|
148 |
this.dropzonevisible = false;
|
|
|
149 |
|
|
|
150 |
// Stores if the mouse is over the element or not.
|
|
|
151 |
this.ismouseover = false;
|
|
|
152 |
}
|
|
|
153 |
|
|
|
154 |
/**
|
|
|
155 |
* Return the component drag and drop CSS classes.
|
|
|
156 |
*
|
|
|
157 |
* @returns {Object} the dragdrop css classes
|
|
|
158 |
*/
|
|
|
159 |
getClasses() {
|
|
|
160 |
return this.classes;
|
|
|
161 |
}
|
|
|
162 |
|
|
|
163 |
/**
|
|
|
164 |
* Return the current drop-zone visible of the element.
|
|
|
165 |
*
|
|
|
166 |
* @returns {boolean} if the dropzone should be visible or not
|
|
|
167 |
*/
|
|
|
168 |
isDropzoneVisible() {
|
|
|
169 |
return this.dropzonevisible;
|
|
|
170 |
}
|
|
|
171 |
|
|
|
172 |
/**
|
|
|
173 |
* Initial state ready method.
|
|
|
174 |
*
|
|
|
175 |
* This method will add all the necessary event listeners to the component depending on the
|
|
|
176 |
* parent methods.
|
|
|
177 |
* - Add drop events to the element if the parent component has validateDropData method.
|
|
|
178 |
* - Configure the elements draggable if the parent component has getDraggableData method.
|
|
|
179 |
*/
|
|
|
180 |
stateReady() {
|
|
|
181 |
// Add drop events to the element if the parent component has dropable types.
|
|
|
182 |
if (typeof this.parent.validateDropData === 'function') {
|
|
|
183 |
this.element.classList.add(this.classes.DROPREADY);
|
|
|
184 |
this.addEventListener(this.element, 'dragenter', this._dragEnter);
|
|
|
185 |
this.addEventListener(this.element, 'dragleave', this._dragLeave);
|
|
|
186 |
this.addEventListener(this.element, 'dragover', this._dragOver);
|
|
|
187 |
this.addEventListener(this.element, 'drop', this._drop);
|
|
|
188 |
this.addEventListener(this.element, 'mouseover', this._mouseOver);
|
|
|
189 |
this.addEventListener(this.element, 'mouseleave', this._mouseLeave);
|
|
|
190 |
}
|
|
|
191 |
|
|
|
192 |
// Configure the elements draggable if the parent component has dragable data.
|
|
|
193 |
if (this.autoconfigDraggable && typeof this.parent.getDraggableData === 'function') {
|
|
|
194 |
this.setDraggable(true);
|
|
|
195 |
}
|
|
|
196 |
}
|
|
|
197 |
|
|
|
198 |
/**
|
|
|
199 |
* Enable or disable the draggable property.
|
|
|
200 |
*
|
|
|
201 |
* @param {bool} value the new draggable value
|
|
|
202 |
*/
|
|
|
203 |
setDraggable(value) {
|
|
|
204 |
if (typeof this.parent.getDraggableData !== 'function') {
|
|
|
205 |
throw new Error(`Draggable components must have a getDraggableData method`);
|
|
|
206 |
}
|
|
|
207 |
this.element.setAttribute('draggable', value);
|
|
|
208 |
if (value) {
|
|
|
209 |
this.addEventListener(this.element, 'dragstart', this._dragStart);
|
|
|
210 |
this.addEventListener(this.element, 'dragend', this._dragEnd);
|
|
|
211 |
this.element.classList.add(this.classes.DRAGGABLEREADY);
|
|
|
212 |
} else {
|
|
|
213 |
this.removeEventListener(this.element, 'dragstart', this._dragStart);
|
|
|
214 |
this.removeEventListener(this.element, 'dragend', this._dragEnd);
|
|
|
215 |
this.element.classList.remove(this.classes.DRAGGABLEREADY);
|
|
|
216 |
}
|
|
|
217 |
}
|
|
|
218 |
|
|
|
219 |
/**
|
|
|
220 |
* Mouse over handle.
|
|
|
221 |
*/
|
|
|
222 |
_mouseOver() {
|
|
|
223 |
this.ismouseover = true;
|
|
|
224 |
}
|
|
|
225 |
|
|
|
226 |
/**
|
|
|
227 |
* Mouse leave handler.
|
|
|
228 |
*/
|
|
|
229 |
_mouseLeave() {
|
|
|
230 |
this.ismouseover = false;
|
|
|
231 |
}
|
|
|
232 |
|
|
|
233 |
/**
|
|
|
234 |
* Drag start event handler.
|
|
|
235 |
*
|
|
|
236 |
* This method will generate the current dropable data. This data is the one used to determine
|
|
|
237 |
* if a droparea accepts the dropping or not.
|
|
|
238 |
*
|
|
|
239 |
* @param {Event} event the event.
|
|
|
240 |
*/
|
|
|
241 |
_dragStart(event) {
|
|
|
242 |
// Cancel dragging if any editable form element is focussed.
|
|
|
243 |
if (document.activeElement.matches(`textarea, input`)) {
|
|
|
244 |
event.preventDefault();
|
|
|
245 |
return;
|
|
|
246 |
}
|
|
|
247 |
|
|
|
248 |
const dropdata = this.parent.getDraggableData();
|
|
|
249 |
if (!dropdata) {
|
|
|
250 |
return;
|
|
|
251 |
}
|
|
|
252 |
|
|
|
253 |
// Save the starting point.
|
|
|
254 |
dragStartPoint = {
|
|
|
255 |
pageX: event.pageX,
|
|
|
256 |
pageY: event.pageY,
|
|
|
257 |
};
|
|
|
258 |
|
|
|
259 |
// If the drag event is accepted we prevent any other draggable element from interfiering.
|
|
|
260 |
event.stopPropagation();
|
|
|
261 |
|
|
|
262 |
// Save the drop data of the current reactive intance.
|
|
|
263 |
activeDropData.set(this.reactive, dropdata);
|
|
|
264 |
|
|
|
265 |
// Add some CSS classes to indicate the state.
|
|
|
266 |
document.body.classList.add(this.classes.BODYDRAGGING);
|
|
|
267 |
this.element.classList.add(this.classes.DRAGGING);
|
|
|
268 |
this.fullregion?.classList.add(this.classes.DRAGGING);
|
|
|
269 |
|
|
|
270 |
// Force the drag image. This makes the UX more consistent in case the
|
|
|
271 |
// user dragged an internal element like a link or some other element.
|
|
|
272 |
let dragImage = this.element;
|
|
|
273 |
if (this.parent.setDragImage !== undefined) {
|
|
|
274 |
const customImage = this.parent.setDragImage(dropdata, event);
|
|
|
275 |
if (customImage) {
|
|
|
276 |
dragImage = customImage;
|
|
|
277 |
}
|
|
|
278 |
}
|
|
|
279 |
// Define the image position relative to the mouse.
|
|
|
280 |
const position = {x: 0, y: 0};
|
|
|
281 |
if (this.relativeDrag) {
|
|
|
282 |
position.x = event.offsetX;
|
|
|
283 |
position.y = event.offsetY;
|
|
|
284 |
}
|
|
|
285 |
event.dataTransfer.setDragImage(dragImage, position.x, position.y);
|
|
|
286 |
event.dataTransfer.effectAllowed = 'copyMove';
|
|
|
287 |
this._callParentMethod('dragStart', dropdata, event);
|
|
|
288 |
}
|
|
|
289 |
|
|
|
290 |
/**
|
|
|
291 |
* Drag end event handler.
|
|
|
292 |
*
|
|
|
293 |
* @param {Event} event the event.
|
|
|
294 |
*/
|
|
|
295 |
_dragEnd(event) {
|
|
|
296 |
const dropdata = activeDropData.get(this.reactive);
|
|
|
297 |
if (!dropdata) {
|
|
|
298 |
return;
|
|
|
299 |
}
|
|
|
300 |
|
|
|
301 |
// Remove the current dropdata.
|
|
|
302 |
activeDropData.delete(this.reactive);
|
|
|
303 |
|
|
|
304 |
// Remove the dragging classes.
|
|
|
305 |
document.body.classList.remove(this.classes.BODYDRAGGING);
|
|
|
306 |
this.element.classList.remove(this.classes.DRAGGING);
|
|
|
307 |
this.fullregion?.classList.remove(this.classes.DRAGGING);
|
|
|
308 |
this.removeAllOverlays();
|
|
|
309 |
|
|
|
310 |
// We add the total movement to the event in case the component
|
|
|
311 |
// wants to move its absolute position.
|
|
|
312 |
this._addEventTotalMovement(event);
|
|
|
313 |
|
|
|
314 |
this._callParentMethod('dragEnd', dropdata, event);
|
|
|
315 |
}
|
|
|
316 |
|
|
|
317 |
/**
|
|
|
318 |
* Drag enter event handler.
|
|
|
319 |
*
|
|
|
320 |
* The JS drag&drop API triggers several dragenter events on the same element because it bubbles the
|
|
|
321 |
* child events as well. To prevent this form affecting the dropzones display, this methods use
|
|
|
322 |
* "entercount" to determine if it's one extra child event or a valid one.
|
|
|
323 |
*
|
|
|
324 |
* @param {Event} event the event.
|
|
|
325 |
*/
|
|
|
326 |
_dragEnter(event) {
|
|
|
327 |
const dropdata = this._processEvent(event);
|
|
|
328 |
if (dropdata) {
|
|
|
329 |
this.entercount++;
|
|
|
330 |
this.element.classList.add(this.classes.DRAGOVER);
|
|
|
331 |
if (this.entercount == 1 && !this.dropzonevisible) {
|
|
|
332 |
this.dropzonevisible = true;
|
|
|
333 |
this.element.classList.add(this.classes.DRAGOVER);
|
|
|
334 |
this._callParentMethod('showDropZone', dropdata, event);
|
|
|
335 |
}
|
|
|
336 |
}
|
|
|
337 |
}
|
|
|
338 |
|
|
|
339 |
/**
|
|
|
340 |
* Drag over event handler.
|
|
|
341 |
*
|
|
|
342 |
* We only use dragover event when a draggable action starts inside a valid dropzone. In those cases
|
|
|
343 |
* the API won't trigger any dragEnter because the dragged alement was already there. We use the
|
|
|
344 |
* dropzonevisible to determine if the component needs to display the dropzones or not.
|
|
|
345 |
*
|
|
|
346 |
* @param {Event} event the event.
|
|
|
347 |
*/
|
|
|
348 |
_dragOver(event) {
|
|
|
349 |
const dropdata = this._processEvent(event);
|
|
|
350 |
event.dataTransfer.dropEffect = (event.altKey) ? 'copy' : 'move';
|
|
|
351 |
if (dropdata && !this.dropzonevisible) {
|
|
|
352 |
this.dropzonevisible = true;
|
|
|
353 |
this.element.classList.add(this.classes.DRAGOVER);
|
|
|
354 |
this._callParentMethod('showDropZone', dropdata, event);
|
|
|
355 |
}
|
|
|
356 |
}
|
|
|
357 |
|
|
|
358 |
/**
|
|
|
359 |
* Drag over leave handler.
|
|
|
360 |
*
|
|
|
361 |
* The JS drag&drop API triggers several dragleave events on the same element because it bubbles the
|
|
|
362 |
* child events as well. To prevent this form affecting the dropzones display, this methods use
|
|
|
363 |
* "entercount" to determine if it's one extra child event or a valid one.
|
|
|
364 |
*
|
|
|
365 |
* @param {Event} event the event.
|
|
|
366 |
*/
|
|
|
367 |
_dragLeave(event) {
|
|
|
368 |
const dropdata = this._processEvent(event);
|
|
|
369 |
if (dropdata) {
|
|
|
370 |
this.entercount--;
|
|
|
371 |
if (this.entercount <= 0 && this.dropzonevisible) {
|
|
|
372 |
this.dropzonevisible = false;
|
|
|
373 |
this.element.classList.remove(this.classes.DRAGOVER);
|
|
|
374 |
this._callParentMethod('hideDropZone', dropdata, event);
|
|
|
375 |
}
|
|
|
376 |
}
|
|
|
377 |
}
|
|
|
378 |
|
|
|
379 |
/**
|
|
|
380 |
* Drop event handler.
|
|
|
381 |
*
|
|
|
382 |
* This method will call both hideDropZones and drop methods on the parent component.
|
|
|
383 |
*
|
|
|
384 |
* @param {Event} event the event.
|
|
|
385 |
*/
|
|
|
386 |
_drop(event) {
|
|
|
387 |
const dropdata = this._processEvent(event);
|
|
|
388 |
if (dropdata) {
|
|
|
389 |
this.entercount = 0;
|
|
|
390 |
if (this.dropzonevisible) {
|
|
|
391 |
this.dropzonevisible = false;
|
|
|
392 |
this._callParentMethod('hideDropZone', dropdata, event);
|
|
|
393 |
}
|
|
|
394 |
this.element.classList.remove(this.classes.DRAGOVER);
|
|
|
395 |
this.removeAllOverlays();
|
|
|
396 |
this._callParentMethod('drop', dropdata, event);
|
|
|
397 |
// An accepted drop resets the initial position.
|
|
|
398 |
// Save the starting point.
|
|
|
399 |
dragStartPoint = {};
|
|
|
400 |
}
|
|
|
401 |
}
|
|
|
402 |
|
|
|
403 |
/**
|
|
|
404 |
* Process a drag and drop event and delegate logic to the parent component.
|
|
|
405 |
*
|
|
|
406 |
* @param {Event} event the drag and drop event
|
|
|
407 |
* @return {Object|false} the dropdata or null if the event should not be processed
|
|
|
408 |
*/
|
|
|
409 |
_processEvent(event) {
|
|
|
410 |
const dropdata = this._getDropData(event);
|
|
|
411 |
if (!dropdata) {
|
|
|
412 |
return null;
|
|
|
413 |
}
|
|
|
414 |
if (this.parent.validateDropData(dropdata)) {
|
|
|
415 |
// All accepted drag&drop event must prevent bubbling and defaults, otherwise
|
|
|
416 |
// parent dragdrop instances could capture it by mistake.
|
|
|
417 |
event.preventDefault();
|
|
|
418 |
event.stopPropagation();
|
|
|
419 |
this._addEventTotalMovement(event);
|
|
|
420 |
return dropdata;
|
|
|
421 |
}
|
|
|
422 |
return null;
|
|
|
423 |
}
|
|
|
424 |
|
|
|
425 |
/**
|
|
|
426 |
* Add the total amout of movement to a mouse event.
|
|
|
427 |
*
|
|
|
428 |
* @param {MouseEvent} event
|
|
|
429 |
*/
|
|
|
430 |
_addEventTotalMovement(event) {
|
|
|
431 |
if (dragStartPoint.pageX === undefined || event.pageX === undefined) {
|
|
|
432 |
return;
|
|
|
433 |
}
|
|
|
434 |
event.fixedMovementX = event.pageX - dragStartPoint.pageX;
|
|
|
435 |
event.fixedMovementY = event.pageY - dragStartPoint.pageY;
|
|
|
436 |
event.initialPageX = dragStartPoint.pageX;
|
|
|
437 |
event.initialPageY = dragStartPoint.pageY;
|
|
|
438 |
// The element possible new top.
|
|
|
439 |
const current = this.element.getBoundingClientRect();
|
|
|
440 |
// Add the new position fixed position.
|
|
|
441 |
event.newFixedTop = current.top + event.fixedMovementY;
|
|
|
442 |
event.newFixedLeft = current.left + event.fixedMovementX;
|
|
|
443 |
// The affected region possible new top.
|
|
|
444 |
if (this.fullregion !== undefined) {
|
|
|
445 |
const current = this.fullregion.getBoundingClientRect();
|
|
|
446 |
event.newRegionFixedxTop = current.top + event.fixedMovementY;
|
|
|
447 |
event.newRegionFixedxLeft = current.left + event.fixedMovementX;
|
|
|
448 |
}
|
|
|
449 |
}
|
|
|
450 |
|
|
|
451 |
/**
|
|
|
452 |
* Convenient method for calling parent component functions if present.
|
|
|
453 |
*
|
|
|
454 |
* @param {string} methodname the name of the method
|
|
|
455 |
* @param {Object} dropdata the current drop data object
|
|
|
456 |
* @param {Event} event the original event
|
|
|
457 |
*/
|
|
|
458 |
_callParentMethod(methodname, dropdata, event) {
|
|
|
459 |
if (typeof this.parent[methodname] === 'function') {
|
|
|
460 |
this.parent[methodname](dropdata, event);
|
|
|
461 |
}
|
|
|
462 |
}
|
|
|
463 |
|
|
|
464 |
/**
|
|
|
465 |
* Get the current dropdata for a specific event.
|
|
|
466 |
*
|
|
|
467 |
* The browser can generate drag&drop events related to several user interactions:
|
|
|
468 |
* - Drag a page elements: this case is registered in the activeDropData map
|
|
|
469 |
* - Drag some HTML selections: ignored for now
|
|
|
470 |
* - Drag a file over the browser: file drag may appear in the future but for now they are ignored.
|
|
|
471 |
*
|
|
|
472 |
* @param {Event} event the original event.
|
|
|
473 |
* @returns {Object|undefined} with the dragged data (or undefined if none)
|
|
|
474 |
*/
|
|
|
475 |
_getDropData(event) {
|
|
|
476 |
this._isOnlyFilesDragging = this._containsOnlyFiles(event);
|
|
|
477 |
if (this._isOnlyFilesDragging) {
|
|
|
478 |
// Check if the reactive instance can provide a files draggable data.
|
|
|
479 |
if (this.reactive.getFilesDraggableData !== undefined && typeof this.reactive.getFilesDraggableData === 'function') {
|
|
|
480 |
return this.reactive.getFilesDraggableData(event.dataTransfer);
|
|
|
481 |
}
|
|
|
482 |
return undefined;
|
|
|
483 |
}
|
|
|
484 |
return activeDropData.get(this.reactive);
|
|
|
485 |
}
|
|
|
486 |
|
|
|
487 |
/**
|
|
|
488 |
* Check if the dragged event contains only files.
|
|
|
489 |
*
|
|
|
490 |
* Files dragging does not generate drop data because they came from outside the page and the component
|
|
|
491 |
* must check it before validating the event.
|
|
|
492 |
*
|
|
|
493 |
* Some browsers like Firefox add extra types to file dragging. To discard the false positives
|
|
|
494 |
* a double check is necessary.
|
|
|
495 |
*
|
|
|
496 |
* @param {Event} event the original event.
|
|
|
497 |
* @returns {boolean} if the drag dataTransfers contains files.
|
|
|
498 |
*/
|
|
|
499 |
_containsOnlyFiles(event) {
|
|
|
500 |
if (!event.dataTransfer.types.includes('Files')) {
|
|
|
501 |
return false;
|
|
|
502 |
}
|
|
|
503 |
return event.dataTransfer.types.every((type) => {
|
|
|
504 |
return (type.toLowerCase() != 'text/uri-list'
|
|
|
505 |
&& type.toLowerCase() != 'text/html'
|
|
|
506 |
&& type.toLowerCase() != 'text/plain'
|
|
|
507 |
);
|
|
|
508 |
});
|
|
|
509 |
}
|
|
|
510 |
}
|