(function () { "use strict"; angular.module('shared') .component('customStandards', { templateUrl: '/shared/js/angular/file-cabinet/custom-standards.html', controller: 'CustomStandardsCtrl', bindings: { folder: '<', saveButtonEnabled: '=' } }) .controller('CustomStandardsCtrl', ['standardsService', 'folderService', 'messageHandler', '$scope', 'MessageBox','currentSubFolders', function CustomStandardsController(standardsService, folderService, messageHandler, $scope, MessageBox, currentSubFolders) { var ctrl = this; ctrl.maxCodeLength = standardsService.standardLimits.MAX_CODE_LENGTH; ctrl.maxDescriptionLength = standardsService.standardLimits.MAX_DESCRIPTION_LENGTH; ctrl.$onInit = function() { standardsService.registerOnSelectedStandardsUpdate($scope, updateSelectedCustomStandards); ctrl.addCode = ''; ctrl.addDescription = ''; ctrl.selectedCustomStandards = standardsService.getSelectedCustomStandards(); getCustomStandards(); }; ctrl.isSubmitEnabled = function(customStandardForm) { var isCodeEmpty = !!customStandardForm.addCode.$modelValue; var isDescriptionEmpty = !!customStandardForm.addDescription.$modelValue; var isSubmitEnabled = !ctrl.pendingSubmit && !(isCodeEmpty && isDescriptionEmpty); ctrl.saveButtonEnabled = !isSubmitEnabled; return isSubmitEnabled; }; ctrl.submitCustomStandard = function(customStandardForm) { if (ctrl.pendingSubmit) { return; } ctrl.pendingSubmit = true; var data = { custom_code: folderService.ansiOnly(customStandardForm.addCode.$modelValue), custom_description: folderService.ansiOnly(customStandardForm.addDescription.$modelValue), everyone_folder: currentSubFolders.getAreFoldersForEveryone() }; standardsService.createCustomStandard(data) .then(function(response) { ctrl.addCode = ''; ctrl.addDescription = ''; updateClientFromAddCustomStandard(response); customStandardForm.addCode.$setPristine(); customStandardForm.addDescription.$setPristine(); }) .catch(function(error) { console.debug(error); messageHandler.error('There was a problem adding the custom standard. Please try again.'); }) .finally(function() { ctrl.pendingSubmit = false; }); }; function getCustomStandards() { standardsService.getCustomStandards(currentSubFolders.getAreFoldersForEveryone()).then( function(response) { ctrl.customStandards = response; }, function(reason) { console.debug(reason); messageHandler.publishError("There was a problem retrieving your custom standards"); } ) } function updateSelectedCustomStandards() { var removedCustomStandard = standardsService.getLastRemovedCustomStandard(); ctrl.customStandards.forEach(function(customStandard) { if (customStandard.custom_standard_id === removedCustomStandard.custom_standard_id) { customStandard.selected = false; } }); } function updateClientFromAddCustomStandard(customStandard) { if (!ctrl.customStandards) { ctrl.customStandards = []; } ctrl.customStandards.push(customStandard); } ctrl.updateClientFromRemoveCustomStandard = function(customStandard) { var index = getIndexFromCustomStandardId(ctrl.customStandards, customStandard.custom_standard_id); if (index > -1) { ctrl.customStandards.splice(index, 1); } standardsService.updateSelectedCustomStandards(customStandard, true); }; ctrl.updateClientFromEditCustomStandard = function(customStandard) { standardsService.updateSelectedCustomStandards(customStandard, false); }; function getIndexFromCustomStandardId(standards, customStandardId) { for (var i = 0; i < standards.length; i++) { if (standards[i].custom_standard_id === customStandardId) { return i; } } return -1; } } ]) })();