|
1 | 1 | (function() {
|
2 | 2 | 'use strict';
|
3 | 3 |
|
4 |
| - angular.module('theHiveControllers').controller('AdminCustomFieldDialogCtrl', |
5 |
| - function($scope, $uibModalInstance, ListSrv, NotificationSrv, customField) { |
6 |
| - var self = this; |
7 |
| - self.config = { |
8 |
| - types: ['string', 'number', 'boolean', 'date'], |
9 |
| - referencePattern: '^[a-zA-Z]{1}[a-zA-Z0-9_-]*' |
10 |
| - }; |
11 |
| - |
12 |
| - self.customField = customField; |
13 |
| - self.customField.options = (customField.options || []).join('\n'); |
14 |
| - |
15 |
| - var onSuccess = function(data) { |
16 |
| - $uibModalInstance.close(data); |
17 |
| - }; |
18 |
| - |
19 |
| - var onFailure = function(response) { |
20 |
| - NotificationSrv.error('AdminCustomFieldDialogCtrl', response.data, response.status); |
21 |
| - }; |
22 |
| - |
23 |
| - var buildOptionsCollection = function(options) { |
24 |
| - if(!options || options === '') { |
25 |
| - return []; |
26 |
| - } |
27 |
| - |
28 |
| - var type = self.customField.type; |
29 |
| - var values = self.customField.options.split('\n'); |
30 |
| - |
31 |
| - if(type === 'number') { |
32 |
| - return _.without(values.map(function(item) { |
33 |
| - return Number(item); |
34 |
| - }), NaN); |
35 |
| - } |
36 |
| - |
37 |
| - return values; |
38 |
| - }; |
39 |
| - |
40 |
| - self.saveField = function(form) { |
41 |
| - if (!form.$valid) { |
42 |
| - return; |
43 |
| - } |
44 |
| - |
45 |
| - var postData = _.pick(self.customField, 'name', 'reference', 'description', 'type'); |
46 |
| - postData.options = buildOptionsCollection(self.customField.options); |
47 |
| - |
48 |
| - if(self.customField.id) { |
49 |
| - ListSrv.update( |
50 |
| - {'itemId': self.customField.id}, |
51 |
| - {'value': JSON.stringify(postData)}, |
52 |
| - onSuccess, |
53 |
| - onFailure); |
54 |
| - } else { |
55 |
| - ListSrv.exists( |
56 |
| - {'listId': 'custom_fields'}, |
57 |
| - { |
58 |
| - key: 'reference', |
59 |
| - value: postData.reference |
60 |
| - }, |
61 |
| - function(response) { |
62 |
| - if(response.data) { |
63 |
| - ListSrv.save( |
64 |
| - {'listId': 'custom_fields'}, |
65 |
| - {'value': JSON.stringify(postData)}, |
66 |
| - onSuccess, |
67 |
| - onFailure); |
68 |
| - } else { |
69 |
| - // TODO handle field validation |
70 |
| - form.reference.$setValidity('unique', false); |
71 |
| - form.reference.$setDirty(); |
72 |
| - NotificationSrv.error('AdminCustomFieldDialogCtrl', 'There is already a custom field with the specified reference: ' + postData.reference); |
73 |
| - } |
74 |
| - }, |
75 |
| - onFailure |
76 |
| - ) |
77 |
| - } |
78 |
| - }; |
79 |
| - |
80 |
| - self.clearUniqueReferenceError = function(form) { |
81 |
| - form.reference.$setValidity('unique', true); |
82 |
| - form.reference.$setPristine(); |
| 4 | + angular.module('theHiveControllers').controller('AdminCustomFieldDialogCtrl', function($scope, $uibModalInstance, ListSrv, NotificationSrv, customField) { |
| 5 | + var self = this; |
| 6 | + self.config = { |
| 7 | + types: [ |
| 8 | + 'string', 'number', 'boolean', 'date' |
| 9 | + ], |
| 10 | + referencePattern: '^[a-zA-Z]{1}[a-zA-Z0-9_-]*' |
| 11 | + }; |
| 12 | + |
| 13 | + self.customField = customField; |
| 14 | + self.customField.options = (customField.options || []).join('\n'); |
| 15 | + |
| 16 | + var onSuccess = function(data) { |
| 17 | + $uibModalInstance.close(data); |
| 18 | + }; |
| 19 | + |
| 20 | + var onFailure = function(response) { |
| 21 | + NotificationSrv.error('AdminCustomFieldDialogCtrl', response.data, response.status); |
| 22 | + }; |
| 23 | + |
| 24 | + var buildOptionsCollection = function(options) { |
| 25 | + if (!options || options === '') { |
| 26 | + return []; |
83 | 27 | }
|
84 | 28 |
|
85 |
| - self.cancel = function() { |
86 |
| - $uibModalInstance.dismiss(); |
| 29 | + var type = self.customField.type; |
| 30 | + var values = self.customField.options.split('\n'); |
| 31 | + |
| 32 | + if (type === 'number') { |
| 33 | + return _.without(values.map(function(item) { |
| 34 | + return Number(item); |
| 35 | + }), NaN); |
87 | 36 | }
|
88 | 37 |
|
89 |
| - self.onNamechanged = function(form) { |
90 |
| - if(!self.customField.name) { |
91 |
| - return; |
92 |
| - } |
| 38 | + return values; |
| 39 | + }; |
| 40 | + |
| 41 | + self.saveField = function(form) { |
| 42 | + if (!form.$valid) { |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + var postData = _.pick(self.customField, 'name', 'reference', 'description', 'type'); |
| 47 | + postData.options = buildOptionsCollection(self.customField.options); |
| 48 | + |
| 49 | + if (self.customField.id) { |
| 50 | + ListSrv.update({ |
| 51 | + 'itemId': self.customField.id |
| 52 | + }, { |
| 53 | + 'value': postData |
| 54 | + }, onSuccess, onFailure); |
| 55 | + } else { |
| 56 | + |
| 57 | + ListSrv.exists({ |
| 58 | + 'listId': 'custom_fields' |
| 59 | + }, { |
| 60 | + key: 'reference', |
| 61 | + value: postData.reference |
| 62 | + }, function(response) { |
| 63 | + |
| 64 | + if (response.toJSON().found === true) { |
| 65 | + form.reference.$setValidity('unique', false); |
| 66 | + form.reference.$setDirty(); |
| 67 | + } else { |
| 68 | + ListSrv.save({ |
| 69 | + 'listId': 'custom_fields' |
| 70 | + }, { |
| 71 | + 'value': JSON.stringify(postData) |
| 72 | + }, onSuccess, onFailure); |
| 73 | + } |
| 74 | + }, onFailure); |
| 75 | + } |
| 76 | + }; |
| 77 | + |
| 78 | + self.clearUniqueReferenceError = function(form) { |
| 79 | + form.reference.$setValidity('unique', true); |
| 80 | + form.reference.$setPristine(); |
| 81 | + } |
| 82 | + |
| 83 | + self.cancel = function() { |
| 84 | + $uibModalInstance.dismiss(); |
| 85 | + } |
| 86 | + |
| 87 | + self.onNamechanged = function(form) { |
| 88 | + if (!self.customField.name) { |
| 89 | + return; |
| 90 | + } |
93 | 91 |
|
94 |
| - var reference = s.trim(s.classify(self.customField.name)); |
95 |
| - reference = reference.charAt(0).toLowerCase() + reference.slice(1); |
| 92 | + var reference = s.trim(s.classify(self.customField.name)); |
| 93 | + reference = reference.charAt(0).toLowerCase() + reference.slice(1); |
96 | 94 |
|
97 |
| - self.customField.reference = reference; |
| 95 | + self.customField.reference = reference; |
98 | 96 |
|
99 |
| - self.clearUniqueReferenceError(form); |
100 |
| - }; |
| 97 | + self.clearUniqueReferenceError(form); |
| 98 | + }; |
101 | 99 |
|
102 |
| - }); |
| 100 | + }); |
103 | 101 | })();
|
0 commit comments