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 $('#selection-form-content').html(html);
62 for (var i = 0; i < lst.length; i++) {
63 $('#selection-form-content > li > table').append("<tr>"
65 + "<input class='remove-selection' name='selection[]' type='checkbox' value='" + lst[i].uuid + "' checked='true' data-stoppropagation='true' />"
69 + "<div style='padding-left: 1em'><a href=\"" + lst[i].href + "\">" + lst[i].name + "</a></div>"
72 + "<td style=\"vertical-align: top\">"
73 + "<span style=\"padding-right: 1em\">" + lst[i].type + "</span>"
79 $('#selection-form-content').html("<li class='notification empty'>No selections.</li>");
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;
90 if (j == lst.length) {
91 checkboxes[i].checked = false;
95 $('.remove-selection').on('click', remove_selection_click);
96 $('#clear_selections_button').on('click', clear_selections);
97 $(document).trigger('selections-updated', [lst]);
101 on('change', '.persistent-selection:checkbox', function(e) {
103 if ($(this).is(":checked")) {
104 add_selection($(this).val(), $(this).attr('friendly_name'), $(this).attr('href'), $(this).attr('friendly_type'));
107 remove_selection($(this).val());
111 $(window).on('load storage', update_count);
113 $('#selection-form-content').on("click", function(e) {
118 add_form_selection_sources = null;
119 select_form_sources = null;
122 var form_selection_sources = {};
123 add_form_selection_sources = function (src) {
124 for (var i = 0; i < src.length; i++) {
125 var t = form_selection_sources[src[i].type];
127 t = form_selection_sources[src[i].type] = {};
129 if (!t[src[i].uuid]) {
130 t[src[i].uuid] = src[i];
135 select_form_sources = function(type) {
138 if (get_selection_list) {
139 var lst = get_selection_list();
140 if (lst.length > 0) {
141 var text = "― Selections ―";
142 var span = document.createElement('span');
143 span.innerHTML = text;
144 ret.push({text: span.innerHTML, value: "***invalid***"});
146 for (var i = 0; i < lst.length; i++) {
147 if (lst[i].type == type) {
149 n = n.replace(/<span[^>]*>/i, "[");
150 n = n.replace(/<\/span>/i, "]");
151 ret.push({text: n, value: lst[i].uuid})
157 var text = "― Recent ―";
158 var span = document.createElement('span');
159 span.innerHTML = text;
160 ret.push({text: span.innerHTML, value: "***invalid***"});
162 var t = form_selection_sources[type];
164 if (t.hasOwnProperty(key)) {
166 ret.push({text: obj.name, value: obj.uuid})
173 function dispatch_selection_action() {
174 // Build a new "href" attribute for this link by starting with the
175 // "data-href" attribute and appending ?foo[]=bar&foo[]=baz (or
176 // &foo=... as appropriate) to reflect the current object
179 var param_name = $(this).attr('data-selection-param-name');
180 var href = $(this).attr('data-href');
181 if ($(this).closest('.disabled').length > 0) {
185 closest('.selection-action-container').
186 find(':checkbox:checked').
188 data.push({name: param_name, value: $(this).val()});
190 if (href.indexOf('?') >= 0)
194 href += $.param(data, true);
195 $(this).attr('href', href);
199 function enable_disable_selection_actions() {
200 var $container = $(this).closest('.selection-action-container');
201 var $checked = $('.persistent-selection:checkbox:checked', $container);
202 $('[data-selection-action]').
203 closest('div.btn-group-sm').
205 toggleClass('disabled', ($checked.length == 0));
206 $('[data-selection-action=compare]').
208 toggleClass('disabled',
209 ($checked.filter('[value*=-d1hrv-]').length < 2) ||
210 ($checked.not('[value*=-d1hrv-]').length > 0));
211 <% unless Group.copies_to_projects? %>
212 $('[data-selection-action=copy]').
214 toggleClass('disabled',
215 ($checked.filter('[value*=-j7d0g-]').length > 0) ||
216 ($checked.length < 1));
218 $('[data-selection-action=combine-project-contents]').
220 toggleClass('disabled',
221 ($checked.filter('[value*=-4zz18-]').length < 1) ||
222 ($checked.length != $checked.filter('[value*=-4zz18-]').length));
226 on('selections-updated ready ajax:complete', function() {
227 var $btn = $('[data-selection-action]');
228 $btn.click(dispatch_selection_action);
229 enable_disable_selection_actions.call($btn);