(function() { "use strict"; angular.module('shared') .component('customStandard', { templateUrl: '/shared/js/angular/file-cabinet/custom-standard.html', controller: 'CustomStandardCtrl', bindings: { customStandard: '<', onDelete: '<', onEdit: '<' } }) .controller('CustomStandardCtrl', ['standardsService', 'MessageBox', 'folderService', 'messageHandler','currentSubFolders', function CustomStandardController(standardsService, MessageBox, folderService, messageHandler,currentSubFolders) { var ctrl = this; var displayMode = "display"; var editMode = "edit"; var mode = displayMode; ctrl.maxCodeLength = standardsService.standardLimits.MAX_CODE_LENGTH; ctrl.maxDescriptionLength = standardsService.standardLimits.MAX_DESCRIPTION_LENGTH; ctrl.selectedStandardLimit = standardsService.standardLimits.SELECTED_STANDARD_LIMIT; ctrl.controlEventPropagation = function (event) { if (ctrl.isEditMode()) { event.preventDefault(); event.stopPropagation(); } }; ctrl.isCheckboxDisabled = function() { return (ctrl.customStandard.selected ? false : standardsService.hasReachedStandardsLimit()) && !ctrl.isEditMode(); }; ctrl.getCheckboxClass = function() { return ctrl.isCheckboxDisabled() ? 'tooltip tooltip-top tooltip-icon' : ''; }; ctrl.selectCustomStandard = function() { ctrl.customStandard.selected ? standardsService.selectCustomStandard(ctrl.customStandard) : standardsService.deselectCustomStandard(ctrl.customStandard); }; ctrl.isSaveButtonEnabled = function(customStandardEdit) { var isCodeEmpty = !!customStandardEdit.editCode.$modelValue; var isDescriptionEmpty = !!customStandardEdit.editDescription.$modelValue; return !ctrl.pendingSave && !(isCodeEmpty && isDescriptionEmpty); }; ctrl.isEditMode = function() { return mode === editMode; }; ctrl.displayCustomStandard = function() { mode = displayMode; }; ctrl.editCustomStandard = function(event) { ctrl.editCode = ctrl.customStandard.custom_code; ctrl.editDescription = ctrl.customStandard.custom_description; mode = editMode; event.preventDefault(); event.stopPropagation(); }; ctrl.saveCustomStandard = function(customStandardEdit) { if (ctrl.pendingSave) { return; } ctrl.pendingSave = true; var data = { custom_standard_id: ctrl.customStandard.custom_standard_id, custom_code: folderService.ansiOnly(customStandardEdit.editCode.$modelValue), custom_description: folderService.ansiOnly(customStandardEdit.editDescription.$modelValue), everyone_folder : currentSubFolders.getAreFoldersForEveryone() }; standardsService.editCustomStandard(data) .then(function(response) { ctrl.customStandard.custom_code = response.custom_code; ctrl.customStandard.custom_description = response.custom_description; ctrl.onEdit(ctrl.customStandard); }) .catch(function(error) { console.debug(error); messageHandler.error('There was a problem saving the custom standard. Please try again.'); }) .finally(function() { ctrl.pendingSave = false; ctrl.displayCustomStandard(); standardsService.emitCustomStandardsUpdate(); }); }; ctrl.deleteCustomStandard = function(event) { event.preventDefault(); event.stopPropagation(); if (ctrl.pendingDelete) { return; } folderService.getFoldersByCustomStandardId(ctrl.customStandard.custom_standard_id,currentSubFolders.getAreFoldersForEveryone()) .then(function(response) { var folderIds = response.data.length > 0 ? response.data : false; var messageStr = folderIds ? 'This standard is currently added to one or more folders. ' + 'Are you sure you want to continue?' : 'Are you sure you want to remove this standard?'; MessageBox.show({ message: messageStr, responses: [ { id: 'cancel', subtle: true, label: 'Cancel' }, { id: 'delete', confirm: true, label: 'Delete Custom Standard' } ] }).then(function(response) { if (response.id === 'delete') { ctrl.pendingDelete = true; if (folderIds) { removeStandard(folderIds).then(function() { deleteStandard(); }); } else { deleteStandard(); } } }).catch(function(error) { console.debug(error); messageHandler.error("There was a problem deleting this standard. Please try again."); }); }) .catch(function(error) { console.debug(error); messageHandler.error("There was a problem processing your request. Please try again."); }); }; function removeStandard(folderIds) { var folderIdsStr = folderIds.join(','); return folderService.removeCustomStandardsFromFolder(folderIdsStr, ctrl.customStandard.custom_standard_id,currentSubFolders.getAreFoldersForEveryone()).then( function(response) { if (response.data) { standardsService.emitCustomStandardsUpdate(); } else { messageHandler.error("There was a problem removing this standard. Please try again."); } }, function(reason) { console.debug(reason); messageHandler.error("There was a problem removing this standard. Please try again."); } ); } function deleteStandard() { standardsService.deleteCustomStandard(ctrl.customStandard.custom_standard_id).then( function(response) { if (response) { ctrl.pendingDelete = false; ctrl.onDelete(ctrl.customStandard); } else { messageHandler.error("There was a problem deleting this standard. Please try again."); } }, function(reason) { console.debug(reason); messageHandler.error("There was a problem deleting this standard. Please try again."); } ) } } ]) })();