Bug 19630: Removed constant message from Status column
[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.escapeHtml();
26
27                             $.each(oObj.subtitle, function( index, value ) {
28                                       title += " " + value.subfield.escapeHtml();
29                             });
30
31                             title += "</a>";
32
33                             if ( oObj.author ) {
34                                 title += " " + BY.replace( "_AUTHOR_",  oObj.author.escapeHtml() );
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.escapeHtml() + "</span>"
43                             }
44
45                             return title;
46                         }
47                     },
48                     {
49                         "mDataProp": function( oObj ) {
50                             return oObj.itemcallnumber && oObj.itemcallnumber.escapeHtml() || "";
51                         }
52                     },
53                     {
54                         "mDataProp": function( oObj ) {
55                             var data = "";
56                             if ( oObj.barcode ) {
57                                 data += " <a href='/cgi-bin/koha/catalogue/detail.pl?biblionumber="
58                                   + oObj.biblionumber
59                                   + "&itemnumber="
60                                   + oObj.itemnumber
61                                   + "#"
62                                   + oObj.itemnumber
63                                   + "'>"
64                                   + oObj.barcode.escapeHtml()
65                                   + "</a>";
66                             }
67                             return data;
68                         }
69                     },
70                     {
71                         "mDataProp": function( oObj ) {
72                             if( oObj.branches.length > 1 && oObj.found !== 'W' && oObj.found !== 'T' ){
73                                 var branchSelect='<select priority='+oObj.priority+' class="hold_location_select" reserve_id="'+oObj.reserve_id+'" name="pick-location">';
74                                 for ( var i=0; i < oObj.branches.length; i++ ){
75                                     var selectedbranch;
76                                     var setbranch;
77                                     if( oObj.branches[i].selected ){
78
79                                         selectedbranch = " selected='selected' ";
80                                         setbranch = CURRENT;
81                                     }
82                                     else{
83                                         selectedbranch = '';
84                                         setbranch = '';
85                                     }
86                                     branchSelect += '<option value="'+ oObj.branches[i].branchcode.escapeHtml() +'"'+selectedbranch+'>'+oObj.branches[i].branchname.escapeHtml()+setbranch+'</option>';
87                                 }
88                                 branchSelect +='</select>';
89                                 return branchSelect;
90                             }
91                             else { return oObj.branchcode.escapeHtml() || ""; }
92                         }
93                     },
94                     { "mDataProp": "expirationdate_formatted" },
95                     {
96                         "mDataProp": function( oObj ) {
97                             if ( oObj.priority && parseInt( oObj.priority ) && parseInt( oObj.priority ) > 0 ) {
98                                 return oObj.priority;
99                             } else {
100                                 return "";
101                             }
102                         }
103                     },
104                     {
105                         "bSortable": false,
106                         "mDataProp": function( oObj ) {
107                             return "<select name='rank-request'>"
108                                  + "<option value='n'>" + NO + "</option>"
109                                  + "<option value='del'>" + YES  + "</option>"
110                                  + "</select>"
111                                  + "<input type='hidden' name='biblionumber' value='" + oObj.biblionumber + "'>"
112                                  + "<input type='hidden' name='borrowernumber' value='" + borrowernumber + "'>"
113                                  + "<input type='hidden' name='reserve_id' value='" + oObj.reserve_id + "'>";
114                         }
115                     },
116                     {
117                         "bSortable": false,
118                         "mDataProp": function( oObj ) {
119                             holds[oObj.reserve_id] = oObj; //Store holds for later use
120
121                             if ( oObj.found ) {
122                                 return "";
123                             } else if ( oObj.suspend == 1 ) {
124                                 return "<a class='hold-resume btn btn-default btn-xs' id='resume" + oObj.reserve_id + "'>"
125                                      + "<i class='fa fa-play'></i> " + RESUME + "</a>";
126                             } else {
127                                 return "<a class='hold-suspend btn btn-default btn-xs' id='suspend" + oObj.reserve_id + "'>"
128                                      + "<i class='fa fa-pause'></i> " + SUSPEND + "</a>";
129                             }
130                         }
131                     },
132                     {
133                         "mDataProp": function( oObj ) {
134                             var data = "";
135
136                             if ( oObj.suspend == 1 ) {
137                                 data += "<p>" + HOLD_IS_SUSPENDED;
138                                 if ( oObj.suspend_until ) {
139                                     data += " " + UNTIL.format( oObj.suspend_until_formatted );
140                                 }
141                                 data += "</p>";
142                             }
143
144                             if ( oObj.itemtype_limit ) {
145                                 data += NEXT_AVAILABLE_ITYPE.format( oObj.itemtype_limit );
146                             }
147
148                             if ( oObj.barcode ) {
149                                 data += "<em>";
150                                 if ( oObj.found == "W" ) {
151
152                                     if ( oObj.waiting_here ) {
153                                         data += ITEM_IS_WAITING_HERE;
154                                     } else {
155                                         data += ITEM_IS_WAITING;
156                                         data += " " + AT.format( oObj.waiting_at );
157                                     }
158
159                                 } else if ( oObj.transferred ) {
160                                     data += ITEM_IS_IN_TRANSIT.format( oObj.from_branch, oObj.date_sent );
161                                 } else if ( oObj.not_transferred ) {
162                                     data += NOT_TRANSFERRED_YET.format( oObj.not_transferred_by );
163                                 }
164                                 data += "</em>";
165                             }
166                             return data;
167                         }
168                    }
169                 ],
170                 "bPaginate": false,
171                 "bProcessing": true,
172                 "bServerSide": false,
173                 "ajax": {
174                     "url": '/cgi-bin/koha/svc/holds',
175                     "data": function ( d ) {
176                         d.borrowernumber = borrowernumber;
177                     }
178                 },
179             });
180
181             $('#holds-table').on( 'draw.dt', function () {
182                 $(".hold-suspend").on( "click", function() {
183                     var id = $(this).attr("id").replace("suspend", "");
184                     var hold = holds[id];
185                     $("#suspend-modal-title").html( hold.title );
186                     $("#suspend-modal-reserve_id").val( hold.reserve_id );
187                     $('#suspend-modal').modal('show');
188                 });
189
190                 $(".hold-resume").on( "click", function() {
191                     var id = $(this).attr("id").replace("resume", "");
192                     var hold = holds[id];
193                     $.post('/cgi-bin/koha/svc/hold/resume', { "reserve_id": hold.reserve_id }, function( data ){
194                       if ( data.success ) {
195                           holdsTable.api().ajax.reload();
196                       } else {
197                         if ( data.error == "HOLD_NOT_FOUND" ) {
198                             alert ( RESUME_HOLD_ERROR_NOT_FOUND );
199                             holdsTable.api().ajax.reload();
200                         }
201                       }
202                     });
203                 });
204
205                 $(".hold_location_select").change(function(){
206                     $(this).prop("disabled",true);
207                     var cur_select = $(this);
208                     var res_id = $(this).attr('reserve_id');
209                     $(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>');
210                     var api_url = '/api/v1/holds/'+res_id;
211                     var update_info = JSON.stringify({ pickup_library_id: $(this).val(), priority: parseInt($(this).attr("priority"),10) });
212                     $.ajax({
213                         method: "PUT",
214                         url: api_url,
215                         data: update_info ,
216                         success: function( data ){ holdsTable.api().ajax.reload(); },
217                         error: function( jqXHR, textStatus, errorThrown) {
218                             alert('There was an error:'+textStatus+" "+errorThrown);
219                             cur_select.prop("disabled",false);
220                             $("#updating_reserveno"+res_id).remove();
221                             cur_select.val( cur_select.children('option[selected="selected"]').val() );
222                         },
223                     });
224                 });
225
226             });
227
228             if ( $("#holds-table").length ) {
229                 $("#holds-table_processing").position({
230                     of: $( "#holds-table" ),
231                     collision: "none"
232                 });
233             }
234         }
235     }
236
237     $("body").append("\
238         <div id='suspend-modal' class='modal fade' role='dialog' aria-hidden='true'>\
239             <div class='modal-dialog'>\
240             <div class='modal-content'>\
241             <form id='suspend-modal-form' class='form-inline'>\
242                 <div class='modal-header'>\
243                     <button type='button' class='closebtn' data-dismiss='modal' aria-hidden='true'>×</button>\
244                     <h3 id='suspend-modal-label'>" + SUSPEND_HOLD_ON + " <i><span id='suspend-modal-title'></span></i></h3>\
245                 </div>\
246 \
247                 <div class='modal-body'>\
248                     <input type='hidden' id='suspend-modal-reserve_id' name='reserve_id' />\
249 \
250                     <label for='suspend-modal-until'>" + SUSPEND_UNTIL + "</label>\
251                     <input name='suspend_until' id='suspend-modal-until' class='suspend-until' size='10' />\
252 \
253                     <p/><a class='btn btn-link' id='suspend-modal-clear-date' >" + CLEAR_DATE_TO_SUSPEND_INDEFINITELY + "</a></p>\
254 \
255                 </div>\
256 \
257                 <div class='modal-footer'>\
258                     <button id='suspend-modal-submit' class='btn btn-primary' type='submit' name='submit'>" + SUSPEND + "</button>\
259                     <a href='#' data-dismiss='modal' aria-hidden='true' class='cancel'>" + CANCEL + "</a>\
260                 </div>\
261             </form>\
262             </div>\
263             </div>\
264         </div>\
265     ");
266
267     $("#suspend-modal-until").datepicker({ minDate: 1 }); // Require that "until date" be in the future
268     $("#suspend-modal-clear-date").on( "click", function() { $("#suspend-modal-until").val(""); } );
269
270     $("#suspend-modal-submit").on( "click", function( e ) {
271         e.preventDefault();
272         $.post('/cgi-bin/koha/svc/hold/suspend', $('#suspend-modal-form').serialize(), function( data ){
273           $('#suspend-modal').modal('hide');
274           if ( data.success ) {
275               holdsTable.api().ajax.reload();
276           } else {
277             if ( data.error == "INVALID_DATE" ) {
278                 alert( SUSPEND_HOLD_ERROR_DATE );
279             }
280             else if ( data.error == "HOLD_NOT_FOUND" ) {
281                 alert ( SUSPEND_HOLD_ERROR_NOT_FOUND );
282                 holdsTable.api().ajax.reload();
283             }
284           }
285         });
286     });
287
288 });