Merge branch '3235-top-nav-site-search' refs #3235
[arvados.git] / apps / workbench / app / assets / javascripts / filterable.js
1 $(document).
2     on('paste keyup input', 'input[type=text].filterable-control', function() {
3         var q = new RegExp($(this).val(), 'i');
4         $($(this).attr('data-filterable-target')).
5             addClass('filterable-container').
6             data('q', q).
7             trigger('refresh');
8     }).on('refresh', '.filterable-container', function() {
9         var $container = $(this);
10         var q = $(this).data('q');
11         var filters = $(this).data('filters');
12         $('.filterable', this).hide().filter(function() {
13             var $row = $(this);
14             var pass = true;
15             if (q && !$row.text().match(q))
16                 return false;
17             if (filters) {
18                 $.each(filters, function(filterby, val) {
19                     if (!val) return;
20                     if (!pass) return;
21                     pass = false;
22                     $.each(val.split(" "), function(i, e) {
23                         if ($row.attr(filterby) == e)
24                             pass = true;
25                     });
26                 });
27             }
28             return pass;
29         }).show();
30
31         // Show/hide each section heading depending on whether any
32         // content rows are visible in that section.
33         $('.row[data-section-heading]', this).each(function(){
34             $(this).toggle($('.row.filterable[data-section-name="' +
35                              $(this).attr('data-section-name') +
36                              '"]:visible').length > 0);
37         });
38
39         // Load more content if the last result is showing.
40         $('.infinite-scroller').add(window).trigger('scroll');
41     }).on('change', 'select.filterable-control', function() {
42         var val = $(this).val();
43         var filterby = $(this).attr('data-filterable-attribute');
44         var $target = $($(this).attr('data-filterable-target')).
45             addClass('filterable-container');
46         var filters = $target.data('filters') || {};
47         filters[filterby] = val;
48         $target.
49             data('filters', filters).
50             trigger('refresh');
51     }).on('ajax:complete', function() {
52         $('.filterable-control').trigger('input');
53     });