add AJAX cart clearing action
authorGalen Charlton <gmc@equinoxinitiative.org>
Fri, 15 Jun 2018 17:06:41 +0000 (13:06 -0400)
committerGalen Charlton <gmc@equinoxinitiative.org>
Fri, 15 Jun 2018 17:06:41 +0000 (13:06 -0400)
Signed-off-by: Galen Charlton <gmc@equinoxinitiative.org>
Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader.pm
Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader/Container.pm
Open-ILS/src/templates/opac/css/style.css.tt2
Open-ILS/src/templates/opac/parts/result/table.tt2
Open-ILS/web/js/ui/default/opac/record_selectors.js

index 09afe3c..eeeba4a 100644 (file)
@@ -136,6 +136,7 @@ sub load {
     return $self->load_api_mylist_retrieve if $path =~ m|opac/api/mylist/retrieve|;
     return $self->load_api_mylist_add if $path =~ m|opac/api/mylist/add|;
     return $self->load_api_mylist_delete if $path =~ m|opac/api/mylist/delete|;
+    return $self->load_api_mylist_clear if $path =~ m|opac/api/mylist/clear|;
 
     return $self->load_simple("home") if $path =~ m|opac/home|;
     return $self->load_simple("css") if $path =~ m|opac/css|;
index d17c0e2..aba9d85 100644 (file)
@@ -93,6 +93,14 @@ sub load_api_mylist_retrieve {
     return Apache2::Const::OK;
 }
 
+sub load_api_mylist_clear {
+    my $self = shift;
+
+    $self->clear_anon_cache;
+
+    # and return fresh, empty cart
+    return $self->load_api_mylist_retrieve();
+}
 
 # Adds a record (by id) to My List, creating a new anon cache + list if necessary.
 sub load_mylist_add {
index 565022f..d8780fa 100644 (file)
@@ -1034,7 +1034,7 @@ tr.result_table_row > td.result_table_pic_header {
 .result_table_row_selected {
     background-color: [% css_colors.item_selected %];
 }
-#selected_records_summary, #clear_selected_records {
+#selected_records_summary, #clear_cart {
     margin-left: 5em;
 }
 
index efbffdf..e3a9497 100644 (file)
@@ -45,7 +45,7 @@
                         </a>
                         <span id="hit_selected_record_limit" class="hidden">Reached limit!</span>
                     <span>
-                    <a id="clear_selected_records" href="#">[% l('Clear selections') %]</a>
+                    <a id="clear_cart" href="#">[% l('Clear cart') %]</a>
                 </div>
                 [% END %]
                 <table id="result_table_table" title="[% l('Search Results') %]"
index 1aa0418..0faa7fc 100644 (file)
@@ -5,7 +5,7 @@
     var record_cart_count_el = document.getElementById('record_cart_count');
     var selected_records_count_el = document.getElementById('selected_records_count');
     var select_all_records_el = document.getElementById('select_all_records');
-    var clear_selected_records_el = document.getElementById('clear_selected_records');
+    var clear_cart_el = document.getElementById('clear_cart');
     var mylist = [];
 
     function initialize() {
@@ -39,6 +39,7 @@
             }
             toggleRowHighlighting(el);
         });
+        checkMaxCartSize();
     }
 
     function handleUpdate(result) {
     }
 
     function checkMaxCartSize() {
-        if ((typeof max_cart_size === 'undefined') || !max_cart_size || !mylist.length) return;
+        if ((typeof max_cart_size === 'undefined') || !max_cart_size) return;
         var alertel = document.getElementById('hit_selected_record_limit');
         [].forEach.call(rec_selectors, function(el) {
             if (!el.checked) el.disabled = (mylist.length >= max_cart_size);
     if (rec_selector_block) rec_selector_block.classList.remove("hidden");
 
     function deselectSelectedOnPage() {
-        var to_del = [];
         [].forEach.call(rec_selectors, function(el) {
             if (el.checked) {
                 el.checked = false;
                 adjustLegacyControlsVis('delete', el.value);
                 toggleRowHighlighting(el);
-                to_del.push(el.value);
             }
         });
-        if (to_del.length > 0) {
-            mungeList('delete', to_del);
-        }
     }
 
     if (select_all_records_el) {
         });
     }
 
-    if (clear_selected_records_el) {
-        clear_selected_records_el.addEventListener('click', function() {
-            deselectSelectedOnPage();
-            if (select_all_records_el) select_all_records_el.checked = false;
+    function clearCart() {
+        var req = new window.XMLHttpRequest();
+        req.open('GET', '/eg/opac/api/mylist/clear');
+        if (('responseType' in req) && (req.responseType = 'json')) {
+            req.onload = function (evt) {
+                var result = req.response;
+                handleUpdate(result);
+                syncPageState();
+            }
+        } else {
+            // IE 10/11
+            req.onload = function (evt) {
+                var result = JSON.parse(req.responseText);
+                handleUpdate(result);
+                syncPageState();
+            }
+        }
+        req.send();
+    }
+
+    if (clear_cart_el) {
+        clear_cart_el.addEventListener('click', function() {
+            clearCart();
         });
     }