Skip to content

Commit 9c55c52

Browse files
committed
Allow autocomplete results to be grouped.
Show past events in event linking fields. Fixes #737
1 parent 1edbade commit 9c55c52

7 files changed

Lines changed: 59 additions & 11 deletions

File tree

app/assets/javascripts/autocompleters.js

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,36 @@
11
var Autocompleters = {
2+
formatResultWithHint: function (suggestion, currentValue) {
3+
var result = $.Autocomplete.formatResult(suggestion, currentValue);
4+
5+
if (suggestion.data && suggestion.data.hint) {
6+
result += '<span class="autocomplete-hint">' + suggestion.data.hint + '</span>';
7+
}
8+
9+
return result;
10+
},
211
transformFunctions: {
312
default: function (response, config) {
413
return {
514
suggestions: $.map(response, function(item) {
6-
return { value: item[config.labelField], data: item[config.idField], item: item };
15+
return { value: item[config.labelField], data: { id: item[config.idField], item: item } };
16+
})
17+
};
18+
},
19+
events: function (response, config) {
20+
var today = new Date();
21+
return {
22+
suggestions: $.map(response, function(item) {
23+
var group;
24+
if (item.end && new Date(item.end) < today) {
25+
group = 'Past';
26+
} else {
27+
group = 'Upcoming';
28+
}
29+
var hint = null;
30+
if (item.start) {
31+
hint = item.start.substr(0,10);
32+
}
33+
return { value: item[config.labelField], data: { id: item[config.idField], group: group, item: item, hint: hint } };
734
})
835
};
936
},
@@ -15,7 +42,7 @@ var Autocompleters = {
1542
name = name + " (" + item.firstname + " " + item.surname + ")";
1643
}
1744
item.name = name;
18-
return { value: name, data: item[config.idField], item: item };
45+
return { value: name, data: { id: item[config.idField], item: item } };
1946
})
2047
};
2148
}
@@ -32,6 +59,7 @@ var Autocompleters = {
3259
var labelField = $(element).data("labelField") || "title";
3360
var idField = $(element).data("idField") || "id";
3461
var singleton = $(element).data("singleton") || false;
62+
var groupBy = $(element).data("groupBy") || false;
3563
var templateName = $(element).data("template") ||
3664
(singleton ? "autocompleter/singleton_resource" : "autocompleter/resource");
3765
var transformFunction = Autocompleters.transformFunctions[$(element).data("transformFunction") || "default"];
@@ -52,13 +80,16 @@ var Autocompleters = {
5280
dataType: "json",
5381
deferRequestBy: 300, // Wait 300ms before submitting to stop search being flooded
5482
paramName: "q",
83+
groupBy: groupBy,
84+
formatResult: Autocompleters.formatResultWithHint,
5585
transformResult: function(response) {
5686
return transformFunction(response, { labelField: labelField, idField: idField });
5787
},
5888
onSelect: function (suggestion) {
5989
// Don't add duplicates
60-
if (!$("[data-id='" + suggestion.data + "']", listElement).length) {
61-
var obj = { item: suggestion.item };
90+
var id = suggestion.data.id;
91+
if (!$("[data-id='" + id + "']", listElement).length) {
92+
var obj = { item: suggestion.data.item };
6293
if (prefix) {
6394
obj.prefix = prefix;
6495
}
@@ -72,7 +103,6 @@ var Autocompleters = {
72103
$(this).val('').focus();
73104
},
74105
onSearchStart: function (query) {
75-
query.q = query.q + "*";
76106
inputElement.addClass("loading");
77107
},
78108
onSearchComplete: function () {

app/assets/javascripts/collaborations.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ var Collaborations = {
1515
});
1616
},
1717
onSelect: function (suggestion) {
18-
Collaborations.add(suggestion.data)
18+
Collaborations.add(suggestion.data.id)
1919
$(this).val('').focus();
2020
}
2121
});

app/assets/stylesheets/autocomplete.scss

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
}
66

77
.autocomplete-suggestion {
8-
padding: 2px 5px;
8+
padding: 2px 5px 2px 10px;
99
white-space: nowrap;
1010
overflow: hidden;
1111
cursor: pointer;
@@ -21,10 +21,17 @@
2121
}
2222

2323
.autocomplete-group {
24-
padding: 2px 5px;
24+
padding: 5px 5px;
25+
background: #eee;
2526
}
2627

2728
.autocomplete-group strong {
2829
display: block;
29-
border-bottom: 1px solid #000;
30+
color: inherit;
31+
font-weight: bold;
3032
}
33+
34+
.autocomplete-hint {
35+
color: #999;
36+
padding-left: 1em;
37+
}

app/helpers/application_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,7 @@ def autocompleter(name, options = {})
391391
form_field_name: options[:form_field_name],
392392
existing_items_method: options[:existing_items_method],
393393
transform_function: options[:transform_function],
394+
group_by: options[:group_by],
394395
singleton: options[:singleton],
395396
})
396397
end

app/views/collections/_form.html.erb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@
1919

2020
<%= f.autocompleter :materials %>
2121

22-
<%= f.autocompleter :events %>
22+
<%= f.autocompleter :events,
23+
url: events_path(include_expired: true, sort: 'late'),
24+
group_by: 'group',
25+
transform_function: 'events'
26+
%>
2327

2428
<div class="form-group actions">
2529
<%= f.submit ( f.object.new_record? ? "Create" : "Update") + " collection", :class => 'btn btn-primary' %>

app/views/common/_autocompleter.html.erb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
form_field_name ||= "#{model_name}[#{field_name.to_s.singularize}_#{id_field.to_s}#{singleton ? '' : 's'}]"
44
template ||= nil
55
transform_function ||= nil
6+
group_by ||= nil
67
existing_items_method ||= field_name
78
data = { role: 'autocompleter-group', url: url, prefix: form_field_name,
89
'label-field' => label_field.to_s,
910
'id-field' => id_field.to_s }
1011
data[:template] = template if template
1112
data[:singleton] = singleton unless singleton.nil?
1213
data['transform-function'] = transform_function if transform_function
14+
data['group-by'] = group_by if group_by
1315
json = Array(f.object.send(existing_items_method)).map do |r|
1416
{ item: { id: r.send(id_field),
1517
id_field => r.send(id_field),

app/views/materials/_form.html.erb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,11 @@
5050
<%= f.multi_input :contributors, suggestions: people_suggestions, title: t('materials.hints.contributors') %>
5151

5252
<!-- Field: Events -->
53-
<%= f.autocompleter :events, input_html: { title: t('materials.hints.events') } %>
53+
<%= f.autocompleter :events, input_html: { title: t('materials.hints.events') },
54+
url: events_path(include_expired: true, sort: 'late'),
55+
group_by: 'group',
56+
transform_function: 'events'
57+
%>
5458

5559
<!-- Field: Target Audience -->
5660
<%= f.multi_input :target_audience, label: 'Target audiences', errors: @material.errors[:target_audience],

0 commit comments

Comments
 (0)