From 155ef47b32e5ed7ce510a7997b018f98146a95aa Mon Sep 17 00:00:00 2001 From: gconsidine Date: Thu, 3 Aug 2017 17:49:17 -0400 Subject: [PATCH 1/2] Greg's Commit which fixed the issue with saving adhoc modules --- .../configuration/configuration.controller.js | 4 +- .../configuration-jobs.controller.js | 75 +++++++++++-------- 2 files changed, 45 insertions(+), 34 deletions(-) diff --git a/awx/ui/client/src/configuration/configuration.controller.js b/awx/ui/client/src/configuration/configuration.controller.js index a915601cba..51bc404fdd 100644 --- a/awx/ui/client/src/configuration/configuration.controller.js +++ b/awx/ui/client/src/configuration/configuration.controller.js @@ -86,7 +86,7 @@ export default [ // does a string.split(', ') w/ an extra space // behind the comma. if(key === "AD_HOC_COMMANDS"){ - $scope[key] = data[key].toString(); + $scope[key] = data[key]; } else if (key === "AUTH_LDAP_USER_SEARCH" || key === "AUTH_LDAP_GROUP_SEARCH") { $scope[key] = JSON.stringify(data[key]); } else { @@ -382,7 +382,7 @@ export default [ } else if($scope[key][0] && $scope[key][0].value !== undefined) { if(multiselectDropdowns.indexOf(key) !== -1) { // Handle AD_HOC_COMMANDS - payload[key] = ConfigurationUtils.listToArray(_.map($scope[key], 'value').join(',')); + payload[key] = $scope[`${key}_values`]; } else { payload[key] = _.map($scope[key], 'value').join(','); } diff --git a/awx/ui/client/src/configuration/jobs-form/configuration-jobs.controller.js b/awx/ui/client/src/configuration/jobs-form/configuration-jobs.controller.js index b70e3d71f9..df6f8f28af 100644 --- a/awx/ui/client/src/configuration/jobs-form/configuration-jobs.controller.js +++ b/awx/ui/client/src/configuration/jobs-form/configuration-jobs.controller.js @@ -79,9 +79,6 @@ export default [ noPanel: true }); - // Flag to avoid re-rendering and breaking Select2 dropdowns on tab switching - var dropdownRendered = false; - function initializeCodeInput () { let name = 'AWX_TASK_ENV'; @@ -95,35 +92,33 @@ export default [ $scope.parseTypeChange('parseType', name); } - function populateAdhocCommand(flag){ - $scope.$parent.AD_HOC_COMMANDS = $scope.$parent.AD_HOC_COMMANDS.toString(); - var ad_hoc_commands = $scope.$parent.AD_HOC_COMMANDS.split(','); - $scope.$parent.AD_HOC_COMMANDS = _.map(ad_hoc_commands, function(item){ - let option = _.find($scope.$parent.AD_HOC_COMMANDS_options, { value: item }); - if(!option){ - option = { - name: item, - value: item, - label: item - }; - $scope.$parent.AD_HOC_COMMANDS_options.push(option); - } - return option; + function loadAdHocCommands () { + $scope.$parent.AD_HOC_COMMANDS_values = $scope.$parent.AD_HOC_COMMANDS.map(value => value); + $scope.$parent.AD_HOC_COMMANDS = $scope.$parent.AD_HOC_COMMANDS.map(value => ({ + value, + name: value, + label: value + })); + + $scope.$parent.AD_HOC_COMMANDS_options = $scope.$parent.AD_HOC_COMMANDS.map(tag => tag); + + CreateSelect2({ + element: '#configuration_jobs_template_AD_HOC_COMMANDS', + multiple: true, + addNew: true, + placeholder: i18n._('Select commands') }); + } - if(flag !== undefined){ - dropdownRendered = flag; - } + function revertAdHocCommands (defaults) { + $scope.$parent.AD_HOC_COMMANDS = defaults.map(value => ({ + value, + name: value, + label: value + })); - if(!dropdownRendered) { - dropdownRendered = true; - CreateSelect2({ - element: '#configuration_jobs_template_AD_HOC_COMMANDS', - multiple: true, - addNew: true, - placeholder: i18n._('Select commands') - }); - } + $scope.$parent.AD_HOC_COMMANDS_options = $scope.$parent.AD_HOC_COMMANDS.map(tag => tag); + $scope.$parent.AD_HOC_COMMANDS_values = $scope.$parent.AD_HOC_COMMANDS.map(tag => tag.value); } // Fix for bug where adding selected opts causes form to be $dirty and triggering modal @@ -132,10 +127,26 @@ export default [ $scope.$parent.configuration_jobs_template_form.$setPristine(); }, 1000); - $scope.$on('AD_HOC_COMMANDS_populated', function(e, data, flag) { - populateAdhocCommand(flag); + + // Managing the state of select2's tags since the behavior is unpredictable otherwise. + let commandsElement = $('#configuration_jobs_template_AD_HOC_COMMANDS'); + + commandsElement.on('select2:select', event => { + let command = event.params.data.text; + let commands = $scope.$parent.AD_HOC_COMMANDS_values; + + commands.push(command); }); + commandsElement.on('select2:unselect', event => { + let command = event.params.data.text; + let commands = $scope.$parent.AD_HOC_COMMANDS_values; + + $scope.$parent.AD_HOC_COMMANDS_values = commands.filter(value => value !== command); + }); + + $scope.$on('AD_HOC_COMMANDS_reverted', (e, defaults) => revertAdHocCommands(defaults)); + /* * Controllers for each tab are initialized when configuration is opened. A listener * on the URL itself is necessary to determine which tab is active. If a non-active @@ -174,7 +185,7 @@ export default [ codeInputInitialized = true; } - populateAdhocCommand(false); + loadAdHocCommands(); }); } ]; From bf2aabda66c83843126286e1bc8737ab7491228a Mon Sep 17 00:00:00 2001 From: Jared Tabor Date: Thu, 3 Aug 2017 16:43:39 -0700 Subject: [PATCH 2/2] My fix for reverting adhoc modules --- .../configuration/configuration.controller.js | 8 +++++- .../configuration-jobs.controller.js | 28 +++++++++++++++---- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/awx/ui/client/src/configuration/configuration.controller.js b/awx/ui/client/src/configuration/configuration.controller.js index 51bc404fdd..7d35be84e3 100644 --- a/awx/ui/client/src/configuration/configuration.controller.js +++ b/awx/ui/client/src/configuration/configuration.controller.js @@ -321,6 +321,9 @@ export default [ // We need to re-instantiate the Select2 element // after resetting the value. Example: $scope.$broadcast(key+'_populated', null, false); + if(key === "AD_HOC_COMMANDS"){ + $scope.$broadcast(key+'_reverted', null, false); + } } else if($scope[key + '_field'].reset === "CUSTOM_LOGO"){ $scope.$broadcast(key+'_reverted'); @@ -379,7 +382,7 @@ export default [ //Parse dropdowns and dropdowns labeled as lists if($scope[key] === null) { payload[key] = null; - } else if($scope[key][0] && $scope[key][0].value !== undefined) { + } else if(!_.isEmpty($scope[`${key}_values`])) { if(multiselectDropdowns.indexOf(key) !== -1) { // Handle AD_HOC_COMMANDS payload[key] = $scope[`${key}_values`]; @@ -501,6 +504,9 @@ export default [ // We need to re-instantiate the Select2 element // after resetting the value. Example: $scope.$broadcast(key+'_populated', null, false); + if(key === "AD_HOC_COMMANDS"){ + $scope.$broadcast(key+'_reverted', null, false); + } } else if($scope[key + '_field'].reset === "CUSTOM_LOGO"){ $scope.$broadcast(key+'_reverted'); diff --git a/awx/ui/client/src/configuration/jobs-form/configuration-jobs.controller.js b/awx/ui/client/src/configuration/jobs-form/configuration-jobs.controller.js index df6f8f28af..7325c8fd42 100644 --- a/awx/ui/client/src/configuration/jobs-form/configuration-jobs.controller.js +++ b/awx/ui/client/src/configuration/jobs-form/configuration-jobs.controller.js @@ -108,17 +108,33 @@ export default [ addNew: true, placeholder: i18n._('Select commands') }); + } - function revertAdHocCommands (defaults) { - $scope.$parent.AD_HOC_COMMANDS = defaults.map(value => ({ + function revertAdHocCommands () { + $scope.$parent.AD_HOC_COMMANDS = $scope.$parent.configDataResolve.AD_HOC_COMMANDS.default.map(value => ({ value, name: value, label: value })); + $('.select2-selection__choice').each(function(i, element){ + if(!_.contains($scope.$parent.configDataResolve.AD_HOC_COMMANDS.default, element.title)){ + $(`#configuration_jobs_template_AD_HOC_COMMANDS option[value='${element.title}']`).remove(); + element.remove(); + } + }); + $scope.$parent.AD_HOC_COMMANDS_options = $scope.$parent.AD_HOC_COMMANDS.map(tag => tag); $scope.$parent.AD_HOC_COMMANDS_values = $scope.$parent.AD_HOC_COMMANDS.map(tag => tag.value); + CreateSelect2({ + element: '#configuration_jobs_template_AD_HOC_COMMANDS', + multiple: true, + addNew: true, + placeholder: i18n._('Select commands'), + options: $scope.$parent.AD_HOC_COMMANDS_options + }); + } // Fix for bug where adding selected opts causes form to be $dirty and triggering modal @@ -131,21 +147,21 @@ export default [ // Managing the state of select2's tags since the behavior is unpredictable otherwise. let commandsElement = $('#configuration_jobs_template_AD_HOC_COMMANDS'); - commandsElement.on('select2:select', event => { + commandsElement.on('select2:select', event => { let command = event.params.data.text; let commands = $scope.$parent.AD_HOC_COMMANDS_values; commands.push(command); }); - commandsElement.on('select2:unselect', event => { + commandsElement.on('select2:unselect', event => { let command = event.params.data.text; let commands = $scope.$parent.AD_HOC_COMMANDS_values; $scope.$parent.AD_HOC_COMMANDS_values = commands.filter(value => value !== command); }); - $scope.$on('AD_HOC_COMMANDS_reverted', (e, defaults) => revertAdHocCommands(defaults)); + $scope.$on('AD_HOC_COMMANDS_reverted', () => revertAdHocCommands()); /* * Controllers for each tab are initialized when configuration is opened. A listener @@ -174,7 +190,7 @@ export default [ /* * This event is fired if the user navigates directly to this tab, where the - * $locationChangeStart does not. Watching this and location ensure proper display on + * $locationChangeStart does not. Watching this and location ensure proper display on * direct load of this tab or if the user comes from a different tab. */ $scope.$on('populated', () => {