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

236 lines
4.6 KiB
JavaScript
Raw Normal View History

(function() {
var Status = {
NONE : 0
,WAITING : 1
,UPLOADING : 2
,UPLOADED : 3
};
2015-02-01 03:21:54 +00:00
2016-09-26 09:28:47 +00:00
Hedera.Photos = new Class
2015-02-01 03:21:54 +00:00
({
2016-09-26 09:28:47 +00:00
Extends: Hedera.Form
2017-05-08 15:54:35 +00:00
,filesData: []
,uploadCount: 0
,errors: false
,uploadQueue: []
,isUploading: false
2015-02-01 03:21:54 +00:00
,activate: function ()
{
2015-03-06 23:33:54 +00:00
this.$('schema').value = 'catalog';
2015-02-01 03:21:54 +00:00
}
2017-05-08 15:54:35 +00:00
,addFiles: function (files)
2015-02-01 03:21:54 +00:00
{
2017-05-08 15:54:35 +00:00
if (!files)
return;
for (var i = 0; i < files.length; i++)
this.addFile (files[i]);
}
,addFile: function (file)
{
var doc = document;
var div = doc.createElement ('div');
var button = new Htk.Button ({
tip: 'Remove',
icon: 'delete'
});
button.on ('click', this.onFileRemove, this);
div.appendChild (button.node);
2015-02-01 03:21:54 +00:00
2017-05-08 15:54:35 +00:00
var thumb = doc.createElement ('img');
thumb.file = file;
thumb.className = 'thumb';
div.appendChild (thumb);
var reader = new FileReader ();
reader.onload = function (e) { thumb.src = e.target.result; };
reader.readAsDataURL(file);
var name = doc.createElement ('input');
name.type = 'text';
name.value = getFileName (file.name);
div.appendChild (name);
var statusNode = doc.createElement ('span');
statusNode.className = 'status';
div.appendChild (statusNode);
2017-05-08 15:54:35 +00:00
var fileData = {
div: div,
file: file,
name: name,
statusNode: statusNode,
status: Status.NONE
2017-05-08 15:54:35 +00:00
};
this.filesData.push (fileData);
button.value = fileData;
this.$('file-list').appendChild (div);
2016-08-30 07:43:47 +00:00
}
2017-05-08 15:54:35 +00:00
,onUploadClick: function ()
{
var filesData = this.filesData;
var count = 0;
for (var i = 0; i < filesData.length; i++)
{
var fileData = filesData[i];
if (fileData.status === Status.NONE)
2017-05-08 15:54:35 +00:00
{
this.setImageStatus (
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)
2017-05-09 12:03:06 +00:00
Htk.Toast.showWarning (_('There are no files to upload'));
else
this.uploadNextFile ();
}
,uploadNextFile: function ()
{
if (this.isUploading)
return;
this.isUploading = true;
var fileData = this.uploadQueue.shift ();
this.setImageStatus (
fileData, Status.UPLOADING, 'upload', _('Uploading file'));
var formData = new FormData();
formData.append ('image', fileData.file);
formData.append ('name', fileData.name.value);
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
}
,onFileUpload: function (fileData, data, error)
2016-08-30 07:43:47 +00:00
{
this.isUploading = false;
2017-05-08 15:54:35 +00:00
if (data)
{
this.setImageStatus (
fileData, Status.UPLOADED, 'ok', _('Image uploaded'));
2017-05-08 15:54:35 +00:00
}
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)
2017-05-08 15:54:35 +00:00
{
if (this.errors)
2017-05-08 15:54:35 +00:00
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 ();
}
,setImageStatus: function (fileData, status, icon, title)
{
fileData.status = status;
var statusNode = fileData.statusNode;
Vn.Node.removeChilds (statusNode);
var iconNode = new Htk.Icon ({icon: icon});
statusNode.appendChild (iconNode.node);
statusNode.title = title ? title : '';
2017-05-08 15:54:35 +00:00
}
,onFileRemove: function (button)
{
var fileData = button.value;
this.$('file-list').removeChild (fileData.div);
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);
break;
}
}
,onClearClick: function ()
{
this.filesData = [];
Vn.Node.removeChilds (this.$('file-list'));
}
,onDropzoneClick: function ()
{
this.$('file').click ();
}
,onFileChange: function ()
{
this.addFiles (this.$('file').files);
}
,onDragEnter: function (event)
{
Vn.Node.addClass (this.$('dropzone'), 'dragover');
}
,onDragLeave: function (event)
{
Vn.Node.removeClass (this.$('dropzone'), 'dragover');
}
,onDragOver: function (event)
{
event.preventDefault ();
}
,onDragEnd: function (event)
{
Vn.Node.removeClass (this.$('dropzone'), 'dragover');
event.dataTransfer.clearData ();
}
,onDrop: function (event)
{
event.preventDefault ();
this.addFiles (event.dataTransfer.files);
2015-02-01 03:21:54 +00:00
}
});
2017-05-08 15:54:35 +00:00
function getFileName (path)
{
var barIndex = path.lastIndexOf ('/');
if (barIndex === -1)
barIndex = path.lastIndexOf ('\\');
if (barIndex === -1)
barIndex = 0;
var dotIndex = path.lastIndexOf ('.');
if (dotIndex === -1)
dotIndex = 0;
return path.substr (barIndex, dotIndex);
}
})();