promise-ify createselect2 and use that instead for deferring launch button enabling

This commit is contained in:
John Mitchell 2019-02-20 17:08:24 -05:00
parent 2e4eb1885f
commit 635aa9fd56
No known key found for this signature in database
GPG Key ID: FE6A9B5BD4EB5C94
3 changed files with 56 additions and 82 deletions

View File

@ -579,8 +579,8 @@ angular.module('Utilities', ['RestServices', 'Utilities'])
* ]
* ```
*/
.factory('CreateSelect2', ['$filter',
function($filter) {
.factory('CreateSelect2', ['$filter', '$q',
function($filter, $q) {
return function(params) {
var element = params.element,
@ -591,10 +591,10 @@ angular.module('Utilities', ['RestServices', 'Utilities'])
addNew = params.addNew,
scope = params.scope,
selectOptions = params.options,
callback = params.callback,
model = params.model,
original_options,
minimumResultsForSearch = params.minimumResultsForSearch ? params.minimumResultsForSearch : Infinity;
minimumResultsForSearch = params.minimumResultsForSearch ? params.minimumResultsForSearch : Infinity,
defer = $q.defer();
if (scope && selectOptions) {
original_options = _.get(scope, selectOptions);
@ -704,12 +704,10 @@ angular.module('Utilities', ['RestServices', 'Utilities'])
$(element).trigger('change');
}
if (callback) {
scope.$emit(callback);
}
defer.resolve("select2 loaded");
});
return defer.promise;
};
}
])

View File

@ -46,7 +46,9 @@ export default
callback,
choicesCount = 0,
select2Count = 0,
instance_group_url = defaultUrl + id + '/instance_groups';
instance_group_url = defaultUrl + id + '/instance_groups',
select2LoadDefer = [],
launchHasBeenEnabled = false;
init();
function init() {
@ -169,86 +171,71 @@ export default
};
function sync_playbook_select2() {
CreateSelect2({
select2LoadDefer.push(CreateSelect2({
element:'#playbook-select',
multiple: false
});
}));
}
function sync_verbosity_select2() {
CreateSelect2({
select2LoadDefer.push(CreateSelect2({
element:'#job_template_verbosity',
multiple: false
});
}));
}
function jobTemplateLoadFinished(){
CreateSelect2({
select2LoadDefer.push(CreateSelect2({
element:'#job_template_job_type',
multiple: false,
callback: 'select2Loaded',
scope: $scope
});
multiple: false
}));
CreateSelect2({
select2LoadDefer.push(CreateSelect2({
element:'#playbook-select',
multiple: false,
callback: 'select2Loaded',
scope: $scope
});
multiple: false
}));
CreateSelect2({
select2LoadDefer.push(CreateSelect2({
element:'#job_template_job_tags',
multiple: true,
addNew: true,
callback: 'select2Loaded',
scope: $scope
});
addNew: true
}));
CreateSelect2({
select2LoadDefer.push(CreateSelect2({
element:'#job_template_skip_tags',
multiple: true,
addNew: true,
callback: 'select2Loaded',
scope: $scope
});
addNew: true
}));
CreateSelect2({
select2LoadDefer.push(CreateSelect2({
element: '#job_template_custom_virtualenv',
multiple: false,
opts: $scope.custom_virtualenvs_options,
callback: 'select2Loaded',
scope: $scope
});
}
opts: $scope.custom_virtualenvs_options
}));
$scope.$on('select2Loaded', () => {
select2Count++;
if (select2Count === 10) {
$scope.$emit('select2LoadFinished');
if (!launchHasBeenEnabled) {
$q.all(select2LoadDefer).then(() => {
// updates based on lookups will initially set the form as dirty.
// we need to set it as pristine when it contains the values given by the api
// so that we can enable launching when the two are the same
$scope.job_template_form.$setPristine();
// this is used to set the overall form as dirty for the values
// that don't actually set this internally (lookups, toggles and code mirrors).
$scope.$watchGroup([
'inventory',
'project',
'multiCredential.selectedCredentials',
'extra_vars',
'diff_mode',
'instance_groups'
], (val, prevVal) => {
if (!_.isEqual(val, prevVal)) {
$scope.job_template_form.$setDirty();
}
});
});
}
});
$scope.$on('select2LoadFinished', () => {
// updates based on lookups will initially set the form as dirty.
// we need to set it as pristine when it contains the values given by the api
// so that we can enable launching when the two are the same
$scope.job_template_form.$setPristine();
// this is used to set the overall form as dirty for the values
// that don't actually set this internally (lookups, toggles and code mirrors).
$scope.$watchGroup([
'inventory',
'project',
'multiCredential.selectedCredentials',
'extra_vars',
'diff_mode',
'instance_groups'
], (val, prevVal) => {
if (!_.isEqual(val, prevVal)) {
$scope.job_template_form.$setDirty();
}
});
});
}
$scope.toggleForm = function(key) {
$scope[key] = !$scope[key];
@ -298,8 +285,8 @@ export default
variable: 'extra_vars',
onChange: callback
});
jobTemplateLoadFinished();
launchHasBeenEnabled = true;
});
Wait('start');
@ -497,12 +484,12 @@ export default
.map(i => ({id: i.id + "",
test: i.name}));
CreateSelect2({
select2LoadDefer.push(CreateSelect2({
element:'#job_template_labels',
multiple: true,
addNew: true,
opts: opts
});
}));
$scope.$emit("choicesReady");

View File

@ -257,22 +257,11 @@ export default [
// Select2-ify the lables input
CreateSelect2({
scope: $scope,
element:'#workflow_job_template_labels',
multiple: true,
addNew: true,
opts,
callback: 'select2Loaded'
});
$scope.$on('select2Loaded', () => {
select2Count++;
if (select2Count === 1) {
$scope.$emit('select2LoadFinished');
}
});
$scope.$on('select2LoadFinished', () => {
opts
}).then(() => {
// updates based on lookups will initially set the form as dirty.
// we need to set it as pristine when it contains the values given by the api
// so that we can enable launching when the two are the same