-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdroppable.umd.js
186 lines (183 loc) · 7.86 KB
/
droppable.umd.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global.Droppable = factory());
}(this, (function () { 'use strict';
// Import here Polyfills if needed. Recommended core-js (npm i -D core-js)
// import "core-js/fn/array.find"
// ...}
/**
* @author Joel Hernandez <[email protected]>
*/
var Droppable = /** @class */ (function () {
function Droppable(config) {
this.onFilesDroppedEventListeners = [];
config = config || {};
if (!config.element) {
throw new Error('config.element: HTMLElement is required');
}
// This must be called before calling setAcceptsMultipleFiles
this.virtualInputElement = Droppable.makeVirtualInputElement();
var isClickable = typeof config.isClickable === 'boolean' ? config.isClickable : true;
var acceptsMultipleFiles = typeof config.acceptsMultipleFiles === 'boolean' ? config.acceptsMultipleFiles : true;
var appendStatusClasses = typeof config.appendStatusClasses === 'boolean' ? config.appendStatusClasses : true;
this.dragOverClass = typeof config.dragOverClass === 'string' ? config.dragOverClass : 'dragover';
this.setIsClickable(isClickable);
this.setAcceptsMultipleFiles(acceptsMultipleFiles);
this.setAppendStatusClasses(appendStatusClasses);
this.element = config.element;
this.elementEventsRemover = this.registerElementEvents();
Droppable.addAccessibilityAttributesToDroppableElement(this.element);
this.virtualInputElementEventsRemover = this.registerVirtualInputElementEvents();
}
Droppable.makeVirtualInputElement = function () {
var input = document.createElement('input');
input.setAttribute('type', 'file');
input.style.display = 'none';
return input;
};
Droppable.addAccessibilityAttributesToDroppableElement = function (element) {
element.tabIndex = 0;
element.role = 'button';
};
Droppable.removeAccessibilityAttributesToDroppableElement = function (element) {
delete element.role;
element.removeAttribute('tabIndex');
};
Droppable.prototype.onFilesDropped = function (listener) {
var _this = this;
this.onFilesDroppedEventListeners.push(listener);
return function () {
var listenerIndex = _this.onFilesDroppedEventListeners.indexOf(listener);
_this.onFilesDroppedEventListeners.splice(listenerIndex, 1);
};
};
Droppable.prototype.destroy = function () {
this.elementEventsRemover();
this.virtualInputElementEventsRemover();
this.onFilesDroppedEventListeners = [];
Droppable.removeAccessibilityAttributesToDroppableElement(this.element);
};
Droppable.prototype.getLatestDroppedFiles = function () {
if (this.latestDroppedFiles) {
return this.latestDroppedFiles;
}
return [];
};
Droppable.prototype.promptForFiles = function () {
this.virtualInputElement.click();
};
Droppable.prototype.setIsClickable = function (clickable) {
this.isClickable = clickable;
};
Droppable.prototype.setAcceptsMultipleFiles = function (acceptsMultipleFiles) {
this.virtualInputElement.setAttribute('multiple', acceptsMultipleFiles.toString());
};
Droppable.prototype.setAppendStatusClasses = function (appendStatusClasses) {
this.appendStatusClasses = appendStatusClasses;
};
Droppable.prototype.registerElementEvents = function () {
var eventNameToEventListenerDictionary = this.getElementEventsDictionary();
return this.registerElementEventsWithDictionary(this.element, eventNameToEventListenerDictionary);
};
Droppable.prototype.registerVirtualInputElementEvents = function () {
var eventNameToEventListenerDictionary = this.getVirtualInputElementEventsDictionary();
return this.registerElementEventsWithDictionary(this.virtualInputElement, eventNameToEventListenerDictionary);
};
Droppable.prototype.getVirtualInputElementEventsDictionary = function () {
return {
change: this.onVirtualInputElementChange
};
};
Droppable.prototype.getElementEventsDictionary = function () {
return {
dragover: this.onElementDragOver,
dragleave: this.onElementDragLeave,
drop: this.onElementDrop,
click: this.onElementClick,
focus: this.onElementFocus,
focusout: this.onElementFocusOut
};
};
Droppable.prototype.onElementDragOver = function (e) {
e.preventDefault();
e.stopPropagation();
if (this.appendStatusClasses) {
this.element.classList.add(this.dragOverClass);
}
};
Droppable.prototype.onElementDragLeave = function (e) {
e.preventDefault();
e.stopPropagation();
this.element.classList.remove(this.dragOverClass);
};
Droppable.prototype.onElementDrop = function (e) {
e.preventDefault();
e.stopPropagation();
this.element.classList.remove(this.dragOverClass);
this.onDroppableElementChange(e);
};
Droppable.prototype.onElementClick = function () {
if (this.isClickable)
this.promptForFiles();
};
Droppable.prototype.onElementKeyDown = function (e) {
if (e['keyCode'] === Droppable.ENTER_KEY_CODE) {
this.promptForFiles();
this.element.blur();
}
};
Droppable.prototype.onElementFocus = function () {
this.elementKeyDownEventRemover = this.registerElementEventsWithDictionary(this.element, {
keydown: this.onElementKeyDown
});
};
Droppable.prototype.onElementFocusOut = function () {
if (this.elementKeyDownEventRemover)
this.elementKeyDownEventRemover();
};
Droppable.prototype.onVirtualInputElementChange = function (e) {
this.onDroppableElementChange(e);
this.virtualInputElement.value = '';
};
Droppable.prototype.onDroppableElementChange = function (event) {
var files;
if (event['dataTransfer']) {
files = event['dataTransfer'].files;
}
else if (event['target']) {
files = event['target'].files;
}
else {
throw Error('Fired event contains no files');
}
// Files is FileList, we convert to array
var filesArray = Array.from(files);
this.setLatestDrop(filesArray);
};
Droppable.prototype.setLatestDrop = function (files) {
this.latestDroppedFiles = files;
this.emitFilesWereDropped(files);
};
Droppable.prototype.emitFilesWereDropped = function (files) {
this.onFilesDroppedEventListeners.forEach(function (listener) {
listener(files);
});
};
Droppable.prototype.registerElementEventsWithDictionary = function (element, eventNameToEventListenerDictionary) {
var _this = this;
var eventRemovers = [];
Object.keys(eventNameToEventListenerDictionary).forEach(function (eventName) {
var eventListener = eventNameToEventListenerDictionary[eventName];
var boundEventListener = eventListener.bind(_this);
element.addEventListener(eventName, boundEventListener);
eventRemovers.push(function () { return element.removeEventListener(eventName, boundEventListener); });
});
return function () { return eventRemovers.forEach(function (eventRemover) { return eventRemover(); }); };
};
Droppable.ENTER_KEY_CODE = 13;
return Droppable;
}());
return Droppable;
})));
//# sourceMappingURL=droppable.umd.js.map