Merge branch 'master' into 3177-collection-choose-files
[arvados.git] / apps / workbench / app / assets / javascripts / selection.js.erb
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).val());
44     };
45
46     var clear_selections = function() {
47         put_storage([]);
48         update_count();
49     }
50
51     var update_count = function(e) {
52         var html;
53         var this_object_uuid = $('#selection-form-content').
54             closest('form').
55             find('input[name=uuid]').val();
56         var lst = get_selection_list();
57         $("#persistent-selection-count").text(lst.length);
58         if (lst.length > 0) {
59             html = '<li><a href="#" class="btn btn-xs btn-info" id="clear_selections_button"><i class="fa fa-fw fa-ban"></i> Clear selections</a></li>';
60             $('#selection-form-content').html(html);
61
62             for (var i = 0; i < lst.length; i++) {
63                 $('#selection-form-content > li > table').append("<tr>"
64                                                        + "<td>"
65                                                        + "<input class='remove-selection' name='selection[]' type='checkbox' value='" + lst[i].uuid + "' checked='true' data-stoppropagation='true' />"
66                                                        + "</td>"
67
68                                                        + "<td>"
69                                                        + "<div style='padding-left: 1em'><a href=\"" + lst[i].href + "\">" + lst[i].name + "</a></div>"
70                                                        + "</td>"
71
72                                                        + "<td style=\"vertical-align: top\">"
73                                                        + "<span style=\"padding-right: 1em\">" + lst[i].type + "</span>"
74                                                        + "</td>"
75
76                                                        + "</tr>");
77             }
78         } else {
79             $('#selection-form-content').html("<li class='notification empty'>No selections.</li>");
80         }
81
82         var checkboxes = $('.persistent-selection:checkbox');
83         for (i = 0; i < checkboxes.length; i++) {
84             for (var j = 0; j < lst.length; j++) {
85                 if (lst[j].uuid == $(checkboxes[i]).val()) {
86                     checkboxes[i].checked = true;
87                     break;
88                 }
89             }
90             if (j == lst.length) {
91                 checkboxes[i].checked = false;
92             }
93         }
94
95         $('.remove-selection').on('click', remove_selection_click);
96         $('#clear_selections_button').on('click', clear_selections);
97         $(document).trigger('selections-updated', [lst]);
98     };
99
100     $(document).
101         on('change', '.persistent-selection:checkbox', function(e) {
102             var inc = 0;
103             if ($(this).is(":checked")) {
104                 add_selection($(this).val(), $(this).attr('friendly_name'), $(this).attr('href'), $(this).attr('friendly_type'));
105             }
106             else {
107                 remove_selection($(this).val());
108             }
109         });
110
111     $(window).on('load', clear_selections);
112     $(window).on('storage', update_count);
113
114     $('#selection-form-content').on("click", function(e) {
115         e.stopPropagation();
116     });
117 });
118
119 add_form_selection_sources = null;
120 select_form_sources = null;
121
122 (function() {
123     var form_selection_sources = {};
124     add_form_selection_sources = function (src) {
125         for (var i = 0; i < src.length; i++) {
126             var t = form_selection_sources[src[i].type];
127             if (!t) {
128                 t = form_selection_sources[src[i].type] = {};
129             }
130             if (!t[src[i].uuid]) {
131                 t[src[i].uuid] = src[i];
132             }
133         }
134     };
135
136     select_form_sources = function(type) {
137         var ret = [];
138
139         if (get_selection_list) {
140             var lst = get_selection_list();
141             if (lst.length > 0) {
142                 var text = "&horbar; Selections &horbar;";
143                 var span = document.createElement('span');
144                 span.innerHTML = text;
145                 ret.push({text: span.innerHTML, value: "***invalid***"});
146
147                 for (var i = 0; i < lst.length; i++) {
148                     if (lst[i].type == type) {
149                         var n = lst[i].name;
150                         n = n.replace(/<span[^>]*>/i, "[");
151                         n = n.replace(/<\/span>/i, "]");
152                         ret.push({text: n, value: lst[i].uuid})
153                     }
154                 }
155             }
156         }
157
158         var text = "&horbar; Recent &horbar;";
159         var span = document.createElement('span');
160         span.innerHTML = text;
161         ret.push({text: span.innerHTML, value: "***invalid***"});
162
163         var t = form_selection_sources[type];
164         for (var key in t) {
165             if (t.hasOwnProperty(key)) {
166                 var obj = t[key];
167                 ret.push({text: obj.name, value: obj.uuid})
168             }
169         }
170         return ret;
171     };
172 })();
173
174 function dispatch_selection_action() {
175     // Build a new "href" attribute for this link by starting with the
176     // "data-href" attribute and appending ?foo[]=bar&foo[]=baz (or
177     // &foo=... as appropriate) to reflect the current object
178     // selections.
179     var data = [];
180     var param_name = $(this).attr('data-selection-param-name');
181     var href = $(this).attr('data-href');
182     if ($(this).closest('.disabled').length > 0) {
183         return false;
184     }
185     $(this).
186         closest('.selection-action-container').
187         find(':checkbox:checked:visible').
188         each(function() {
189             data.push({name: param_name, value: $(this).val()});
190         });
191     if (href.indexOf('?') >= 0)
192         href += '&';
193     else
194         href += '?';
195     href += $.param(data, true);
196     $(this).attr('href', href);
197     return true;
198 }
199
200 function enable_disable_selection_actions() {
201     var $container = $(this).closest('.selection-action-container');
202     var $checked = $('.persistent-selection:checkbox:checked', $container);
203     $('[data-selection-action]').
204         closest('div.btn-group-sm').
205         find('ul li').
206         toggleClass('disabled', ($checked.length == 0));
207     $('[data-selection-action=compare]').
208         closest('li').
209         toggleClass('disabled',
210                     ($checked.filter('[value*=-d1hrv-]').length < 2) ||
211                     ($checked.not('[value*=-d1hrv-]').length > 0));
212     <% unless Group.copies_to_projects? %>
213         $('[data-selection-action=copy]').
214             closest('li').
215             toggleClass('disabled',
216                         ($checked.filter('[value*=-j7d0g-]').length > 0) ||
217                         ($checked.length < 1));
218     <% end %>
219     $('[data-selection-action=combine-project-contents]').
220         closest('li').
221         toggleClass('disabled',
222                     ($checked.filter('[value*=-4zz18-]').length < 1) ||
223                     ($checked.length != $checked.filter('[value*=-4zz18-]').length));
224 }
225
226 $(document).
227     on('selections-updated ready ajax:complete', function() {
228         var $btn = $('[data-selection-action]');
229         $btn.click(dispatch_selection_action);
230         enable_disable_selection_actions.call($btn);
231     });