1 /* global BROWSER_PREVIOUS BROWSER_NEXT BROWSER_RETURN_TO_SEARCH */
3 if ( KOHA === undefined ) var KOHA = {};
5 KOHA.browser = function (searchid, biblionumber) {
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
12 me.searchid = searchid;
14 var searches_stored = sessionStorage.getItem('searches');
17 if ( searches_stored ) {
18 searches = JSON.parse(searches_stored);
19 current_search = searches[me.searchid];
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]];
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);
39 window.location = window.location.href.replace('biblionumber=' + biblionumber, 'biblionumber=' + current_search.results[newSearchPos]);
43 me.create = function (offset, query, limit, sort, newresults, total) {
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);
50 delete current_search;
53 if (!current_search) {
54 current_search = { offset: offset,
58 pagelen: newresults.length,
64 searches[me.searchid] = current_search;
65 sessionStorage.setItem('searches', JSON.stringify(searches));
66 $(document).ready(function () {
67 //FIXME It's not a good idea to modify the click events
68 $('#searchresults table tr a[href*="/detail.pl"]').on('click', function (ev) {
71 $('#searchresults table tr a[href*="/detail.pl"]').on('mousedown', function (ev) {
72 if ( ev.which == 2 || ev.which == 1 && ev.ctrlKey ) {
73 // Middle click or ctrl + click
75 var newwindow = window.open( $(this).attr('href') + '&searchid=' + me.searchid, '_blank' );
78 } else if ( ev.which == 1 ) {
81 window.location = $(this).attr('href') + '&searchid=' + me.searchid;
87 me.show = function () {
89 me.curPos = $.inArray(biblionumber, current_search.results);
90 me.offset = Math.floor((current_search.offset + me.curPos - 1) / current_search.pagelen) * current_search.pagelen;
92 $(document).ready(function () {
94 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;
97 if (me.curPos === 0 && current_search.offset === 1) {
98 prevbutton = '<span id="browse-previous" class="browse-button" title="' + BROWSER_PREVIOUS + '"><i class="fa fa-arrow-left"></i></span>';
100 prevbutton = '<a href="#" id="browse-previous" class="browse-button" title="' + BROWSER_PREVIOUS + '"><i class="fa fa-arrow-left"></i></a>';
102 if (current_search.offset + me.curPos == current_search.total) {
103 nextbutton = '<span id="browse-next" class="browse-button" title="' + BROWSER_NEXT + '"><i class="fa fa-arrow-right"></i></span>';
105 nextbutton = '<a href="#" id="browse-next" class="browse-button" title="' + BROWSER_NEXT + '"><i class="fa fa-arrow-right"></i></a>';
107 $('#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>');
108 $('a#browse-previous').click(function (ev) {
112 $('a#browse-next').click(function (ev) {
116 $('a[href*="biblionumber="]').not('a[target="_blank"]').click(function (ev) {
118 window.location = $(this).attr('href') + '&searchid=' + me.searchid;
120 $('form[name="f"]').append('<input type="hidden" name="searchid" value="' + me.searchid + '"></input>');