Bug 29407: Make the pickup locations dropdown JS reusable
[koha.git] / koha-tmpl / intranet-tmpl / prog / js / letter.js
1 /* Variables defined in letter.tt: */
2 /* global _ module add_form copy_form no_op_set code interface theme KohaTable columns_settings */
3
4 var modal_loading = "<div id=\"loading\"><img src=\"" + interface + "/" + theme + "/img/spinner-small.gif\" alt=\"\" /> "+ __('Loading...') +"</div>";
5
6 var editing = 0;
7 if( add_form == 1 && code !== '' ){
8     editing = 1;
9 }
10
11 function checkCodes( new_lettercode, new_branchcode ){
12     $(".spinner").show();
13     return $.ajax({
14         data: { code: new_lettercode, branchcode: new_branchcode },
15         type: 'GET',
16         url: '/cgi-bin/koha/svc/letters/get/',
17         async: !1,
18         success: function (data) {
19             if ( data.letters.length > 0 ) {
20                 if( new_branchcode === '' ) {
21                     alert(__("A default letter with the code '%s' already exists.").format(new_lettercode));
22                 } else {
23                     alert(__("A letter with the code '%s' already exists for '%s'.").format(new_lettercode, new_branchcode));
24                 }
25                 $(".spinner").hide();
26             } else {
27                 $(".spinner").hide();
28             }
29         }
30     });
31 }
32
33 var Sticky;
34
35 $(document).ready(function() {
36     if( add_form || copy_form ){
37         Sticky = $("#toolbar");
38         Sticky.hcSticky({
39             stickTo: ".main",
40             stickyClass: "floating"
41         });
42     }
43
44     var ntable = KohaTable("lettert", {
45         "autoWidth": false,
46         "paging": false,
47         "aaSorting": [[ 3, "asc" ]],
48     }, columns_settings);
49
50     if( no_op_set ){
51         $('#branch').change(function() {
52             $('#op').val("");
53             $('#selectlibrary').submit();
54         });
55         $('#newnotice').click(function() {
56             $('#op').val("add_form");
57             return true;
58         });
59     }
60
61     $("#newmodule").on("change",function(){
62         var branchcode;
63         if( $("#branch").val() === ""){
64             branchcode = "*";
65         } else {
66             branchcode = $("#branch").val();
67         }
68         window.location.href = "/cgi-bin/koha/tools/letter.pl?op=add_form&module=" + $(this).val() + "&branchcode=" + branchcode;
69     });
70
71     $("#submit_form").on("click",function(){
72         $("#add_notice").submit();
73     });
74
75     $("#add_notice").validate({
76         submitHandler: function(form){
77             var at_least_one_exists = 0;
78             var are_valid = 1;
79             $("fieldset.mtt").each( function(){
80                 var title = $(this).find('input[name="title"]').val();
81                 var content = $(this).find('textarea[name="content"]').val();
82                 if (
83                     ( title.length === 0 && content.length > 0 ) || ( title.length > 0 && content.length === 0 )
84                 ) {
85                     var mtt = $(this).find('input[name="message_transport_type"]').val();
86                     at_least_one_exists = 1; // Only one template has to be filled in for form to be valid
87                     alert(__("Please specify title and content for %s").format(mtt));
88                     are_valid = 0;
89                 } else if ( title.length > 0 && content.length > 0 ) {
90                     at_least_one_exists = 1;
91                 }
92             });
93
94             if ( ! at_least_one_exists ) {
95                 // No templates were filled out
96                 alert( __("Please fill at least one template.") );
97                 return false;
98             }
99
100             if ( ! are_valid ){
101                 return false;
102             }
103
104             // Test if code already exists in DB
105             if( editing == 1 ){ // This is an edit operation
106                 // We don't need to check for an existing Code
107             } else {
108                 var new_lettercode = $("#code").val();
109                 var new_branchcode = $("#branch").val();
110                 var code_check = checkCodes( new_lettercode, new_branchcode );
111                 if( code_check.responseJSON.letters.length > 0 ){
112                     return false;
113                 }
114             }
115             form.submit();
116         }
117     });
118
119     var sms_limit = 160;
120     $(".content_sms").on("keyup", function(){
121         var length = $(this).val().length;
122         var sms_counter = ("#sms_counter_" + $(this).data('lang'));
123         $(sms_counter).html(length + "/" + sms_limit + _(" characters"));
124         if ( length  > sms_limit ) {
125             $(sms_counter).css("color", "red");
126         } else {
127             $(sms_counter).css("color", "black");
128         }
129     });
130
131     $( ".transport-types" ).accordion({
132         collapsible: true,
133         active: parseInt( $("#section").val(), 10),
134         animate: 200,
135         activate: function() {
136             var active = $( ".transport-types" ).accordion( "option", "active" );
137             if( active === false ){
138                 $("#section").val("");
139             } else {
140                 $("#section").val( active );
141             }
142         }
143     });
144
145     $(".insert").on("click",function(){
146         var containerid = $(this).data("containerid");
147         insertValueQuery( containerid );
148     });
149
150     $("#saveandcontinue").on("click",function(e){
151         e.preventDefault();
152         $("#redirect").val("just_save");
153         $("#submit_form").click();
154     });
155
156     $("#tabs").tabs();
157
158     $("body").on("click", ".preview_template", function(e){
159         e.preventDefault();
160         var mtt = $(this).data("mtt");
161         var lang = $(this).data("lang");
162
163         var code = $("#code").val();
164         var content = $("#content_"+mtt+"_"+lang).val();
165         var title = $("#title_"+mtt+"_"+lang).val();
166
167         var is_html = $("#is_html_"+mtt+"_"+lang).val();
168         var page = $(this).attr("href");
169         var data_preview = $("#data_preview").val();
170         page += '?code='+encodeURIComponent(code);
171         page += '&title='+encodeURIComponent(title);
172         page += '&content='+encodeURIComponent(content);
173         page += '&data_preview='+encodeURIComponent(data_preview);
174         page += '&is_html='+encodeURIComponent(is_html);
175         $("#preview_template .modal-body").load(page + " .main");
176         $('#preview_template').modal('show');
177         $("#preview_template_button").attr("href", "/cgi-bin/koha/svc/letters/convert?module="+module+"&code="+code+"&mtt="+mtt+"&lang="+lang);
178     });
179
180     $("#preview_template").on("hidden.bs.modal", function(){
181         $("#preview_template_label").html("");
182         $("#preview_template .modal-body").html( modal_loading );
183     });
184
185     function insertValueQuery(containerid) {
186         var fieldset = $("#" + containerid);
187         var myQuery = $(fieldset).find('textarea[name="content"]');
188         var myListBox = $(fieldset).find('select[name="SQLfieldname"]');
189
190         if($(myListBox).find('option').length > 0) {
191             $(myListBox).find('option').each( function (){
192                 if ( $(this).attr('selected') && $(this).val().length > 0 ) {
193                     $(myQuery).insertAtCaret("<<" + $(this).val() + ">>");
194                 }
195             });
196         }
197     }
198
199 });