Fix 2.4.2 upgrade notes formatting refs #19330
[arvados.git] / apps / workbench / app / assets / javascripts / filterable.js
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
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.
9 //
10 // Usage:
11 //
12 // 1. Add the "filterable" class to each filterable content item.
13 // Typically, each item is a 'tr' or a 'div class="row"'.
14 //
15 // <div id="results">
16 //   <div class="filterable row">First row</div>
17 //   <div class="filterable row">Second row</div>
18 // </div>
19 //
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.
24 //
25 // <input class="filterable-control" data-filterable-target="#results"
26 //        type="text" />
27 //
28 // Supported widgets:
29 //
30 // <input type="text" ... />
31 //
32 // The input value is used as a regular expression. Rows with content
33 // matching the regular expression are shown.
34 //
35 // <select ... data-filterable-attribute="data-example-attr">
36 //  <option value="foo">Foo</option>
37 //  <option value="">Show all</option>
38 // </select>
39 //
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.
43 //
44 // <input type="checkbox" data-on-value="{}" data-off-value="{}" ... />
45 //
46 // Merges on- or off-value with other params in query. Only works with
47 // infinite-scroll.
48 //
49 // Notes:
50 //
51 // When multiple filterable-control widgets operate on the same
52 // data-filterable-target, items must pass _all_ filters in order to
53 // be shown.
54 //
55 // If one data-filterable-target is the parent of another
56 // data-filterable-target, results are undefined. Don't do this.
57 //
58 // Combining "select" filterable-controls with infinite-scroll is not
59 // yet supported.
60
61 function updateFilterableQueryNow($target) {
62     var newquery = $target.data('filterable-query-new');
63     var params = $target.data('infinite-content-params-filterable') || {};
64     params.filters = ilike_filters(newquery);
65     $(".modal-dialog-preview-pane").html("");
66     $target.data('infinite-content-params-filterable', params);
67     $target.data('filterable-query', newquery);
68 }
69
70 $(document).
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() {
75             var $this = $(this);
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);
81             }
82         });
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.
88             if (this != e.target)
89                 return;
90             if ($(this).data('filterable-query') == $(this).data('filterable-query-new'))
91                 return;
92             updateFilterableQueryNow($(this));
93         });
94     }).
95     on('change', 'input[type=checkbox].filterable-control', function(e) {
96         if (this != e.target) return;
97         var $target = $($(this).attr('data-filterable-target'));
98         var currentquery = $target.data('filterable-query');
99         if (currentquery === undefined) currentquery = '';
100         if ($target.is('[data-infinite-scroller]')) {
101             var datakey = 'infiniteContentParamsFrom'+this.id;
102             var whichvalue = $(this).is(':checked') ? 'on-value' : 'off-value';
103             if (JSON.stringify($target.data(datakey)) == JSON.stringify($(this).data(whichvalue)))
104                 return;
105             $target.data(datakey, $(this).data(whichvalue));
106             updateFilterableQueryNow($target);
107             $target.trigger('refresh-content');
108         }
109     }).
110     on('paste keyup input', 'input[type=text].filterable-control', function(e) {
111         var regexp;
112         if (this != e.target) return;
113         var $target = $($(this).attr('data-filterable-target'));
114         var currentquery = $target.data('filterable-query');
115         if (currentquery === undefined) currentquery = '';
116         if ($target.is('[data-infinite-scroller]')) {
117             // We already know how to load content dynamically, so we
118             // can do all filtering on the server side.
119
120             if ($target.data('infinite-cooloff-timer') > 0) {
121                 // Clear a stale refresh-after-delay timer.
122                 clearTimeout($target.data('infinite-cooloff-timer'));
123             }
124             // Stash the new query string in the filterable container.
125             $target.data('filterable-query-new', $(this).val());
126             if (currentquery == $(this).val()) {
127                 // Don't mess with existing results or queries in
128                 // progress.
129                 return;
130             }
131             $target.data('infinite-cooloff-timer', setTimeout(function() {
132                 // If the user doesn't do any query-changing actions
133                 // in the next 1/4 second (like type or erase
134                 // characters in the search box), hide the stale
135                 // content and ask the server for new results.
136                 updateFilterableQueryNow($target);
137                 $target.trigger('refresh-content');
138             }, 250));
139         } else {
140             // Target does not have infinite-scroll capability. Just
141             // filter the rows in the browser using a RegExp.
142             regexp = undefined;
143             try {
144                 regexp = new RegExp($(this).val(), 'i');
145             } catch(e) {
146                 if (e instanceof SyntaxError) {
147                     // Invalid/partial regexp. See 'has-error' below.
148                 } else {
149                     throw e;
150                 }
151             }
152             $target.
153                 toggleClass('has-error', regexp === undefined).
154                 addClass('filterable-container').
155                 data('q', regexp).
156                 trigger('refresh');
157         }
158     }).on('refresh', '.filterable-container', function() {
159         var $container = $(this);
160         var q = $(this).data('q');
161         var filters = $(this).data('filters');
162         $('.filterable', this).hide().filter(function() {
163             var $row = $(this);
164             var pass = true;
165             if (q && !$row.text().match(q))
166                 return false;
167             if (filters) {
168                 $.each(filters, function(filterby, val) {
169                     if (!val) return;
170                     if (!pass) return;
171                     pass = false;
172                     $.each(val.split(" "), function(i, e) {
173                         if ($row.attr(filterby) == e)
174                             pass = true;
175                     });
176                 });
177             }
178             return pass;
179         }).show();
180
181         // Show/hide each section heading depending on whether any
182         // content rows are visible in that section.
183         $('.row[data-section-heading]', this).each(function(){
184             $(this).toggle($('.row.filterable[data-section-name="' +
185                              $(this).attr('data-section-name') +
186                              '"]:visible').length > 0);
187         });
188
189         // Load more content if the last result is showing.
190         $('.infinite-scroller').add(window).trigger('scroll');
191     }).on('change', 'select.filterable-control', function() {
192         var val = $(this).val();
193         var filterby = $(this).attr('data-filterable-attribute');
194         var $target = $($(this).attr('data-filterable-target')).
195             addClass('filterable-container');
196         var filters = $target.data('filters') || {};
197         filters[filterby] = val;
198         $target.
199             data('filters', filters).
200             trigger('refresh');
201     }).on('ajax:complete', function() {
202         $('.filterable-control').trigger('input');
203     });