--- /dev/null
+(function() {
+ "use strict";
+
+ angular
+ .module("nero.app")
+ .directive("attachmentDirective", attachmentDirective);
+
+ /*
+ IsEditor : Boolean -
+ attachments : Array - list of existing attachments to display
+ idProperties : Object - portlet information needed to get/save attachments
+ isButton : Boolean - Don't save on add attachment juste build the js array (and don't display attachments menu)
+ saveOnAdd : Boolean - Save the attachment when added
+ deleteConfirm : Boolean (Optionnal) - Should we display confirm-dialog when removing an attachment
+ hasContextMenu : Boolean (Optional) - Should we display context menu on attachment click
+ */
+ function attachmentDirective() {
+ return {
+ restrict: "E",
+ scope: {
+ isEditor: "=",
+ attachments: "=",
+ idProperties: "=",
+ isButton: "=",
+ saveOnAdd: "=?",
+ deleteConfirm : '=?',
+ hasContextMenu: "=?"
+ },
+ templateUrl: Global.Theme.getURLPortal() + Liferay.ThemeDisplay.getPathJavaScript() + "/angularjs/attachment/attachment.html?t="+Liferay.extTimestamp,
+ controller: AttachmentController,
+ controllerAs: "AttachmentCtrl",
+ bindToController: true
+ };
+ }
+
+ AttachmentController.$inject = ["attachmentService", "toastr", "$uibModal"];
+
+ function AttachmentController(attachmentService, toastr, $uibModal) {
+ var vm = this;
+ vm.isEditionMode = isEditionMode;
+ vm.onViewAttachment = onViewAttachment;
+ vm.onSendToDropboxAttachment = onSendToDropboxAttachment;
+ vm.onDownloadAttachment = onDownloadAttachment;
+ vm.onAddAttachment = onAddAttachment;
+ vm.onDeleteAttachment = onDeleteAttachment;
+ vm.isDeleteWithConfirmDisplay = isDeleteWithConfirmDisplay;
+ vm.isDeleteWithoutConfirmDisplay = isDeleteWithoutConfirmDisplay;
+
+ if (vm.saveOnAdd == undefined) {
+ vm.saveOnAdd = false;
+ }
+
+ if (vm.deleteConfirm == undefined) {
+ vm.deleteConfirm = true;
+ }
+
+ if (vm.hasContextMenu == undefined) {
+ vm.hasContextMenu = !vm.isButton;
+ }
+
+ function isEditionMode() {
+ return vm.isEditor;
+ }
+
+ /**
+ Display delete button with confirmation on click
+ */
+ function isDeleteWithConfirmDisplay() {
+ return (vm.isEditor && vm.deleteConfirm);
+ }
+
+ /**
+ Display delete button without confirmation on click
+ */
+ function isDeleteWithoutConfirmDisplay() {
+ return (vm.isEditor && !vm.deleteConfirm);
+ }
+
+ /*
+ Display viewer
+ */
+ function onViewAttachment(index) {
+ var modalInstance = $uibModal.open({
+ templateUrl: Global.Theme.getURLPortal() + Liferay.ThemeDisplay.getPathJavaScript() + "/angularjs/viewer/viewer.html?t="+Liferay.extTimestamp,
+ controller: "ViewerController",
+ controllerAs: "ViewerCtrl",
+ size: "viewer",
+ windowClass: "modal-viewer",
+ resolve: {
+ parameters: function() {
+ return {
+ attachment: vm.attachments[index],
+ idProperties: vm.idProperties
+ }
+ }
+ },
+ backdrop: 'static', /* this prevent user interaction with the background (we have to stop the conversion) */
+ keyboard: false
+ });
+
+ modalInstance.result.then(function() {
+ //No return value expected
+ }, function() {
+ });
+ }
+
+ function onSendToDropboxAttachment(index) {
+ attachmentService.sendAttachmentToDropbox(vm.attachments[index], vm.idProperties)
+ .then(function(data) {
+ toastr.success(data.message);
+ })
+ ["catch"](function(message) {
+ toastr.error(message);
+ });
+ }
+
+ /*
+ Download attachment file
+ */
+ function onDownloadAttachment(attachment) {
+ window.open(attachment.fileLink + "&contentDispo=attachment");
+ }
+
+ /*
+ Open the file selector
+ */
+ function onAddAttachment() {
+ var modalInstance = $uibModal.open({
+ templateUrl: Global.Theme.getURLPortal() + Liferay.ThemeDisplay.getPathJavaScript() + "/angularjs/file-selector/file-selector.html?t="+Liferay.extTimestamp,
+ controller: "FileSelectorController",
+ controllerAs: "FileSelectorCtrl",
+ resolve: {
+ parameters : function() {
+ return {};
+ }
+ }
+ });
+
+ modalInstance.result.then(function(attachFiles) {
+ if (!vm.saveOnAdd) {
+ // Dashboard
+ vm.attachments.push.apply(vm.attachments, attachFiles);
+ }
+ else {
+ // CDT
+ attachmentService.addAttachments(attachFiles, vm.idProperties)
+ .then(function(data) {
+ vm.attachments.push.apply(vm.attachments, data.attachFiles);
+ toastr.success(data.message);
+ })
+ ["catch"](function(message) {
+ toastr.error(message);
+ });
+ }
+ }, function() {
+ // Cancel file selection so don't do anything
+ });
+ }
+
+ /*
+ Delete attachment
+ */
+ function onDeleteAttachment(index) {
+ if (!vm.saveOnAdd) {
+ //Dashboard
+ vm.attachments.splice(index,1);
+ }
+ else {
+ //CDT
+ attachmentService.deleteAttachment(vm.attachments[index], vm.idProperties)
+ .then(function(data) {
+ // Remove attachment from array
+ vm.attachments.splice(index, 1);
+ toastr.success(data.message);
+ })
+ ["catch"](function(message) {
+ toastr.error(message);
+ });
+ }
+ }
+
+ }
+})();