"use strict"; /** * Created by dmytro.novosad on 7/22/2015. */ angular.module('resourceModule') .directive('rsKaAttachments', ['systemAlertService', 'attachmentService', 'i18nService', 'events', function (systemAlertService, attachmentService, i18nService, events) { return { require: '^rs', restrict: 'E', scope: { printMode: '@?' }, templateUrl: 'views/resource/rs-ka-attachments.html', link: function (scope, element, attrs, rsController) { angular.extend(scope, rsController); var context = scope.context, state = { loadingAttachments: false, uploadingAttachments: false }; requestArticleAttachment(); scope.$on(events.REFRESH_RESOURCE_FEED, function () { requestArticleAttachment(); }); scope.handleFileChange = function (fileInput) { var newAttachment = attachmentService.prepareFileToUpload({ fileInput: fileInput }); if (newAttachment && newAttachment.size <= 0) { systemAlertService.warning({ text: $filter('i18n')('attachment.file_empty'), icon: 'icon-exclamation_triangle', clear: true, hide: 5000 }); return; } if (newAttachment) { uploadAttachments([newAttachment]); } }; scope.removeAttachment = function (attachment, $event) { $event.stopPropagation(); var modalInstance = getRemoveAttachmentAlert(); modalInstance.result.then(function (data) { if (data) { handleRemoveAttachmentResult(data, attachment); } }); }; scope.getAttachment = function (attachment) { state.loadingAttachments = true; attachmentService.getAttachmentFile(EntityVO.TYPE_KNOWLEDGE, attachment) .finally(function () { scope.state.loadingAttachments = false; }); }; function requestArticleAttachment() { state.loadingAttachments = true; return attachmentService.getAttachmentsInfo(context.id, context.type) .then(function (attachments) { scope.attachments = attachments; }) .finally(function () { state.loadingAttachments = false; }); } function uploadAttachments(attachments) { scope.state.uploadingAttachments = true; return attachmentService.uploadAttachment(scope.context.type, scope.context.id, attachments) .then(function (uploads) { Array.prototype.push.apply(scope.attachments, uploads); }) .finally(function () { scope.state.uploadingAttachments = false; }); } function getRemoveAttachmentAlert() { return systemAlertService.modal({ title: i18nService.getLocalizedString('common.notification.delete.title'), text: i18nService.getLocalizedString('common.notification.delete.message'), buttons: [ { text: i18nService.getLocalizedString('common.notification.delete.button1'), data: true }, { text: i18nService.getLocalizedString('common.notification.delete.button2'), data: false } ] }); } function handleRemoveAttachmentResult(data, attachment) { _.pull(scope.attachments, attachment); attachmentService.deleteAttachment(EntityVO.TYPE_KNOWLEDGE, { dataSourceId: attachment.attachmentReference.dataSourceId, attachmentId: attachment.attachmentReference.attachmentId }); } scope.context = context; scope.state = state; } }; }]);