Bug 18421: (follow-up) QA fixes
[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 = sessionStorage.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=' + 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=' + 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         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) {
69                 ev.preventDefault();
70             });
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
74                     ev.preventDefault();
75                     var newwindow = window.open( $(this).attr('href') + '&searchid=' + me.searchid, '_blank' );
76                     newwindow.blur();
77                     window.focus();
78                 } else if ( ev.which == 1 ) {
79                     // Left click
80                     ev.preventDefault();
81                     window.location = $(this).attr('href') + '&searchid=' + me.searchid;
82                 }
83             });
84         });
85     };
86
87     me.show = function () {
88         if (current_search) {
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;
91
92             $(document).ready(function () {
93                 if (me.curPos > -1) {
94                     var searchURL = '/cgi-bin/koha/catalogue/search.pl?' + decodeURIComponent(current_search.query) + '&limit=' + decodeURIComponent(current_search.limit) + '&sort=' + current_search.sort + '&searchid=' + me.searchid + '&offset=' + me.offset;
95                     var prevbutton;
96                     var nextbutton;
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>';
99                     } else {
100                         prevbutton = '<a href="#" id="browse-previous" class="browse-button" title="' + BROWSER_PREVIOUS + '"><i class="fa fa-arrow-left"></i></a>';
101                     }
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>';
104                     } else {
105                         nextbutton = '<a href="#" id="browse-next" class="browse-button" title="' + BROWSER_NEXT + '"><i class="fa fa-arrow-right"></i></a>';
106                     }
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) {
109                         ev.preventDefault();
110                         browseRecords(-1);
111                     });
112                     $('a#browse-next').click(function (ev) {
113                         ev.preventDefault();
114                         browseRecords(1);
115                     });
116                     $('a[href*="biblionumber="]').not('a[target="_blank"]').click(function (ev) {
117                         ev.preventDefault();
118                         window.location = $(this).attr('href') + '&searchid=' + me.searchid;
119                     });
120                     $('form[name="f"]').append('<input type="hidden" name="searchid" value="' + me.searchid + '"></input>');
121                 }
122             });
123         }
124     };
125
126     return me;
127 };