Bug 27779: (QA follow-up) Better translatability
[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 table_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     }, table_settings);
48
49     if( no_op_set ){
50         $('#branch').change(function() {
51             $('#op').val("");
52             $('#selectlibrary').submit();
53         });
54         $('#newnotice').click(function() {
55             $('#op').val("add_form");
56             return true;
57         });
58     }
59
60     $("#newmodule").on("change",function(){
61         var branchcode;
62         if( $("#branch").val() === ""){
63             branchcode = "*";
64         } else {
65             branchcode = $("#branch").val();
66         }
67         window.location.href = "/cgi-bin/koha/tools/letter.pl?op=add_form&module=" + $(this).val() + "&branchcode=" + branchcode;
68     });
69
70     $("#submit_form").on("click",function(){
71         $("#add_notice").submit();
72     });
73
74     $("#add_notice").validate({
75         submitHandler: function(form){
76             var at_least_one_exists = 0;
77             var are_valid = 1;
78             $("fieldset.mtt").each( function(){
79                 var title = $(this).find('input[name="title"]').val();
80                 var content = $(this).find('textarea[name="content"]').val();
81                 if (
82                     ( title.length === 0 && content.length > 0 ) || ( title.length > 0 && content.length === 0 )
83                 ) {
84                     var mtt = $(this).find('input[name="message_transport_type"]').val();
85                     at_least_one_exists = 1; // Only one template has to be filled in for form to be valid
86                     alert(__("Please specify title and content for %s").format(mtt));
87                     are_valid = 0;
88                 } else if ( title.length > 0 && content.length > 0 ) {
89                     at_least_one_exists = 1;
90                 }
91             });
92
93             if ( ! at_least_one_exists ) {
94                 // No templates were filled out
95                 alert( __("Please fill at least one template.") );
96                 return false;
97             }
98
99             if ( ! are_valid ){
100                 return false;
101             }
102
103             // Test if code already exists in DB
104             if( editing == 1 ){ // This is an edit operation
105                 // We don't need to check for an existing Code
106             } else {
107                 var new_lettercode = $("#code").val();
108                 var new_branchcode = $("#branch").val();
109                 var code_check = checkCodes( new_lettercode, new_branchcode );
110                 if( code_check.responseJSON.letters.length > 0 ){
111                     return false;
112                 }
113             }
114             form.submit();
115         }
116     });
117
118     var sms_limit = 160;
119     $(".content_sms").on("keyup", function(){
120         var length = $(this).val().length;
121         var sms_counter = ("#sms_counter_" + $(this).data('lang'));
122         $(sms_counter).html(length + "/" + sms_limit + _(" characters"));
123         if ( length  > sms_limit ) {
124             $(sms_counter).css("color", "red");
125         } else {
126             $(sms_counter).css("color", "black");
127         }
128     });
129
130     let section = $("#section").val();
131     if( section != "" ){
132         $("a[href='#" + section + "']").click();
133     }
134
135     $(".panel-group").on("shown.bs.collapse", function (e) {
136         $("#section").val( e.target.id );
137     }).on("hidden.bs.collapse", function (e) {
138         $("#section").val("");
139     });
140
141     if( $("#tabs").length > 0 ){
142         let langtab = $("#langtab").val();
143         $("#tabs a[data-toggle='tab']").on("shown.bs.tab", function (e) {
144             var link = e.target.hash.replace("#","");
145             $("#langtab").val( link );
146         });
147
148         if( langtab != "" ){
149             $("#tabs a[href='#" + langtab + "']").tab("show");
150         } else {
151             $("#tabs a:first").tab("show");
152         }
153     }
154
155     $(".insert").on("click",function(){
156         var containerid = $(this).data("containerid");
157         insertValueQuery( containerid );
158     });
159
160     $("#saveandcontinue").on("click",function(e){
161         e.preventDefault();
162         $("#redirect").val("just_save");
163         $("#submit_form").click();
164     });
165
166     $("body").on("click", ".preview_template", function(e){
167         e.preventDefault();
168         var mtt = $(this).data("mtt");
169         var lang = $(this).data("lang");
170
171         var code = $("#code").val();
172         var content = $("#content_"+mtt+"_"+lang).val();
173         var title = $("#title_"+mtt+"_"+lang).val();
174
175         var is_html = $("#is_html_"+mtt+"_"+lang).val();
176         var page = $(this).attr("href");
177         var data_preview = $("#data_preview").val();
178         page += '?code='+encodeURIComponent(code);
179         page += '&title='+encodeURIComponent(title);
180         page += '&content='+encodeURIComponent(content);
181         page += '&data_preview='+encodeURIComponent(data_preview);
182         page += '&is_html='+encodeURIComponent(is_html);
183         $("#preview_template .modal-body").load(page + " .main");
184         $('#preview_template').modal('show');
185         $("#preview_template_button").attr("href", "/cgi-bin/koha/svc/letters/convert?module="+module+"&code="+code+"&mtt="+mtt+"&lang="+lang);
186     });
187
188     $("#preview_template").on("hidden.bs.modal", function(){
189         $("#preview_template_label").html("");
190         $("#preview_template .modal-body").html( modal_loading );
191     });
192
193     function insertValueQuery(containerid) {
194         var fieldset = $("#" + containerid);
195         var myQuery = $(fieldset).find('textarea[name="content"]');
196         var myListBox = $(fieldset).find('select[name="SQLfieldname"]');
197
198         if($(myListBox).find('option').length > 0) {
199             $(myListBox).find('option').each( function (){
200                 if ( $(this).prop('selected') && $(this).val().length > 0 ) {
201                     $(myQuery).insertAtCaret("<<" + $(this).val() + ">>");
202                 }
203             });
204         }
205     }
206
207 });