Bug 25031: Improve handling of multiple covers on the biblio detail page in the staff...
[koha.git] / koha-tmpl / intranet-tmpl / js / browser.js
1 /* global BROWSER_PREVIOUS BROWSER_NEXT BROWSER_RETURN_TO_SEARCH */
2
3 if ( KOHA === undefined ) var KOHA = {};
4
5 KOHA.browser = function (searchid, biblionumber) {
6     var me = this;
7
8     if (!searchid) {
9         // We are generating a clean numeric datetime representation so we can easily compare them using the default javascript lexigraphic sorter.
10         searchid = 'scs_' + (new Date()).getTime(); // scs for Staff Client Search
11     }
12     me.searchid = searchid;
13
14     var searches_stored = localStorage.getItem('searches');
15     var current_search;
16     var searches = {};
17     if ( searches_stored ) {
18         searches = JSON.parse(searches_stored);
19         current_search = searches[me.searchid];
20
21         // Remove old entries
22         var searchids = Object.keys(searches);
23         var nb_searches = searchids.length;
24         if ( nb_searches > 20 ) { // No need to keep more than 20 searches
25             searchids = searchids.sort();
26             for ( var i = 0 ; i < nb_searches - 20 ; i++ ) {
27                 delete searches[searchids[i]];
28             }
29         }
30     }
31
32     var browseRecords = function (movement) {
33         var newSearchPos = me.curPos + movement;
34         if (newSearchPos > current_search.results.length - 1) {
35             window.location = '/cgi-bin/koha/catalogue/search.pl?' + decodeURIComponent(current_search.query) + '&limit=' + decodeURIComponent(current_search.limit) + '&sort_by=' + current_search.sort + '&gotoPage=detail.pl&gotoNumber=first&searchid=' + me.searchid + '&offset=' + newSearchPos;
36         } else if (newSearchPos < 0) {
37             window.location = '/cgi-bin/koha/catalogue/search.pl?' + decodeURIComponent(current_search.query) + '&limit=' + decodeURIComponent(current_search.limit) + '&sort_by=' + current_search.sort + '&gotoPage=detail.pl&gotoNumber=last&searchid=' + me.searchid + '&offset=' + (me.offset - current_search.pagelen);
38         } else {
39             window.location = window.location.href.replace('biblionumber=' + biblionumber, 'biblionumber=' + current_search.results[newSearchPos]);
40         }
41     }
42
43     me.create = function (offset, query, limit, sort, newresults, total) {
44         if (current_search) {
45             if (offset === current_search.offset - newresults.length) {
46                 current_search.results = newresults.concat(current_search.results);
47             } else if (searchOffset = current_search.offset + newresults.length) {
48                 current_search.results = current_search.results.concat(newresults);
49             } else {
50                 delete current_search;
51             }
52         }
53         if (!current_search) {
54             current_search = { offset: offset,
55                 query: query,
56                 limit: limit,
57                 sort:  sort,
58                 pagelen: newresults.length,
59                 results: newresults,
60                 total: total,
61                 searchid: searchid
62             };
63         }
64         searches[me.searchid] = current_search;
65         localStorage.setItem('searches', JSON.stringify(searches));
66         $(document).ready(function () {
67             $('#searchresults table tr a[href*="/detail.pl"]').each(function(){
68                 $(this).attr('href', $(this).attr('href') + '&searchid=' + me.searchid );
69             });
70         });
71     };
72
73     me.show = function () {
74         if (current_search) {
75             me.curPos = $.inArray(biblionumber, current_search.results);
76             me.offset = Math.floor((current_search.offset + me.curPos - 1) / current_search.pagelen) * current_search.pagelen;
77
78             $(document).ready(function () {
79                 if (me.curPos > -1) {
80                     var searchURL = '/cgi-bin/koha/catalogue/search.pl?' + decodeURIComponent(current_search.query) + '&limit=' + decodeURIComponent(current_search.limit) + '&sort_by=' + current_search.sort + '&searchid=' + me.searchid + '&offset=' + me.offset;
81                     var prevbutton;
82                     var nextbutton;
83                     if (me.curPos === 0 && current_search.offset === 1) {
84                         prevbutton = '<span id="browse-previous" class="browse-button" title="' + BROWSER_PREVIOUS + '"><i class="fa fa-arrow-left"></i></span>';
85                     } else {
86                         prevbutton = '<a href="#" id="browse-previous" class="browse-button" title="' + BROWSER_PREVIOUS + '"><i class="fa fa-arrow-left"></i></a>';
87                     }
88                     if (current_search.offset + me.curPos == current_search.total) {
89                         nextbutton = '<span id="browse-next" class="browse-button" title="' + BROWSER_NEXT + '"><i class="fa fa-arrow-right"></i></span>';
90                     } else {
91                         nextbutton = '<a href="#" id="browse-next" class="browse-button" title="' + BROWSER_NEXT + '"><i class="fa fa-arrow-right"></i></a>';
92                     }
93                     $('#menu').before('<div class="browse-controls"><div class="browse-controls-inner"><div class="browse-label"><a href="' + searchURL + '" id="browse-return-to-results" class="searchwithcontext"><i class="fa fa-list"></i> ' + BROWSER_RETURN_TO_SEARCH + '</a></div><div class="browse-prev-next">' + prevbutton + nextbutton + '</div></div></div>');
94                     $('a#browse-previous').click(function (ev) {
95                         ev.preventDefault();
96                         browseRecords(-1);
97                     });
98                     $('a#browse-next').click(function (ev) {
99                         ev.preventDefault();
100                         browseRecords(1);
101                     });
102                     $('a[href*="biblionumber="]').not('a[target="_blank"]').click(function (ev) {
103                         ev.preventDefault();
104                         window.location = $(this).attr('href') + '&searchid=' + me.searchid;
105                     });
106                     $('form[name="f"]').append('<input type="hidden" name="searchid" value="' + me.searchid + '"></input>');
107                 }
108             });
109         }
110     };
111
112     return me;
113 };