mirror of
https://github.com/ZwareBear/awx.git
synced 2026-04-04 23:21:48 -05:00
Adding notifications into the UI
This commit is contained in:
@@ -6,91 +6,179 @@
|
||||
|
||||
export default
|
||||
[ 'Rest', 'Wait',
|
||||
'notificationsFormObject', 'ProcessErrors', 'GetBasePath',
|
||||
'NotificationsFormObject', 'ProcessErrors', 'GetBasePath',
|
||||
'GenerateForm', 'SearchInit' , 'PaginateInit',
|
||||
'LookUpInit', 'OrganizationList', 'inventory_script',
|
||||
'$scope', '$state',
|
||||
'LookUpInit', 'OrganizationList', 'notifier',
|
||||
'$scope', '$state', 'GetChoices', 'CreateSelect2', 'Empty',
|
||||
'$rootScope', 'NotificationsTypeChange',
|
||||
function(
|
||||
Rest, Wait,
|
||||
notificationsFormObject, ProcessErrors, GetBasePath,
|
||||
NotificationsFormObject, ProcessErrors, GetBasePath,
|
||||
GenerateForm, SearchInit, PaginateInit,
|
||||
LookUpInit, OrganizationList, inventory_script,
|
||||
$scope, $state
|
||||
LookUpInit, OrganizationList, notifier,
|
||||
$scope, $state, GetChoices, CreateSelect2, Empty,
|
||||
$rootScope, NotificationsTypeChange
|
||||
) {
|
||||
var generator = GenerateForm,
|
||||
id = inventory_script.id,
|
||||
form = notificationsFormObject,
|
||||
id = notifier.id,
|
||||
form = NotificationsFormObject,
|
||||
master = {},
|
||||
url = GetBasePath('notifications');
|
||||
url = GetBasePath('notifiers');
|
||||
|
||||
$scope.inventory_script = inventory_script;
|
||||
$scope.notifier = notifier;
|
||||
generator.inject(form, {
|
||||
mode: 'edit' ,
|
||||
scope:$scope,
|
||||
related: false,
|
||||
activityStream: false
|
||||
related: false
|
||||
});
|
||||
if ($scope.removeChoicesReady) {
|
||||
$scope.removeChoicesReady();
|
||||
}
|
||||
$scope.removeChoicesReady = $scope.$on('choicesReady', function () {
|
||||
var i;
|
||||
for (i = 0; i < $scope.notification_type_options.length; i++) {
|
||||
if ($scope.notification_type_options[i].value === '') {
|
||||
$scope.notification_type_options[i].value="manual";
|
||||
// $scope.scm_type = $scope.scm_type_options[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Wait('start');
|
||||
Rest.setUrl(url + id+'/');
|
||||
Rest.get()
|
||||
.success(function (data) {
|
||||
var fld;
|
||||
for (fld in form.fields) {
|
||||
if (data[fld]) {
|
||||
$scope[fld] = data[fld];
|
||||
master[fld] = data[fld];
|
||||
}
|
||||
|
||||
if(data.notification_configuration[fld]){
|
||||
$scope[fld] = data.notification_configuration[fld];
|
||||
master[fld] = data.notification_configuration[fld];
|
||||
|
||||
if(form.fields[fld].type === 'textarea'){
|
||||
$scope[fld] = $scope[fld].toString().replace(',' , '\n');
|
||||
}
|
||||
}
|
||||
|
||||
if (form.fields[fld].sourceModel && data.summary_fields &&
|
||||
data.summary_fields[form.fields[fld].sourceModel]) {
|
||||
$scope[form.fields[fld].sourceModel + '_' + form.fields[fld].sourceField] =
|
||||
data.summary_fields[form.fields[fld].sourceModel][form.fields[fld].sourceField];
|
||||
master[form.fields[fld].sourceModel + '_' + form.fields[fld].sourceField] =
|
||||
data.summary_fields[form.fields[fld].sourceModel][form.fields[fld].sourceField];
|
||||
}
|
||||
}
|
||||
data.notification_type = (Empty(data.notification_type)) ? '' : data.notification_type;
|
||||
for (var i = 0; i < $scope.notification_type_options.length; i++) {
|
||||
if ($scope.notification_type_options[i].value === data.notification_type) {
|
||||
$scope.notification_type = $scope.notification_type_options[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// if ($scope.notification_type.value !== 'manual') {
|
||||
// $scope.pathRequired = false;
|
||||
// $scope.scmRequired = true;
|
||||
// } else {
|
||||
// $scope.pathRequired = true;
|
||||
// $scope.scmRequired = false;
|
||||
// }
|
||||
|
||||
master.notification_type = $scope.notification_type;
|
||||
CreateSelect2({
|
||||
element: '#notifier_notification_type',
|
||||
multiple: false
|
||||
});
|
||||
NotificationsTypeChange.getDetailFields($scope.notification_type.value).forEach(function(field) {
|
||||
$scope[field[0]] = field[1];
|
||||
});
|
||||
Wait('stop');
|
||||
})
|
||||
.error(function (data, status) {
|
||||
ProcessErrors($scope, data, status, form, { hdr: 'Error!',
|
||||
msg: 'Failed to retrieve notification: ' + id + '. GET status: ' + status });
|
||||
});
|
||||
});
|
||||
generator.reset();
|
||||
LookUpInit({
|
||||
url: GetBasePath('organization'),
|
||||
scope: $scope,
|
||||
form: form,
|
||||
// hdr: "Select Custom Inventory",
|
||||
list: OrganizationList,
|
||||
field: 'organization',
|
||||
input_type: 'radio'
|
||||
});
|
||||
|
||||
// Retrieve detail record and prepopulate the form
|
||||
Wait('start');
|
||||
Rest.setUrl(url + id+'/');
|
||||
Rest.get()
|
||||
.success(function (data) {
|
||||
var fld;
|
||||
for (fld in form.fields) {
|
||||
if (data[fld]) {
|
||||
$scope[fld] = data[fld];
|
||||
master[fld] = data[fld];
|
||||
}
|
||||
GetChoices({
|
||||
scope: $scope,
|
||||
url: url,
|
||||
field: 'notification_type',
|
||||
variable: 'notification_type_options',
|
||||
callback: 'choicesReady'
|
||||
});
|
||||
|
||||
if (form.fields[fld].sourceModel && data.summary_fields &&
|
||||
data.summary_fields[form.fields[fld].sourceModel]) {
|
||||
$scope[form.fields[fld].sourceModel + '_' + form.fields[fld].sourceField] =
|
||||
data.summary_fields[form.fields[fld].sourceModel][form.fields[fld].sourceField];
|
||||
master[form.fields[fld].sourceModel + '_' + form.fields[fld].sourceField] =
|
||||
data.summary_fields[form.fields[fld].sourceModel][form.fields[fld].sourceField];
|
||||
}
|
||||
$scope.typeChange = function () {
|
||||
for(var fld in form.fields){
|
||||
if(form.fields[fld] && form.fields[fld].subForm){
|
||||
$scope[fld] = null;
|
||||
$scope.notifier_form[fld].$setPristine();
|
||||
}
|
||||
}
|
||||
|
||||
NotificationsTypeChange.getDetailFields($scope.notification_type.value).forEach(function(field) {
|
||||
$scope[field[0]] = field[1];
|
||||
});
|
||||
};
|
||||
|
||||
$scope.formSave = function(){
|
||||
var params,
|
||||
// config,
|
||||
v = $scope.notification_type.value;
|
||||
|
||||
generator.clearApiErrors();
|
||||
params = {
|
||||
"name" : $scope.name,
|
||||
"description": $scope.description,
|
||||
"organization": $scope.organization,
|
||||
"notification_type" : v,
|
||||
"notification_configuration": {}
|
||||
};
|
||||
|
||||
function processValue(value, i , field){
|
||||
if(field.type === 'textarea'){
|
||||
$scope[i] = $scope[i].toString().split('\n');
|
||||
}
|
||||
if(field.type === 'checkbox'){
|
||||
$scope[i] = Boolean($scope[i]);
|
||||
}
|
||||
return $scope[i];
|
||||
|
||||
}
|
||||
|
||||
params.notification_configuration = _.object(Object.keys(form.fields)
|
||||
.filter(i => (form.fields[i].ngShow && form.fields[i].ngShow.indexOf(v) > -1))
|
||||
.map(i => [i, processValue($scope[i], i , form.fields[i])]));
|
||||
|
||||
Wait('start');
|
||||
Rest.setUrl(url+ id+'/');
|
||||
Rest.put(params)
|
||||
.success(function (data) {
|
||||
$rootScope.addedItem = data.id;
|
||||
$state.go('notifications', {}, {reload: true});
|
||||
Wait('stop');
|
||||
})
|
||||
.error(function (data, status) {
|
||||
ProcessErrors($scope, data, status, form, { hdr: 'Error!',
|
||||
msg: 'Failed to retrieve inventory script: ' + id + '. GET status: ' + status });
|
||||
msg: 'Failed to add new notifier. POST returned status: ' + status });
|
||||
});
|
||||
|
||||
$scope.formSave = function () {
|
||||
generator.clearApiErrors();
|
||||
Wait('start');
|
||||
Rest.setUrl(url+ id+'/');
|
||||
Rest.put({
|
||||
name: $scope.name,
|
||||
description: $scope.description,
|
||||
organization: $scope.organization,
|
||||
script: $scope.script
|
||||
})
|
||||
.success(function () {
|
||||
$state.transitionTo('inventoryScriptsList');
|
||||
Wait('stop');
|
||||
|
||||
})
|
||||
.error(function (data, status) {
|
||||
ProcessErrors($scope, data, status, form, { hdr: 'Error!',
|
||||
msg: 'Failed to add new inventory script. PUT returned status: ' + status });
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
$scope.formCancel = function () {
|
||||
$state.transitionTo('inventoryScripts');
|
||||
$state.transitionTo('notifications');
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user