Bug 21795: Switch two-column templates to Bootstrap grid: Notices and slips
[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 MSG_EMPTY_TITLE_AND_CONTENT MSG_EMPTY_TEMPLATES code MSG_CODE_EXISTS MSG_CODE_EXISTS_FOR_LIBRARY MSG_DT_LOADING_RECORDS MSG_NO_NOTICE_FOUND interface theme KohaTable columns_settings */
3
4 var modal_loading = "<div id=\"loading\"><img src=\"" + interface + "/" + theme + "/img/spinner-small.gif\" alt=\"\" /> "+ MSG_DT_LOADING_RECORDS +"</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( MSG_CODE_EXISTS.format(new_lettercode));
22                 } else {
23                     alert( MSG_CODE_EXISTS_FOR_LIBRARY.format(new_lettercode, new_branchcode) );
24                 }
25                 $(".spinner").hide();
26             } else {
27                 $(".spinner").hide();
28             }
29         }
30     });
31 }
32
33 $(document).ready(function() {
34     if( add_form || copy_form ){
35         $('#toolbar').fixFloat();
36     }
37
38     var ntable = KohaTable("lettert", {
39         "autoWidth": false,
40         "paging": false,
41         "aaSorting": [[ 3, "asc" ]],
42         "columnDefs": [
43             { "bSortable": false, "aTargets": [ "nosort" ] },
44             { "sType": "title-string", "aTargets" : [ "title-string"] }
45         ],
46         "oLanguage": {
47             "sZeroRecords": MSG_NO_NOTICE_FOUND
48         },
49         "exportColumns": [0,1,2,3],
50     }, columns_settings);
51
52     if( no_op_set ){
53         $('#branch').change(function() {
54             $('#op').val("");
55             $('#selectlibrary').submit();
56         });
57         $('#newnotice').click(function() {
58             $('#op').val("add_form");
59             return true;
60         });
61     }
62
63     $("#newmodule").on("change",function(){
64         var branchcode;
65         if( $("#branch").val() === ""){
66             branchcode = "*";
67         } else {
68             branchcode = $("#branch").val();
69         }
70         window.location.href = "/cgi-bin/koha/tools/letter.pl?op=add_form&module=" + $(this).val() + "&branchcode=" + branchcode;
71     });
72
73     $("#submit_form").on("click",function(){
74         $("#add_notice").submit();
75     });
76
77     $("#add_notice").validate({
78         submitHandler: function(form){
79             var at_least_one_exists = 0;
80             var are_valid = 1;
81             $("fieldset.mtt").each( function(){
82                 var title = $(this).find('input[name="title"]').val();
83                 var content = $(this).find('textarea[name="content"]').val();
84                 if (
85                     ( title.length === 0 && content.length > 0 ) || ( title.length > 0 && content.length === 0 )
86                 ) {
87                     var mtt = $(this).find('input[name="message_transport_type"]').val();
88                     at_least_one_exists = 1; // Only one template has to be filled in for form to be valid
89                     alert( MSG_EMPTY_TITLE_AND_CONTENT.format( mtt ) );
90                     are_valid = 0;
91                 } else if ( title.length > 0 && content.length > 0 ) {
92                     at_least_one_exists = 1;
93                 }
94             });
95
96             if ( ! at_least_one_exists ) {
97                 // No templates were filled out
98                 alert( MSG_EMPTY_TEMPLATES );
99                 return false;
100             }
101
102             if ( ! are_valid ){
103                 return false;
104             }
105
106             // Test if code already exists in DB
107             if( editing == 1 ){ // This is an edit operation
108                 // We don't need to check for an existing Code
109             } else {
110                 var new_lettercode = $("#code").val();
111                 var new_branchcode = $("#branch").val();
112                 var code_check = checkCodes( new_lettercode, new_branchcode );
113                 if( code_check.responseJSON.letters.length > 0 ){
114                     return false;
115                 }
116             }
117             form.submit();
118         }
119     });
120
121     var sms_limit = 160;
122     $(".content_sms").on("keyup", function(){
123         var length = $(this).val().length;
124         var sms_counter = ("#sms_counter_" + $(this).data('lang'));
125         $(sms_counter).html(length + "/" + sms_limit + _(" characters"));
126         if ( length  > sms_limit ) {
127             $(sms_counter).css("color", "red");
128         } else {
129             $(sms_counter).css("color", "black");
130         }
131     });
132
133     $( ".transport-types" ).accordion({ collapsible: true, active:false, animate: 200 });
134
135     $(".insert").on("click",function(){
136         var containerid = $(this).data("containerid");
137         insertValueQuery( containerid );
138     });
139
140     $("#saveandcontinue").on("click",function(e){
141         e.preventDefault();
142         $("#redirect").val("just_save");
143         $("#submit_form").click();
144     });
145
146     $("#tabs").tabs();
147
148     $("body").on("click", ".preview_template", function(e){
149         e.preventDefault();
150         var mtt = $(this).data("mtt");
151         var lang = $(this).data("lang");
152
153         var code = $("#code").val();
154         var content = $("#content_"+mtt+"_"+lang).val();
155         var title = $("#title_"+mtt+"_"+lang).val();
156
157         var is_html = $("#is_html_"+mtt+"_"+lang).val();
158         var page = $(this).attr("href");
159         var data_preview = $("#data_preview").val();
160         page += '?code='+encodeURIComponent(code);
161         page += '&title='+encodeURIComponent(title);
162         page += '&content='+encodeURIComponent(content);
163         page += '&data_preview='+encodeURIComponent(data_preview);
164         page += '&is_html='+encodeURIComponent(is_html);
165         $("#preview_template .modal-body").load(page + " .main");
166         $('#preview_template').modal('show');
167         $("#preview_template_button").attr("href", "/cgi-bin/koha/svc/letters/convert?module="+module+"&code="+code+"&mtt="+mtt+"&lang="+lang);
168     });
169
170     $("#preview_template").on("hidden.bs.modal", function(){
171         $("#preview_template_label").html("");
172         $("#preview_template .modal-body").html( modal_loading );
173     });
174
175     function insertValueQuery(containerid) {
176         var fieldset = $("#" + containerid);
177         var myQuery = $(fieldset).find('textarea[name="content"]');
178         var myListBox = $(fieldset).find('select[name="SQLfieldname"]');
179
180         if($(myListBox).find('option').length > 0) {
181             $(myListBox).find('option').each( function (){
182                 if ( $(this).attr('selected') && $(this).val().length > 0 ) {
183                     $(myQuery).insertAtCaret("<<" + $(this).val() + ">>");
184                 }
185             });
186         }
187     }
188
189 });