Bug 26727: Fix <p/> appearing in the templates
[koha.git] / koha-tmpl / intranet-tmpl / prog / js / holds.js
1 /* global __ dataTablesDefaults borrowernumber SuspendHoldsIntranet */
2 $(document).ready(function() {
3     var holdsTable;
4
5     // Don't load holds table unless it is clicked on
6     $("#holds-tab").on( "click", function(){ load_holds_table() } );
7
8     // If the holds tab is preselected on load, we need to load the table
9     if ( $("#holds-tab").parent().hasClass('ui-state-active') ) { load_holds_table() }
10
11     function load_holds_table() {
12         var holds = new Array();
13         if ( ! holdsTable ) {
14             var title;
15             holdsTable = $("#holds-table").dataTable($.extend(true, {}, dataTablesDefaults, {
16                 "bAutoWidth": false,
17                 "sDom": "rt",
18                 "columns": [
19                     {
20                         "data": { _: "reservedate_formatted", "sort": "reservedate" }
21                     },
22                     {
23                         "mDataProp": function ( oObj ) {
24                             title = "<a href='/cgi-bin/koha/reserve/request.pl?biblionumber="
25                                   + oObj.biblionumber
26                                   + "'>"
27                                   + oObj.title.escapeHtml();
28
29                             $.each(oObj.subtitle, function( index, value ) {
30                                 title += " " + value.escapeHtml();
31                             });
32
33                             title += " " + oObj.part_number + " " + oObj.part_name;
34
35                             if ( oObj.enumchron ) {
36                                 title += " (" + oObj.enumchron.escapeHtml() + ")";
37                             }
38
39                             title += "</a>";
40
41                             if ( oObj.author ) {
42                                 title += " " + __("by _AUTHOR_").replace("_AUTHOR_", oObj.author.escapeHtml());
43                             }
44
45                             if ( oObj.itemnotes ) {
46                                 var span_class = "";
47                                 if ( $.datepicker.formatDate('yy-mm-dd', new Date(oObj.issuedate) ) == ymd ) {
48                                     span_class = "circ-hlt";
49                                 }
50                                 title += " - <span class='" + span_class + "'>" + oObj.itemnotes.escapeHtml() + "</span>"
51                             }
52
53                             return title;
54                         }
55                     },
56                     {
57                         "mDataProp": function( oObj ) {
58                             return oObj.itemcallnumber && oObj.itemcallnumber.escapeHtml() || "";
59                         }
60                     },
61                     {
62                         "mDataProp": function( oObj ) {
63                             var data = "";
64                             if ( oObj.barcode ) {
65                                 data += " <a href='/cgi-bin/koha/catalogue/moredetail.pl?biblionumber="
66                                   + oObj.biblionumber
67                                   + "&itemnumber="
68                                   + oObj.itemnumber
69                                   + "#item"
70                                   + oObj.itemnumber
71                                   + "'>"
72                                   + oObj.barcode.escapeHtml()
73                                   + "</a>";
74                             }
75                             return data;
76                         }
77                     },
78                     {
79                         "mDataProp": function( oObj ) {
80                             if( oObj.branches.length > 1 && oObj.found !== 'W' && oObj.found !== 'T' ){
81                                 var branchSelect='<select priority='+oObj.priority+' class="hold_location_select" reserve_id="'+oObj.reserve_id+'" name="pick-location">';
82                                 for ( var i=0; i < oObj.branches.length; i++ ){
83                                     var selectedbranch;
84                                     var setbranch;
85                                     if( oObj.branches[i].selected ){
86
87                                         selectedbranch = " selected='selected' ";
88                                         setbranch = __(" (current) ");
89                                     } else if ( oObj.branches[i].pickup_location == 0 ) {
90                                         continue;
91                                     } else{
92                                         selectedbranch = '';
93                                         setbranch = '';
94                                     }
95                                     branchSelect += '<option value="'+ oObj.branches[i].branchcode.escapeHtml() +'"'+selectedbranch+'>'+oObj.branches[i].branchname.escapeHtml()+setbranch+'</option>';
96                                 }
97                                 branchSelect +='</select>';
98                                 return branchSelect;
99                             }
100                             else { return oObj.branchcode.escapeHtml() || ""; }
101                         }
102                     },
103                     { "data": { _: "expirationdate_formatted", "sort": "expirationdate" } },
104                     {
105                         "mDataProp": function( oObj ) {
106                             if ( oObj.priority && parseInt( oObj.priority ) && parseInt( oObj.priority ) > 0 ) {
107                                 return oObj.priority;
108                             } else {
109                                 return "";
110                             }
111                         }
112                     },
113                     {
114                         "bSortable": false,
115                         "mDataProp": function( oObj ) {
116                             return "<select name='rank-request'>"
117                                  +"<option value='n'>" + __("No") + "</option>"
118                                  +"<option value='del'>" + __("Yes") + "</option>"
119                                  + "</select>"
120                                  + "<input type='hidden' name='biblionumber' value='" + oObj.biblionumber + "'>"
121                                  + "<input type='hidden' name='borrowernumber' value='" + borrowernumber + "'>"
122                                  + "<input type='hidden' name='reserve_id' value='" + oObj.reserve_id + "'>";
123                         }
124                     },
125                     {
126                         "bSortable": false,
127                         "visible": SuspendHoldsIntranet,
128                         "mDataProp": function( oObj ) {
129                             holds[oObj.reserve_id] = oObj; //Store holds for later use
130
131                             if ( oObj.found ) {
132                                 return "";
133                             } else if ( oObj.suspend == 1 ) {
134                                 return "<a class='hold-resume btn btn-default btn-xs' id='resume" + oObj.reserve_id + "'>"
135                                      +"<i class='fa fa-play'></i> " + __("Resume") + "</a>";
136                             } else {
137                                 return "<a class='hold-suspend btn btn-default btn-xs' id='suspend" + oObj.reserve_id + "'>"
138                                      +"<i class='fa fa-pause'></i> " + __("Suspend") + "</a>";
139                             }
140                         }
141                     },
142                     {
143                         "mDataProp": function( oObj ) {
144                             var data = "";
145
146                             if ( oObj.suspend == 1 ) {
147                                 data += "<p>" + __("Hold is <strong>suspended</strong>");
148                                 if ( oObj.suspend_until ) {
149                                     data += " " + __("until %s").format(oObj.suspend_until_formatted);
150                                 }
151                                 data += "</p>";
152                             }
153
154                             if ( oObj.itemtype_limit ) {
155                                 data += __("Next available %s item").format(oObj.itemtype_limit);
156                             }
157
158                             if ( oObj.barcode ) {
159                                 data += "<em>";
160                                 if ( oObj.found == "W" ) {
161
162                                     if ( oObj.waiting_here ) {
163                                         data += __("Item is <strong>waiting here</strong>");
164                                     } else {
165                                         data += __("Item is <strong>waiting</strong>");
166                                         data += " " + __("at %s").format(oObj.waiting_at);
167                                     }
168
169                                 } else if ( oObj.transferred ) {
170                                     data += __("Item is <strong>in transit</strong> from %s since %s").format(oObj.from_branch, oObj.date_sent);
171                                 } else if ( oObj.not_transferred ) {
172                                     data += __("Item hasn't been transferred yet from %s").format(oObj.not_transferred_by);
173                                 }
174                                 data += "</em>";
175                             }
176                             return data;
177                         }
178                     }
179                 ],
180                 "bPaginate": false,
181                 "bProcessing": true,
182                 "bServerSide": false,
183                 "ajax": {
184                     "url": '/cgi-bin/koha/svc/holds',
185                     "data": function ( d ) {
186                         d.borrowernumber = borrowernumber;
187                     }
188                 },
189             }));
190
191             $('#holds-table').on( 'draw.dt', function () {
192                 $(".hold-suspend").on( "click", function() {
193                     var id = $(this).attr("id").replace("suspend", "");
194                     var hold = holds[id];
195                     $("#suspend-modal-title").html( hold.title );
196                     $("#suspend-modal-reserve_id").val( hold.reserve_id );
197                     $('#suspend-modal').modal('show');
198                 });
199
200                 $(".hold-resume").on( "click", function() {
201                     var id = $(this).attr("id").replace("resume", "");
202                     var hold = holds[id];
203                     $.post('/cgi-bin/koha/svc/hold/resume', { "reserve_id": hold.reserve_id }, function( data ){
204                       if ( data.success ) {
205                           holdsTable.api().ajax.reload();
206                       } else {
207                         if ( data.error == "HOLD_NOT_FOUND" ) {
208                             alert( __("Unable to resume, hold not found") );
209                             holdsTable.api().ajax.reload();
210                         }
211                       }
212                     });
213                 });
214
215                 $(".hold_location_select").change(function(){
216                     $(this).prop("disabled",true);
217                     var cur_select = $(this);
218                     var res_id = $(this).attr('reserve_id');
219                     $(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>');
220                     var api_url = '/api/v1/holds/'+res_id;
221                     var update_info = JSON.stringify({ pickup_library_id: $(this).val(), priority: parseInt($(this).attr("priority"),10) });
222                     $.ajax({
223                         method: "PUT",
224                         url: api_url,
225                         data: update_info ,
226                         success: function( data ){ holdsTable.api().ajax.reload(); },
227                         error: function( jqXHR, textStatus, errorThrown) {
228                             alert('There was an error:'+textStatus+" "+errorThrown);
229                             cur_select.prop("disabled",false);
230                             $("#updating_reserveno"+res_id).remove();
231                             cur_select.val( cur_select.children('option[selected="selected"]').val() );
232                         },
233                     });
234                 });
235
236             });
237
238             if ( $("#holds-table").length ) {
239                 $("#holds-table_processing").position({
240                     of: $( "#holds-table" ),
241                     collision: "none"
242                 });
243             }
244         }
245     }
246
247     $("body").append("\
248         <div id='suspend-modal' class='modal fade' role='dialog' aria-hidden='true'>\
249             <div class='modal-dialog'>\
250             <div class='modal-content'>\
251             <form id='suspend-modal-form' class='form-inline'>\
252                 <div class='modal-header'>\
253                     <button type='button' class='closebtn' data-dismiss='modal' aria-hidden='true'>×</button>\
254                     <h3 id='suspend-modal-label'>" + __("Suspend hold on") + " <i><span id='suspend-modal-title'></span></i></h3>\
255                 </div>\
256 \
257                 <div class='modal-body'>\
258                     <input type='hidden' id='suspend-modal-reserve_id' name='reserve_id' />\
259 \
260                     <label for='suspend-modal-until'>" + __("Suspend until:") + "</label>\
261                     <input name='suspend_until' id='suspend-modal-until' class='suspend-until' size='10' />\
262 \
263                     <p><a class='btn btn-link' id='suspend-modal-clear-date' >" + __("Clear date to suspend indefinitely") + "</a></p>\
264 \
265                 </div>\
266 \
267                 <div class='modal-footer'>\
268                     <button id='suspend-modal-submit' class='btn btn-primary' type='submit' name='submit'>" + __("Suspend") + "</button>\
269                     <a href='#' data-dismiss='modal' aria-hidden='true' class='cancel'>" + __("Cancel") + "</a>\
270                 </div>\
271             </form>\
272             </div>\
273             </div>\
274         </div>\
275     ");
276
277     $("#suspend-modal-until").datepicker({ minDate: 1 }); // Require that "until date" be in the future
278     $("#suspend-modal-clear-date").on( "click", function() { $("#suspend-modal-until").val(""); } );
279
280     $("#suspend-modal-submit").on( "click", function( e ) {
281         e.preventDefault();
282         $.post('/cgi-bin/koha/svc/hold/suspend', $('#suspend-modal-form').serialize(), function( data ){
283           $('#suspend-modal').modal('hide');
284           if ( data.success ) {
285               holdsTable.api().ajax.reload();
286           } else {
287             if ( data.error == "INVALID_DATE" ) {
288                 alert( __("Unable to suspend hold, invalid date") );
289             }
290             else if ( data.error == "HOLD_NOT_FOUND" ) {
291                 alert( __("Unable to suspend hold, hold not found") );
292                 holdsTable.api().ajax.reload();
293             }
294           }
295         });
296     });
297
298 });