Bug 29407: Make the pickup locations dropdown JS reusable
[koha.git] / koha-tmpl / intranet-tmpl / prog / js / messaging-preference-form.js
1 $(document).ready(function(){
2     $("#info_digests").tooltip();
3
4     var message_prefs_dirty = false;
5     $('#memberentry_messaging_prefs > *').change(function() {
6         message_prefs_dirty = true;
7     });
8
9     if( $("#messaging_prefs_loading").length ){ // This element only appears in the template if op=add
10         $('#categorycode_entry').change(function() {
11             var messaging_prefs_loading = $("#messaging_prefs_loading");
12             // Upon selecting a new patron category, show "Loading" message for messaging defaults
13             messaging_prefs_loading.show();
14             var categorycode = $(this).val();
15             if (message_prefs_dirty) {
16                 if (!confirm( MSG_MESSAGING_DFEAULTS )) {
17                     // Not loading messaging defaults. Hide loading indicator
18                     messaging_prefs_loading.hide();
19                     return;
20                 }
21             }
22             var jqxhr = $.getJSON('/cgi-bin/koha/members/default_messageprefs.pl?categorycode=' + categorycode, function(data) {
23                 $.each(data.messaging_preferences, function(i, item) {
24                     var attrid = item.message_attribute_id;
25                     var transports = ['email', 'rss', 'sms'];
26                     $.each(transports, function(j, transport) {
27                         if (item['transports_' + transport] == 1) {
28                             $('#' + transport + attrid).prop('checked', true);
29                         } else {
30                             $('#' + transport + attrid).prop('checked', false);
31                         }
32                     });
33                     if (item.digest && item.digest != ' ') {
34                         $('#digest' + attrid).prop('checked', true);
35                     } else {
36                         $('#digest' + attrid).prop('checked', false);
37                     }
38                     if (item.takes_days == '1') {
39                         $('[name=' + attrid + '-DAYS]').val('' + item.days_in_advance);
40                     }
41                 });
42                 message_prefs_dirty = false;
43             })
44                 .always(function() {
45                     // Loaded messaging defaults. Hide loading indicator
46                     messaging_prefs_loading.hide();
47                 });
48         });
49     }
50
51     function toggle_digest(id){
52         if ( $("#email"+id).prop("checked") || $("#sms"+id).prop("checked") ) {
53             $("#digest"+id).attr("disabled", false).tooltip('disable');
54         } else {
55             $("#digest"+id).attr("disabled", true).prop("checked",false).tooltip('enable');
56         }
57
58     }
59     // At load time, we want digest disabled if no digest using transport is enabled
60     $(".pmp_email").each(function(){
61         toggle_digest(Number($(this).attr("id").replace("email", "")));
62     });
63
64     // If user clears all digest using transports for a notice, disable digest checkbox
65     $(".pmp_email").click(function(){
66         toggle_digest(Number($(this).attr("id").replace("email", "")));
67     });
68     $(".pmp_sms").click(function(){
69         toggle_digest(Number($(this).attr("id").replace("sms", "")));
70     });
71
72 //    $('#memberentry_messaging_prefs [data-toggle="tooltip"][disabled]').tooltip();
73 });