From 4e8b758c640c3d2b09e4c79a27d1bb68bcaa8560 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Fri, 22 May 2015 13:11:19 +0200 Subject: [PATCH] Bug 13265: Use sessionStorage to store searches instead of cookies This is a counter patch. The idea is to provide a permanent solution for the cookie length issue we occurred on storing the searches (intranet side). Test plan: Launch as many searches as you can (don't forget to sleep). You should not get any error. Confirm there is no regression using the results browser. --- koha-tmpl/intranet-tmpl/js/browser.js | 80 ++++++++++++++++++----------------- 1 file changed, 41 insertions(+), 39 deletions(-) diff --git a/koha-tmpl/intranet-tmpl/js/browser.js b/koha-tmpl/intranet-tmpl/js/browser.js index 824e409..9f5a1ac 100644 --- a/koha-tmpl/intranet-tmpl/js/browser.js +++ b/koha-tmpl/intranet-tmpl/js/browser.js @@ -7,58 +7,60 @@ KOHA.browser = function (searchid, biblionumber) { // We are generating a clean numeric datetime representation so we can easily compare them using the default javascript lexigraphic sorter. searchid = 'scs_' + (new Date()).getTime(); // scs for Staff Client Search } - this.searchid = searchid; + me.searchid = searchid; - var cookie = $.cookie(me.searchid) - if (cookie) { - me.searchCookie = JSON.parse(cookie); + var searches_stored = sessionStorage.getItem('searches'); + var current_search; + var searches = {}; + if ( searches_stored ) { + searches = JSON.parse(searches_stored); + current_search = searches[me.searchid]; + + // Remove old entries + var searchids = Object.keys(searches); + var nb_searches = searchids.length; + if ( nb_searches > 20 ) { // No need to keep more than 20 searches + searchids = searchids.sort(); + for ( var i = 0 ; i < nb_searches - 20 ; i++ ) { + delete searches[searchids[i]]; + } + } } var browseRecords = function (movement) { var newSearchPos = me.curPos + movement; - if (newSearchPos > me.searchCookie.results.length - 1) { - window.location = '/cgi-bin/koha/catalogue/search.pl?' + decodeURIComponent(me.searchCookie.query) + '&limit=' + decodeURIComponent(me.searchCookie.limit) + '&sort=' + me.searchCookie.sort + '&gotoPage=detail.pl&gotoNumber=first&searchid=' + me.searchid + '&offset=' + newSearchPos; + if (newSearchPos > current_search.results.length - 1) { + window.location = '/cgi-bin/koha/catalogue/search.pl?' + decodeURIComponent(current_search.query) + '&limit=' + decodeURIComponent(current_search.limit) + '&sort=' + current_search.sort + '&gotoPage=detail.pl&gotoNumber=first&searchid=' + me.searchid + '&offset=' + newSearchPos; } else if (newSearchPos < 0) { - window.location = '/cgi-bin/koha/catalogue/search.pl?' + decodeURIComponent(me.searchCookie.query) + '&limit=' + decodeURIComponent(me.searchCookie.limit) + '&sort=' + me.searchCookie.sort + '&gotoPage=detail.pl&gotoNumber=last&searchid=' + me.searchid + '&offset=' + (me.offset - me.searchCookie.pagelen); + window.location = '/cgi-bin/koha/catalogue/search.pl?' + decodeURIComponent(current_search.query) + '&limit=' + decodeURIComponent(current_search.limit) + '&sort=' + current_search.sort + '&gotoPage=detail.pl&gotoNumber=last&searchid=' + me.searchid + '&offset=' + (me.offset - current_search.pagelen); } else { - window.location = window.location.href.replace('biblionumber=' + biblionumber, 'biblionumber=' + me.searchCookie.results[newSearchPos]); + window.location = window.location.href.replace('biblionumber=' + biblionumber, 'biblionumber=' + current_search.results[newSearchPos]); } } - this.create = function (offset, query, limit, sort, newresults, total) { - if (me.searchCookie) { - if (offset === me.searchCookie.offset - newresults.length) { - me.searchCookie.results = newresults.concat(me.searchCookie.results); - } else if (searchOffset = me.searchCookie.offset + newresults.length) { - me.searchCookie.results = me.searchCookie.results.concat(newresults); + me.create = function (offset, query, limit, sort, newresults, total) { + if (current_search) { + if (offset === current_search.offset - newresults.length) { + current_search.results = newresults.concat(current_search.results); + } else if (searchOffset = current_search.offset + newresults.length) { + current_search.results = current_search.results.concat(newresults); } else { - delete me.searchCookie; + delete current_search; } } - if (!me.searchCookie) { - me.searchCookie = { offset: offset, + if (!current_search) { + current_search = { offset: offset, query: query, limit: limit, sort: sort, pagelen: newresults.length, results: newresults, - total: total + total: total, + searchid: searchid }; - - //Bug_11369 Cleaning up excess searchCookies to prevent cookie overflow in the browser memory. - var allVisibleCookieKeys = Object.keys( $.cookie() ); - var scsCookieKeys = $.grep( allVisibleCookieKeys, - function(elementOfArray, indexInArray) { - return ( elementOfArray.search(/^scs_\d/) != -1 ); //We are looking for specifically staff client searchCookies. - } - ); - if (scsCookieKeys.length >= 10) { - scsCookieKeys.sort(); //Make sure they are in order, oldest first! - $.removeCookie( scsCookieKeys[0], { path: '/' } ); - } - //EO Bug_11369 } - $.cookie(me.searchid, JSON.stringify(me.searchCookie), { path: '/' }); + searches[me.searchid] = current_search; + sessionStorage.setItem('searches', JSON.stringify(searches)); $(document).ready(function () { //FIXME It's not a good idea to modify the click events $('#searchresults table tr a[href*="detail.pl"]').on('click', function (ev) { @@ -80,22 +82,22 @@ KOHA.browser = function (searchid, biblionumber) { }); }; - this.show = function () { - if (me.searchCookie) { - me.curPos = $.inArray(biblionumber, me.searchCookie.results); - me.offset = Math.floor((me.searchCookie.offset + me.curPos - 1) / me.searchCookie.pagelen) * me.searchCookie.pagelen; + me.show = function () { + if (current_search) { + me.curPos = $.inArray(biblionumber, current_search.results); + me.offset = Math.floor((current_search.offset + me.curPos - 1) / current_search.pagelen) * current_search.pagelen; $(document).ready(function () { if (me.curPos > -1) { - var searchURL = '/cgi-bin/koha/catalogue/search.pl?' + decodeURIComponent(me.searchCookie.query) + '&limit=' + decodeURIComponent(me.searchCookie.limit) + '&sort=' + me.searchCookie.sort + '&searchid=' + me.searchid + '&offset=' + me.offset; + var searchURL = '/cgi-bin/koha/catalogue/search.pl?' + decodeURIComponent(current_search.query) + '&limit=' + decodeURIComponent(current_search.limit) + '&sort=' + current_search.sort + '&searchid=' + me.searchid + '&offset=' + me.offset; var prevbutton; var nextbutton; - if (me.curPos === 0 && me.searchCookie.offset === 1) { + if (me.curPos === 0 && current_search.offset === 1) { prevbutton = '« ' + BROWSER_PREVIOUS + ''; } else { prevbutton = '« ' + BROWSER_PREVIOUS + ''; } - if (me.searchCookie.offset + me.curPos == me.searchCookie.total) { + if (current_search.offset + me.curPos == current_search.total) { nextbutton = '' + BROWSER_NEXT + ' »'; } else { nextbutton = '' + BROWSER_NEXT + ' »'; -- 2.1.0