Skip to content

Commit

Permalink
Merge pull request #177 from ox-it/no-duplicate-suggestions-ids
Browse files Browse the repository at this point in the history
No duplicate suggestions (still using ids)
  • Loading branch information
martinfilliau committed Jan 6, 2015
2 parents f08a428 + 3cc6d01 commit 5d8f93e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
3 changes: 2 additions & 1 deletion static/js/event-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ $(function() {
}

function updateEventDepartment(location_id) {
if(!location_id) { return; }
$.ajax({
type:'GET',
url: 'http://api.m.ox.ac.uk/places/' + location_id,
Expand All @@ -117,7 +118,7 @@ $(function() {
//User has probably selected the 'Please select' option
return;
}
var url = '/api/eventgroups/id/' + groupID
var url = '/api/series/id/' + groupID

//retrieve the ID of the event organiser and apply that to the department field of the form
$.ajax({
Expand Down
38 changes: 35 additions & 3 deletions static/js/form-controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,16 @@ $(function() {
}
function removeItem(e) {
e.preventDefault();
$(this).parents('.list-group-item').prev('input').remove();
var input = $(this).parents('.list-group-item').prev('input')
var removed_data_id = input.val();
var mainInput = input.siblings(".tt-input");
input.remove();
$(this).parents('.list-group-item').remove();

//remove from the local list
var currentValues = mainInput.data('currentValues');
var index = currentValues.indexOf(removed_data_id);
currentValues.splice(index, 1);
}
function showSelectedValue(mainInput, input, suggestion) {
var valueKey = $(mainInput).data('valueKey');
Expand All @@ -32,6 +40,10 @@ $(function() {
if(!$(mainInput).prop('multiple')) {
$(mainInput).hide();
}
//add the value to the local list
var currentValues = $(mainInput).data('currentValues');
var value = String(suggestion[valueKey]);
currentValues.push(value);
}
function showSelectedValuesForInput() {
var $this = $(this);
Expand Down Expand Up @@ -81,6 +93,15 @@ $(function() {
attr('value', value);
}

var filter = function(suggestions, currentValues, valueKey) {
//Filter the given suggestions, removing any already in the element's list of chosen values
var filtered = $.grep(suggestions, function(suggestion) {
return $.inArray(String(suggestion[valueKey]), currentValues) === -1;
});
return filtered;
}


//Select and initialise all typeaheads in the original document. Note that multiple new '.typeahead' elements are created.
//Those from the original document lack the tt-hint or tt-input classes, so exclude these from the selection
var typeaheads = $('.typeahead:not(.tt-hint):not(.tt-input)');
Expand All @@ -102,6 +123,10 @@ $(function() {
return;
}

//Keep track of current selected values, to enable filtering them out of suggestions
var currentValues = [];
$this.data('currentValues', currentValues);

var prefetchUrl = $this.data('prefetch-url');
var bloodhound = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace(sourceConfig.displayKey),
Expand All @@ -113,17 +138,24 @@ $(function() {
remote: {
url: sourceConfig.url,
filter: sourceConfig.responseExpression && eval("(function(response) { return " + sourceConfig.responseExpression + ";})")
}
},
});
var name = $this.attr('name');

$this.data('name', name);
var value_key = sourceConfig.valueKey;
$this.data('valueKey', sourceConfig.valueKey);
$this.data('labelKey', sourceConfig.displayKey);
$this.data('bloodhound', bloodhound);
$this.removeAttr('name');
dataSource = {
displayKey: sourceConfig.displayKey,
source: bloodhound.ttAdapter(),
//source: bloodhound.ttAdapter(),
source: function(query, cb) {
bloodhound.get(query, function (suggestions) {
cb(filter(suggestions, currentValues, value_key));
});
},
templates: {
empty: ' Nothing matches your query '
}
Expand Down

0 comments on commit 5d8f93e

Please sign in to comment.