Skip to content

Commit

Permalink
Update autocomplete code for package branch modal
Browse files Browse the repository at this point in the history
* Make use of the autocomplete helpers
* Update the 'source' location of the package autocomplete
  whenever the project autocomplete changes
  • Loading branch information
bgeuken committed Jan 7, 2019
1 parent 77c0b32 commit d1b9790
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 46 deletions.
46 changes: 11 additions & 35 deletions src/api/app/assets/javascripts/webui2/autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,40 +51,16 @@ $(document).ready(function() {
}
});
});
});

function autocomleteBranchPackageName(linkedPackageSource) { // jshint ignore:line
$('#linked_package').autocomplete({
appendTo: '#original-package-name',
source: function(request, response) {
$.ajax({
url: linkedPackageSource,
data: {
project: $('#linked_project').val(),
term: request.term,
},
success: function(data) {
response($.map(data, function(item) { return { label: item, value: item }; }));
},
});
},
search: function(event, ui) { // jshint ignore:line
autocompleteSpinner('search-icon-package', true);
},
response: function(event, ui) { // jshint ignore:line
autocompleteSpinner('search-icon-package', false);
},
minLength: 2
});
}
$('#linked_project').on('autocompletechange', function() {
var projectName = $(this).val(),
source = $('#linked_package').autocomplete('option', 'source');

if (!projectName) return;

function autocompleteSpinner(spinnerId, searching) {
var icon = $('#' + spinnerId + ' i:first-child');
if (searching) {
icon.addClass('d-none');
icon.next().removeClass('d-none');
} else {
icon.removeClass('d-none');
icon.next().addClass('d-none');
}
}
// Ensure old parameters got removed
source = source.replace(/\?.+/, '') + '?project=' + projectName;
// Update the source target of the package autocomplete
$('#linked_package').autocomplete('option', { source: source });
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,10 @@
= form_tag(package_branch_path) do
= render partial: 'webui/autocomplete', locals: { html_id: 'linked_project', label: 'Original project name',
source: autocomplete_projects_path }
.form-group#original-package-name
%label{ for: 'linked_package' } Orginal package name
.input-group
.input-group-prepend
%span.input-group-text#search-icon-package
%i.fa.fa-search
%i.fas.fa-spinner.fa-spin.d-none
= text_field_tag 'linked_package', nil, size: 80, class: 'form-control', placeholder: 'Type to autocomplete', required: true
= render partial: 'webui/autocomplete', locals: { html_id: 'linked_package', label: 'Original package name',
source: autocomplete_packages_path }
= hidden_field_tag 'target_project', project
= render partial: 'shared/package_branch_form', locals: { show_project_field: false, target_project: project,
package: nil, revision: nil }
.modal-footer
= render partial: 'webui2/shared/dialog_action_buttons'

:javascript
autocomleteBranchPackageName('#{autocomplete_packages_path}');

0 comments on commit d1b9790

Please sign in to comment.