Bug 16239: Update javascript files
[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                             if( oObj.branches.length > 1 && oObj.found !== 'W' && oObj.found !== 'T' ){
104                                 var branchSelect='<select priority='+oObj.priority+' class="hold_location_select" reserve_id="'+oObj.reserve_id+'" name="pick-location">';
105                                 for ( var i=0; i < oObj.branches.length; i++ ){
106                                     var selectedbranch;
107                                     var setbranch;
108                                     if( oObj.branches[i].selected ){
109
110                                         selectedbranch = " selected='selected' ";
111                                         setbranch = CURRENT;
112                                     }
113                                     else{
114                                         selectedbranch = '';
115                                         setbranch = '';
116                                     }
117                                     branchSelect += '<option value="'+ oObj.branches[i].branchcode +'"'+selectedbranch+'>'+oObj.branches[i].branchname+setbranch+'</option>';
118                                 }
119                                 branchSelect +='</select>';
120                                 return branchSelect;
121                             }
122                             else { return oObj.branchcode || ""; }
123                         }
124                     },
125                     { "mDataProp": "expirationdate_formatted" },
126                     {
127                         "mDataProp": function( oObj ) {
128                             if ( oObj.priority && parseInt( oObj.priority ) && parseInt( oObj.priority ) > 0 ) {
129                                 return oObj.priority;
130                             } else {
131                                 return "";
132                             }
133                         }
134                     },
135                     {
136                         "bSortable": false,
137                         "mDataProp": function( oObj ) {
138                             return "<select name='rank-request'>"
139                                  + "<option value='n'>" + NO + "</option>"
140                                  + "<option value='del'>" + YES  + "</option>"
141                                  + "</select>"
142                                  + "<input type='hidden' name='biblionumber' value='" + oObj.biblionumber + "'>"
143                                  + "<input type='hidden' name='borrowernumber' value='" + borrowernumber + "'>"
144                                  + "<input type='hidden' name='reserve_id' value='" + oObj.reserve_id + "'>";
145                         }
146                     },
147                     {
148                         "bSortable": false,
149                         "mDataProp": function( oObj ) {
150                             holds[oObj.reserve_id] = oObj; //Store holds for later use
151
152                             if ( oObj.found ) {
153                                 return "";
154                             } else if ( oObj.suspend == 1 ) {
155                                 return "<a class='hold-resume btn btn-default btn-xs' id='resume" + oObj.reserve_id + "'>"
156                                      + "<i class='fa fa-play'></i> " + RESUME + "</a>";
157                             } else {
158                                 return "<a class='hold-suspend btn btn-default btn-xs' id='suspend" + oObj.reserve_id + "'>"
159                                      + "<i class='fa fa-pause'></i> " + SUSPEND + "</a>";
160                             }
161                         }
162                     }
163                 ],
164                 "bPaginate": false,
165                 "bProcessing": true,
166                 "bServerSide": false,
167                 "ajax": {
168                     "url": '/cgi-bin/koha/svc/holds',
169                     "data": function ( d ) {
170                         d.borrowernumber = borrowernumber;
171                     }
172                 },
173             });
174
175             $('#holds-table').on( 'draw.dt', function () {
176                 $(".hold-suspend").on( "click", function() {
177                     var id = $(this).attr("id").replace("suspend", "");
178                     var hold = holds[id];
179                     $("#suspend-modal-title").html( hold.title );
180                     $("#suspend-modal-reserve_id").val( hold.reserve_id );
181                     $('#suspend-modal').modal('show');
182                 });
183
184                 $(".hold-resume").on( "click", function() {
185                     var id = $(this).attr("id").replace("resume", "");
186                     var hold = holds[id];
187                     $.post('/cgi-bin/koha/svc/hold/resume', { "reserve_id": hold.reserve_id }, function( data ){
188                       if ( data.success ) {
189                           holdsTable.api().ajax.reload();
190                       } else {
191                         if ( data.error == "HOLD_NOT_FOUND" ) {
192                             alert ( RESUME_HOLD_ERROR_NOT_FOUND );
193                             holdsTable.api().ajax.reload();
194                         }
195                       }
196                     });
197                 });
198
199                 $(".hold_location_select").change(function(){
200                     $(this).prop("disabled",true);
201                     var cur_select = $(this);
202                     var res_id = $(this).attr('reserve_id');
203                     $(this).after('<div id="updating_reserveno'+res_id+'" class="waiting"><img src="/intranet-tmpl/prog/img/spinner-small.gif" alt="" /><span class="waiting_msg"></span></div>');
204                     var api_url = '/api/v1/holds/'+res_id;
205                     var update_info = JSON.stringify({ branchcode: $(this).val(), priority: parseInt($(this).attr("priority"),10) });
206                     $.ajax({
207                         method: "PUT",
208                         url: api_url,
209                         data: update_info ,
210                         success: function( data ){ holdsTable.api().ajax.reload(); },
211                         error: function( jqXHR, textStatus, errorThrown) {
212                             alert('There was an error:'+textStatus+" "+errorThrown);
213                             cur_select.prop("disabled",false);
214                             $("#updating_reserveno"+res_id).remove();
215                             cur_select.val( cur_select.children('option[selected="selected"]').val() );
216                         },
217                     });
218                 });
219
220             });
221
222             if ( $("#holds-table").length ) {
223                 $("#holds-table_processing").position({
224                     of: $( "#holds-table" ),
225                     collision: "none"
226                 });
227             }
228         }
229     }
230
231     $("body").append("\
232         <div id='suspend-modal' class='modal fade' tabindex='-1' role='dialog' aria-hidden='true'>\
233             <div class='modal-dialog'>\
234             <div class='modal-content'>\
235             <form id='suspend-modal-form' class='form-inline'>\
236                 <div class='modal-header'>\
237                     <button type='button' class='closebtn' data-dismiss='modal' aria-hidden='true'>×</button>\
238                     <h3 id='suspend-modal-label'>" + SUSPEND_HOLD_ON + " <i><span id='suspend-modal-title'></span></i></h3>\
239                 </div>\
240 \
241                 <div class='modal-body'>\
242                     <input type='hidden' id='suspend-modal-reserve_id' name='reserve_id' />\
243 \
244                     <label for='suspend-modal-until'>Suspend until:</label>\
245                     <input name='suspend_until' id='suspend-modal-until' class='suspend-until' size='10' />\
246 \
247                     <p/><a class='btn btn-link' id='suspend-modal-clear-date' >" + CLEAR_DATE_TO_SUSPEND_INDEFINITELY + "</a></p>\
248 \
249                 </div>\
250 \
251                 <div class='modal-footer'>\
252                     <button id='suspend-modal-submit' class='btn btn-primary' type='submit' name='submit'>" + SUSPEND + "</button>\
253                     <a href='#' data-dismiss='modal' aria-hidden='true' class='cancel'>" + CANCEL + "</a>\
254                 </div>\
255             </form>\
256             </div>\
257             </div>\
258         </div>\
259     ");
260
261     $("#suspend-modal-until").datepicker({ minDate: 1 }); // Require that "until date" be in the future
262     $("#suspend-modal-clear-date").on( "click", function() { $("#suspend-modal-until").val(""); } );
263
264     $("#suspend-modal-submit").on( "click", function( e ) {
265         e.preventDefault();
266         $.post('/cgi-bin/koha/svc/hold/suspend', $('#suspend-modal-form').serialize(), function( data ){
267           $('#suspend-modal').modal('hide');
268           if ( data.success ) {
269               holdsTable.api().ajax.reload();
270           } else {
271             if ( data.error == "INVALID_DATE" ) {
272                 alert( SUSPEND_HOLD_ERROR_DATE );
273             }
274             else if ( data.error == "HOLD_NOT_FOUND" ) {
275                 alert ( SUSPEND_HOLD_ERROR_NOT_FOUND );
276                 holdsTable.api().ajax.reload();
277             }
278           }
279         });
280     });
281
282 });