92 lines
4.4 KiB
JavaScript
92 lines
4.4 KiB
JavaScript
"use strict";
|
|
(function () {
|
|
'use strict';
|
|
angular.module('myitsmApp')
|
|
.directive('fileDropTarget', ['$filter', '$timeout', 'attachmentService', 'systemAlertService',
|
|
function ($filter, $timeout, attachmentService, systemAlertService) {
|
|
return {
|
|
restrict: 'A',
|
|
replace: false,
|
|
link: function (scope, $elem, attr) {
|
|
var maxAttachments = 3;
|
|
var clearMaxFielsObserve = attr.$observe('maxFiles', function (newVal) {
|
|
if (newVal && !isNaN(newVal)) {
|
|
maxAttachments = Number(newVal);
|
|
}
|
|
});
|
|
function dragOverHandler($event) {
|
|
var originalEvent = $event.originalEvent;
|
|
$event.stopPropagation();
|
|
$event.preventDefault();
|
|
originalEvent.dataTransfer.dropEffect = 'copy';
|
|
$elem.addClass('drag-over');
|
|
}
|
|
function dragLeaveDragEndHandler() {
|
|
$elem.removeClass('drag-over');
|
|
}
|
|
function dropHandler($event) {
|
|
$event.stopPropagation();
|
|
$event.preventDefault();
|
|
$elem.removeClass('drag-over');
|
|
var files = $event.originalEvent.dataTransfer.files;
|
|
if (scope.attachments && scope.attachments.length === maxAttachments) {
|
|
$timeout(function () {
|
|
showWarning();
|
|
});
|
|
return false;
|
|
}
|
|
if (scope.attachments && scope.attachments.length < maxAttachments) {
|
|
var freeSlots = parseInt(maxAttachments - scope.attachments.length);
|
|
if (files.length > freeSlots) {
|
|
$timeout(function () {
|
|
showWarning();
|
|
});
|
|
return false;
|
|
}
|
|
if (files && files.length) {
|
|
_.each(files, function (file) {
|
|
var attachment = attachmentService.prepareFileToUpload({
|
|
fileToPrepare: file,
|
|
droppedFile: true
|
|
});
|
|
if (attachment && attachment.size <= 0) {
|
|
systemAlertService.warning({
|
|
text: $filter('i18n')('attachment.file_empty'),
|
|
icon: 'icon-exclamation_triangle',
|
|
clear: true,
|
|
hide: 5000
|
|
});
|
|
return;
|
|
}
|
|
scope.attachments ? scope.attachments.push(attachment) : angular.noop();
|
|
});
|
|
}
|
|
}
|
|
}
|
|
$elem
|
|
.on('dragover', dragOverHandler)
|
|
.on('dragleave dragend', dragLeaveDragEndHandler)
|
|
.on('drop', dropHandler);
|
|
scope.$on("$destroy", function () {
|
|
console.log("fileDropTarget: unbind events");
|
|
$elem.off('dragover', dragOverHandler);
|
|
$elem.off('dragleave', dragLeaveDragEndHandler);
|
|
$elem.off('dragend', dragLeaveDragEndHandler);
|
|
$elem.off('drop', dropHandler);
|
|
$elem.off();
|
|
clearMaxFielsObserve();
|
|
});
|
|
function showWarning() {
|
|
var notification = $filter('i18n')('timeline.note.max.attachments', maxAttachments);
|
|
systemAlertService.error({
|
|
text: notification,
|
|
clear: false
|
|
});
|
|
return;
|
|
}
|
|
}
|
|
};
|
|
}
|
|
]);
|
|
})();
|