4 /** Javascript for local persistent selection. */
6 get_selection_list = null;
7 form_selection_sources = {};
10 var storage = localStorage; // sessionStorage
12 get_selection_list = function() {
13 if (!storage.persistentSelection) {
14 storage.persistentSelection = JSON.stringify([]);
16 return JSON.parse(storage.persistentSelection);
19 var put_storage = function(lst) {
20 storage.persistentSelection = JSON.stringify(lst);
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});
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) {
42 var remove_selection_click = function(e) {
43 remove_selection($(this).val());
46 var clear_selections = function() {
51 var update_count = function(e) {
53 var this_object_uuid = $('#selection-form-content').
55 find('input[name=uuid]').val();
56 var lst = get_selection_list();
57 $("#persistent-selection-count").text(lst.length);
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 html += '<li><button class="btn btn-xs btn-info" type="submit" name="combine_selected_files_into_collection" '
61 + ' id="combine_selected_files_into_collection">'
62 + '<i class="fa fa-fw fa-archive"></i> Combine selected collections and files into a new collection</button></li>'
63 + '<li class="notification"><table style="width: 100%"></table></li>';
64 $('#selection-form-content').html(html);
66 for (var i = 0; i < lst.length; i++) {
67 $('#selection-form-content > li > table').append("<tr>"
69 + "<input class='remove-selection' name='selection[]' type='checkbox' value='" + lst[i].uuid + "' checked='true' data-stoppropagation='true' />"
73 + "<div style='padding-left: 1em'><a href=\"" + lst[i].href + "\">" + lst[i].name + "</a></div>"
76 + "<td style=\"vertical-align: top\">"
77 + "<span style=\"padding-right: 1em\">" + lst[i].type + "</span>"
83 $('#selection-form-content').html("<li class='notification empty'>No selections.</li>");
86 var checkboxes = $('.persistent-selection:checkbox');
87 for (i = 0; i < checkboxes.length; i++) {
88 for (var j = 0; j < lst.length; j++) {
89 if (lst[j].uuid == $(checkboxes[i]).val()) {
90 checkboxes[i].checked = true;
94 if (j == lst.length) {
95 checkboxes[i].checked = false;
99 $('.remove-selection').on('click', remove_selection_click);
100 $('#clear_selections_button').on('click', clear_selections);
101 $(document).trigger('selections-updated', [lst]);
105 on('change', '.persistent-selection:checkbox', function(e) {
107 if ($(this).is(":checked")) {
108 add_selection($(this).val(), $(this).attr('friendly_name'), $(this).attr('href'), $(this).attr('friendly_type'));
111 remove_selection($(this).val());
115 $(window).on('load storage', update_count);
117 $('#selection-form-content').on("click", function(e) {
122 add_form_selection_sources = null;
123 select_form_sources = null;
126 var form_selection_sources = {};
127 add_form_selection_sources = function (src) {
128 for (var i = 0; i < src.length; i++) {
129 var t = form_selection_sources[src[i].type];
131 t = form_selection_sources[src[i].type] = {};
133 if (!t[src[i].uuid]) {
134 t[src[i].uuid] = src[i];
139 select_form_sources = function(type) {
142 if (get_selection_list) {
143 var lst = get_selection_list();
144 if (lst.length > 0) {
145 var text = "― Selections ―";
146 var span = document.createElement('span');
147 span.innerHTML = text;
148 ret.push({text: span.innerHTML, value: "***invalid***"});
150 for (var i = 0; i < lst.length; i++) {
151 if (lst[i].type == type) {
153 n = n.replace(/<span[^>]*>/i, "[");
154 n = n.replace(/<\/span>/i, "]");
155 ret.push({text: n, value: lst[i].uuid})
161 var text = "― Recent ―";
162 var span = document.createElement('span');
163 span.innerHTML = text;
164 ret.push({text: span.innerHTML, value: "***invalid***"});
166 var t = form_selection_sources[type];
168 if (t.hasOwnProperty(key)) {
170 ret.push({text: obj.name, value: obj.uuid})
177 function dispatch_selection_action() {
178 // Build a new "href" attribute for this link by starting with the
179 // "data-href" attribute and appending ?foo[]=bar&foo[]=baz (or
180 // &foo=... as appropriate) to reflect the current object
183 var param_name = $(this).attr('data-selection-param-name');
184 var href = $(this).attr('data-href');
185 if ($(this).closest('.disabled').length > 0) {
189 closest('.selection-action-container').
190 find(':checkbox:checked').
192 data.push({name: param_name, value: $(this).val()});
194 if (href.indexOf('?') >= 0)
198 href += $.param(data, true);
199 $(this).attr('href', href);
203 function enable_disable_selection_actions() {
204 var $container = $(this).closest('.selection-action-container');
205 var $checked = $('.persistent-selection:checkbox:checked', $container);
206 $('[data-selection-action]').
207 closest('div.btn-group-sm').
209 toggleClass('disabled', ($checked.length == 0));
210 $('[data-selection-action=compare]').
212 toggleClass('disabled',
213 ($checked.filter('[value*=-d1hrv-]').length < 2) ||
214 ($checked.not('[value*=-d1hrv-]').length > 0));
215 $('[data-selection-action=copy]').
217 toggleClass('disabled',
218 ($checked.filter('[value*=-j7d0g-]').length > 0) ||
219 ($checked.length < 1));
220 $('[data-selection-action=combine]').
222 toggleClass('disabled',
223 ($checked.filter('[value*=-4zz18-]').length < 1) ||
224 ($checked.length != $checked.filter('[value*=-4zz18-]').length));
228 on('selections-updated ready ajax:complete', function() {
229 var $btn = $('[data-selection-action]');
230 $btn.click(dispatch_selection_action);
231 enable_disable_selection_actions.call($btn);