Skip to content

Commit fa85f89

Browse files
committed
#12 Fix the dblist rows parsing and saving
1 parent 5e99e7a commit fa85f89

File tree

3 files changed

+94
-100
lines changed

3 files changed

+94
-100
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,103 +1,101 @@
11
(function() {
22
'use strict';
33

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 [];
8327
}
8428

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);
8736
}
8837

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+
}
9391

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);
9694

97-
self.customField.reference = reference;
95+
self.customField.reference = reference;
9896

99-
self.clearUniqueReferenceError(form);
100-
};
97+
self.clearUniqueReferenceError(form);
98+
};
10199

102-
});
100+
});
103101
})();

ui/app/scripts/controllers/admin/AdminCustomFieldsCtrl.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,8 @@
2525
}, {}, function(response) {
2626

2727
self.customFields = _.map(response.toJSON(), function(value, key) {
28-
var obj = JSON.parse(value);
29-
obj.id = key;
30-
31-
return obj;
28+
value.id = key;
29+
return value;
3230
});
3331

3432
}, function(response) {

ui/app/scripts/services/CustomFieldsCacheSrv.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,12 @@
2727

2828
if(cache === null) {
2929
resource.query({listId: 'custom_fields'}, {}, function(response) {
30+
var json = response.toJSON();
3031
cache = {};
31-
var data = _.values(response).filter(_.isString).map(function(item) {
32-
return JSON.parse(item);
33-
});
3432

35-
_.each(data, function(field){
33+
_.each(_.values(json), function(field) {
3634
cache[field.reference] = field;
37-
});
35+
})
3836

3937
deferred.resolve(cache);
4038
}, function(response) {

0 commit comments

Comments
 (0)