Bug 29940: Phase out jquery.cookie.js in the OPAC

This patch replaces the use of jquery.cookie.js in the OPAC with the
newer js-cookie plugin. The patch adds the latest version of the
js-cookie library and removes the old jquery.cookie plugin.

To test, apply the patch and go to the advanced search page in the OPAC.

- Perform a search using a number of different limits and settings, e.g.
  keyword, item type, and sort-by.
- On the search results page you should see at the top of the results
  list a link to "Return to the last advanced search."
- The link should take you back to the advanced search page with all the
  same settings filled into the form.
- A search with the "More options" view enabled in the advanced search
  form should result in a link which returns to the "More options" view.
- Searching via the "quick search" form at the top of the page should
  clear the cookies which were set in order to remember the advanced
  search. See
  https://developer.mozilla.org/en-US/docs/Tools/Storage_Inspector for
  information on viewing your browser's stored cookies.

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Fridolin Somers <fridolin.somers@biblibre.com>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
This commit is contained in:
Owen Leonard 2022-01-25 12:32:28 +00:00 committed by Kyle Hall
parent 521efb75c3
commit 3cc0dc5b9e
4 changed files with 15 additions and 17 deletions

View File

@ -295,14 +295,14 @@ $.widget.bridge('uitooltip', $.ui.tooltip);
</script>
[% END %]
[% Asset.js("lib/jquery/plugins/jquery.cookie.min.js") | $raw %]
[% Asset.js("lib/js-cookie/js.cookie-3.0.1.min.js") | $raw %]
<script>
$(document).ready(function() {
if($('#searchsubmit').length) {
$(document).on("click", '#searchsubmit', function(e) {
jQuery.removeCookie("form_serialized", { path: '/'});
jQuery.removeCookie("form_serialized_limits", { path: '/'});
jQuery.removeCookie("search_path_code", { path: '/'});
Cookies.remove("form_serialized", { path: '/'});
Cookies.remove("form_serialized_limits", { path: '/'});
Cookies.remove("search_path_code", { path: '/'});
});
}
});

View File

@ -449,7 +449,6 @@
[% BLOCK jsinclude %]
[% Asset.js("lib/jquery/plugins/jquery.deserialize.min.js") | $raw %]
[% Asset.js("lib/jquery/plugins/jquery.cookie.min.js") | $raw %]
<script>
$(document).ready(function() {
[% IF search_groups %]
@ -472,12 +471,11 @@ $(document).ready(function() {
[% END %]
$('#advsearches').tabs();
jQuery.cookie.json = true;
$('.search-term-row .search-term-input select[name="op"]').first().prop("disabled",true).hide();
[% IF ( ReturnToSearch ) %]
if (form_serialized = jQuery.cookie("form_serialized")) {
if (form_serialized = JSON.parse( Cookies.get("form_serialized") ) ) {
// Add missing term inputs if there were more than 3
var count = (form_serialized.match(/&q=/g) || []).length;
for(var i = 3 ; i < count ; i++) {
@ -485,7 +483,7 @@ $(document).ready(function() {
}
$('#advsearch form').deserialize(form_serialized);
}
if (form_serialized_limits = jQuery.cookie("form_serialized_limits")) {
if (form_serialized_limits = JSON.parse( Cookies.get("form_serialized_limits") ) ) {
$('#language-limit') .val(form_serialized_limits[0]);
$('#branchloop') .val(form_serialized_limits[1]);
$('#subtype_audience') .val(form_serialized_limits[2]);
@ -496,9 +494,9 @@ $(document).ready(function() {
}
[% ELSE %]
//Clear all form cookies
jQuery.removeCookie("form_serialized", { path: '/'});
jQuery.removeCookie("form_serialized_limits", { path: '/'});
jQuery.removeCookie("search_path_code", { path: '/'});
Cookies.remove("form_serialized", { path: '/'});
Cookies.remove("form_serialized_limits", { path: '/'});
Cookies.remove("search_path_code", { path: '/'});
[% END %]
$('#advsearch_form').submit(function() {
$('[name^="limit"]').each(function(){
@ -514,18 +512,18 @@ $(document).ready(function() {
}
});
form_serialized = $(this).serialize();
jQuery.cookie("form_serialized", form_serialized,{ path: '/'});
Cookies.set("form_serialized", JSON.stringify( form_serialized ), { path: '/'});
form_serialized_limits = [
$('#language-limit').val(), $('#branchloop').val(),
$('#subtype_audience').val(), $('#subtype_content').val(),
$('#subtype_format').val(), $('#subtype_additional').val(),
$('#locloop').val()
];
jQuery.cookie("form_serialized_limits", form_serialized_limits,{ path: '/'});
Cookies.set("form_serialized_limits", JSON.stringify( form_serialized_limits ),{ path: '/'});
[% IF ( expanded_options ) %]
jQuery.cookie("search_path_code", 'exs',{ path: '/'});
Cookies.set("search_path_code", JSON.stringify('exs'), { path: '/'});
[% ELSE %]
jQuery.cookie("search_path_code", 'ads',{ path: '/'});
Cookies.set("search_path_code", JSON.stringify('ads'), { path: '/'});
[% END %]
});

View File

@ -1,2 +0,0 @@
/*! jquery.cookie v1.4.1 | MIT */
!function(a){"function"==typeof define&&define.amd?define(["jquery"],a):"object"==typeof exports?a(require("jquery")):a(jQuery)}(function(a){function b(a){return h.raw?a:encodeURIComponent(a)}function c(a){return h.raw?a:decodeURIComponent(a)}function d(a){return b(h.json?JSON.stringify(a):String(a))}function e(a){0===a.indexOf('"')&&(a=a.slice(1,-1).replace(/\\"/g,'"').replace(/\\\\/g,"\\"));try{return a=decodeURIComponent(a.replace(g," ")),h.json?JSON.parse(a):a}catch(b){}}function f(b,c){var d=h.raw?b:e(b);return a.isFunction(c)?c(d):d}var g=/\+/g,h=a.cookie=function(e,g,i){if(void 0!==g&&!a.isFunction(g)){if(i=a.extend({},h.defaults,i),"number"==typeof i.expires){var j=i.expires,k=i.expires=new Date;k.setTime(+k+864e5*j)}return document.cookie=[b(e),"=",d(g),i.expires?"; expires="+i.expires.toUTCString():"",i.path?"; path="+i.path:"",i.domain?"; domain="+i.domain:"",i.secure?"; secure":""].join("")}for(var l=e?void 0:{},m=document.cookie?document.cookie.split("; "):[],n=0,o=m.length;o>n;n++){var p=m[n].split("="),q=c(p.shift()),r=p.join("=");if(e&&e===q){l=f(r,g);break}e||void 0===(r=f(r))||(l[q]=r)}return l};h.defaults={},a.removeCookie=function(b,c){return void 0===a.cookie(b)?!1:(a.cookie(b,"",a.extend({},c,{expires:-1})),!a.cookie(b))}});

View File

@ -0,0 +1,2 @@
/*! js-cookie v3.0.1 | MIT */
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e=e||self,function(){var n=e.Cookies,o=e.Cookies=t();o.noConflict=function(){return e.Cookies=n,o}}())}(this,(function(){"use strict";function e(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var o in n)e[o]=n[o]}return e}return function t(n,o){function r(t,r,i){if("undefined"!=typeof document){"number"==typeof(i=e({},o,i)).expires&&(i.expires=new Date(Date.now()+864e5*i.expires)),i.expires&&(i.expires=i.expires.toUTCString()),t=encodeURIComponent(t).replace(/%(2[346B]|5E|60|7C)/g,decodeURIComponent).replace(/[()]/g,escape);var c="";for(var u in i)i[u]&&(c+="; "+u,!0!==i[u]&&(c+="="+i[u].split(";")[0]));return document.cookie=t+"="+n.write(r,t)+c}}return Object.create({set:r,get:function(e){if("undefined"!=typeof document&&(!arguments.length||e)){for(var t=document.cookie?document.cookie.split("; "):[],o={},r=0;r<t.length;r++){var i=t[r].split("="),c=i.slice(1).join("=");try{var u=decodeURIComponent(i[0]);if(o[u]=n.read(c,u),e===u)break}catch(e){}}return e?o[e]:o}},remove:function(t,n){r(t,"",e({},n,{expires:-1}))},withAttributes:function(n){return t(this.converter,e({},this.attributes,n))},withConverter:function(n){return t(e({},this.converter,n),this.attributes)}},{attributes:{value:Object.freeze(o)},converter:{value:Object.freeze(n)}})}({read:function(e){return'"'===e[0]&&(e=e.slice(1,-1)),e.replace(/(%[\dA-F]{2})+/gi,decodeURIComponent)},write:function(e){return encodeURIComponent(e).replace(/%(2[346BF]|3[AC-F]|40|5[BDE]|60|7[BCD])/g,decodeURIComponent)}},{path:"/"})}));