0
1
Fork 0
hedera-web-mindshore/forms/admin/photos/index.js

216 lines
4.4 KiB
JavaScript
Raw Normal View History

2022-11-16 01:44:39 +00:00
import './style.scss';
var Status = {
NONE : 0
,WAITING : 1
,UPLOADING : 2
,UPLOADED : 3
};
2015-02-01 03:21:54 +00:00
2022-11-16 01:44:39 +00:00
export default new Class({
Extends: Hedera.Form,
Template: require('./ui.xml')
2017-05-08 15:54:35 +00:00
,filesData: []
,uploadCount: 0
,errors: false
,uploadQueue: []
,isUploading: false
2015-02-01 03:21:54 +00:00
2022-11-16 01:46:44 +00:00
,activate() {
2022-05-28 01:18:06 +00:00
this.$.schema.value = 'catalog';
2015-02-01 03:21:54 +00:00
}
2017-05-08 15:54:35 +00:00
2022-11-16 01:46:44 +00:00
,addFiles(files) {
2017-05-08 15:54:35 +00:00
if (!files)
return;
for (var i = 0; i < files.length; i++)
this.addFile(files[i]);
2017-05-08 15:54:35 +00:00
}
2022-11-16 01:46:44 +00:00
,addFile(file) {
2017-05-08 15:54:35 +00:00
var doc = document;
var li = doc.createElement('div');
2017-05-08 15:54:35 +00:00
var div = doc.createElement('div');
div.className = 'thumb';
li.appendChild(div);
2015-02-01 03:21:54 +00:00
var thumb = doc.createElement('img');
2017-05-08 15:54:35 +00:00
thumb.file = file;
div.appendChild(thumb);
2017-05-08 15:54:35 +00:00
var reader = new FileReader();
reader.onload = function(e) {
thumb.src = e.target.result;
};
2017-05-08 15:54:35 +00:00
reader.readAsDataURL(file);
var name = doc.createElement('input');
2017-05-08 15:54:35 +00:00
name.type = 'text';
name.value = getFileName(file.name);
li.appendChild(name);
2017-05-08 15:54:35 +00:00
var statusNode = doc.createElement('div');
statusNode.className = 'status';
li.appendChild(statusNode);
2017-05-08 15:54:35 +00:00
var fileData = {
li: li,
2017-05-08 15:54:35 +00:00
file: file,
name: name,
statusNode: statusNode
2017-05-08 15:54:35 +00:00
};
2022-05-26 06:08:31 +00:00
var button = new Htk.Button({
tip: 'Remove',
icon: 'delete'
});
button.node.addEventListener('click',
() => this.onFileRemove(fileData));
li.appendChild(button.node);
this.filesData.push(fileData);
2017-05-08 15:54:35 +00:00
2022-05-28 01:18:06 +00:00
this.$.fileList.appendChild(li);
this.setImageStatus(fileData, Status.NONE, 'add', _('Pending upload'));
2016-08-30 07:43:47 +00:00
}
2017-05-08 15:54:35 +00:00
2022-11-16 01:46:44 +00:00
,onUploadClick() {
2017-05-08 15:54:35 +00:00
var filesData = this.filesData;
var count = 0;
for (var i = 0; i < filesData.length; i++) {
2017-05-08 15:54:35 +00:00
var fileData = filesData[i];
if (fileData.status === Status.NONE) {
this.setImageStatus(
2022-10-03 13:50:39 +00:00
fileData, Status.WAITING, 'cloud_upload', _('Waiting for upload'));
fileData.name.disabled = true;
this.uploadQueue.push(fileData);
2017-05-08 15:54:35 +00:00
count++;
}
}
if (count === 0)
Htk.Toast.showWarning(_('There are no files to upload'));
else
this.uploadNextFile();
}
2022-11-16 01:46:44 +00:00
,uploadNextFile() {
if (this.isUploading)
return;
this.isUploading = true;
var fileData = this.uploadQueue.shift();
this.setImageStatus(
fileData, Status.UPLOADING, 'upload', _('Uploading file'));
var formData = new FormData();
2022-05-28 01:18:06 +00:00
formData.append('updateMatching', this.$.updateMatching.value);
formData.append('image', fileData.file);
formData.append('name', fileData.name.value);
2022-05-28 01:18:06 +00:00
formData.append('schema', this.$.schema.value);
formData.append('srv', 'json:image/upload');
this.conn.sendFormData(formData,
this.onFileUpload.bind(this, fileData));
2017-05-08 15:54:35 +00:00
}
2022-11-16 01:46:44 +00:00
,onFileUpload(fileData, data, error) {
this.isUploading = false;
2017-05-08 15:54:35 +00:00
if (data) {
this.setImageStatus(
2022-10-03 13:50:39 +00:00
fileData, Status.UPLOADED, 'cloud_done', _('Image uploaded'));
} else {
this.setImageStatus(
fileData, Status.NONE, 'error', error.message);
fileData.name.disabled = false;
2017-05-08 15:54:35 +00:00
this.errors = true;
}
2015-02-01 03:21:54 +00:00
if (this.uploadQueue.length === 0) {
if (this.errors)
Htk.Toast.showError(_('Some errors happened on upload'));
else
Htk.Toast.showMessage(_('Upload finished successfully'));
2017-05-08 15:54:35 +00:00
this.errors = false;
} else
this.uploadNextFile();
}
2022-11-16 01:46:44 +00:00
,setImageStatus(fileData, status, icon, title) {
fileData.status = status;
var statusNode = fileData.statusNode;
Vn.Node.removeChilds(statusNode);
2022-10-03 13:50:39 +00:00
var iconNode = new Htk.Icon({name: icon});
statusNode.appendChild(iconNode.node);
statusNode.title = title ? title : '';
2017-05-08 15:54:35 +00:00
}
2022-11-16 01:46:44 +00:00
,onFileRemove(fileData) {
2022-05-28 01:18:06 +00:00
this.$.fileList.removeChild(fileData.li);
2016-10-11 14:45:10 +00:00
2017-05-08 15:54:35 +00:00
for (var i = 0; i < this.filesData.length; i++)
if (this.filesData[i] === fileData) {
this.filesData.splice(i, 1);
2017-05-08 15:54:35 +00:00
break;
}
}
2022-11-16 01:46:44 +00:00
,onClearClick() {
2017-05-08 15:54:35 +00:00
this.filesData = [];
2022-05-28 01:18:06 +00:00
Vn.Node.removeChilds(this.$.fileList);
2017-05-08 15:54:35 +00:00
}
2022-11-16 01:46:44 +00:00
,onDropzoneClick() {
2022-05-28 01:18:06 +00:00
this.$.file.click();
2017-05-08 15:54:35 +00:00
}
2022-11-16 01:46:44 +00:00
,onFileChange() {
2022-05-28 01:18:06 +00:00
this.addFiles(this.$.file.files);
2017-05-08 15:54:35 +00:00
}
2022-11-16 01:46:44 +00:00
,onDragEnter() {
2022-05-28 01:18:06 +00:00
this.$.dropzone.classList.add('dragover');
2017-05-08 15:54:35 +00:00
}
2022-11-16 01:46:44 +00:00
,onDragLeave() {
2022-05-28 01:18:06 +00:00
this.$.dropzone.classList.remove('dragover');
2017-05-08 15:54:35 +00:00
}
2022-11-16 01:46:44 +00:00
,onDragOver(event) {
event.preventDefault();
2017-05-08 15:54:35 +00:00
}
2022-11-16 01:46:44 +00:00
,onDragEnd(event) {
2022-05-28 01:18:06 +00:00
this.$.dropzone.classList.remove('dragover');
event.dataTransfer.clearData();
2017-05-08 15:54:35 +00:00
}
2022-11-16 01:46:44 +00:00
,onDrop(event) {
event.preventDefault();
this.addFiles(event.dataTransfer.files);
2015-02-01 03:21:54 +00:00
}
});
function getFileName(path) {
var barIndex = path.lastIndexOf('/');
2017-05-08 15:54:35 +00:00
if (barIndex === -1)
barIndex = path.lastIndexOf('\\');
2017-05-08 15:54:35 +00:00
if (barIndex === -1)
barIndex = 0;
var dotIndex = path.lastIndexOf('.');
2017-05-08 15:54:35 +00:00
if (dotIndex === -1)
dotIndex = 0;
return path.substr(barIndex, dotIndex);
2017-05-08 15:54:35 +00:00
}