Bug 6815: Capture member photo via webcam
[koha.git] / koha-tmpl / intranet-tmpl / prog / js / members-menu.js
1 /* global borrowernumber advsearch dateformat __ CAN_user_borrowers_delete_borrowers CAN_user_borrowers_edit_borrowers number_of_adult_categories destination Sticky Cookies*/
2
3 $(document).ready(function(){
4
5     $("#filteraction_off, #filteraction_on").on('click', function(e) {
6         e.preventDefault();
7         $('#filters').toggle();
8         $('.filteraction').toggle();
9         if (typeof Sticky !== "undefined" && typeof hcSticky === "function") {
10             Sticky.hcSticky('update');
11         }
12     });
13     if( advsearch ){
14         $("#filteraction_on").toggle();
15         $("#filters").show();
16     } else {
17         $("#filteraction_off").toggle();
18     }
19
20     searchfield_date_tooltip("");
21     searchfield_date_tooltip('_filter');
22     $("#searchfieldstype").change(function() {
23         searchfield_date_tooltip("");
24     });
25     $("#searchfieldstype_filter").change(function() {
26         searchfield_date_tooltip('_filter');
27     });
28
29     if( CAN_user_borrowers_delete_borrowers ){
30         $("#deletepatron").click(function(){
31             window.location='/cgi-bin/koha/members/deletemem.pl?member=' + borrowernumber;
32         });
33     }
34     if( CAN_user_borrowers_edit_borrowers ){
35         $("#renewpatron").click(function(){
36             confirm_reregistration();
37             $(".btn-group").removeClass("open");
38             return false;
39         });
40         $("#updatechild").click(function(e){
41             if( $(this).data("toggle") == "tooltip"){ // Disabled menu option has tooltip attribute
42                 e.preventDefault();
43             } else {
44                 update_child();
45                 $(".btn-group").removeClass("open");
46             }
47         });
48     }
49
50     $("#updatechild, #patronflags, #renewpatron, #deletepatron, #exportbarcodes").tooltip();
51     $("#exportcheckins").click(function(){
52         export_barcodes();
53         $(".btn-group").removeClass("open");
54         return false;
55     });
56     $("#printsummary").click(function(){
57         printx_window("page");
58         $(".btn-group").removeClass("open");
59         return false;
60     });
61     $("#printslip").click(function(){
62         printx_window("slip");
63         $(".btn-group").removeClass("open");
64         return false;
65     });
66     $("#printquickslip").click(function(){
67         printx_window("qslip");
68         $(".btn-group").removeClass("open");
69         return false;
70     });
71     $("#print_overdues").click(function(){
72         window.open("/cgi-bin/koha/members/print_overdues.pl?borrowernumber=" + borrowernumber, "printwindow");
73         $(".btn-group").removeClass("open");
74         return false;
75     });
76     $("#printcheckinslip").click(function(){
77         printx_window("checkinslip");
78         $(".btn-group").removeClass("open");
79         return false;
80     });
81     $("#printclearscreen").click(function(){
82         printx_window("slip");
83         window.location.replace("/cgi-bin/koha/circ/circulation.pl");
84     });
85     $("#searchtohold").click(function(){
86         searchToHold();
87         return false;
88     });
89     $("#select_patron_messages").on("change",function(){
90         $("#borrower_message").val( $(this).val() );
91     });
92
93     $("#patronImageEdit").on("shown.bs.modal", function(){
94         startup();
95     });
96
97     $(".edit-patronimage").on("click", function(e){
98         e.preventDefault();
99         var borrowernumber = $(this).data("borrowernumber");
100         var cardnumber = $(this).data("cardnumber");
101         var modalTitle = $(this).attr("title");
102         $.ajax({
103             url: "/cgi-bin/koha/members/moremember-patronimage.pl",
104             type: "GET",
105             data: { borrowernumber: borrowernumber, cardnumber: cardnumber },
106             success: function ( data ) {
107                 $("#patronImageEdit .modal-body").html( data );
108                 $("#patronImageEdit .modal-title").text(modalTitle);
109                 $("#patronImageEdit").modal("show");
110             },
111             error: function () {
112                 location.href="/cgi-bin/koha/members/moremember-patronimage.pl?borrowernumber=" + borrowernumber;
113             }
114         });
115         $("#patronImageEdit").on("hidden.bs.modal", function(){
116             /* Stop using the user's camera when modal is closed */
117             let viewfinder = document.getElementById("viewfinder");
118             if( viewfinder.srcObject ){
119                 viewfinder.srcObject.getTracks().forEach( track => {
120                     if( track.readyState == 'live' && track.kind === 'video'){
121                         track.stop();
122                     }
123                 });
124             }
125         });
126     });
127 });
128
129 function searchfield_date_tooltip(filter) {
130     var field = "#searchmember" + filter;
131     var type = "#searchfieldstype" + filter;
132     if ( $(type).val() == 'dateofbirth' ) {
133         var MSG_DATE_FORMAT = "";
134         if( dateformat == 'us' ){
135             MSG_DATE_FORMAT = __("Dates of birth should be entered in the format 'MM/DD/YYYY'");
136         } else if( dateformat == 'iso' ){
137             MSG_DATE_FORMAT = __("Dates of birth should be entered in the format 'YYYY-MM-DD'");
138         } else if( dateformat == 'metric' ){
139             MSG_DATE_FORMAT = __("Dates of birth should be entered in the format 'DD/MM/YYYY'");
140         } else if( dateformat == 'dmydot' ){
141             MSG_DATE_FORMAT = __("Dates of birth should be entered in the format 'DD.MM.YYYY'");
142         }
143         $(field).attr("title", MSG_DATE_FORMAT).tooltip('show');
144     } else {
145         $(field).tooltip('destroy');
146     }
147 }
148
149 function confirm_updatechild() {
150     var is_confirmed = window.confirm( __("Are you sure you want to update this child to an Adult category? This cannot be undone.") );
151     if (is_confirmed) {
152         window.location='/cgi-bin/koha/members/update-child.pl?op=update&borrowernumber=' + borrowernumber;
153     }
154 }
155
156 function update_child() {
157     if( number_of_adult_categories > 1 ){
158         window.open('/cgi-bin/koha/members/update-child.pl?op=multi&borrowernumber=' + borrowernumber,'UpdateChild','width=400,height=300,toolbar=no,scrollbars=yes,resizable=yes');
159     } else {
160         confirm_updatechild();
161     }
162 }
163
164 function confirm_reregistration() {
165     var is_confirmed = window.confirm( __("Are you sure you want to renew this patron's registration?") );
166     if (is_confirmed) {
167         window.location = '/cgi-bin/koha/members/setstatus.pl?borrowernumber=' + borrowernumber + '&destination=' + destination + '&reregistration=y';
168     }
169 }
170 function export_barcodes() {
171     window.open('/cgi-bin/koha/members/readingrec.pl?borrowernumber=' + borrowernumber + '&op=export_barcodes');
172 }
173 var slip_re = /slip/;
174 function printx_window(print_type) {
175     var handler = print_type.match(slip_re) ? "printslip" : "summary-print";
176     window.open("/cgi-bin/koha/members/" + handler + ".pl?borrowernumber=" + borrowernumber + "&print=" + print_type, "printwindow");
177     return false;
178 }
179 function searchToHold(){
180     var date = new Date();
181     date.setTime(date.getTime() + (10 * 60 * 1000));
182     Cookies.set("holdfor", borrowernumber, { path: "/", expires: date });
183     location.href="/cgi-bin/koha/catalogue/search.pl";
184 }