|
| 1 | +import { HXElement } from './HXElement'; |
| 2 | + |
| 3 | +export class HXDropZoneElement extends HXElement { |
| 4 | + static get is () { |
| 5 | + return 'hx-drop-zone'; |
| 6 | + } |
| 7 | + |
| 8 | + $onCreate () { |
| 9 | + this._isDocDragging = false; |
| 10 | + this._isZoneDragging = false; |
| 11 | + this._onDocDragLeave = this._onDocDragLeave.bind(this); |
| 12 | + this._onDocDragOver = this._onDocDragOver.bind(this); |
| 13 | + this._onDrop = this._onDrop.bind(this); |
| 14 | + this._stopDragging = this._stopDragging.bind(this); |
| 15 | + } |
| 16 | + |
| 17 | + $onConnect () { |
| 18 | + document.addEventListener('dragleave', this._onDocDragLeave); |
| 19 | + document.addEventListener('dragover', this._onDocDragOver); |
| 20 | + document.addEventListener('drop', this._onDrop); |
| 21 | + this.addEventListener('dragleave', this._onDragLeave); |
| 22 | + this.addEventListener('dragover', this._onDragOver); |
| 23 | + this.addEventListener('drop', this._onDrop); |
| 24 | + } |
| 25 | + |
| 26 | + $onDisconnect () { |
| 27 | + document.removeEventListener('dragleave', this._onDocDragLeave); |
| 28 | + document.removeEventListener('dragover', this._onDocDragOver); |
| 29 | + document.removeEventListener('drop', this._onDrop); |
| 30 | + this.removeEventListener('dragleave', this._onDragLeave); |
| 31 | + this.removeEventListener('dragover', this._onDragOver); |
| 32 | + this.removeEventListener('drop', this._onDrop); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * @readonly |
| 37 | + * @type {String} |
| 38 | + */ |
| 39 | + get drag () { |
| 40 | + return this.getAttribute('drag'); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * @private |
| 45 | + * @returns {Boolean} |
| 46 | + */ |
| 47 | + _isFileDrag (evt) { |
| 48 | + let _types = evt.dataTransfer.types; |
| 49 | + if (_types) { |
| 50 | + if (_types.indexOf) { |
| 51 | + return (_types.indexOf('Files') !== -1); |
| 52 | + } else { |
| 53 | + return _types.contains('Files'); |
| 54 | + } |
| 55 | + } else { |
| 56 | + return false; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + // #2 this gets called when the dragged item leaves the document |
| 61 | + // (leaves to a child element or window altogether) |
| 62 | + /** @private */ |
| 63 | + _onDocDragLeave () { |
| 64 | + window.clearTimeout(this._docDragLeaveTimeout); |
| 65 | + // callback must be an arrow function to preserve "this" |
| 66 | + this._docDragLeaveTimeout = window.setTimeout(this._stopDragging, 250); |
| 67 | + } |
| 68 | + |
| 69 | + // #1 this handler fires continuously as long as the user is dragging on the page |
| 70 | + /** @private */ |
| 71 | + _onDocDragOver (evt) { |
| 72 | + if (!this._isDocDragging) { |
| 73 | + this._isDocDragging = true; |
| 74 | + if (this._isFileDrag(evt)) { |
| 75 | + this.setAttribute('drag', 'away'); |
| 76 | + } |
| 77 | + } |
| 78 | + window.clearTimeout(this._docDragLeaveTimeout); |
| 79 | + } |
| 80 | + |
| 81 | + // #4 this gets called when the dragged item leaves the zone |
| 82 | + // (leaves to a child element or zone altogether) |
| 83 | + /** @private */ |
| 84 | + _onDragLeave () { |
| 85 | + window.clearTimeout(this._zoneDragLeaveTimeout); |
| 86 | + // callback must be an arrow function to preserve "this" |
| 87 | + this._zoneDragLeaveTimeout = window.setTimeout(() => { |
| 88 | + this._isZoneDragging = false; |
| 89 | + this.setAttribute('drag', 'away'); |
| 90 | + }, 0); |
| 91 | + } |
| 92 | + |
| 93 | + // #3 this handler fires continuously as long as the user is dragging on the zone |
| 94 | + /** @private */ |
| 95 | + _onDragOver (evt) { |
| 96 | + evt.preventDefault(); // needed for onDrop to work |
| 97 | + if (!this._isZoneDragging) { |
| 98 | + this._isZoneDragging = true; |
| 99 | + if (this._isFileDrag(evt)) { |
| 100 | + this.setAttribute('drag', 'over'); |
| 101 | + } |
| 102 | + } |
| 103 | + window.clearTimeout(this._docDragLeaveTimeout); |
| 104 | + window.clearTimeout(this._zoneDragLeaveTimeout); |
| 105 | + } |
| 106 | + |
| 107 | + /** @private */ |
| 108 | + _onDrop () { |
| 109 | + this._stopDragging(); |
| 110 | + } |
| 111 | + |
| 112 | + /** @private */ |
| 113 | + _stopDragging () { |
| 114 | + this.removeAttribute('drag'); |
| 115 | + this._isDocDragging = false; |
| 116 | + this._isZoneDragging = false; |
| 117 | + } |
| 118 | +} |
0 commit comments