Bug 30393: Make datatables wrapper handle searching for %, _, \
[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 ( window.location.href.indexOf("op=add") === -1 ) {
10         message_prefs_dirty = true; // unless op=add consider the message prefs dirty
11     }
12
13     if( $("#messaging_prefs_loading").length ){ // This element only appears in the template if op=add
14         $('#categorycode_entry').change(function() {
15             var messaging_prefs_loading = $("#messaging_prefs_loading");
16             // Upon selecting a new patron category, show "Loading" message for messaging defaults
17             messaging_prefs_loading.show();
18             var categorycode = $(this).val();
19             if (message_prefs_dirty) {
20                 if (!confirm( MSG_MESSAGING_DFEAULTS )) {
21                     // Not loading messaging defaults. Hide loading indicator
22                     messaging_prefs_loading.hide();
23                     return;
24                 }
25             }
26             var jqxhr = $.getJSON('/cgi-bin/koha/members/default_messageprefs.pl?categorycode=' + categorycode, function(data) {
27                 $.each(data.messaging_preferences, function(i, item) {
28                     var attrid = item.message_attribute_id;
29                     var transports = ['email', 'rss', 'sms'];
30                     $.each(transports, function(j, transport) {
31                         if (item['transports_' + transport] == 1) {
32                             $('#' + transport + attrid).prop('checked', true);
33                             toggle_digest(attrid);
34                         } else {
35                             $('#' + transport + attrid).prop('checked', false);
36                             toggle_digest(attrid);
37                         }
38                     });
39                     if (item.digest && item.digest != ' ') {
40                         $('#digest' + attrid).prop('checked', true);
41                     } else {
42                         $('#digest' + attrid).prop('checked', false);
43                     }
44                     if (item.takes_days == '1') {
45                         $('[name=' + attrid + '-DAYS]').val('' + item.days_in_advance);
46                     }
47                 });
48                 message_prefs_dirty = false;
49             })
50                 .always(function() {
51                     // Loaded messaging defaults. Hide loading indicator
52                     messaging_prefs_loading.hide();
53                 });
54         });
55     }
56
57     function toggle_digest(id){
58         if ( $("#email"+id).prop("checked") || $("#sms"+id).prop("checked") ) {
59             $("#digest"+id).attr("disabled", false).tooltip('disable');
60         } else {
61             $("#digest"+id).attr("disabled", true).prop("checked",false).tooltip('enable');
62         }
63
64     }
65     // At load time, we want digest disabled if no digest using transport is enabled
66     $(".pmp_email").each(function(){
67         toggle_digest(Number($(this).attr("id").replace("email", "")));
68     });
69
70     // If user clears all digest using transports for a notice, disable digest checkbox
71     $(".pmp_email").click(function(){
72         toggle_digest(Number($(this).attr("id").replace("email", "")));
73     });
74     $(".pmp_sms").click(function(){
75         toggle_digest(Number($(this).attr("id").replace("sms", "")));
76     });
77
78 //    $('#memberentry_messaging_prefs [data-toggle="tooltip"][disabled]').tooltip();
79 });