- var storage = localStorage; // sessionStorage
-
- get_selection_list = function() {
- if (!storage.persistentSelection) {
- storage.persistentSelection = JSON.stringify([]);
- }
- return JSON.parse(storage.persistentSelection);
- }
-
- var put_storage = function(lst) {
- storage.persistentSelection = JSON.stringify(lst);
- }
-
- var add_selection = function(uuid, name, href, type) {
- var lst = get_selection_list();
- lst.push({"uuid": uuid, "name": name, "href": href, "type": type});
- put_storage(lst);
- update_count();
- };
-
- var remove_selection = function(uuid) {
- var lst = get_selection_list();
- for (var i = 0; i < lst.length; i++) {
- if (lst[i].uuid == uuid) {
- lst.splice(i, 1);
- i--;
- }
- }
- put_storage(lst);
- update_count();
- };
-
- var remove_selection_click = function(e) {
- remove_selection($(this).val());
- };
-
- var clear_selections = function() {
- put_storage([]);
- update_count();
- }
-
- var update_count = function(e) {
- var html;
- var this_object_uuid = $('#selection-form-content').
- closest('form').
- find('input[name=uuid]').val();
- var lst = get_selection_list();
- $("#persistent-selection-count").text(lst.length);
- if (lst.length > 0) {
- 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>';
- $('#selection-form-content').html(html);
-
- for (var i = 0; i < lst.length; i++) {
- $('#selection-form-content > li > table').append("<tr>"
- + "<td>"
- + "<input class='remove-selection' name='selection[]' type='checkbox' value='" + lst[i].uuid + "' checked='true' data-stoppropagation='true' />"
- + "</td>"
-
- + "<td>"
- + "<div style='padding-left: 1em'><a href=\"" + lst[i].href + "\">" + lst[i].name + "</a></div>"
- + "</td>"
-
- + "<td style=\"vertical-align: top\">"
- + "<span style=\"padding-right: 1em\">" + lst[i].type + "</span>"
- + "</td>"
-
- + "</tr>");
- }
- } else {
- $('#selection-form-content').html("<li class='notification empty'>No selections.</li>");
- }
-
- var checkboxes = $('.persistent-selection:checkbox');
- for (i = 0; i < checkboxes.length; i++) {
- for (var j = 0; j < lst.length; j++) {
- if (lst[j].uuid == $(checkboxes[i]).val()) {
- checkboxes[i].checked = true;
- break;
- }
- }
- if (j == lst.length) {
- checkboxes[i].checked = false;
- }
- }
-
- $('.remove-selection').on('click', remove_selection_click);
- $('#clear_selections_button').on('click', clear_selections);
- $(document).trigger('selections-updated', [lst]);
- };
-