file-selection-ui.js
7.47 KB
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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FileSelectionUi = void 0;
var strings_1 = require("../../strings");
var base_1 = require("./base");
var FileSelectionUi = (function () {
function FileSelectionUi(parentElement, showOnRender, onFileSelected) {
this.fileBasedScanRegion = this.createFileBasedScanRegion();
this.fileBasedScanRegion.style.display
= showOnRender ? "block" : "none";
parentElement.appendChild(this.fileBasedScanRegion);
var fileScanLabel = document.createElement("label");
fileScanLabel.setAttribute("for", this.getFileScanInputId());
fileScanLabel.style.display = "inline-block";
this.fileBasedScanRegion.appendChild(fileScanLabel);
this.fileSelectionButton
= base_1.BaseUiElementFactory.createElement("button", base_1.PublicUiElementIdAndClasses.FILE_SELECTION_BUTTON_ID);
this.setInitialValueToButton();
this.fileSelectionButton.addEventListener("click", function (_) {
fileScanLabel.click();
});
fileScanLabel.append(this.fileSelectionButton);
this.fileScanInput
= base_1.BaseUiElementFactory.createElement("input", this.getFileScanInputId());
this.fileScanInput.type = "file";
this.fileScanInput.accept = "image/*";
this.fileScanInput.style.display = "none";
fileScanLabel.appendChild(this.fileScanInput);
var $this = this;
this.fileScanInput.addEventListener("change", function (e) {
if (e == null || e.target == null) {
return;
}
var target = e.target;
if (target.files && target.files.length === 0) {
return;
}
var fileList = target.files;
var file = fileList[0];
var fileName = file.name;
$this.setImageNameToButton(fileName);
onFileSelected(file);
});
var dragAndDropMessage = this.createDragAndDropMessage();
this.fileBasedScanRegion.appendChild(dragAndDropMessage);
this.fileBasedScanRegion.addEventListener("dragenter", function (event) {
$this.fileBasedScanRegion.style.border
= $this.fileBasedScanRegionActiveBorder();
event.stopPropagation();
event.preventDefault();
});
this.fileBasedScanRegion.addEventListener("dragleave", function (event) {
$this.fileBasedScanRegion.style.border
= $this.fileBasedScanRegionDefaultBorder();
event.stopPropagation();
event.preventDefault();
});
this.fileBasedScanRegion.addEventListener("dragover", function (event) {
$this.fileBasedScanRegion.style.border
= $this.fileBasedScanRegionActiveBorder();
event.stopPropagation();
event.preventDefault();
});
this.fileBasedScanRegion.addEventListener("drop", function (event) {
event.stopPropagation();
event.preventDefault();
$this.fileBasedScanRegion.style.border
= $this.fileBasedScanRegionDefaultBorder();
var dataTransfer = event.dataTransfer;
if (dataTransfer) {
var files = dataTransfer.files;
if (!files || files.length === 0) {
return;
}
var isAnyFileImage = false;
for (var i = 0; i < files.length; ++i) {
var file = files.item(i);
if (!file) {
continue;
}
var imageType = /image.*/;
if (!file.type.match(imageType)) {
continue;
}
isAnyFileImage = true;
var fileName = file.name;
$this.setImageNameToButton(fileName);
onFileSelected(file);
dragAndDropMessage.innerText
= strings_1.Html5QrcodeScannerStrings.dragAndDropMessage();
break;
}
if (!isAnyFileImage) {
dragAndDropMessage.innerText
= strings_1.Html5QrcodeScannerStrings
.dragAndDropMessageOnlyImages();
}
}
});
}
FileSelectionUi.prototype.hide = function () {
this.fileBasedScanRegion.style.display = "none";
this.fileScanInput.disabled = true;
};
FileSelectionUi.prototype.show = function () {
this.fileBasedScanRegion.style.display = "block";
this.fileScanInput.disabled = false;
};
FileSelectionUi.prototype.isShowing = function () {
return this.fileBasedScanRegion.style.display === "block";
};
FileSelectionUi.prototype.resetValue = function () {
this.fileScanInput.value = "";
this.setInitialValueToButton();
};
FileSelectionUi.prototype.createFileBasedScanRegion = function () {
var fileBasedScanRegion = document.createElement("div");
fileBasedScanRegion.style.textAlign = "center";
fileBasedScanRegion.style.margin = "auto";
fileBasedScanRegion.style.width = "80%";
fileBasedScanRegion.style.maxWidth = "600px";
fileBasedScanRegion.style.border
= this.fileBasedScanRegionDefaultBorder();
fileBasedScanRegion.style.padding = "10px";
fileBasedScanRegion.style.marginBottom = "10px";
return fileBasedScanRegion;
};
FileSelectionUi.prototype.fileBasedScanRegionDefaultBorder = function () {
return "6px dashed #ebebeb";
};
FileSelectionUi.prototype.fileBasedScanRegionActiveBorder = function () {
return "6px dashed rgb(153 151 151)";
};
FileSelectionUi.prototype.createDragAndDropMessage = function () {
var dragAndDropMessage = document.createElement("div");
dragAndDropMessage.innerText
= strings_1.Html5QrcodeScannerStrings.dragAndDropMessage();
dragAndDropMessage.style.fontWeight = "400";
return dragAndDropMessage;
};
FileSelectionUi.prototype.setImageNameToButton = function (imageFileName) {
var MAX_CHARS = 20;
if (imageFileName.length > MAX_CHARS) {
var start8Chars = imageFileName.substring(0, 8);
var length_1 = imageFileName.length;
var last8Chars = imageFileName.substring(length_1 - 8, length_1);
imageFileName = "".concat(start8Chars, "....").concat(last8Chars);
}
var newText = strings_1.Html5QrcodeScannerStrings.fileSelectionChooseAnother()
+ " - "
+ imageFileName;
this.fileSelectionButton.innerText = newText;
};
FileSelectionUi.prototype.setInitialValueToButton = function () {
var initialText = strings_1.Html5QrcodeScannerStrings.fileSelectionChooseImage()
+ " - "
+ strings_1.Html5QrcodeScannerStrings.fileSelectionNoImageSelected();
this.fileSelectionButton.innerText = initialText;
};
FileSelectionUi.prototype.getFileScanInputId = function () {
return "html5-qrcode-private-filescan-input";
};
FileSelectionUi.create = function (parentElement, showOnRender, onFileSelected) {
var button = new FileSelectionUi(parentElement, showOnRender, onFileSelected);
return button;
};
return FileSelectionUi;
}());
exports.FileSelectionUi = FileSelectionUi;
//# sourceMappingURL=file-selection-ui.js.map