Bug 18007: Interface updates to notices and notice previews
[koha.git] / koha-tmpl / intranet-tmpl / prog / js / letter.js
blob4415d1966db65128deba5a2ed325ff46cb41026a
1 /* Variables defined in letter.tt: */
2 /* global _ module add_form copy_form dataTablesDefaults no_op_set MSG_EMPTY_TITLE_AND_CONTENT MSG_EMPTY_TEMPLATES code MSG_CODE_EXISTS MSG_CODE_EXISTS_FOR_LIBRARY MSG_DT_LOADING_RECORDS interface theme */
4 var modal_loading = "<div id=\"loading\"><img src=\"" + interface + "/" + theme + "/img/spinner-small.gif\" alt=\"\" /> "+ MSG_DT_LOADING_RECORDS +"</div>";
6 var editing = 0;
7 if( add_form == 1 && code !== '' ){
8     editing = 1;
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     });
33 $(document).ready(function() {
34     if( add_form || copy_form ){
35         $('#toolbar').fixFloat();
36     }
38     $("#lettert:has(tbody tr)").dataTable($.extend(true, {}, dataTablesDefaults, {
39         "sDom": 't',
40         "aoColumnDefs": [
41             { "bSortable": false, "bSearchable": false, 'aTargets': [ 'nosort' ] }
42         ],
43         "bPaginate": false
44     }));
46     if( no_op_set ){
47         $('#branch').change(function() {
48             $('#op').val("");
49             $('#selectlibrary').submit();
50         });
51         $('#newnotice').click(function() {
52             $('#op').val("add_form");
53             return true;
54         });
55     }
57     $("#newmodule").on("change",function(){
58         var branchcode;
59         if( $("#branch").val() === ""){
60             branchcode = "*";
61         } else {
62             branchcode = $("#branch").val();
63         }
64         window.location.href = "/cgi-bin/koha/tools/letter.pl?op=add_form&module=" + $(this).val() + "&branchcode=" + branchcode;
65     });
67     $("#submit_form").on("click",function(){
68         $("#add_notice").submit();
69     });
71     $("#add_notice").validate({
72         submitHandler: function(form){
73             var at_least_one_exists = 0;
74             var are_valid = 1;
75             $("fieldset.mtt").each( function(){
76                 var title = $(this).find('input[name="title"]').val();
77                 var content = $(this).find('textarea[name="content"]').val();
78                 if (
79                     ( title.length === 0 && content.length > 0 ) || ( title.length > 0 && content.length === 0 )
80                 ) {
81                     var mtt = $(this).find('input[name="message_transport_type"]').val();
82                     at_least_one_exists = 1; // Only one template has to be filled in for form to be valid
83                     alert( MSG_EMPTY_TITLE_AND_CONTENT.format( mtt ) );
84                     are_valid = 0;
85                 } else if ( title.length > 0 && content.length > 0 ) {
86                     at_least_one_exists = 1;
87                 }
88             });
90             if ( ! at_least_one_exists ) {
91                 // No templates were filled out
92                 alert( MSG_EMPTY_TEMPLATES );
93                 return false;
94             }
96             if ( ! are_valid ){
97                 return false;
98             }
100             // Test if code already exists in DB
101             if( editing == 1 ){ // This is an edit operation
102                 // We don't need to check for an existing Code
103             } else {
104                 var new_lettercode = $("#code").val();
105                 var new_branchcode = $("#branch").val();
106                 var code_check = checkCodes( new_lettercode, new_branchcode );
107                 if( code_check.responseJSON.letters.length > 0 ){
108                     return false;
109                 }
110             }
111             form.submit();
112         }
113     });
115     var sms_limit = 160;
116     $(".content_sms").on("keyup", function(){
117         var length = $(this).val().length;
118         var sms_counter = ("#sms_counter_" + $(this).data('lang'));
119         $(sms_counter).html(length + "/" + sms_limit + _(" characters"));
120         if ( length  > sms_limit ) {
121             $(sms_counter).css("color", "red");
122         } else {
123             $(sms_counter).css("color", "black");
124         }
125     });
127     $( ".transport-types" ).accordion({ collapsible: true, active:false, animate: 200 });
129     $(".insert").on("click",function(){
130         var containerid = $(this).data("containerid");
131         insertValueQuery( containerid );
132     });
134     $("#saveandcontinue").on("click",function(e){
135         e.preventDefault();
136         $("#redirect").val("just_save");
137         $("#submit_form").click();
138     });
140     $("#tabs").tabs();
142     $("body").on("click", ".preview_template", function(e){
143         e.preventDefault();
144         var mtt = $(this).data("mtt");
145         var lang = $(this).data("lang");
147         var code = $("#code").val();
148         var content = $("#content_"+mtt+"_"+lang).val();
149         var title = $("#title_"+mtt+"_"+lang).val();
151         var is_html = $("#is_html_"+mtt+"_"+lang).val();
152         var page = $(this).attr("href");
153         var data_preview = $("#data_preview").val();
154         page += '?code='+encodeURIComponent(code);
155         page += '&title='+encodeURIComponent(title);
156         page += '&content='+encodeURIComponent(content);
157         page += '&data_preview='+encodeURIComponent(data_preview);
158         page += '&is_html='+encodeURIComponent(is_html);
159         $("#preview_template .modal-body").load(page + " #main");
160         $('#preview_template').modal('show');
161         $("#preview_template_button").attr("href", "/cgi-bin/koha/svc/letters/convert?module="+module+"&code="+code+"&mtt="+mtt+"&lang="+lang);
162     });
164     $("#preview_template").on("hidden.bs.modal", function(){
165         $("#preview_template_label").html("");
166         $("#preview_template .modal-body").html( modal_loading );
167     });
169     function insertValueQuery(containerid) {
170         var fieldset = $("#" + containerid);
171         var myQuery = $(fieldset).find('textarea[name="content"]');
172         var myListBox = $(fieldset).find('select[name="SQLfieldname"]');
174         if($(myListBox).find('option').length > 0) {
175             $(myListBox).find('option').each( function (){
176                 if ( $(this).attr('selected') && $(this).val().length > 0 ) {
177                     $(myQuery).insertAtCaret("<<" + $(this).val() + ">>");
178                 }
179             });
180         }
181     }