Bug 11369: (follow-up) Correct usage of me.searchid in browser.js
[koha.git] / koha-tmpl / intranet-tmpl / js / browser.js
1 if ( KOHA === undefined ) var KOHA = {};
2
3 KOHA.browser = function (searchid, biblionumber) {
4     var me = this;
5     this.searchid = searchid;
6
7     if (me.searchid) {
8         var cookie = $.cookie(me.searchid)
9         if (cookie) {
10             me.searchCookie = JSON.parse(cookie);
11         }
12     }
13
14     var browseRecords = function (movement) {
15         var newSearchPos = me.curPos + movement;
16         if (newSearchPos > me.searchCookie.results.length - 1) {
17             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;
18         } else if (newSearchPos < 0) {
19             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);
20         } else {
21             window.location = window.location.href.replace('biblionumber=' + biblionumber, 'biblionumber=' + me.searchCookie.results[newSearchPos]);
22         }
23     }
24
25     this.create = function (offset, query, limit, sort, newresults) {
26         if (me.searchCookie) {
27             if (offset === me.searchCookie.offset - newresults.length) {
28                 me.searchCookie.results = newresults.concat(me.searchCookie.results);
29             } else if (searchOffset = me.searchCookie.offset + newresults.length) {
30                 me.searchCookie.results = me.searchCookie.results.concat(newresults);
31             } else {
32                 delete me.searchCookie;
33             }
34         }
35         if (!me.searchCookie) {
36             me.searchCookie = { offset: offset,
37                 query: query,
38                 limit: limit,
39                 sort:  sort,
40                 pagelen: newresults.length,
41                 results: newresults
42             };
43
44             //Bug_11369 Cleaning up excess searchCookies to prevent cookie overflow in the browser memory.
45             var allVisibleCookieKeys = Object.keys( $.cookie() );
46             var scsCookieKeys = $.grep( allVisibleCookieKeys,
47                 function(elementOfArray, indexInArray) {
48                     return ( elementOfArray.search(/^scs_\d/) != -1 ); //We are looking for specifically staff client searchCookies.
49                 }
50             );
51             if (scsCookieKeys.length >= 10) {
52                 scsCookieKeys.sort(); //Make sure they are in order, oldest first!
53                 $.removeCookie( scsCookieKeys[0], { path: '/' } );
54             }
55             //EO Bug_11369
56         }
57         $.cookie(me.searchid, JSON.stringify(me.searchCookie), { path: '/' });
58         $(document).ready(function () {
59             $('#searchresults table tr a[href*="detail.pl"]').click(function (ev) {
60                 ev.preventDefault();
61                 window.location = $(this).attr('href') + '&searchid=' + me.searchid;
62             });
63         });
64     };
65
66     this.show = function () {
67         if (me.searchCookie) {
68             me.curPos = $.inArray(biblionumber, me.searchCookie.results);
69             me.offset = Math.floor((me.searchCookie.offset + me.curPos - 1) / me.searchCookie.pagelen) * me.searchCookie.pagelen;
70
71             $(document).ready(function () {
72                 if (me.curPos > -1) {
73                     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;
74                     var prevbutton;
75                     var nextbutton;
76                     if (me.curPos === 0 && me.searchCookie.offset === 1) {
77                         prevbutton = '<span id="browse-previous" class="browse-button">« ' + BROWSER_PREVIOUS + '</span>';
78                     } else {
79                         prevbutton = '<a href="#" id="browse-previous" class="browse-button">« ' + BROWSER_PREVIOUS + '</a>';
80                     }
81                     if (me.curPos === me.searchCookie.results.length && me.searchCookie.total == me.curPos + me.searchCookie.offset) {
82                         nextbutton = '<span id="browse-next" class="browse-button">' + BROWSER_NEXT + ' »</span>';
83                     } else {
84                         nextbutton = '<a href="#" id="browse-next" class="browse-button">' + BROWSER_NEXT + ' »</a>';
85                     }
86                     $('#menu').before('<div class="browse-controls"><div class="browse-controls-inner"><div class="browse-label"><a href="' + searchURL + '" id="browse-return-to-results" class="browse-button">' + BROWSER_RETURN_TO_SEARCH + '</a></div><div class="browse-prev-next">' + prevbutton + nextbutton + '</div></div></div>');
87                     $('a#browse-previous').click(function (ev) {
88                         ev.preventDefault();
89                         browseRecords(-1);
90                     });
91                     $('a#browse-next').click(function (ev) {
92                         ev.preventDefault();
93                         browseRecords(1);
94                     });
95                     $('a[href*="biblionumber="]').click(function (ev) {
96                         ev.preventDefault();
97                         window.location = $(this).attr('href') + '&searchid=' + me.searchid;
98                     });
99                     $('form[name="f"]').append('<input type="hidden" name="searchid" value="' + me.searchid + '"></input>');
100                 }
101             });
102         }
103     };
104
105     return me;
106 };