Improvements and bugfixes refs #2242.
[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 get_selection_list = null;
7 form_selection_sources = {};
8
9 jQuery(function($){
10     var storage = localStorage; // sessionStorage
11
12     get_selection_list = function() {
13         if (!storage.persistentSelection) {
14             storage.persistentSelection = JSON.stringify([]);
15         }
16         return JSON.parse(storage.persistentSelection);
17     }
18
19     var put_storage = function(lst) {
20         storage.persistentSelection = JSON.stringify(lst);
21     }
22
23     var add_selection = function(uuid, name, href, type) {
24         var lst = get_selection_list();
25         lst.push({"uuid": uuid, "name": name, "href": href, "type": type});
26         put_storage(lst);
27         update_count();
28     };
29
30     var remove_selection = function(uuid) {
31         var lst = get_selection_list();
32         for (var i = 0; i < lst.length; i++) {
33             if (lst[i].uuid == uuid) {
34                 lst.splice(i, 1);
35                 i--;
36             }
37         }
38         put_storage(lst);
39         update_count();
40     };
41
42     var remove_selection_click = function(e) {
43         //remove_selection($(this).attr('name'));
44         remove_selection($(this).val());
45     };
46
47     var clear_selections = function() {
48         put_storage([]);
49         update_count();
50     }
51
52     var update_count = function(e) {
53         var lst = get_selection_list();
54         $("#persistent-selection-count").text(lst.length);
55
56         if (lst.length > 0) {
57             $('#persistent-selection-list').html('<li><a href="#" class="btn pull-right" id="clear_selections_button">Clear selections</a></li>'
58                                                  +'<li class="notification"><table style="width: 100%"></table></li>');
59             for (var i = 0; i < lst.length; i++) {
60                 $('#persistent-selection-list > li > table').append("<tr>"
61                                                        + "<td>"
62                                                        + "<form>"
63                                                        + "<input class='remove-selection' type='checkbox' value='" + lst[i].uuid + "' checked='true'></input>"
64                                                        + "</form>"
65                                                        + "</td>"
66
67                                                        + "<td>"
68                                                        + "<span style='padding-left: 1em'><a href=\"" + lst[i].href + "\">" + lst[i].name + "</a></span>"
69                                                        + "</td>"
70
71                                                        + "<td style=\"vertical-align: top\">"
72                                                        + "<span style=\"padding-right: 1em\">" + lst[i].type + "</span>"
73                                                        + "</td>"
74
75                                                        + "</tr>");
76             }
77         } else {
78             $('#persistent-selection-list').html("<li class='notification empty'>No selections.</li>");
79         }
80
81         var checkboxes = $('.persistent-selection:checkbox');
82         for (i = 0; i < checkboxes.length; i++) {
83             for (var j = 0; j < lst.length; j++) {
84                 if (lst[j].uuid == $(checkboxes[i]).val()) {
85                     checkboxes[i].checked = true;
86                     break;
87                 }
88             }
89             if (j == lst.length) {
90                 checkboxes[i].checked = false;
91             }
92         }
93         
94         $('.remove-selection').on('click', remove_selection_click);
95         $('#clear_selections_button').on('click', clear_selections);
96     };
97
98     $(document).
99         on('change', '.persistent-selection:checkbox', function(e) {
100             //console.log($(this));
101             //console.log($(this).val());
102             
103             var inc = 0;
104             if ($(this).is(":checked")) {
105                 add_selection($(this).val(), $(this).attr('friendly_name'), $(this).attr('href'), $(this).attr('friendly_type'));
106             }
107             else {
108                 remove_selection($(this).val());
109             }
110         });
111
112
113     $(window).on('load storage', update_count);
114 });
115
116 add_form_selection_sources = null;
117 select_form_sources  = null;
118
119 (function() {
120     var form_selection_sources = {};
121     add_form_selection_sources = function (src) {
122         for (var i = 0; i < src.length; i++) {
123             var t = form_selection_sources[src[i].type];
124             if (!t) {
125                 t = form_selection_sources[src[i].type] = {};
126             }
127             if (!t[src[i].uuid]) {
128                 t[src[i].uuid] = src[i];
129             }
130         }
131     };
132
133     select_form_sources = function(type) {
134         var ret = [];
135
136         if (get_selection_list) {
137             var lst = get_selection_list();
138             if (lst.length > 0) {
139                 var text = "&horbar; Selections &horbar;";
140                 var span = document.createElement('span');
141                 span.innerHTML = text;
142                 ret.push({text: span.innerHTML, value: "***invalid***"});
143
144                 for (var i = 0; i < lst.length; i++) {
145                     if (lst[i].type == type) {
146                         ret.push({text: lst[i].name, value: lst[i].uuid})
147                     }
148                 }
149             }
150         }
151
152         var text = "&horbar; Recent &horbar;";
153         var span = document.createElement('span');
154         span.innerHTML = text;
155         ret.push({text: span.innerHTML, value: "***invalid***"});
156
157         var t = form_selection_sources[type];
158         for (var key in t) {
159             if (t.hasOwnProperty(key)) {
160                 var obj = t[key];
161                 ret.push({text: obj.name, value: obj.uuid})
162             }
163         }
164         return ret;
165     };
166 })();
167