From e17d076e557478fae86b4f40da8d695497e8c0e9 Mon Sep 17 00:00:00 2001 From: Chris Houseknecht Date: Thu, 6 Feb 2014 00:58:30 -0500 Subject: [PATCH] AC-1017 Added events and hosts links to the job status fly-out. Fixed a bug in search where claring the value and hitting enter did not switch the icon back to the magnifying glass. Linted Search.js and Hosts.js helpers. --- .jshintrc | 2 +- awx/ui/static/js/helpers/Hosts.js | 1012 +++++++++++++--------------- awx/ui/static/js/helpers/search.js | 357 +++++----- awx/ui/static/less/ansible-ui.less | 6 + 4 files changed, 658 insertions(+), 719 deletions(-) diff --git a/.jshintrc b/.jshintrc index 9843934c27..e0e02de4d9 100644 --- a/.jshintrc +++ b/.jshintrc @@ -7,7 +7,7 @@ "jquery": true, "esnext": true, "globalstrict": true, - "globals": { "angular":false, "alert":false, "$AnsibleConfig":true, "$basePath":true }, + "globals": { "angular":false, "alert":false, "$AnsibleConfig":true, "$basePath":true, "jsyaml":false }, "strict": false, "quotmark": false, "smarttabs": true, diff --git a/awx/ui/static/js/helpers/Hosts.js b/awx/ui/static/js/helpers/Hosts.js index 8e6915fcc9..cb5e0628b4 100644 --- a/awx/ui/static/js/helpers/Hosts.js +++ b/awx/ui/static/js/helpers/Hosts.js @@ -7,15 +7,16 @@ * */ +/* jshint loopfunc: true */ + angular.module('HostsHelper', [ 'RestServices', 'Utilities', 'ListGenerator', 'HostListDefinition', 'SearchHelper', 'PaginationHelpers', 'ListGenerator', 'AuthService', 'HostsHelper', - 'InventoryHelper', 'RelatedSearchHelper', 'InventoryFormDefinition', 'SelectionHelper', + 'InventoryHelper', 'RelatedSearchHelper', 'InventoryFormDefinition', 'SelectionHelper', 'HostGroupsFormDefinition' ]) - - .factory('SetEnabledMsg', [ function() { - return function(host) { +.factory('SetEnabledMsg', [ function() { + return function(host) { if (host.has_inventory_sources) { // Inventory sync managed, so not clickable host.enabledToolTip = (host.enabled) ? 'Host is available' : 'Host is not available'; @@ -24,43 +25,51 @@ angular.module('HostsHelper', [ 'RestServices', 'Utilities', 'ListGenerator', 'H // Clickable host.enabledToolTip = (host.enabled) ? 'Host is available. Click to toggle.' : 'Host is not available. Click to toggle.'; } - } - }]) + }; +}]) - .factory('SetHostStatus', ['SetEnabledMsg', function(SetEnabledMsg) { - return function(host) { +.factory('SetHostStatus', ['SetEnabledMsg', function(SetEnabledMsg) { + return function(host) { // Set status related fields on a host object host.activeFailuresLink = '/#/hosts/' + host.id + '/job_host_summaries/?inventory=' + host.inventory + - '&host_name=' + escape(host.name); - if (host.has_active_failures == true) { + '&host_name=' + encodeURI(host.name); + if (host.has_active_failures === true) { host.badgeToolTip = 'Most recent job failed. Click to view jobs.'; host.active_failures = 'failed'; } - else if (host.has_active_failures == false && host.last_job == null) { - host.has_active_failures = 'none'; - host.badgeToolTip = "No job data available."; - host.active_failures = 'n/a'; + else if (host.has_active_failures === false && host.last_job === null) { + host.has_active_failures = 'none'; + host.badgeToolTip = "No job data available."; + host.active_failures = 'n/a'; } - else if (host.has_active_failures == false && host.last_job !== null) { - host.badgeToolTip = "Most recent job successful. Click to view jobs."; - host.active_failures = 'success'; + else if (host.has_active_failures === false && host.last_job !== null) { + host.badgeToolTip = "Most recent job successful. Click to view jobs."; + host.active_failures = 'success'; } - host.enabled_flag = host.enabled; + host.enabled_flag = host.enabled; SetEnabledMsg(host); - } - }]) + }; +}]) - .factory('SetStatus', ['SetEnabledMsg', 'Empty', function(SetEnabledMsg, Empty) { +.factory('SetStatus', ['SetEnabledMsg', 'Empty', function(SetEnabledMsg, Empty) { return function(params) { - var scope = params.scope; - var host = params.host; - var html, title; + var scope = params.scope, + host = params.host, + i, html, title; + + function ellipsis(a) { + if (a.length > 25) { + return a.substr(0,25) + '...'; + } + return a; + } function setMsg(host) { - if (host.has_active_failures == true || (host.has_active_failures == false && host.last_job !== null)) { + var j, job, jobs; + if (host.has_active_failures === true || (host.has_active_failures === false && host.last_job !== null)) { if (host.has_active_failures === true) { host.badgeToolTip = 'Most recent job failed. Click to view jobs.'; host.active_failures = 'failed'; @@ -71,28 +80,32 @@ angular.module('HostsHelper', [ 'RestServices', 'Utilities', 'ListGenerator', 'H } if (host.summary_fields.recent_jobs.length > 0) { // build html table of job status info - var jobs = host.summary_fields.recent_jobs.sort( + jobs = host.summary_fields.recent_jobs.sort( function(a,b) { // reverse numerical order - return -1 * (a - b); + return -1 * (a - b); }); title = "Recent Jobs"; - html = "\n"; + html = "
\n"; html += "\n"; html += "\n"; html += "\n"; html += "\n"; html += "\n"; html += "\n"; - for (var j=0; j < jobs.length; j++) { - var job = jobs[j]; - html += "\n"; - html += "\n"; - html += "\n"; - html += "\n"; - html += "\n"; + for (j=0; j < jobs.length; j++) { + job = jobs[j]; + html += "\n"; + html += "\n"; + html += "\n"; + html += "\n"; + html += "\n"; + html += "\n"; } html += "\n"; html += "
ID\n"; html += "Status\n"; + html += "ViewName\n"; html += "
" + job.id + "" + "language_features/roletest.yml(limit:all:cloud)therealsuperlonglaskaname
" + job.id + "Events " + + "Hosts" + ellipsis(job.name) + "
\n"; @@ -102,14 +115,14 @@ angular.module('HostsHelper', [ 'RestServices', 'Utilities', 'ListGenerator', 'H html = '

No recent job data available for this host.

'; } } - else if (host.has_active_failures == false && host.last_job == null) { + else if (host.has_active_failures === false && host.last_job === null) { host.has_active_failures = 'none'; host.badgeToolTip = "No job data available."; host.active_failures = 'n/a'; } - host.job_status_html = html; + host.job_status_html = html; host.job_status_title = title; - } + } if (!Empty(host)) { // update single host @@ -118,36 +131,35 @@ angular.module('HostsHelper', [ 'RestServices', 'Utilities', 'ListGenerator', 'H } else { // update all hosts - for (var i=0; i < scope.hosts.length; i++) { + for (i=0; i < scope.hosts.length; i++) { setMsg(scope.hosts[i]); SetEnabledMsg(scope.hosts[i]); } } - } - }]) + }; +}]) - .factory('HostsReload', [ '$routeParams', 'Empty', 'InventoryHosts', 'GetBasePath', 'SearchInit', 'PaginateInit', 'Wait', +.factory('HostsReload', [ '$routeParams', 'Empty', 'InventoryHosts', 'GetBasePath', 'SearchInit', 'PaginateInit', 'Wait', 'SetHostStatus', 'SetStatus', 'ApplyEllipsis', - function($routeParams, Empty, InventoryHosts, GetBasePath, SearchInit, PaginateInit, Wait, SetHostStatus, SetStatus, +function($routeParams, Empty, InventoryHosts, GetBasePath, SearchInit, PaginateInit, Wait, SetHostStatus, SetStatus, ApplyEllipsis) { return function(params) { - var scope = params.scope; - var group_id = params.group_id; - var tree_id = params.tree_id - var inventory_id = params.inventory_id; - - var list = InventoryHosts; - var url = ( !Empty(group_id) ) ? GetBasePath('groups') + group_id + '/all_hosts/' : - GetBasePath('inventory') + inventory_id + '/hosts/'; + var scope = params.scope, + group_id = params.group_id, + inventory_id = params.inventory_id, + list = InventoryHosts, + + url = ( !Empty(group_id) ) ? GetBasePath('groups') + group_id + '/all_hosts/' : + GetBasePath('inventory') + inventory_id + '/hosts/'; scope.search_place_holder='Search ' + scope.selected_group_name; if (scope.removePostRefresh) { scope.removePostRefresh(); } - scope.removePostRefresh = scope.$on('PostRefresh', function(e) { + scope.removePostRefresh = scope.$on('PostRefresh', function() { for (var i=0; i < scope.hosts.length; i++) { //Set tooltip for host enabled flag scope.hosts[i].enabled_flag = scope.hosts[i].enabled; @@ -157,16 +169,16 @@ angular.module('HostsHelper', [ 'RestServices', 'Utilities', 'ListGenerator', 'H setTimeout(function() { ApplyEllipsis('#hosts_table .host-name a'); }, 2500); Wait('stop'); scope.$emit('HostReloadComplete'); - }); + }); SearchInit({ scope: scope, set: 'hosts', list: list, url: url }); PaginateInit({ scope: scope, list: list, url: url }); - if ($routeParams['host_name']) { + if ($routeParams.host_name) { scope[list.iterator + 'InputDisable'] = false; - scope[list.iterator + 'SearchValue'] = $routeParams['host_name']; + scope[list.iterator + 'SearchValue'] = $routeParams.host_name; scope[list.iterator + 'SearchField'] = 'name'; - scope[list.iterator + 'SearchFieldLabel'] = list.fields['name'].label; + scope[list.iterator + 'SearchFieldLabel'] = list.fields.name.label; scope[list.iterator + 'SearchSelectValue'] = null; } @@ -174,48 +186,45 @@ angular.module('HostsHelper', [ 'RestServices', 'Utilities', 'ListGenerator', 'H scope[list.iterator + 'InputDisable'] = true; scope[list.iterator + 'SearchValue'] = 'true'; scope[list.iterator + 'SearchField'] = 'has_active_failures'; - scope[list.iterator + 'SearchFieldLabel'] = list.fields['has_active_failures'].label; + scope[list.iterator + 'SearchFieldLabel'] = list.fields.has_active_failures.label; scope[list.iterator + 'SearchSelectValue'] = { value: 1 }; } - scope.search(list.iterator); + }; +}]) - } - }]) - - .factory('InjectHosts', ['GenerateList', 'InventoryHosts', 'HostsReload', - function(GenerateList, InventoryHosts, HostsReload) { +.factory('InjectHosts', ['GenerateList', 'InventoryHosts', 'HostsReload', +function(GenerateList, InventoryHosts, HostsReload) { return function(params) { - var scope = params.scope; - var inventory_id = params.inventory_id; - var group_id = params.group_id; - var tree_id = params.tree_id; - + var scope = params.scope, + inventory_id = params.inventory_id, + group_id = params.group_id, + tree_id = params.tree_id, + generator = GenerateList; + // Inject the list html - var generator = GenerateList; generator.inject(InventoryHosts, { scope: scope, mode: 'edit', id: 'hosts-container', breadCrumbs: false, searchSize: 'col-lg-6 col-md-6 col-sm-6' }); // Load data HostsReload({ scope: scope, group_id: group_id, tree_id: tree_id, inventory_id: inventory_id }); - } - }]) + }; +}]) - .factory('ToggleHostEnabled', [ 'GetBasePath', 'Rest', 'Wait', 'ProcessErrors', 'Alert', 'Find', 'SetEnabledMsg', - function(GetBasePath, Rest, Wait, ProcessErrors, Alert, Find, SetEnabledMsg) { +.factory('ToggleHostEnabled', [ 'GetBasePath', 'Rest', 'Wait', 'ProcessErrors', 'Alert', 'Find', 'SetEnabledMsg', +function(GetBasePath, Rest, Wait, ProcessErrors, Alert, Find, SetEnabledMsg) { return function(params) { - var id = params.host_id; - var external_source = params.external_source; - var scope = params.scope - - var host; + var id = params.host_id, + external_source = params.external_source, + scope = params.scope, + host; function setMsg(host) { - host.enabled = (host.enabled) ? false : true; - host.enabled_flag = host.enabled; + host.enabled = (host.enabled) ? false : true; + host.enabled_flag = host.enabled; SetEnabledMsg(host); - } + } if (!external_source) { // Host is not managed by an external source @@ -225,50 +234,50 @@ angular.module('HostsHelper', [ 'RestServices', 'Utilities', 'ListGenerator', 'H Rest.setUrl(GetBasePath('hosts') + id + '/'); Rest.put(host) - .success( function(data, status, headers, config) { + .success( function() { Wait('stop'); - }) - .error( function(data, status, headers, config) { + }) + .error( function(data, status) { // Flip the enabled flag back - setMsg(host); + setMsg(host); Wait('stop'); ProcessErrors(scope, data, status, null, { hdr: 'Error!', msg: 'Failed to update host. PUT returned status: ' + status }); - }); + }); } else { Alert('Action Not Allowed', 'This host is part of a cloud inventory. It can only be disabled in the cloud.' + ' After disabling it, run an inventory sync to see the new status reflected here.', 'alert-info'); } - } - }]) + }; +}]) - .factory('HostsList', ['$rootScope', '$location', '$log', '$routeParams', 'Rest', 'Alert', 'HostList', 'GenerateList', - 'Prompt', 'SearchInit', 'PaginateInit', 'ProcessErrors', 'GetBasePath', 'HostsAdd', 'HostsReload', - 'SelectionInit', - function($rootScope, $location, $log, $routeParams, Rest, Alert, HostList, GenerateList, Prompt, SearchInit, - PaginateInit, ProcessErrors, GetBasePath, HostsAdd, HostsReload, SelectionInit) { +.factory('HostsList', ['$rootScope', '$location', '$log', '$routeParams', 'Rest', 'Alert', 'HostList', 'GenerateList', + 'Prompt', 'SearchInit', 'PaginateInit', 'ProcessErrors', 'GetBasePath', 'HostsAdd', 'HostsReload', 'SelectionInit', +function($rootScope, $location, $log, $routeParams, Rest, Alert, HostList, GenerateList, Prompt, SearchInit, + PaginateInit, ProcessErrors, GetBasePath, HostsAdd, HostsReload, SelectionInit) { return function(params) { - var inventory_id = params.inventory_id; - var group_id = params.group_id; - - var list = HostList; + var inventory_id = params.inventory_id, + group_id = params.group_id, + list = HostList, + generator = GenerateList, + defaultUrl, scope; list.iterator = 'subhost'; //Override the iterator and name so the scope of the modal dialog list.name = 'subhosts'; //will not conflict with the parent scope - var view = GenerateList; + - var scope = view.inject(list, { - id: 'form-modal-body', + scope = generator.inject(list, { + id: 'form-modal-body', mode: 'select', breadCrumbs: false, selectButton: false - }); + }); - var defaultUrl = GetBasePath('inventory') + inventory_id + '/hosts/?not__groups__id=' + scope.group_id; + defaultUrl = GetBasePath('inventory') + inventory_id + '/hosts/?not__groups__id=' + scope.group_id; scope.formModalActionLabel = 'Select'; scope.formModalHeader = 'Add Existing Hosts'; @@ -277,7 +286,7 @@ angular.module('HostsHelper', [ 'RestServices', 'Utilities', 'ListGenerator', 'H SelectionInit({ scope: scope, list: list, url: GetBasePath('groups') + group_id + '/hosts/' }); if (scope.removeModalClosed) { - scope.removeModalClosed(); + scope.removeModalClosed(); } scope.removeModalClosed = scope.$on('modalClosed', function() { // if the modal closed, assume something got changed and reload the host list @@ -293,33 +302,32 @@ angular.module('HostsHelper', [ 'RestServices', 'Utilities', 'ListGenerator', 'H scope.search(list.iterator); if (!scope.$$phase) { - scope.$digest(); + scope.$digest(); } scope.createHost = function() { $('#form-modal').modal('hide'); HostsAdd({ scope: params.scope, inventory_id: inventory_id, group_id: group_id }); - } + }; - } - }]) + }; +}]) - .factory('HostsCreate', ['$rootScope', '$location', '$log', '$routeParams', 'Rest', 'Alert', 'HostForm', 'GenerateForm', - 'Prompt', 'ProcessErrors', 'GetBasePath', 'HostsReload', 'ParseTypeChange', 'Wait', 'WatchInventoryWindowResize', - function($rootScope, $location, $log, $routeParams, Rest, Alert, HostForm, GenerateForm, Prompt, ProcessErrors, - GetBasePath, HostsReload, ParseTypeChange, Wait, WatchInventoryWindowResize) { +.factory('HostsCreate', ['$rootScope', '$location', '$log', '$routeParams', 'Rest', 'Alert', 'HostForm', 'GenerateForm', + 'Prompt', 'ProcessErrors', 'GetBasePath', 'HostsReload', 'ParseTypeChange', 'Wait', 'WatchInventoryWindowResize', +function($rootScope, $location, $log, $routeParams, Rest, Alert, HostForm, GenerateForm, Prompt, ProcessErrors, + GetBasePath, HostsReload, ParseTypeChange, Wait, WatchInventoryWindowResize) { return function(params) { - var parent_scope = params.scope; - var inventory_id = parent_scope.inventory_id; - var group_id = parent_scope.selected_group_id; - - // Inject dynamic view - var defaultUrl = GetBasePath('groups') + group_id + '/hosts/'; - var form = HostForm; - var generator = GenerateForm; - var scope = generator.inject(form, {mode: 'add', modal: true, related: false}); + var parent_scope = params.scope, + inventory_id = parent_scope.inventory_id, + group_id = parent_scope.selected_group_id, + defaultUrl = GetBasePath('groups') + group_id + '/hosts/', + form = HostForm, + generator = GenerateForm, + scope = generator.inject(form, {mode: 'add', modal: true, related: false}), + master={}; scope.formModalActionLabel = 'Save'; scope.formModalHeader = 'Create New Host'; @@ -328,7 +336,7 @@ angular.module('HostsHelper', [ 'RestServices', 'Utilities', 'ListGenerator', 'H ParseTypeChange(scope); if (scope.removeHostsReload) { - scope.removeHostsReload(); + scope.removeHostsReload(); } scope.removeHostsReload = scope.$on('hostsReload', function() { HostsReload(params); @@ -339,10 +347,10 @@ angular.module('HostsHelper', [ 'RestServices', 'Utilities', 'ListGenerator', 'H //$('#form-modal').on('hidden', function () { scope.$emit('hostsReload'); }); generator.reset(); - var master={}; + master={}; if (!scope.$$phase) { - scope.$digest(); + scope.$digest(); } if (scope.removeHostSaveComplete) { @@ -356,99 +364,98 @@ angular.module('HostsHelper', [ 'RestServices', 'Utilities', 'ListGenerator', 'H scope: parent_scope, group_id: parent_scope.selected_group_id, tree_id: parent_scope.selected_tree_id, - inventory_id: parent_scope.inventory_id }); + inventory_id: parent_scope.inventory_id + }); WatchInventoryWindowResize(); - }); + }); // Save scope.formModalAction = function() { + + Wait('start'); - Wait('start'); - - try { - scope.formModalActionDisabled = true; + try { + var fld, json_data, data={}; + scope.formModalActionDisabled = true; - // Make sure we have valid variable data - if (scope.parseType == 'json') { - var json_data = JSON.parse(scope.variables); //make sure JSON parses - } - else { - var json_data = jsyaml.load(scope.variables); //parse yaml - } + // Make sure we have valid variable data + if (scope.parseType === 'json') { + json_data = JSON.parse(scope.variables); //make sure JSON parses + } + else { + json_data = jsyaml.load(scope.variables); //parse yaml + } - // Make sure our JSON is actually an object - if (typeof json_data !== 'object') { - throw "failed to return an object!"; - } + // Make sure our JSON is actually an object + if (typeof json_data !== 'object') { + throw "failed to return an object!"; + } - var data = {} - for (var fld in form.fields) { - if (fld != 'variables') { - data[fld] = scope[fld]; - } - } + for (fld in form.fields) { + if (fld !== 'variables') { + data[fld] = scope[fld]; + } + } - data['inventory'] = inventory_id; - - if ($.isEmptyObject(json_data)) { - data['variables'] = ""; - } - else { - data['variables'] = JSON.stringify(json_data, undefined, '\t'); - } + data.inventory = inventory_id; + + if ($.isEmptyObject(json_data)) { + data.variables = ""; + } + else { + data.variables = JSON.stringify(json_data, undefined, '\t'); + } - Rest.setUrl(defaultUrl); - Rest.post(data) - .success( function(data, status, headers, config) { - scope.$emit('HostSaveComplete'); - }) - .error( function(data, status, headers, config) { - Wait('stop'); - scope.formModalActionDisabled = false; - ProcessErrors(scope, data, status, form, - { hdr: 'Error!', msg: 'Failed to add new host. POST returned status: ' + status }); - }); - } - catch(err) { - Wait('stop'); - scope.formModalActionDisabled = false; - Alert("Error", "Error parsing host variables. Parser returned: " + err); - } - } + Rest.setUrl(defaultUrl); + Rest.post(data) + .success( function() { + scope.$emit('HostSaveComplete'); + }) + .error( function(data, status) { + Wait('stop'); + scope.formModalActionDisabled = false; + ProcessErrors(scope, data, status, form, + { hdr: 'Error!', msg: 'Failed to add new host. POST returned status: ' + status }); + }); + } + catch(err) { + Wait('stop'); + scope.formModalActionDisabled = false; + Alert("Error", "Error parsing host variables. Parser returned: " + err); + } + }; // Cancel scope.formReset = function() { - // Defaults - generator.reset(); - }; + // Defaults + generator.reset(); + }; scope.cancelModal = function() { - WatchInventoryWindowResize(); - } + WatchInventoryWindowResize(); + }; - } - }]) + }; +}]) - .factory('HostsEdit', ['$rootScope', '$location', '$log', '$routeParams', 'Rest', 'Alert', 'HostForm', 'GenerateForm', - 'Prompt', 'ProcessErrors', 'GetBasePath', 'HostsReload', 'ParseTypeChange', 'Wait', 'Find', 'SetStatus', 'ApplyEllipsis', - 'WatchInventoryWindowResize', - function($rootScope, $location, $log, $routeParams, Rest, Alert, HostForm, GenerateForm, Prompt, ProcessErrors, - GetBasePath, HostsReload, ParseTypeChange, Wait, Find, SetStatus, ApplyEllipsis, WatchInventoryWindowResize) { +.factory('HostsEdit', ['$rootScope', '$location', '$log', '$routeParams', 'Rest', 'Alert', 'HostForm', 'GenerateForm', + 'Prompt', 'ProcessErrors', 'GetBasePath', 'HostsReload', 'ParseTypeChange', 'Wait', 'Find', 'SetStatus', 'ApplyEllipsis', + 'WatchInventoryWindowResize', +function($rootScope, $location, $log, $routeParams, Rest, Alert, HostForm, GenerateForm, Prompt, ProcessErrors, + GetBasePath, HostsReload, ParseTypeChange, Wait, Find, SetStatus, ApplyEllipsis, WatchInventoryWindowResize) { return function(params) { - var parent_scope = params.scope; - var host_id = params.host_id; - var inventory_id = params.inventory_id; - var group_id = params.group_id; - - var generator = GenerateForm; - var form = HostForm; - var defaultUrl = GetBasePath('hosts') + host_id + '/'; - var scope = generator.inject(form, { mode: 'edit', modal: true, related: false, show_modal: false }); - var master = {}; - var relatedSets = {}; + var parent_scope = params.scope, + host_id = params.host_id, + inventory_id = params.inventory_id, + generator = GenerateForm, + form = HostForm, + defaultUrl = GetBasePath('hosts') + host_id + '/', + scope = generator.inject(form, { mode: 'edit', modal: true, related: false, show_modal: false }), + master = {}, + relatedSets = {}; generator.reset(); scope.formModalActionLabel = 'Save'; @@ -457,64 +464,64 @@ angular.module('HostsHelper', [ 'RestServices', 'Utilities', 'ListGenerator', 'H scope.parseType = 'yaml'; ParseTypeChange(scope); - if (scope.hostLoadedRemove) { scope.hostLoadedRemove(); } scope.hostLoadedRemove = scope.$on('hostLoaded', function() { // Retrieve host variables if (scope.variable_url) { - Rest.setUrl(scope.variable_url); - Rest.get() - .success( function(data, status, headers, config) { - if ($.isEmptyObject(data)) { - scope.variables = "---"; - } - else { - scope.variables = jsyaml.safeDump(data); - } - Wait('stop'); - $('#form-modal').modal('show'); - }) - .error( function(data, status, headers, config) { - scope.variables = null; - ProcessErrors(scope, data, status, form, - { hdr: 'Error!', msg: 'Failed to retrieve host variables. GET returned status: ' + status }); - }); + Rest.setUrl(scope.variable_url); + Rest.get() + .success( function(data) { + if ($.isEmptyObject(data)) { + scope.variables = "---"; + } + else { + scope.variables = jsyaml.safeDump(data); + } + Wait('stop'); + $('#form-modal').modal('show'); + }) + .error( function(data, status) { + scope.variables = null; + ProcessErrors(scope, data, status, form, + { hdr: 'Error!', msg: 'Failed to retrieve host variables. GET returned status: ' + status }); + }); } else { - scope.variables = "---"; - Wait('stop'); - $('#form-modal').modal('show'); + scope.variables = "---"; + Wait('stop'); + $('#form-modal').modal('show'); } master.variables = scope.variables; - }); + }); Wait('start'); // Retrieve detail record and prepopulate the form - Rest.setUrl(defaultUrl); + Rest.setUrl(defaultUrl); Rest.get() - .success( function(data, status, headers, config) { - for (var fld in form.fields) { + .success( function(data) { + var set, fld, related; + for (fld in form.fields) { if (data[fld]) { - scope[fld] = data[fld]; - master[fld] = scope[fld]; + scope[fld] = data[fld]; + master[fld] = scope[fld]; } } - var related = data.related; - for (var set in form.related) { + related = data.related; + for (set in form.related) { 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.$emit('hostLoaded'); - }) - .error( function(data, status, headers, config) { + }) + .error( function(data, status) { ProcessErrors(scope, data, status, form, { hdr: 'Error!', msg: 'Failed to retrieve host: ' + host_id + '. GET returned status: ' + status }); - }); + }); if (scope.removeSaveCompleted) { @@ -522,8 +529,8 @@ angular.module('HostsHelper', [ 'RestServices', 'Utilities', 'ListGenerator', 'H } scope.removeSaveCompleted = scope.$on('saveCompleted', function() { // Update the name on the list - var host = Find({ list: parent_scope.hosts, key: 'id', val: host_id }); - var old_name = host.name; + var host = Find({ list: parent_scope.hosts, key: 'id', val: host_id }), + old_name = host.name; host.name = scope.name; host.enabled = scope.enabled; host.enabled_flag = scope.enabled; @@ -531,13 +538,13 @@ angular.module('HostsHelper', [ 'RestServices', 'Utilities', 'ListGenerator', 'H // Update any titles attributes created by ApplyEllipsis if (old_name) { - setTimeout(function() { + setTimeout(function() { $('#hosts_table .host-name a[title="' + old_name + '"').attr('title', host.name); ApplyEllipsis('#hosts_table .host-name a'); // Close modal Wait('stop'); $('#form-modal').modal('hide'); - }, 2000); + }, 2000); } else { // Close modal @@ -546,216 +553,133 @@ angular.module('HostsHelper', [ 'RestServices', 'Utilities', 'ListGenerator', 'H } // Restore ellipsis response to window resize WatchInventoryWindowResize(); - }); + }); // Save changes to the parent scope.formModalAction = function() { Wait('start'); - try { - + try { // Make sure we have valid variable data - if (scope.parseType == 'json') { - var json_data = JSON.parse(scope.variables); //make sure JSON parses + var fld, json_data, data={}; + if (scope.parseType === 'json') { + json_data = JSON.parse(scope.variables); //make sure JSON parses } else { - var json_data = jsyaml.load(scope.variables); //parse yaml + json_data = jsyaml.load(scope.variables); //parse yaml } // Make sure our JSON is actually an object if (typeof json_data !== 'object') { - throw "failed to return an object!"; + throw "failed to return an object!"; } - var data = {} - for (var fld in form.fields) { - data[fld] = scope[fld]; + for (fld in form.fields) { + data[fld] = scope[fld]; } - data['inventory'] = inventory_id; + data.inventory = inventory_id; if ($.isEmptyObject(json_data)) { - data['variables'] = ""; + data.variables = ""; } else { - data['variables'] = JSON.stringify(json_data, undefined, '\t'); + data.variables = JSON.stringify(json_data, undefined, '\t'); } Rest.setUrl(defaultUrl); Rest.put(data) - .success( function(data, status, headers, config) { + .success( function() { scope.$emit('saveCompleted'); - }) - .error( function(data, status, headers, config) { + }) + .error( function(data, status) { Wait('stop'); ProcessErrors(scope, data, status, form, { hdr: 'Error!', msg: 'Failed to update host: ' + host_id + '. PUT returned status: ' + status }); - }); + }); } catch(err) { - Wait('stop'); - Alert("Error", "Error parsing host variables. Parser returned: " + err); + Wait('stop'); + Alert("Error", "Error parsing host variables. Parser returned: " + err); } - }; + }; // Cancel scope.formReset = function() { - generator.reset(); - for (var fld in master) { - scope[fld] = master[fld]; - } - scope.parseType = 'yaml'; - } + generator.reset(); + for (var fld in master) { + scope[fld] = master[fld]; + } + scope.parseType = 'yaml'; + }; scope.cancelModal = function() { - WatchInventoryWindowResize(); - } + WatchInventoryWindowResize(); + }; - } - }]) + }; +}]) - .factory('HostsDelete', ['$rootScope', '$location', '$log', '$routeParams', 'Rest', 'Alert', 'Prompt', 'ProcessErrors', 'GetBasePath', - 'HostsReload', 'Wait', 'Find', - function($rootScope, $location, $log, $routeParams, Rest, Alert, Prompt, ProcessErrors, GetBasePath, HostsReload, Wait, Find) { +.factory('HostsDelete', ['$rootScope', '$location', '$log', '$routeParams', 'Rest', 'Alert', 'Prompt', 'ProcessErrors', 'GetBasePath', + 'HostsReload', 'Wait', 'Find', +function($rootScope, $location, $log, $routeParams, Rest, Alert, Prompt, ProcessErrors, GetBasePath, HostsReload, Wait, Find) { return function(params) { // Remove the selected host from the current group by disassociating - var scope = params.scope; - var host_id = params.host_id; - var host_name = params.host_name; - - var url = (scope.selected_group_id == null) ? GetBasePath('inventory') + scope.inventory_id + '/hosts/' : - GetBasePath('groups') + scope.selected_group_id + '/hosts/'; - - var group = (scope.selected_tree_id) ? Find({ list: scope.groups, key: 'id', val: scope.selected_tree_id }) : null; + var action_to_take, body, + scope = params.scope, + host_id = params.host_id, + host_name = params.host_name, + + url = (scope.selected_group_id === null) ? GetBasePath('inventory') + scope.inventory_id + '/hosts/' : + GetBasePath('groups') + scope.selected_group_id + '/hosts/', + + group = (scope.selected_tree_id) ? Find({ list: scope.groups, key: 'id', val: scope.selected_tree_id }) : null; if (scope.removeHostsReload) { - scope.removeHostsReload(); + scope.removeHostsReload(); } scope.removeHostsReload = scope.$on('hostsReload', function() { scope.showHosts(scope.selected_tree_id, scope.selected_group_id, false); - }); + }); - var action_to_take = function() { + action_to_take = function() { $('#prompt-modal').on('hidden.bs.modal', function(){ Wait('start'); }); $('#prompt-modal').modal('hide'); Rest.setUrl(url); Rest.post({ id: host_id, disassociate: 1 }) - .success( function(data, status, headers, config) { - scope.$emit('hostsReload'); - }) - .error( function(data, status, headers, config) { + .success( function() { + scope.$emit('hostsReload'); + }) + .error( function(data, status) { Wait('stop'); ProcessErrors(scope, data, status, null, { hdr: 'Error!', msg: 'Attempt to delete ' + host_name + ' failed. POST returned status: ' + status }); - }); - } + }); + }; - var body = (group) ? '

Are you sure you want to delete host ' + host_name + ' from group ' + group.name + '?

' : + body = (group) ? '

Are you sure you want to delete host ' + host_name + ' from group ' + group.name + '?

' : '

Are you sure you want to delete host ' + host_name + '?

'; Prompt({ hdr: 'Delete Host', body: body, action: action_to_take, 'class': 'btn-danger' }); - } - }]) + }; +}]) -/* - .factory('HostsReload', ['$location', '$routeParams', 'SearchInit', 'PaginateInit', 'InventoryHostsForm', 'GetBasePath', 'Wait', - 'SetHostStatus', - function($location, $routeParams, SearchInit, PaginateInit, InventoryHostsForm, GetBasePath, Wait, SetHostStatus) { - return function(params) { - // Rerfresh the Hosts view on right side of page - - var scope = params.scope; - var group_id = scope.group_id; - var postAction = params.action; - - scope['hosts'] = null; - scope['toggleAllFlag'] = false; - scope['hostDeleteHide'] = true; - - var url = (group_id !== null && group_id !== undefined) ? GetBasePath('groups') + group_id + '/all_hosts/' : - GetBasePath('inventory') + params.inventory_id + '/hosts/'; - - // Set the groups value in each element of hosts array - if (scope.removePostRefresh) { - scope.removePostRefresh(); - } - scope.removePostRefresh = scope.$on('PostRefresh', function() { - - // Add a list of groups to each host - var groups, descr, found, list; - for (var i=0; i < scope.hosts.length; i++) { - groups = scope.hosts[i].summary_fields.groups; - scope.hosts[i].groups = ''; - for (var k=0; k < groups.length; k++) { - if (!groups[k].name.match(/^_deleted/)) { - scope.hosts[i].groups += groups[k].name + ', ' - } - } - scope.hosts[i].groups = scope.hosts[i].groups.replace(/\, $/,''); - } - - for (var i=0; i < scope.hosts.length; i++) { - SetHostStatus(scope.hosts[i]); - } - - if (group_id == null || group_id == undefined) { - if (!scope.hosts.length) { - scope.$emit('ShowHelp'); - } - } - - if (postAction) { - postAction(); - } - - }); - - SearchInit({ scope: scope, set: 'hosts', list: InventoryHostsForm, url: url }); - PaginateInit({ scope: scope, list: InventoryHostsForm, url: url }); - - if ($routeParams['has_active_failures']) { - //scope.resetSearch(InventoryHostsForm.iterator); - scope[InventoryHostsForm.iterator + 'InputDisable'] = true; - scope[InventoryHostsForm.iterator + 'SearchValue'] = $routeParams['has_active_failures']; - scope[InventoryHostsForm.iterator + 'SearchField'] = 'has_active_failures'; - scope[InventoryHostsForm.iterator + 'SearchFieldLabel'] = InventoryHostsForm.fields['has_active_failures'].label; - scope[InventoryHostsForm.iterator + 'SearchSelectValue'] = ($routeParams['has_active_failures'] == 'true') ? { value: 1 } : { value: 0 }; - - } - else if ($routeParams['name']) { - scope[InventoryHostsForm.iterator + 'InputDisable'] = false; - scope[InventoryHostsForm.iterator + 'SearchValue'] = $routeParams['name']; - scope[InventoryHostsForm.iterator + 'SearchField'] = 'name'; - scope[InventoryHostsForm.iterator + 'SearchFieldLabel'] = InventoryHostsForm.fields['name'].label; - scope[InventoryHostsForm.iterator + 'SearchSelectValue'] = null; - } - - scope.search(InventoryHostsForm.iterator); - - if (!params.scope.$$phase) { - params.scope.$digest(); - } - } - }]) - -*/ - - .factory('EditHostGroups', ['$rootScope', '$location', '$log', '$routeParams', 'Rest', 'Alert', 'GenerateForm', - 'Prompt', 'ProcessErrors', 'GetBasePath', 'HostsReload', 'ParseTypeChange', 'Wait', - function($rootScope, $location, $log, $routeParams, Rest, Alert, GenerateForm, Prompt, ProcessErrors, - GetBasePath, HostsReload, ParseTypeChange, Wait) { +.factory('EditHostGroups', ['$rootScope', '$location', '$log', '$routeParams', 'Rest', 'Alert', 'GenerateForm', 'Prompt', + 'ProcessErrors', 'GetBasePath', 'HostsReload', 'ParseTypeChange', 'Wait', +function($rootScope, $location, $log, $routeParams, Rest, Alert, GenerateForm, Prompt, ProcessErrors, GetBasePath, HostsReload, + ParseTypeChange, Wait) { return function(params) { - var host_id = params.host_id; - var inventory_id = params.inventory_id; - var generator = GenerateForm; - var actions = []; + var host_id = params.host_id, + inventory_id = params.inventory_id, + generator = GenerateForm, + actions = [], + i, html, defaultUrl, scope, postAction; - //var html="

{{ host.name }}

\n"; - var html = "
\n"; + html = "
\n"; html += "
\n"; html += "\n"; html += "