/**
|
* Created by wisestone on 2018-05-08.
|
*/
|
'use strict';
|
|
define([
|
'app',
|
'angular'
|
],
|
function (app, angular) {
|
app.controller('customFieldModifyController', ['$scope', '$rootScope', '$log', '$resourceProvider', '$uibModalInstance', 'CustomField', 'parameter', 'SweetAlert', '$timeout', '$filter',
|
function ($scope, $rootScope, $log, $resourceProvider, $uibModalInstance, CustomField, parameter, SweetAlert, $timeout, $filter) {
|
|
$scope.fn = {
|
cancel : cancel, // 팝업 창 닫기
|
formSubmit : formSubmit, // 폼 전송
|
formCheck : formCheck, // 폼 체크
|
detail : detail, // 상세 정보
|
addOption : addOption, // 옵션 값 추가하기
|
removeOption : removeOption, // 옵션 삭제
|
checkModifyOptions : checkModifyOptions, // 옵션 값 변경 확인
|
changeCustomFieldType : changeCustomFieldType, // 사용자 정의 필드 유형이 변경될 때 기본 값 초기화
|
};
|
|
$scope.vm = {
|
search : {
|
id : parameter.id,
|
deep : "01" // 사용자 정의 필드 연관된 모든 정보를 어느정도까지 가져올지 결정, 01 - 일반 정보, 연관된 정보
|
},
|
form : {
|
id : parameter.id,
|
name : "",
|
customFieldType : "INPUT", // 사용자 정의 필드 유형
|
defaultValue : "", // 기본 값
|
options : [], // 옵션
|
optionText : "", // 옵션 값
|
useCustomFieldValue : false, // 이슈에서 사용되고 있는지 여부 확인
|
numberType : "",
|
ipAdress : "",
|
email : "",
|
site : "",
|
tel : "",
|
requiredData: ""
|
},
|
origin : {
|
options : [] // 옵션 값 변경 여부 확인을 위해 서버에서 내려올 때 원본 값을 따로 관리한다.
|
}
|
};
|
|
// 사용자 정의 필드 유형이 변경될 때 기본 값 초기화
|
function changeCustomFieldType() {
|
//$scope.vm.form.name = "";
|
$scope.vm.form.defaultValue = "";
|
}
|
|
// 옵션 삭제
|
function removeOption(index) {
|
$scope.vm.form.options.splice(index, 1);
|
}
|
|
// 옵션 값이 변경되었는지 확인한다.
|
function checkModifyOptions() {
|
// 필드 유형이 문자열일 경우는 바로 종료
|
if ($scope.vm.form.customFieldType === "INPUT") {
|
return false;
|
}
|
|
// 옵션 갯수가 달라졌을 경우
|
if ($scope.vm.form.options.length !== $scope.vm.origin.options.length) {
|
return true;
|
}
|
|
// 갯수가 같을 경우 모든 옵션 값이 같아야 한다. 다를 경우 true 리턴.
|
var checkOptionCompare = false;
|
|
for (var originOptionCount in $scope.vm.origin.options) {
|
var originOption = $scope.vm.origin.options[originOptionCount];
|
var existOption = false;
|
|
for (var optionCount in $scope.vm.form.options) {
|
var option = $scope.vm.form.options[optionCount];
|
|
if (originOption === option) {
|
existOption = true;
|
break;
|
}
|
}
|
|
if (!existOption) {
|
checkOptionCompare = true;
|
}
|
}
|
|
return checkOptionCompare;
|
}
|
|
// Select 옵션 값 추가하기
|
function addOption() {
|
// 문자열일 때 button 요소가 enter 키에 작동하는 것 방지
|
if ($scope.vm.form.customFieldType === "INPUT") {
|
return;
|
}
|
// $scope.vm.form.customFieldType == "NUMBER" 존재한다
|
|
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.swal($filter("translate")("customField.duplicateInputValue"), $filter("translate")("customField.alreadyAddedValue"), "warning"); // "입력 값 중복 알림", "입력한 값이 이미 추가되어 있습니다."
|
}
|
}
|
|
// 폼 체크
|
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);
|
content.numberType =$scope.vm.form.numberType;
|
content.ipAdress = $scope.vm.form.ipAdress;
|
content.email = $scope.vm.form.email;
|
content.site = $scope.vm.form.site;
|
content.tel = $scope.vm.form.tel;
|
content.requiredData = $scope.vm.form.requiredData;
|
|
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;
|
}
|
else {
|
// 문자열 필드일 때 옵션 값 제거
|
content.options = [];
|
}
|
|
CustomField.modify($resourceProvider.getContent(
|
content,
|
$resourceProvider.getPageContent(0, 0))).then(function (result) {
|
|
if (result.data.message.status === "success") {
|
$scope.fn.cancel();
|
// 목록 화면 갱신
|
$rootScope.$broadcast("getCustomFieldList", {});
|
}
|
else {
|
SweetAlert.error($filter("translate")("customField.failedToModifyUserDefinedFields"), result.data.message.message); // "사용자 정의 필드 수정 실패"
|
}
|
|
$rootScope.spinner = false;
|
});
|
}
|
|
// 폼 닫기
|
function cancel() {
|
$rootScope.$broadcast("closeLayer"); // 팝업이 열리고 나서 js-multi, js-single 등에서 body 이벤트가 날아가는 현상 수정
|
$uibModalInstance.dismiss('cancel');
|
$(document).unbind("keydown"); // 단축키 이벤트 제거
|
}
|
|
// 상세 정보
|
function detail() {
|
CustomField.detail($resourceProvider.getContent(
|
$scope.vm.search,
|
$resourceProvider.getPageContent(0, 1))).then(function (result) {
|
|
if (result.data.message.status === "success") {
|
if (angular.isDefined(result.data.data)) {
|
$scope.vm.form.name = result.data.data.name;
|
$scope.vm.form.customFieldType = result.data.data.customFieldType;
|
$scope.vm.form.defaultValue = result.data.data.defaultValue;
|
$scope.vm.form.useCustomFieldValue = result.data.data.useCustomFieldValue;
|
|
if(result.data.data.requiredData === "Y"){
|
$scope.vm.form.requiredData = true;
|
} else {
|
$scope.vm.form.requiredData = false;
|
}
|
|
angular.forEach(result.data.data.customFieldValueVos, function (customFieldValueVo) {
|
$scope.vm.form.options.push(customFieldValueVo.value);
|
});
|
// 옵션 값 원본 저장
|
$scope.vm.origin.options = angular.copy($scope.vm.form.options.sort());
|
}
|
}
|
else {
|
SweetAlert.swal($filter("translate")("customField.failedToDetailUserDefinedFields"), result.data.message.message, "error"); // "사용자 정의 필드 상세 정보 조회 실패"
|
}
|
});
|
}
|
|
$scope.fn.detail();
|
}]);
|
});
|