Commit 8064a400 authored by Alfredo Sumaran's avatar Alfredo Sumaran

Add support to filter groups by filter options

parent 312d02ad
...@@ -12,7 +12,10 @@ export default class FilterableList { ...@@ -12,7 +12,10 @@ export default class FilterableList {
} }
initSearch() { initSearch() {
this.debounceFilter = _.debounce(this.filterResults.bind(this), 500); // Wrap to prevent passing event arguments to .filterResults;
this.debounceFilter = _.debounce(() => {
this.filterResults();
}, 500);
this.unbindEvents(); this.unbindEvents();
this.bindEvents(); this.bindEvents();
...@@ -26,13 +29,15 @@ export default class FilterableList { ...@@ -26,13 +29,15 @@ export default class FilterableList {
this.listFilterElement.removeEventListener('input', this.debounceFilter); this.listFilterElement.removeEventListener('input', this.debounceFilter);
} }
filterResults() { filterResults(url, data) {
const endpoint = url || this.filterForm.getAttribute('action');
const additionalData = data || $(this.filterForm).serialize();
$(this.listHolderElement).fadeTo(250, 0.5); $(this.listHolderElement).fadeTo(250, 0.5);
return $.ajax({ return $.ajax({
url: this.filterForm.getAttribute('action'), url: endpoint,
data: $(this.filterForm).serialize(), data: additionalData,
type: 'GET', type: 'GET',
dataType: 'json', dataType: 'json',
context: this, context: this,
...@@ -42,6 +47,10 @@ export default class FilterableList { ...@@ -42,6 +47,10 @@ export default class FilterableList {
} }
onFilterSuccess(data) { onFilterSuccess(data) {
if (data.html) {
this.listHolderElement.innerHTML = data.html;
}
// Change url so if user reload a page - search results are saved // Change url so if user reload a page - search results are saved
return window.history.replaceState({ return window.history.replaceState({
page: this.filterUrl, page: this.filterUrl,
......
...@@ -6,13 +6,17 @@ export default class GroupFilterableList extends FilterableList { ...@@ -6,13 +6,17 @@ export default class GroupFilterableList extends FilterableList {
super(form, filter, holder); super(form, filter, holder);
this.store = store; this.store = store;
this.$dropdown = $('.js-group-filter-dropdown-wrap');
} }
bindEvents() { bindEvents() {
super.bindEvents(); super.bindEvents();
this.onFormSubmitWrapper = this.onFormSubmit.bind(this); this.onFormSubmitWrapper = this.onFormSubmit.bind(this);
this.onFilterOptionClikWrapper = this.onOptionClick.bind(this);
this.filterForm.addEventListener('submit', this.onFormSubmitWrapper); this.filterForm.addEventListener('submit', this.onFormSubmitWrapper);
this.$dropdown.on('click', 'a', this.onFilterOptionClikWrapper);
} }
onFormSubmit(e) { onFormSubmit(e) {
...@@ -21,8 +25,17 @@ export default class GroupFilterableList extends FilterableList { ...@@ -21,8 +25,17 @@ export default class GroupFilterableList extends FilterableList {
this.filterResults(); this.filterResults();
} }
onOptionClick(e) {
e.preventDefault();
const currentOption = $.trim(e.currentTarget.text);
this.filterUrl = e.currentTarget.href;
this.$dropdown.find('.dropdown-label').text(currentOption);
this.filterResults(this.filterUrl);
}
onFilterSuccess(data) { onFilterSuccess(data) {
super.onFilterSuccess(); super.onFilterSuccess(data);
this.store.setGroups(data); this.store.setGroups(data);
} }
......
.dropdown.inline .dropdown.inline.js-group-filter-dropdown-wrap
%button.dropdown-toggle{ type: 'button', 'data-toggle' => 'dropdown' } %button.dropdown-toggle{ type: 'button', 'data-toggle' => 'dropdown' }
%span.light %span.dropdown-label
- if @sort.present? - if @sort.present?
= sort_options_hash[@sort] = sort_options_hash[@sort]
- else - else
= sort_title_recently_created = sort_title_recently_created
= icon('chevron-down') = icon('chevron-down')
%ul.dropdown-menu.dropdown-menu-align-right %ul.dropdown-menu.dropdown-menu-align-right
%li %li
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment