Line 0
Link Here
|
0 |
- |
1 |
function update_picker(periodPicker, biblionumber) { |
|
|
2 |
|
3 |
// Disable input whilst we fetch dates |
4 |
$("#period").prop('disabled', true); |
5 |
|
6 |
// Fetch dates and re-enable input |
7 |
$.ajax({ |
8 |
url: '/api/v1/bookings?biblio_id=' + biblionumber, |
9 |
async: true, |
10 |
success: function(data) { |
11 |
var dates_to_disable = []; |
12 |
for (booking of data) { |
13 |
dates_to_disable.push({ |
14 |
from: booking.start_date, |
15 |
to: booking.end_date |
16 |
}); |
17 |
} |
18 |
periodPicker.set('disable', dates_to_disable); |
19 |
$("#period").prop('disabled', false); |
20 |
} |
21 |
}); |
22 |
}; |
23 |
|
24 |
$('#placeBookingModal').on('show.bs.modal', function(e) { |
25 |
var button = $(e.relatedTarget); |
26 |
var biblionumber = button.data('biblionumber'); |
27 |
$('#booking_biblio_id').val(biblionumber); |
28 |
|
29 |
$("#booking_patron_id").select2({ |
30 |
dropdownParent: $(".modal-content", "#placeBookingModal"), |
31 |
width: '30%', |
32 |
dropdownAutoWidth: true, |
33 |
allowClear: true, |
34 |
minimumInputLength: 3, |
35 |
ajax: { |
36 |
url: '/api/v1/patrons', |
37 |
delay: 250, |
38 |
dataType: 'json', |
39 |
data: function(params) { |
40 |
var search_term = (params.term === undefined) ? '' : params.term; |
41 |
var query = { |
42 |
'q': JSON.stringify({ |
43 |
"-or": [ |
44 |
{ |
45 |
"firstname": { |
46 |
"-like": search_term + '%' |
47 |
} |
48 |
}, |
49 |
{ |
50 |
"surname": { |
51 |
"-like": search_term + '%' |
52 |
} |
53 |
}, |
54 |
{ |
55 |
"cardnumber": { |
56 |
"-like": search_term + '%' |
57 |
} |
58 |
} |
59 |
] |
60 |
}), |
61 |
'_order_by': 'firstname' |
62 |
}; |
63 |
return query; |
64 |
}, |
65 |
processResults: function(data) { |
66 |
var results = []; |
67 |
data.forEach(function(patron) { |
68 |
results.push({ |
69 |
"id": patron.patron_id, |
70 |
"text": escape_str(patron.firstname) + " " + escape_str(patron.surname) + " (" + escape_str(patron.cardnumber) + ")" |
71 |
}); |
72 |
}); |
73 |
return { |
74 |
"results": results |
75 |
}; |
76 |
} |
77 |
}, |
78 |
placeholder: "Search for a patron" |
79 |
}); |
80 |
|
81 |
var periodPicker = $("#period").flatpickr({ |
82 |
mode: "range", |
83 |
minDate: "today", |
84 |
dateFormat: "Y-m-d", |
85 |
onChange: function(selectedDates) { |
86 |
var _this = this; |
87 |
var dateArr = selectedDates.map(function(date) { |
88 |
return _this.formatDate(date, 'Y-m-d'); |
89 |
}); |
90 |
$('#booking_start_date').val(dateArr[0]); |
91 |
$('#booking_end_date').val(dateArr[1]); |
92 |
} |
93 |
}); |
94 |
|
95 |
// Get blocked dates |
96 |
update_picker(periodPicker, biblionumber); |
97 |
}); |
98 |
|
99 |
$("#placeBookingForm").on('submit', function(e) { |
100 |
e.preventDefault(); |
101 |
|
102 |
var url = '/api/v1/bookings'; |
103 |
|
104 |
var start_date = $datetime($('#booking_start_date').val(), { |
105 |
dateformat: "rfc3339" |
106 |
}); |
107 |
var end_date = $datetime($('#booking_end_date').val(), { |
108 |
dateformat: "rfc3339" |
109 |
}); |
110 |
|
111 |
var posting = $.post( |
112 |
url, |
113 |
JSON.stringify({ |
114 |
"start_date": start_date, |
115 |
"end_date": end_date, |
116 |
"biblio_id": $('#booking_biblio_id').val(), |
117 |
"item_id": $('#booking_item_id').val() ? $('#booking_item_id').val() : null, |
118 |
"patron_id": $('#booking_patron_id').find(':selected').val() |
119 |
}) |
120 |
); |
121 |
|
122 |
posting.done(function(data) { |
123 |
$('#placeBookingModal').modal('hide'); |
124 |
}); |
125 |
|
126 |
posting.fail(function(data) { |
127 |
$('#result').replaceWith('<div id="result" class="alert alert-danger">Failure</div>'); |
128 |
}); |
129 |
}); |