Bug 12194: Add option 'Oblique title' in labels
[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     me.searchid = searchid;
11
12     var searches_stored = sessionStorage.getItem('searches');
13     var current_search;
14     var searches = {};
15     if ( searches_stored ) {
16         searches = JSON.parse(searches_stored);
17         current_search = searches[me.searchid];
18
19         // Remove old entries
20         var searchids = Object.keys(searches);
21         var nb_searches = searchids.length;
22         if ( nb_searches > 20 ) { // No need to keep more than 20 searches
23             searchids = searchids.sort();
24             for ( var i = 0 ; i < nb_searches - 20 ; i++ ) {
25                 delete searches[searchids[i]];
26             }
27         }
28     }
29
30     var browseRecords = function (movement) {
31         var newSearchPos = me.curPos + movement;
32         if (newSearchPos > current_search.results.length - 1) {
33             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;
34         } else if (newSearchPos < 0) {
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=last&searchid=' + me.searchid + '&offset=' + (me.offset - current_search.pagelen);
36         } else {
37             window.location = window.location.href.replace('biblionumber=' + biblionumber, 'biblionumber=' + current_search.results[newSearchPos]);
38         }
39     }
40
41     me.create = function (offset, query, limit, sort, newresults, total) {
42         if (current_search) {
43             if (offset === current_search.offset - newresults.length) {
44                 current_search.results = newresults.concat(current_search.results);
45             } else if (searchOffset = current_search.offset + newresults.length) {
46                 current_search.results = current_search.results.concat(newresults);
47             } else {
48                 delete current_search;
49             }
50         }
51         if (!current_search) {
52             current_search = { offset: offset,
53                 query: query,
54                 limit: limit,
55                 sort:  sort,
56                 pagelen: newresults.length,
57                 results: newresults,
58                 total: total,
59                 searchid: searchid
60             };
61         }
62         searches[me.searchid] = current_search;
63         sessionStorage.setItem('searches', JSON.stringify(searches));
64         $(document).ready(function () {
65             //FIXME It's not a good idea to modify the click events
66             $('#searchresults table tr a[href*="detail.pl"]').on('click', function (ev) {
67                 ev.preventDefault();
68             });
69             $('#searchresults table tr a[href*="detail.pl"]').on('mousedown', function (ev) {
70                 if ( ev.which == 2 || ev.which == 1 && ev.ctrlKey ) {
71                     // Middle click or ctrl + click
72                     ev.preventDefault();
73                     var newwindow = window.open( $(this).attr('href') + '&searchid=' + me.searchid, '_blank' )
74                     newwindow.blur();
75                     window.focus();
76                 } else if ( ev.which == 1 ) {
77                     // Left click
78                     ev.preventDefault();
79                     window.location = $(this).attr('href') + '&searchid=' + me.searchid;
80                 }
81             });
82         });
83     };
84
85     me.show = function () {
86         if (current_search) {
87             me.curPos = $.inArray(biblionumber, current_search.results);
88             me.offset = Math.floor((current_search.offset + me.curPos - 1) / current_search.pagelen) * current_search.pagelen;
89
90             $(document).ready(function () {
91                 if (me.curPos > -1) {
92                     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;
93                     var prevbutton;
94                     var nextbutton;
95                     if (me.curPos === 0 && current_search.offset === 1) {
96                         prevbutton = '<span id="browse-previous" class="browse-button">« ' + BROWSER_PREVIOUS + '</span>';
97                     } else {
98                         prevbutton = '<a href="#" id="browse-previous" class="browse-button">« ' + BROWSER_PREVIOUS + '</a>';
99                     }
100                     if (current_search.offset + me.curPos == current_search.total) {
101                         nextbutton = '<span id="browse-next" class="browse-button">' + BROWSER_NEXT + ' »</span>';
102                     } else {
103                         nextbutton = '<a href="#" id="browse-next" class="browse-button">' + BROWSER_NEXT + ' »</a>';
104                     }
105                     $('#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>');
106                     $('a#browse-previous').click(function (ev) {
107                         ev.preventDefault();
108                         browseRecords(-1);
109                     });
110                     $('a#browse-next').click(function (ev) {
111                         ev.preventDefault();
112                         browseRecords(1);
113                     });
114                     $('a[href*="biblionumber="]').click(function (ev) {
115                         ev.preventDefault();
116                         window.location = $(this).attr('href') + '&searchid=' + me.searchid;
117                     });
118                     $('form[name="f"]').append('<input type="hidden" name="searchid" value="' + me.searchid + '"></input>');
119                 }
120             });
121         }
122     };
123
124     return me;
125 };