8784: Fix test for latest firefox.
[arvados.git] / apps / workbench / app / assets / javascripts / filterable.js
1 // filterable.js shows/hides content when the user operates
2 // search/select widgets. For "infinite scroll" content, it passes the
3 // filters to the server and retrieves new content. For other content,
4 // it filters the existing DOM elements using jQuery show/hide.
5 //
6 // Usage:
7 //
8 // 1. Add the "filterable" class to each filterable content item.
9 // Typically, each item is a 'tr' or a 'div class="row"'.
10 //
11 // <div id="results">
12 //   <div class="filterable row">First row</div>
13 //   <div class="filterable row">Second row</div>
14 // </div>
15 //
16 // 2. Add the "filterable-control" class to each search/select widget.
17 // Also add a data-filterable-target attribute with a jQuery selector
18 // for an ancestor of the filterable items, i.e., the container in
19 // which this widget should apply filtering.
20 //
21 // <input class="filterable-control" data-filterable-target="#results"
22 //        type="text" />
23 //
24 // Supported widgets:
25 //
26 // <input type="text" ... />
27 //
28 // The input value is used as a regular expression. Rows with content
29 // matching the regular expression are shown.
30 //
31 // <select ... data-filterable-attribute="data-example-attr">
32 //  <option value="foo">Foo</option>
33 //  <option value="">Show all</option>
34 // </select>
35 //
36 // When the user selects the "Foo" option, rows with
37 // data-example-attr="foo" are shown, and all others are hidden. When
38 // the user selects the "Show all" option, all rows are shown.
39 //
40 // Notes:
41 //
42 // When multiple filterable-control widgets operate on the same
43 // data-filterable-target, items must pass _all_ filters in order to
44 // be shown.
45 //
46 // If one data-filterable-target is the parent of another
47 // data-filterable-target, results are undefined. Don't do this.
48 //
49 // Combining "select" filterable-controls with infinite-scroll is not
50 // yet supported.
51
52 function updateFilterableQueryNow($target) {
53     var newquery = $target.data('filterable-query-new');
54     var params = $target.data('infinite-content-params-filterable') || {};
55     if (newquery == null || newquery == '') {
56       params.filters = [];
57     } else {
58       params.filters = [['any', '@@', newquery.trim().concat(':*')]];
59     }
60     $(".modal-dialog-preview-pane").html("");
61     $target.data('infinite-content-params-filterable', params);
62     $target.data('filterable-query', newquery);
63 }
64
65 $(document).
66     on('ready ajax:success', function() {
67         // Copy any initial input values into
68         // data-filterable-query[-new].
69         $('input[type=text].filterable-control').each(function() {
70             var $this = $(this);
71             var $target = $($this.attr('data-filterable-target'));
72             if ($target.data('filterable-query-new') === undefined) {
73                 $target.data('filterable-query', $this.val());
74                 $target.data('filterable-query-new', $this.val());
75                 updateFilterableQueryNow($target);
76             }
77         });
78         $('[data-infinite-scroller]').on('refresh-content', '[data-filterable-query]', function(e) {
79             // If some other event causes a refresh-content event while there
80             // is a new query waiting to cooloff, we should use the new query
81             // right away -- otherwise we'd launch an extra ajax request that
82             // would have to be reloaded as soon as the cooloff period ends.
83             if (this != e.target)
84                 return;
85             if ($(this).data('filterable-query') == $(this).data('filterable-query-new'))
86                 return;
87             updateFilterableQueryNow($(this));
88         });
89     }).
90     on('paste keyup input', 'input[type=text].filterable-control', function(e) {
91         var regexp;
92         if (this != e.target) return;
93         var $target = $($(this).attr('data-filterable-target'));
94         var currentquery = $target.data('filterable-query');
95         if (currentquery === undefined) currentquery = '';
96         if ($target.is('[data-infinite-scroller]')) {
97             // We already know how to load content dynamically, so we
98             // can do all filtering on the server side.
99
100             if ($target.data('infinite-cooloff-timer') > 0) {
101                 // Clear a stale refresh-after-delay timer.
102                 clearTimeout($target.data('infinite-cooloff-timer'));
103             }
104             // Stash the new query string in the filterable container.
105             $target.data('filterable-query-new', $(this).val());
106             if (currentquery == $(this).val()) {
107                 // Don't mess with existing results or queries in
108                 // progress.
109                 return;
110             }
111             $target.data('infinite-cooloff-timer', setTimeout(function() {
112                 // If the user doesn't do any query-changing actions
113                 // in the next 1/4 second (like type or erase
114                 // characters in the search box), hide the stale
115                 // content and ask the server for new results.
116                 updateFilterableQueryNow($target);
117                 $target.trigger('refresh-content');
118             }, 250));
119         } else {
120             // Target does not have infinite-scroll capability. Just
121             // filter the rows in the browser using a RegExp.
122             regexp = undefined;
123             try {
124                 regexp = new RegExp($(this).val(), 'i');
125             } catch(e) {
126                 if (e instanceof SyntaxError) {
127                     // Invalid/partial regexp. See 'has-error' below.
128                 } else {
129                     throw e;
130                 }
131             }
132             $target.
133                 toggleClass('has-error', regexp === undefined).
134                 addClass('filterable-container').
135                 data('q', regexp).
136                 trigger('refresh');
137         }
138     }).on('refresh', '.filterable-container', function() {
139         var $container = $(this);
140         var q = $(this).data('q');
141         var filters = $(this).data('filters');
142         $('.filterable', this).hide().filter(function() {
143             var $row = $(this);
144             var pass = true;
145             if (q && !$row.text().match(q))
146                 return false;
147             if (filters) {
148                 $.each(filters, function(filterby, val) {
149                     if (!val) return;
150                     if (!pass) return;
151                     pass = false;
152                     $.each(val.split(" "), function(i, e) {
153                         if ($row.attr(filterby) == e)
154                             pass = true;
155                     });
156                 });
157             }
158             return pass;
159         }).show();
160
161         // Show/hide each section heading depending on whether any
162         // content rows are visible in that section.
163         $('.row[data-section-heading]', this).each(function(){
164             $(this).toggle($('.row.filterable[data-section-name="' +
165                              $(this).attr('data-section-name') +
166                              '"]:visible').length > 0);
167         });
168
169         // Load more content if the last result is showing.
170         $('.infinite-scroller').add(window).trigger('scroll');
171     }).on('change', 'select.filterable-control', function() {
172         var val = $(this).val();
173         var filterby = $(this).attr('data-filterable-attribute');
174         var $target = $($(this).attr('data-filterable-target')).
175             addClass('filterable-container');
176         var filters = $target.data('filters') || {};
177         filters[filterby] = val;
178         $target.
179             data('filters', filters).
180             trigger('refresh');
181     }).on('ajax:complete', function() {
182         $('.filterable-control').trigger('input');
183     });