Bug 11629 - Add ability to update not for loan status on checkin
[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) {
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             };
46
47             //Bug_11369 Cleaning up excess searchCookies to prevent cookie overflow in the browser memory.
48             var allVisibleCookieKeys = Object.keys( $.cookie() );
49             var scsCookieKeys = $.grep( allVisibleCookieKeys,
50                 function(elementOfArray, indexInArray) {
51                     return ( elementOfArray.search(/^scs_\d/) != -1 ); //We are looking for specifically staff client searchCookies.
52                 }
53             );
54             if (scsCookieKeys.length >= 10) {
55                 scsCookieKeys.sort(); //Make sure they are in order, oldest first!
56                 $.removeCookie( scsCookieKeys[0], { path: '/' } );
57             }
58             //EO Bug_11369
59         }
60         $.cookie(me.searchid, JSON.stringify(me.searchCookie), { path: '/' });
61         $(document).ready(function () {
62             $('#searchresults table tr a[href*="detail.pl"]').click(function (ev) {
63                 ev.preventDefault();
64                 window.location = $(this).attr('href') + '&searchid=' + me.searchid;
65             });
66         });
67     };
68
69     this.show = function () {
70         if (me.searchCookie) {
71             me.curPos = $.inArray(biblionumber, me.searchCookie.results);
72             me.offset = Math.floor((me.searchCookie.offset + me.curPos - 1) / me.searchCookie.pagelen) * me.searchCookie.pagelen;
73
74             $(document).ready(function () {
75                 if (me.curPos > -1) {
76                     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;
77                     var prevbutton;
78                     var nextbutton;
79                     if (me.curPos === 0 && me.searchCookie.offset === 1) {
80                         prevbutton = '<span id="browse-previous" class="browse-button">« ' + BROWSER_PREVIOUS + '</span>';
81                     } else {
82                         prevbutton = '<a href="#" id="browse-previous" class="browse-button">« ' + BROWSER_PREVIOUS + '</a>';
83                     }
84                     if (me.curPos === me.searchCookie.results.length && me.searchCookie.total == me.curPos + me.searchCookie.offset) {
85                         nextbutton = '<span id="browse-next" class="browse-button">' + BROWSER_NEXT + ' »</span>';
86                     } else {
87                         nextbutton = '<a href="#" id="browse-next" class="browse-button">' + BROWSER_NEXT + ' »</a>';
88                     }
89                     $('#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>');
90                     $('a#browse-previous').click(function (ev) {
91                         ev.preventDefault();
92                         browseRecords(-1);
93                     });
94                     $('a#browse-next').click(function (ev) {
95                         ev.preventDefault();
96                         browseRecords(1);
97                     });
98                     $('a[href*="biblionumber="]').click(function (ev) {
99                         ev.preventDefault();
100                         window.location = $(this).attr('href') + '&searchid=' + me.searchid;
101                     });
102                     $('form[name="f"]').append('<input type="hidden" name="searchid" value="' + me.searchid + '"></input>');
103                 }
104             });
105         }
106     };
107
108     return me;
109 };