1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 // filterable.js shows/hides content when the user operates
6 // search/select widgets. For "infinite scroll" content, it passes the
7 // filters to the server and retrieves new content. For other content,
8 // it filters the existing DOM elements using jQuery show/hide.
12 // 1. Add the "filterable" class to each filterable content item.
13 // Typically, each item is a 'tr' or a 'div class="row"'.
16 // <div class="filterable row">First row</div>
17 // <div class="filterable row">Second row</div>
20 // 2. Add the "filterable-control" class to each search/select widget.
21 // Also add a data-filterable-target attribute with a jQuery selector
22 // for an ancestor of the filterable items, i.e., the container in
23 // which this widget should apply filtering.
25 // <input class="filterable-control" data-filterable-target="#results"
30 // <input type="text" ... />
32 // The input value is used as a regular expression. Rows with content
33 // matching the regular expression are shown.
35 // <select ... data-filterable-attribute="data-example-attr">
36 // <option value="foo">Foo</option>
37 // <option value="">Show all</option>
40 // When the user selects the "Foo" option, rows with
41 // data-example-attr="foo" are shown, and all others are hidden. When
42 // the user selects the "Show all" option, all rows are shown.
46 // When multiple filterable-control widgets operate on the same
47 // data-filterable-target, items must pass _all_ filters in order to
50 // If one data-filterable-target is the parent of another
51 // data-filterable-target, results are undefined. Don't do this.
53 // Combining "select" filterable-controls with infinite-scroll is not
56 function updateFilterableQueryNow($target) {
57 var newquery = $target.data('filterable-query-new');
58 var params = $target.data('infinite-content-params-filterable') || {};
59 var tsquery = to_tsquery(newquery);
60 if (tsquery == null) {
63 params.filters = [['any', '@@', tsquery]];
65 $(".modal-dialog-preview-pane").html("");
66 $target.data('infinite-content-params-filterable', params);
67 $target.data('filterable-query', newquery);
71 on('ready ajax:success', function() {
72 // Copy any initial input values into
73 // data-filterable-query[-new].
74 $('input[type=text].filterable-control').each(function() {
76 var $target = $($this.attr('data-filterable-target'));
77 if ($target.data('filterable-query-new') === undefined) {
78 $target.data('filterable-query', $this.val());
79 $target.data('filterable-query-new', $this.val());
80 updateFilterableQueryNow($target);
83 $('[data-infinite-scroller]').on('refresh-content', '[data-filterable-query]', function(e) {
84 // If some other event causes a refresh-content event while there
85 // is a new query waiting to cooloff, we should use the new query
86 // right away -- otherwise we'd launch an extra ajax request that
87 // would have to be reloaded as soon as the cooloff period ends.
90 if ($(this).data('filterable-query') == $(this).data('filterable-query-new'))
92 updateFilterableQueryNow($(this));
95 on('paste keyup input', 'input[type=text].filterable-control', function(e) {
97 if (this != e.target) return;
98 var $target = $($(this).attr('data-filterable-target'));
99 var currentquery = $target.data('filterable-query');
100 if (currentquery === undefined) currentquery = '';
101 if ($target.is('[data-infinite-scroller]')) {
102 // We already know how to load content dynamically, so we
103 // can do all filtering on the server side.
105 if ($target.data('infinite-cooloff-timer') > 0) {
106 // Clear a stale refresh-after-delay timer.
107 clearTimeout($target.data('infinite-cooloff-timer'));
109 // Stash the new query string in the filterable container.
110 $target.data('filterable-query-new', $(this).val());
111 if (currentquery == $(this).val()) {
112 // Don't mess with existing results or queries in
116 $target.data('infinite-cooloff-timer', setTimeout(function() {
117 // If the user doesn't do any query-changing actions
118 // in the next 1/4 second (like type or erase
119 // characters in the search box), hide the stale
120 // content and ask the server for new results.
121 updateFilterableQueryNow($target);
122 $target.trigger('refresh-content');
125 // Target does not have infinite-scroll capability. Just
126 // filter the rows in the browser using a RegExp.
129 regexp = new RegExp($(this).val(), 'i');
131 if (e instanceof SyntaxError) {
132 // Invalid/partial regexp. See 'has-error' below.
138 toggleClass('has-error', regexp === undefined).
139 addClass('filterable-container').
143 }).on('refresh', '.filterable-container', function() {
144 var $container = $(this);
145 var q = $(this).data('q');
146 var filters = $(this).data('filters');
147 $('.filterable', this).hide().filter(function() {
150 if (q && !$row.text().match(q))
153 $.each(filters, function(filterby, val) {
157 $.each(val.split(" "), function(i, e) {
158 if ($row.attr(filterby) == e)
166 // Show/hide each section heading depending on whether any
167 // content rows are visible in that section.
168 $('.row[data-section-heading]', this).each(function(){
169 $(this).toggle($('.row.filterable[data-section-name="' +
170 $(this).attr('data-section-name') +
171 '"]:visible').length > 0);
174 // Load more content if the last result is showing.
175 $('.infinite-scroller').add(window).trigger('scroll');
176 }).on('change', 'select.filterable-control', function() {
177 var val = $(this).val();
178 var filterby = $(this).attr('data-filterable-attribute');
179 var $target = $($(this).attr('data-filterable-target')).
180 addClass('filterable-container');
181 var filters = $target.data('filters') || {};
182 filters[filterby] = val;
184 data('filters', filters).
186 }).on('ajax:complete', function() {
187 $('.filterable-control').trigger('input');