/** * Created by wisestone on 2018-05-08. */ 'use strict'; define([ 'app', 'angular' ], function (app, angular) { app.controller('customFieldAddController', ['$scope', '$rootScope', '$log', '$resourceProvider', 'User', '$uibModalInstance', 'CustomField', 'SweetAlert', '$timeout', '$filter', function ($scope, $rootScope, $log, $resourceProvider, User, $uibModalInstance, CustomField, SweetAlert, $timeout, $filter) { $scope.fn = { cancel : cancel, // 팝업 창 닫기 formSubmit : formSubmit, // 폼 전송 formCheck : formCheck, // 폼 체크 addOption : addOption, // 옵션 값 추가하기 removeOption : removeOption, // 옵션 삭제 changeFieldType : changeFieldType // 필드 유형 변경 }; $scope.vm = { form : { name : "", // 사용자 정의 필드 명 customFieldType : "INPUT", // 사용자 정의 필드 유형 defaultValue : "", // 기본 값 options : [], // 옵션 optionText : "" // 옵션 값 } }; // 옵션 삭제 function removeOption(index) { $scope.vm.form.options.splice(index, 1); } // 필드 유형을 변경 했을 때 문자열 필드일 경우에는 옵션 값을 초기화해준다. function changeFieldType() { if ($scope.vm.form.customFieldType === "INPUT") { $scope.vm.form.options = []; $scope.vm.form.optionText = ""; } $scope.vm.form.defaultValue = ""; } // Select 옵션 값 추가하기 function addOption() { // 문자열일 때 button 요소가 enter 키에 작동하는 것 방지 if ($scope.vm.form.customFieldType === "INPUT") { return; } var duplication = false; // 해시 태그는 입력 금지 - 공백 치환 $scope.vm.form.optionText = $rootScope.preventXss($scope.vm.form.optionText.replace(/#/gi, "")); // 중복 여부 체크 for (var count in $scope.vm.form.options) { var option = $scope.vm.form.options[count]; if (option === $scope.vm.form.optionText) { duplication = true; break; } } // 중복이 아닐 경우 if (!duplication) { if (!$rootScope.isDefined($scope.vm.form.optionText)) { $scope.vm.form.optionText = ""; SweetAlert.warning($filter("translate")("customField.emptyInputValue"), $filter("translate")("customField.emptyAddValue")); // 입력 값 확인 알림, 입력한 값이 없습니다. return; } $scope.vm.form.options.push($scope.vm.form.optionText); $scope.vm.form.options.sort(); $scope.vm.form.optionText = ""; $timeout(function () { $("#optionAdd").trigger("focus") }, 200); } else { SweetAlert.warning($filter("translate")("customField.duplicateInputValue"), $filter("translate")("customField.alreadyAddedValue")); // "입력 값 중복 알림", "입력한 값이 이미 추가되어 있습니다." } } // 폼 체크 function formCheck(formInvalid) { if (formInvalid) { return true; } // 다중, 단일 선택일 경우에 if ($scope.vm.form.customFieldType === "MULTI_SELECT" || $scope.vm.form.customFieldType === "SINGLE_SELECT") { // 옵션이 1개 이하일 경우에는 셀렉트 태그를 만들 수 없다. if ($scope.vm.form.options.length < 1) { return true; } } return false; } // 폼 전송 function formSubmit() { $rootScope.spinner = true; var content = angular.copy($scope.vm.form); content.name = $rootScope.preventXss(content.name); if ($scope.vm.form.customFieldType === 'MULTI_SELECT'|| $scope.vm.form.customFieldType === "SINGLE_SELECT") { var convertDefaultValues = ""; angular.forEach(content.defaultValue.split("#"), function (value) { if ($rootScope.isDefined(value)) { convertDefaultValues += "#" + value.trim(); } }); content.defaultValue = convertDefaultValues; } CustomField.add($resourceProvider.getContent( content, $resourceProvider.getPageContent(0, 10))).then(function (result) { if (result.data.message.status === "success") { $scope.fn.cancel(); // 목록 화면 갱신 $rootScope.$broadcast("getCustomFieldList", {}); } else { SweetAlert.error($filter("translate")("customField.failedToCreateUserDefinedFields"), result.data.message.message); // "사용자 정의 필드 생성 실패" } $rootScope.spinner = false; }); } // 팝업 창 닫기 function cancel() { $rootScope.$broadcast("closeLayer"); // 팝업이 열리고 나서 js-multi, js-single 등에서 body 이벤트가 날아가는 현상 수정 $uibModalInstance.dismiss('cancel'); $(document).unbind("keydown"); // 단축키 이벤트 제거 } }]); });