Bug 33417: Remove old components
[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 !== '' || copy_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 function confirmOverwrite( new_lettercode, new_branchcode ){
34     var letter_exists;
35     $.ajax({
36         data: { code: new_lettercode, branchcode: new_branchcode },
37         type: 'GET',
38         url: '/cgi-bin/koha/svc/letters/get/',
39         async: !1,
40         success: function (data) {
41             if ( data.letters.length > 0 ) {
42                 letter_exists = 1;
43             }
44         }
45     });
46     if(letter_exists){
47         return confirm(__("A letter with the code '%s' already exists for '%s'. Overwrite this letter?").format(new_lettercode, new_branchcode));
48     }
49 }
50
51 var Sticky;
52
53 $(document).ready(function() {
54     if( add_form || copy_form ){
55         Sticky = $("#toolbar");
56         Sticky.hcSticky({
57             stickTo: ".main",
58             stickyClass: "floating"
59         });
60     }
61
62     var ntable = KohaTable("lettert", {
63         "autoWidth": false,
64         "paging": false,
65     }, table_settings);
66
67     if( no_op_set ){
68         $('#branch').change(function() {
69             $('#op').val("");
70             $('#selectlibrary').submit();
71         });
72         $('#newnotice').click(function() {
73             $('#op').val("add_form");
74             return true;
75         });
76     }
77
78     $("#newmodule").on("change",function(){
79         var branchcode;
80         if( $("#branch").val() === ""){
81             branchcode = "*";
82         } else {
83             branchcode = $("#branch").val();
84         }
85         window.location.href = "/cgi-bin/koha/tools/letter.pl?op=add_form&module=" + $(this).val() + "&branchcode=" + branchcode;
86     });
87
88     $("#submit_form").on("click",function(){
89         $("#add_notice").submit();
90     });
91
92     $("#add_notice").validate({
93         submitHandler: function(form){
94             var at_least_one_exists = 0;
95             var are_valid = 1;
96             $("fieldset.mtt").each( function(){
97                 var title = $(this).find('input[name="title"]').val();
98                 var content = $(this).find('textarea[name="content"]').val();
99                 if (
100                     ( title.length === 0 && content.length > 0 ) || ( title.length > 0 && content.length === 0 )
101                 ) {
102                     var mtt = $(this).find('input[name="message_transport_type"]').val();
103                     at_least_one_exists = 1; // Only one template has to be filled in for form to be valid
104                     alert(__("Please specify title and content for %s").format(mtt));
105                     are_valid = 0;
106                 } else if ( title.length > 0 && content.length > 0 ) {
107                     at_least_one_exists = 1;
108                 }
109             });
110
111             if ( ! at_least_one_exists ) {
112                 // No templates were filled out
113                 alert( __("Please fill at least one template.") );
114                 return false;
115             }
116
117             if ( ! are_valid ){
118                 return false;
119             }
120
121             // Test if code already exists in DB
122             if( editing == 1 ){ // This is an edit operation
123                 // We don't need to check for an existing Code
124                 // However if we're copying, provide confirm
125                 // pop up of overwriting existing notice or slip
126                 if(copy_form == 1){
127                     var new_lettercode = $("#code").val();
128                     var new_branchcode = $("#branch").val();
129                     var confirm = confirmOverwrite( new_lettercode, new_branchcode );
130                     if(confirm === false){
131                         return false;
132                     }
133                 }
134             } else {
135                 var new_lettercode = $("#code").val();
136                 var new_branchcode = $("#branch").val();
137                 var code_check = checkCodes( new_lettercode, new_branchcode );
138                 if( code_check.responseJSON.letters.length > 0 ){
139                     return false;
140                 }
141             }
142             form.submit();
143         }
144     });
145
146     var sms_limit = 160;
147     $(".content_sms").on("keyup", function(){
148         var length = $(this).val().length;
149         var sms_counter = ("#sms_counter_" + $(this).data('lang'));
150         $(sms_counter).html(length + "/" + sms_limit + __(" characters"));
151         if ( length  > sms_limit ) {
152             $(sms_counter).css("color", "red");
153         } else {
154             $(sms_counter).css("color", "black");
155         }
156     });
157
158     let section = $("#section").val();
159     if( section != "" ){
160         $("a[href='#" + section + "']").click();
161     }
162
163     $(".panel-group").on("shown.bs.collapse", function (e) {
164         $("#section").val( e.target.id );
165     }).on("hidden.bs.collapse", function (e) {
166         $("#section").val("");
167     });
168
169     if( $("#tabs").length > 0 ){
170         let langtab = $("#langtab").val();
171         $("#tabs a[data-toggle='tab']").on("shown.bs.tab", function (e) {
172             var link = e.target.hash.replace("#","");
173             $("#langtab").val( link );
174         });
175
176         if( langtab != "" ){
177             $("#tabs a[href='#" + langtab + "']").tab("show");
178         } else {
179             $("#tabs a[href='#lang_default_panel']").tab("show");
180         }
181     }
182
183     $(".insert").on("click",function(){
184         var containerid = $(this).data("containerid");
185         insertValueQuery( containerid );
186     });
187
188     $("#saveandcontinue").on("click",function(e){
189         e.preventDefault();
190         $("#redirect").val("just_save");
191         $("#submit_form").click();
192     });
193
194     $("body").on("click", ".preview_template", function(e){
195         e.preventDefault();
196         var mtt = $(this).data("mtt");
197         var lang = $(this).data("lang");
198
199         var code = $("#code").val();
200         var content = $("#content_"+mtt+"_"+lang).val();
201         var title = $("#title_"+mtt+"_"+lang).val();
202
203         var is_html = $("#is_html_"+mtt+"_"+lang).val();
204         var page = $(this).attr("href");
205         var data_preview = $("#data_preview").val();
206         page += '?code='+encodeURIComponent(code);
207         page += '&title='+encodeURIComponent(title);
208         page += '&content='+encodeURIComponent(content);
209         page += '&data_preview='+encodeURIComponent(data_preview);
210         page += '&is_html='+encodeURIComponent(is_html);
211         $("#preview_template .modal-body").load(page + " .main");
212         $('#preview_template').modal('show');
213         $("#preview_template_button").attr("href", "/cgi-bin/koha/svc/letters/convert?module="+module+"&code="+code+"&mtt="+mtt+"&lang="+lang);
214     });
215
216     $("#preview_template").on("hidden.bs.modal", function(){
217         $("#preview_template_label").html("");
218         $("#preview_template .modal-body").html( modal_loading );
219     });
220
221     function insertValueQuery(containerid) {
222         var fieldset = $("#" + containerid);
223         var myQuery = $(fieldset).find('textarea[name="content"]');
224         var myListBox = $(fieldset).find('select[name="SQLfieldname"]');
225
226         if($(myListBox).find('option').length > 0) {
227             $(myListBox).find('option').each( function (){
228                 if ( $(this).prop('selected') && $(this).val().length > 0 ) {
229                     $(myQuery).insertAtCaret("<<" + $(this).val() + ">>");
230                 }
231             });
232         }
233     }
234
235 });