CAT-200 Remember last search template used
authorKatlyn Beck <kbeck@catalyte.io>
Wed, 30 Jan 2019 18:11:47 +0000 (18:11 +0000)
committerBill Erickson <berickxx@gmail.com>
Thu, 21 Mar 2019 19:46:23 +0000 (15:46 -0400)
- When selected, the template name is saved to localStorage.
- When the page is loaded or refreshed, the template is retrieved
and populated from localStorage based on stored name.

Signed-off-by: Katlyn Beck <kbeck@catalyte.io>
 Changes to be committed:
modified:   Open-ILS/web/js/ui/kcls/conify/global/config/search_templates.js

Open-ILS/web/js/ui/kcls/conify/global/config/search_templates.js

index 99e1a23..6a2ca43 100644 (file)
@@ -46,6 +46,7 @@ function getXULSearchStash() {
 function templateActionSave() {
     var nameInput = document.getElementById('nameInput').value;
     create_template(nameInput);
+    templateSelected();
 }
 
 // Removes template from templateList and localStorage if it exists and if user wants it deleted
@@ -300,6 +301,46 @@ function clearSearchFilters() {
     }
 }
 
+// Adds "selected" attribute to chosen template from list
+function templateSelected() {
+    var templateSelector = document.getElementById('templateList');
+    var templateOptions = templateSelector.getElementsByTagName('option');
+    for (var i = 0; i < templateOptions.length; i++) {
+        if (!templateOptions[i].selected) {
+            templateOptions[i].removeAttribute('selected');
+        }
+        if (templateOptions[i].value == templateSelector.value) {
+            templateOptions[i].setAttribute('selected', '');
+            var selectedTemplate = templateOptions[i].value;
+            window.localStorage.setItem('eg.catalog.staff.search_templates.last_used', selectedTemplate);
+        }
+    }
+}
+
+// Retrieves and populates selected template from localStorage
+function retrieveSelectedTemplate() {
+    var selectedTemplate = window.localStorage.getItem('eg.catalog.staff.search_templates.last_used');
+    var templateList = JSON.parse(window.localStorage.getItem('eg.catalog.staff.search_templates'));
+    var templateSelector = document.getElementById('templateList');
+    var templateOptions = templateSelector.getElementsByTagName('option');
+
+    for (var i = 0; i < templateList.length; i++) {
+        if (templateList[i].name == selectedTemplate) {
+            populateSearchOptions(templateList[i]);
+        }
+    }
+    // ensures template stays selected on reload
+    for (var j = 0; j < templateOptions.length; j++) {
+        if (templateOptions[j].value == selectedTemplate) {
+            templateOptions[j].setAttribute('selected', '');
+        }
+    }
+}
+
+window.onload = function () {
+    retrieveSelectedTemplate();
+}
+
 //Given a template name, saves template as the current search template if name found in TemplateList
 //Populates options, or not, accordingly
 function selectSearchTemplateOrClear(tName) {
@@ -320,6 +361,7 @@ function selectSearchTemplateOrClear(tName) {
 function selectSearchTemplateOrClearAll(tName)  {
     selectSearchTemplateOrClear(tName);
     populateTemplateNameValue(tName);
+    templateSelected();
     clearGlobalRowInputs();
     clearPubDateInputs();
 }