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