Inventory host edit method now supports and 'edit' and an 'add' mode. Adding a host now uses the shared modal module, giving it a resizable and draggable dialog along with auto-resizing textarea.

This commit is contained in:
Chris Houseknecht
2014-03-20 13:20:03 -04:00
parent 1bc9ab68eb
commit d38c8ee93e
2 changed files with 109 additions and 63 deletions

View File

@@ -328,7 +328,7 @@ InventoriesAdd.$inject = ['$scope', '$rootScope', '$compile', '$location', '$log
function InventoriesEdit($scope, $location, $routeParams, $compile, GenerateList, ClearScope, InventoryGroups, InventoryHosts, BuildTree, Wait, function InventoriesEdit($scope, $location, $routeParams, $compile, GenerateList, ClearScope, InventoryGroups, InventoryHosts, BuildTree, Wait,
GetSyncStatusMsg, InjectHosts, HostsReload, GroupsEdit, GroupsDelete, Breadcrumbs, LoadBreadCrumbs, Empty, Rest, ProcessErrors, GetSyncStatusMsg, InjectHosts, HostsReload, GroupsEdit, GroupsDelete, Breadcrumbs, LoadBreadCrumbs, Empty, Rest, ProcessErrors,
InventoryUpdate, Alert, ToggleChildren, ViewUpdateStatus, GroupsCancelUpdate, Find, HostsCreate, EditInventoryProperties, HostsEdit, InventoryUpdate, Alert, ToggleChildren, ViewUpdateStatus, GroupsCancelUpdate, Find, EditInventoryProperties, HostsEdit,
HostsDelete, ToggleHostEnabled, CopyMoveGroup, CopyMoveHost, Stream, GetBasePath, ShowJobSummary, ApplyEllipsis, WatchInventoryWindowResize, HostsDelete, ToggleHostEnabled, CopyMoveGroup, CopyMoveHost, Stream, GetBasePath, ShowJobSummary, ApplyEllipsis, WatchInventoryWindowResize,
HelpDialog, InventoryGroupsHelp, Store) { HelpDialog, InventoryGroupsHelp, Store) {
@@ -586,7 +586,7 @@ function InventoriesEdit($scope, $location, $routeParams, $compile, GenerateList
}; };
$scope.createHost = function () { $scope.createHost = function () {
HostsCreate({ scope: $scope }); HostsEdit({ scope: $scope, mode: 'add', host_id: null, selected_group_id: $scope.selected_tree_id, inventory_id: $scope.inventory_id });
}; };
$scope.editInventoryProperties = function () { $scope.editInventoryProperties = function () {
@@ -594,7 +594,7 @@ function InventoriesEdit($scope, $location, $routeParams, $compile, GenerateList
}; };
$scope.editHost = function (host_id) { $scope.editHost = function (host_id) {
HostsEdit({ scope: $scope, host_id: host_id, inventory_id: $scope.inventory_id }); HostsEdit({ scope: $scope, mode: 'edit', host_id: host_id, inventory_id: $scope.inventory_id });
}; };
$scope.deleteHost = function (host_id, host_name) { $scope.deleteHost = function (host_id, host_name) {
@@ -667,6 +667,6 @@ function InventoriesEdit($scope, $location, $routeParams, $compile, GenerateList
InventoriesEdit.$inject = ['$scope', '$location', '$routeParams', '$compile', 'GenerateList', 'ClearScope', 'InventoryGroups', 'InventoryHosts', InventoriesEdit.$inject = ['$scope', '$location', '$routeParams', '$compile', 'GenerateList', 'ClearScope', 'InventoryGroups', 'InventoryHosts',
'BuildTree', 'Wait', 'GetSyncStatusMsg', 'InjectHosts', 'HostsReload', 'GroupsEdit', 'GroupsDelete', 'Breadcrumbs', 'BuildTree', 'Wait', 'GetSyncStatusMsg', 'InjectHosts', 'HostsReload', 'GroupsEdit', 'GroupsDelete', 'Breadcrumbs',
'LoadBreadCrumbs', 'Empty', 'Rest', 'ProcessErrors', 'InventoryUpdate', 'Alert', 'ToggleChildren', 'ViewUpdateStatus', 'GroupsCancelUpdate', 'LoadBreadCrumbs', 'Empty', 'Rest', 'ProcessErrors', 'InventoryUpdate', 'Alert', 'ToggleChildren', 'ViewUpdateStatus', 'GroupsCancelUpdate',
'Find', 'HostsCreate', 'EditInventoryProperties', 'HostsEdit', 'HostsDelete', 'ToggleHostEnabled', 'CopyMoveGroup', 'CopyMoveHost', 'Find', 'EditInventoryProperties', 'HostsEdit', 'HostsDelete', 'ToggleHostEnabled', 'CopyMoveGroup', 'CopyMoveHost',
'Stream', 'GetBasePath', 'ShowJobSummary', 'ApplyEllipsis', 'WatchInventoryWindowResize', 'HelpDialog', 'InventoryGroupsHelp', 'Store' 'Stream', 'GetBasePath', 'ShowJobSummary', 'ApplyEllipsis', 'WatchInventoryWindowResize', 'HelpDialog', 'InventoryGroupsHelp', 'Store'
]; ];

View File

@@ -420,22 +420,24 @@ function($rootScope, $location, $log, $routeParams, Rest, Alert, HostForm, Gener
.factory('HostsEdit', ['$rootScope', '$location', '$log', '$routeParams', 'Rest', 'Alert', 'HostForm', 'GenerateForm', .factory('HostsEdit', ['$rootScope', '$location', '$log', '$routeParams', 'Rest', 'Alert', 'HostForm', 'GenerateForm',
'Prompt', 'ProcessErrors', 'GetBasePath', 'HostsReload', 'ParseTypeChange', 'Wait', 'Find', 'SetStatus', 'ApplyEllipsis', 'Prompt', 'ProcessErrors', 'GetBasePath', 'HostsReload', 'ParseTypeChange', 'Wait', 'Find', 'SetStatus', 'ApplyEllipsis',
'WatchInventoryWindowResize', 'ToJSON', 'ParseVariableString', 'CreateDialog', 'TextareaResize', 'WatchInventoryWindowResize', 'ToJSON', 'ParseVariableString', 'CreateDialog', 'TextareaResize', 'Empty',
function($rootScope, $location, $log, $routeParams, Rest, Alert, HostForm, GenerateForm, Prompt, ProcessErrors, function($rootScope, $location, $log, $routeParams, Rest, Alert, HostForm, GenerateForm, Prompt, ProcessErrors,
GetBasePath, HostsReload, ParseTypeChange, Wait, Find, SetStatus, ApplyEllipsis, WatchInventoryWindowResize, ToJSON, GetBasePath, HostsReload, ParseTypeChange, Wait, Find, SetStatus, ApplyEllipsis, WatchInventoryWindowResize, ToJSON,
ParseVariableString, CreateDialog, TextareaResize) { ParseVariableString, CreateDialog, TextareaResize, Empty) {
return function(params) { return function(params) {
var parent_scope = params.scope, var parent_scope = params.scope,
host_id = params.host_id, host_id = params.host_id,
inventory_id = params.inventory_id, inventory_id = params.inventory_id,
mode = params.mode, // 'add' or 'edit'
selected_group_id = params.selected_group_id,
generator = GenerateForm, generator = GenerateForm,
form = HostForm, form = HostForm,
defaultUrl = GetBasePath('hosts') + host_id + '/', defaultUrl,
scope = parent_scope.$new(), scope = parent_scope.$new(),
master = {}, master = {},
relatedSets = {}, relatedSets = {},
buttons; group, buttons;
generator.inject(HostForm, { mode: 'edit', id: 'host-modal-dialog', breadCrumbs: false, related: false, scope: scope }); generator.inject(HostForm, { mode: 'edit', id: 'host-modal-dialog', breadCrumbs: false, related: false, scope: scope });
generator.reset(); generator.reset();
@@ -481,12 +483,14 @@ function($rootScope, $location, $log, $routeParams, Rest, Alert, HostForm, Gener
}); });
}, },
onOpen: function() { onOpen: function() {
TextareaResize({ setTimeout(function() {
scope: scope, TextareaResize({
textareaId: 'host_variables', scope: scope,
modalId: 'host-modal-dialog', textareaId: 'host_variables',
formId: 'host_form' modalId: 'host-modal-dialog',
}); formId: 'host_form'
});
}, 300);
} }
}); });
@@ -529,58 +533,86 @@ function($rootScope, $location, $log, $routeParams, Rest, Alert, HostForm, Gener
Wait('start'); Wait('start');
// Retrieve detail record and prepopulate the form // Retrieve detail record and prepopulate the form
Rest.setUrl(defaultUrl); if (mode === 'edit') {
Rest.get() defaultUrl = GetBasePath('hosts') + host_id + '/';
.success( function(data) { Rest.setUrl(defaultUrl);
var set, fld, related; Rest.get()
for (fld in form.fields) { .success( function(data) {
if (data[fld]) { var set, fld, related;
scope[fld] = data[fld]; for (fld in form.fields) {
master[fld] = scope[fld]; if (data[fld]) {
scope[fld] = data[fld];
master[fld] = scope[fld];
}
} }
} related = data.related;
related = data.related; for (set in form.related) {
for (set in form.related) { if (related[set]) {
if (related[set]) { relatedSets[set] = { url: related[set], iterator: form.related[set].iterator };
relatedSets[set] = { url: related[set], iterator: form.related[set].iterator }; }
} }
} scope.variable_url = data.related.variable_data;
scope.variable_url = data.related.variable_data; scope.has_inventory_sources = data.has_inventory_sources;
scope.has_inventory_sources = data.has_inventory_sources; scope.$emit('hostLoaded');
scope.$emit('hostLoaded'); })
}) .error( function(data, status) {
.error( function(data, status) { ProcessErrors(scope, data, status, form,
ProcessErrors(scope, data, status, form, { hdr: 'Error!', msg: 'Failed to retrieve host: ' + host_id + '. GET returned status: ' + status });
{ hdr: 'Error!', msg: 'Failed to retrieve host: ' + host_id + '. GET returned status: ' + status }); });
}); }
else {
// Add mode
group = Find({ list: scope.groups, key: 'id', val: selected_group_id });
if (!Empty(group)) {
scope.has_inventory_sources = group.has_inventory_sources;
scope.enabled = true;
scope.variables = '---';
defaultUrl = GetBasePath('groups') + group.group_id + '/hosts/';
scope.$emit('hostVariablesLoaded');
}
else {
ProcessErrors(scope, null, status, null, { hdr: 'Error',
msg: 'Group lookup failed. Selected group id: ' + selected_group_id });
}
}
if (scope.removeSaveCompleted) { if (scope.removeSaveCompleted) {
scope.removeSaveCompleted(); scope.removeSaveCompleted();
} }
scope.removeSaveCompleted = scope.$on('saveCompleted', function() { scope.removeSaveCompleted = scope.$on('saveCompleted', function() {
// Update the name on the list var host, old_name;
var host = Find({ list: parent_scope.hosts, key: 'id', val: host_id }), if (mode === 'edit') {
// Update the name on the list
host = Find({ list: parent_scope.hosts, key: 'id', val: host_id });
old_name = host.name; old_name = host.name;
host.name = scope.name; host.name = scope.name;
host.enabled = (scope.enabled) ? true : false; host.enabled = (scope.enabled) ? true : false;
host.enabled_flag = host.enabled; host.enabled_flag = host.enabled;
SetStatus({ scope: parent_scope, host: host }); SetStatus({ scope: parent_scope, host: host });
// Update any titles attributes created by ApplyEllipsis
// Update any titles attributes created by ApplyEllipsis if (old_name) {
if (old_name) { setTimeout(function() {
setTimeout(function() { $('#hosts_table .host-name a[title="' + old_name + '"]').attr('title', host.name);
$('#hosts_table .host-name a[title="' + old_name + '"]').attr('title', host.name); ApplyEllipsis('#hosts_table .host-name a');
ApplyEllipsis('#hosts_table .host-name a'); // Close modal
$('#host-modal-dialog').dialog('close');
}, 2000);
}
else {
// Close modal // Close modal
$('#host-modal-dialog').dialog('close'); $('#host-modal-dialog').dialog('close');
}, 2000); }
} }
else { else {
// Close modal
Wait('stop');
$('#host-modal-dialog').dialog('close'); $('#host-modal-dialog').dialog('close');
HostsReload({
scope: parent_scope,
group_id: parent_scope.selected_group_id,
tree_id: parent_scope.selected_tree_id,
inventory_id: parent_scope.inventory_id
});
} }
// Restore ellipsis response to window resize // Restore ellipsis response to window resize
WatchInventoryWindowResize(); WatchInventoryWindowResize();
}); });
@@ -598,15 +630,26 @@ function($rootScope, $location, $log, $routeParams, Rest, Alert, HostForm, Gener
} }
data.inventory = inventory_id; data.inventory = inventory_id;
Rest.setUrl(defaultUrl); Rest.setUrl(defaultUrl);
Rest.put(data) if (mode === 'edit') {
.success( function() { Rest.put(data)
scope.$emit('saveCompleted'); .success( function() {
}) scope.$emit('saveCompleted');
.error( function(data, status) { })
Wait('stop'); .error( function(data, status) {
ProcessErrors(scope, data, status, form, ProcessErrors(scope, data, status, form,
{ hdr: 'Error!', msg: 'Failed to update host: ' + host_id + '. PUT returned status: ' + status }); { hdr: 'Error!', msg: 'Failed to update host: ' + host_id + '. PUT returned status: ' + status });
}); });
}
else {
Rest.post(data)
.success( function() {
scope.$emit('saveCompleted');
})
.error( function(data, status) {
ProcessErrors(scope, data, status, form,
{ hdr: 'Error!', msg: 'Failed to create host. POST returned status: ' + status });
});
}
} }
catch(e) { catch(e) {
// ignore. ToJSON will have already alerted the user // ignore. ToJSON will have already alerted the user
@@ -671,14 +714,16 @@ function($rootScope, $location, $log, $routeParams, Rest, Alert, Prompt, Process
scope.removeHostsReload(); scope.removeHostsReload();
} }
scope.removeHostsReload = scope.$on('hostsReload', function() { scope.removeHostsReload = scope.$on('hostsReload', function() {
$('#prompt-modal').modal('hide');
scope.showHosts(scope.selected_tree_id, scope.selected_group_id, false); scope.showHosts(scope.selected_tree_id, scope.selected_group_id, false);
}); });
$('#prompt-modal').on('hidden.bs.modal', function(){ Wait('stop'); });
action_to_take = function() { action_to_take = function() {
var count=0, i; var count=0, i;
$('#prompt-modal').on('hidden.bs.modal', function(){ Wait('start'); }); Wait('start');
$('#prompt-modal').modal('hide');
if (scope.removeHostRemoved) { if (scope.removeHostRemoved) {
scope.removeHostRemoved(); scope.removeHostRemoved();
@@ -686,6 +731,7 @@ function($rootScope, $location, $log, $routeParams, Rest, Alert, Prompt, Process
scope.removeHostRemoved = scope.$on('hostRemoved', function(){ scope.removeHostRemoved = scope.$on('hostRemoved', function(){
count++; count++;
if (count === url_list.length) { if (count === url_list.length) {
Wait('start');
scope.$emit('hostsReload'); scope.$emit('hostsReload');
} }
}); });