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