Merge remote-tracking branch 'origin/master' into 2049-run-job-from-workbench
[arvados.git] / apps / workbench / app / assets / javascripts / selection.js
1 //= require jquery
2 //= require jquery_ujs
3
4 /** Javascript for local persistent selection. */
5
6 (function($){
7     var get_storage = function() {
8         if (!sessionStorage.persistentSelection) {
9             sessionStorage.persistentSelection = JSON.stringify([]);
10         }
11         return JSON.parse(sessionStorage.persistentSelection);
12     }
13
14     var put_storage = function(lst) {
15         sessionStorage.persistentSelection = JSON.stringify(lst);
16     }
17
18     var add_selection = function(uuid, name, href, type) {
19         var lst = get_storage();
20         lst.push({"uuid": uuid, "name": name, "href": href, "type": type});
21         put_storage(lst);
22         update_count();
23     };
24
25     var remove_selection = function(uuid) {
26         var lst = get_storage();
27         for (var i = 0; i < lst.length; i++) {
28             if (lst[i].uuid == uuid) {
29                 lst.splice(i, 1);
30                 i--;
31             }
32         }
33         put_storage(lst);
34         update_count();
35     };
36
37     var remove_selection_click = function(e) {
38         remove_selection($(this).attr('name'));
39     };
40
41     var update_count = function(e) {
42         var lst = get_storage();
43         $("#persistent-selection-count").text(lst.length);
44
45         $('#persistent-selection-list > li > table').empty();
46         if (lst.length > 0) {
47             for (var i = 0; i < lst.length; i++) {
48                 $('#persistent-selection-list > li > table').append("<tr>"
49                                                        + "<td style=\"vertical-align: top\">"
50                                                        + "<span style=\"padding-right: 1em\">" + lst[i].type + "</span>"
51                                                        + "</td>"
52
53                                                        + "<td>"
54                                                        + "<span><a href=\"" + lst[i].href + "\">" + lst[i].name + "</a></span>"
55                                                        + "</td>"
56
57                                                        + "<td>"
58                                                        + "<a href=\"#\" class=\"remove-selection\" name=\"" + lst[i].uuid + "\">" 
59                                                        + "<span class=\"glyphicon glyphicon-trash pull-right\"></span>"
60                                                        + "</a></span>"
61                                                        + "</td>"
62                                                        + "</tr>");
63             }
64         } else {
65             $('#persistent-selection-list > li > table').html("<tr><td>No selections.</td></tr>");
66         }
67
68         var checkboxes = $('.persistent-selection:checkbox');
69         for (i = 0; i < checkboxes.length; i++) {
70             for (var j = 0; j < lst.length; j++) {
71                 if (lst[j].uuid == $(checkboxes[i]).val()) {
72                     checkboxes[i].checked = true;
73                     break;
74                 }
75             }
76             if (j == lst.length) {
77                 checkboxes[i].checked = false;
78             }
79         }
80         
81         $('.remove-selection').on('click', remove_selection_click);
82     };
83
84     $(document).
85         on('change', '.persistent-selection:checkbox', function(e) {
86             //console.log($(this));
87             //console.log($(this).val());
88             
89             var inc = 0;
90             if ($(this).is(":checked")) {
91                 add_selection($(this).val(), $(this).attr('friendly_name'), $(this).attr('href'), $(this).attr('friendly_type'));
92             }
93             else {
94                 remove_selection($(this).val());
95             }
96         });
97
98
99     $(window).on('load storage', update_count);
100 })(jQuery);