-
Notifications
You must be signed in to change notification settings - Fork 640
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#12 Fix the dblist rows parsing and saving
- Loading branch information
Showing
3 changed files
with
94 additions
and
100 deletions.
There are no files selected for viewing
180 changes: 89 additions & 91 deletions
180
ui/app/scripts/controllers/admin/AdminCustomFieldDialogCtrl.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,103 +1,101 @@ | ||
(function() { | ||
'use strict'; | ||
|
||
angular.module('theHiveControllers').controller('AdminCustomFieldDialogCtrl', | ||
function($scope, $uibModalInstance, ListSrv, NotificationSrv, customField) { | ||
var self = this; | ||
self.config = { | ||
types: ['string', 'number', 'boolean', 'date'], | ||
referencePattern: '^[a-zA-Z]{1}[a-zA-Z0-9_-]*' | ||
}; | ||
|
||
self.customField = customField; | ||
self.customField.options = (customField.options || []).join('\n'); | ||
|
||
var onSuccess = function(data) { | ||
$uibModalInstance.close(data); | ||
}; | ||
|
||
var onFailure = function(response) { | ||
NotificationSrv.error('AdminCustomFieldDialogCtrl', response.data, response.status); | ||
}; | ||
|
||
var buildOptionsCollection = function(options) { | ||
if(!options || options === '') { | ||
return []; | ||
} | ||
|
||
var type = self.customField.type; | ||
var values = self.customField.options.split('\n'); | ||
|
||
if(type === 'number') { | ||
return _.without(values.map(function(item) { | ||
return Number(item); | ||
}), NaN); | ||
} | ||
|
||
return values; | ||
}; | ||
|
||
self.saveField = function(form) { | ||
if (!form.$valid) { | ||
return; | ||
} | ||
|
||
var postData = _.pick(self.customField, 'name', 'reference', 'description', 'type'); | ||
postData.options = buildOptionsCollection(self.customField.options); | ||
|
||
if(self.customField.id) { | ||
ListSrv.update( | ||
{'itemId': self.customField.id}, | ||
{'value': JSON.stringify(postData)}, | ||
onSuccess, | ||
onFailure); | ||
} else { | ||
ListSrv.exists( | ||
{'listId': 'custom_fields'}, | ||
{ | ||
key: 'reference', | ||
value: postData.reference | ||
}, | ||
function(response) { | ||
if(response.data) { | ||
ListSrv.save( | ||
{'listId': 'custom_fields'}, | ||
{'value': JSON.stringify(postData)}, | ||
onSuccess, | ||
onFailure); | ||
} else { | ||
// TODO handle field validation | ||
form.reference.$setValidity('unique', false); | ||
form.reference.$setDirty(); | ||
NotificationSrv.error('AdminCustomFieldDialogCtrl', 'There is already a custom field with the specified reference: ' + postData.reference); | ||
} | ||
}, | ||
onFailure | ||
) | ||
} | ||
}; | ||
|
||
self.clearUniqueReferenceError = function(form) { | ||
form.reference.$setValidity('unique', true); | ||
form.reference.$setPristine(); | ||
angular.module('theHiveControllers').controller('AdminCustomFieldDialogCtrl', function($scope, $uibModalInstance, ListSrv, NotificationSrv, customField) { | ||
var self = this; | ||
self.config = { | ||
types: [ | ||
'string', 'number', 'boolean', 'date' | ||
], | ||
referencePattern: '^[a-zA-Z]{1}[a-zA-Z0-9_-]*' | ||
}; | ||
|
||
self.customField = customField; | ||
self.customField.options = (customField.options || []).join('\n'); | ||
|
||
var onSuccess = function(data) { | ||
$uibModalInstance.close(data); | ||
}; | ||
|
||
var onFailure = function(response) { | ||
NotificationSrv.error('AdminCustomFieldDialogCtrl', response.data, response.status); | ||
}; | ||
|
||
var buildOptionsCollection = function(options) { | ||
if (!options || options === '') { | ||
return []; | ||
} | ||
|
||
self.cancel = function() { | ||
$uibModalInstance.dismiss(); | ||
var type = self.customField.type; | ||
var values = self.customField.options.split('\n'); | ||
|
||
if (type === 'number') { | ||
return _.without(values.map(function(item) { | ||
return Number(item); | ||
}), NaN); | ||
} | ||
|
||
self.onNamechanged = function(form) { | ||
if(!self.customField.name) { | ||
return; | ||
} | ||
return values; | ||
}; | ||
|
||
self.saveField = function(form) { | ||
if (!form.$valid) { | ||
return; | ||
} | ||
|
||
var postData = _.pick(self.customField, 'name', 'reference', 'description', 'type'); | ||
postData.options = buildOptionsCollection(self.customField.options); | ||
|
||
if (self.customField.id) { | ||
ListSrv.update({ | ||
'itemId': self.customField.id | ||
}, { | ||
'value': postData | ||
}, onSuccess, onFailure); | ||
} else { | ||
|
||
ListSrv.exists({ | ||
'listId': 'custom_fields' | ||
}, { | ||
key: 'reference', | ||
value: postData.reference | ||
}, function(response) { | ||
|
||
if (response.toJSON().found === true) { | ||
form.reference.$setValidity('unique', false); | ||
form.reference.$setDirty(); | ||
} else { | ||
ListSrv.save({ | ||
'listId': 'custom_fields' | ||
}, { | ||
'value': JSON.stringify(postData) | ||
}, onSuccess, onFailure); | ||
} | ||
}, onFailure); | ||
} | ||
}; | ||
|
||
self.clearUniqueReferenceError = function(form) { | ||
form.reference.$setValidity('unique', true); | ||
form.reference.$setPristine(); | ||
} | ||
|
||
self.cancel = function() { | ||
$uibModalInstance.dismiss(); | ||
} | ||
|
||
self.onNamechanged = function(form) { | ||
if (!self.customField.name) { | ||
return; | ||
} | ||
|
||
var reference = s.trim(s.classify(self.customField.name)); | ||
reference = reference.charAt(0).toLowerCase() + reference.slice(1); | ||
var reference = s.trim(s.classify(self.customField.name)); | ||
reference = reference.charAt(0).toLowerCase() + reference.slice(1); | ||
|
||
self.customField.reference = reference; | ||
self.customField.reference = reference; | ||
|
||
self.clearUniqueReferenceError(form); | ||
}; | ||
self.clearUniqueReferenceError(form); | ||
}; | ||
|
||
}); | ||
}); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters