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).attr('name'));
44 remove_selection($(this).val());
47 var clear_selections = function() {
52 var update_count = function(e) {
53 var lst = get_selection_list();
54 $("#persistent-selection-count").text(lst.length);
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>"
63 + "<input class='remove-selection' type='checkbox' value='" + lst[i].uuid + "' checked='true'></input>"
68 + "<span style='padding-left: 1em'><a href=\"" + lst[i].href + "\">" + lst[i].name + "</a></span>"
71 + "<td style=\"vertical-align: top\">"
72 + "<span style=\"padding-right: 1em\">" + lst[i].type + "</span>"
78 $('#persistent-selection-list').html("<li class='notification empty'>No selections.</li>");
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;
89 if (j == lst.length) {
90 checkboxes[i].checked = false;
94 $('.remove-selection').on('click', remove_selection_click);
95 $('#clear_selections_button').on('click', clear_selections);
99 on('change', '.persistent-selection:checkbox', function(e) {
100 //console.log($(this));
101 //console.log($(this).val());
104 if ($(this).is(":checked")) {
105 add_selection($(this).val(), $(this).attr('friendly_name'), $(this).attr('href'), $(this).attr('friendly_type'));
108 remove_selection($(this).val());
113 $(window).on('load storage', update_count);
116 add_form_selection_sources = null;
117 select_form_sources = null;
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];
125 t = form_selection_sources[src[i].type] = {};
127 if (!t[src[i].uuid]) {
128 t[src[i].uuid] = src[i];
133 select_form_sources = function(type) {
136 if (get_selection_list) {
137 var lst = get_selection_list();
138 if (lst.length > 0) {
139 ret.push({text: "--- Selections ---", value: ""});
141 for (var i = 0; i < lst.length; i++) {
142 if (lst[i].type == type) {
143 ret.push({text: lst[i].name, value: lst[i].uuid})
148 ret.push({text: "--- Recent ---", value: ""});
150 var t = form_selection_sources[type];
152 if (t.hasOwnProperty(key)) {
154 ret.push({text: obj.name, value: obj.uuid})