Bug 16242 - Move staff client JavaScript out of language directory
[koha.git] / koha-tmpl / intranet-tmpl / prog / js / holds.js
1 $(document).ready(function() {
2     var holdsTable;
3
4     // Don't load holds table unless it is clicked on
5     $("#holds-tab").on( "click", function(){ load_holds_table() } );
6
7     // If the holds tab is preselected on load, we need to load the table
8     if ( $("#holds-tab").parent().hasClass('ui-state-active') ) { load_holds_table() }
9
10     function load_holds_table() {
11         var holds = new Array();
12         if ( ! holdsTable ) {
13             holdsTable = $("#holds-table").dataTable({
14                 "bAutoWidth": false,
15                 "sDom": "rt",
16                 "columns": [
17                     {
18                         "mDataProp": "reservedate_formatted"
19                     },
20                     {
21                         "mDataProp": function ( oObj ) {
22                             title = "<a href='/cgi-bin/koha/reserve/request.pl?biblionumber="
23                                   + oObj.biblionumber
24                                   + "'>"
25                                   + oObj.title;
26
27                             $.each(oObj.subtitle, function( index, value ) {
28                                       title += " " + value.subfield;
29                             });
30
31                             title += "</a>";
32
33                             if ( oObj.author ) {
34                                 title += " " + BY.replace( "_AUTHOR_",  oObj.author );
35                             }
36
37                             if ( oObj.itemnotes ) {
38                                 var span_class = "";
39                                 if ( $.datepicker.formatDate('yy-mm-dd', new Date(oObj.issuedate) ) == ymd ) {
40                                     span_class = "circ-hlt";
41                                 }
42                                 title += " - <span class='" + span_class + "'>" + oObj.itemnotes + "</span>"
43                             }
44
45                             return title;
46                         }
47                     },
48                     {
49                         "mDataProp": function( oObj ) {
50                             return oObj.itemcallnumber || "";
51                         }
52                     },
53                     {
54                         "mDataProp": function( oObj ) {
55                             var data = "";
56
57                             if ( oObj.suspend == 1 ) {
58                                 data += "<p>" + HOLD_IS_SUSPENDED;
59                                 if ( oObj.suspend_until ) {
60                                     data += " " + UNTIL.format( oObj.suspend_until_formatted );
61                                 }
62                                 data += "</p>";
63                             }
64
65                             if ( oObj.itemtype_limit ) {
66                                 data += NEXT_AVAILABLE_ITYPE.format( oObj.itemtype_limit );
67                             }
68
69                             if ( oObj.barcode ) {
70                                 data += "<em>";
71                                 if ( oObj.found == "W" ) {
72
73                                     if ( oObj.waiting_here ) {
74                                         data += ITEM_IS_WAITING_HERE;
75                                     } else {
76                                         data += ITEM_IS_WAITING;
77                                         data += " " + AT.format( oObj.waiting_at );
78                                     }
79
80                                 } else if ( oObj.transferred ) {
81                                     data += ITEM_IS_IN_TRANSIT.format( oObj.from_branch, oObj.date_sent );
82                                 } else if ( oObj.not_transferred ) {
83                                     data += NOT_TRANSFERRED_YET.format( oObj.not_transferred_by );
84                                 }
85                                 data += "</em>";
86
87                                 data += " <a href='/cgi-bin/koha/catalogue/detail.pl?biblionumber="
88                                   + oObj.biblionumber
89                                   + "&itemnumber="
90                                   + oObj.itemnumber
91                                   + "#"
92                                   + oObj.itemnumber
93                                   + "'>"
94                                   + oObj.barcode
95                                   + "</a>";
96                             }
97
98                             return data;
99                         }
100                     },
101                     {
102                         "mDataProp": function( oObj ) {
103                             return oObj.branchcode || "";
104                         }
105                     },
106                     { "mDataProp": "expirationdate_formatted" },
107                     {
108                         "mDataProp": function( oObj ) {
109                             if ( oObj.priority && parseInt( oObj.priority ) && parseInt( oObj.priority ) > 0 ) {
110                                 return oObj.priority;
111                             } else {
112                                 return "";
113                             }
114                         }
115                     },
116                     {
117                         "bSortable": false,
118                         "mDataProp": function( oObj ) {
119                             return "<select name='rank-request'>"
120                                  + "<option value='n'>" + NO + "</option>"
121                                  + "<option value='del'>" + YES  + "</option>"
122                                  + "</select>"
123                                  + "<input type='hidden' name='biblionumber' value='" + oObj.biblionumber + "'>"
124                                  + "<input type='hidden' name='borrowernumber' value='" + borrowernumber + "'>"
125                                  + "<input type='hidden' name='reserve_id' value='" + oObj.reserve_id + "'>";
126                         }
127                     },
128                     {
129                         "bSortable": false,
130                         "mDataProp": function( oObj ) {
131                             holds[oObj.reserve_id] = oObj; //Store holds for later use
132
133                             if ( oObj.found ) {
134                                 return "";
135                             } else if ( oObj.suspend == 1 ) {
136                                 return "<a class='hold-resume btn btn-link' id='resume" + oObj.reserve_id + "' style='display: inline; white-space: nowrap;'>"
137                                      + "<i class='icon-play'></i> " + RESUME + "</a>";
138                             } else {
139                                 return "<a class='hold-suspend btn btn-link' id='suspend" + oObj.reserve_id + "' style='display: inline; white-space: nowrap;'>"
140                                      + "<i class='icon-pause'></i> " + SUSPEND + "</a>";
141                             }
142                         }
143                     }
144                 ],
145                 "bPaginate": false,
146                 "bProcessing": true,
147                 "bServerSide": false,
148                 "ajax": {
149                     "url": '/cgi-bin/koha/svc/holds',
150                     "data": function ( d ) {
151                         d.borrowernumber = borrowernumber;
152                     }
153                 },
154             });
155
156             $('#holds-table').on( 'draw.dt', function () {
157                 $(".hold-suspend").on( "click", function() {
158                     var id = $(this).attr("id").replace("suspend", "");
159                     var hold = holds[id];
160                     $("#suspend-modal-title").html( hold.title );
161                     $("#suspend-modal-reserve_id").val( hold.reserve_id );
162                     $('#suspend-modal').modal('show');
163                 });
164
165                 $(".hold-resume").on( "click", function() {
166                     var id = $(this).attr("id").replace("resume", "");
167                     var hold = holds[id];
168                     $.post('/cgi-bin/koha/svc/hold/resume', { "reserve_id": hold.reserve_id }, function( data ){
169                       if ( data.success ) {
170                           holdsTable.api().ajax.reload();
171                       } else {
172                         if ( data.error == "HOLD_NOT_FOUND" ) {
173                             alert ( RESUME_HOLD_ERROR_NOT_FOUND );
174                             holdsTable.api().ajax.reload();
175                         }
176                       }
177                     });
178                 });
179             });
180
181             if ( $("#holds-table").length ) {
182                 $("#holds-table_processing").position({
183                     of: $( "#holds-table" ),
184                     collision: "none"
185                 });
186             }
187         }
188     }
189
190     $("body").append("\
191         <div id='suspend-modal' class='modal hide fade' tabindex='-1' role='dialog' aria-hidden='true'>\
192             <form id='suspend-modal-form' class='form-inline'>\
193                 <div class='modal-header'>\
194                     <button type='button' class='closebtn' data-dismiss='modal' aria-hidden='true'>×</button>\
195                     <h3 id='suspend-modal-label'>" + SUSPEND_HOLD_ON + " <i><span id='suspend-modal-title'></span></i></h3>\
196                 </div>\
197 \
198                 <div class='modal-body'>\
199                     <input type='hidden' id='suspend-modal-reserve_id' name='reserve_id' />\
200 \
201                     <label for='suspend-modal-until'>Suspend until:</label>\
202                     <input name='suspend_until' id='suspend-modal-until' class='suspend-until' size='10' />\
203 \
204                     <p/><a class='btn btn-link' id='suspend-modal-clear-date' >" + CLEAR_DATE_TO_SUSPEND_INDEFINITELY + "</a></p>\
205 \
206                 </div>\
207 \
208                 <div class='modal-footer'>\
209                     <button id='suspend-modal-submit' class='btn btn-primary' type='submit' name='submit'>" + SUSPEND + "</button>\
210                     <a href='#' data-dismiss='modal' aria-hidden='true' class='cancel'>" + CANCEL + "</a>\
211                 </div>\
212             </form>\
213         </div>\
214     ");
215
216     $("#suspend-modal-until").datepicker({ minDate: 1 }); // Require that "until date" be in the future
217     $("#suspend-modal-clear-date").on( "click", function() { $("#suspend-modal-until").val(""); } );
218
219     $("#suspend-modal-submit").on( "click", function( e ) {
220         e.preventDefault();
221         $.post('/cgi-bin/koha/svc/hold/suspend', $('#suspend-modal-form').serialize(), function( data ){
222           $('#suspend-modal').modal('hide');
223           if ( data.success ) {
224               holdsTable.api().ajax.reload();
225           } else {
226             if ( data.error == "INVALID_DATE" ) {
227                 alert( SUSPEND_HOLD_ERROR_DATE );
228             }
229             else if ( data.error == "HOLD_NOT_FOUND" ) {
230                 alert ( SUSPEND_HOLD_ERROR_NOT_FOUND );
231                 holdsTable.api().ajax.reload();
232             }
233           }
234         });
235     });
236
237 });