Bug 23278: Reopen last panel upon "Save and continue" in notices
[koha.git] / koha-tmpl / intranet-tmpl / prog / js / letter.js
blobf4824abab3032a6379dece4cf578ddcd3242682f
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 */
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 var Sticky;
35 $(document).ready(function() {
36     if( add_form || copy_form ){
37         Sticky = $("#toolbar");
38         Sticky.hcSticky({
39             stickTo: ".main",
40             stickyClass: "floating"
41         });
42     }
44     var ntable = KohaTable("lettert", {
45         "autoWidth": false,
46         "paging": false,
47         "aaSorting": [[ 3, "asc" ]],
48         "columnDefs": [
49             { "bSortable": false, "aTargets": [ "nosort" ] },
50             { "sType": "title-string", "aTargets" : [ "title-string"] }
51         ],
52         "oLanguage": {
53             "sZeroRecords": MSG_NO_NOTICE_FOUND
54         },
55         "exportColumns": [0,1,2,3],
56     }, columns_settings);
58     if( no_op_set ){
59         $('#branch').change(function() {
60             $('#op').val("");
61             $('#selectlibrary').submit();
62         });
63         $('#newnotice').click(function() {
64             $('#op').val("add_form");
65             return true;
66         });
67     }
69     $("#newmodule").on("change",function(){
70         var branchcode;
71         if( $("#branch").val() === ""){
72             branchcode = "*";
73         } else {
74             branchcode = $("#branch").val();
75         }
76         window.location.href = "/cgi-bin/koha/tools/letter.pl?op=add_form&module=" + $(this).val() + "&branchcode=" + branchcode;
77     });
79     $("#submit_form").on("click",function(){
80         $("#add_notice").submit();
81     });
83     $("#add_notice").validate({
84         submitHandler: function(form){
85             var at_least_one_exists = 0;
86             var are_valid = 1;
87             $("fieldset.mtt").each( function(){
88                 var title = $(this).find('input[name="title"]').val();
89                 var content = $(this).find('textarea[name="content"]').val();
90                 if (
91                     ( title.length === 0 && content.length > 0 ) || ( title.length > 0 && content.length === 0 )
92                 ) {
93                     var mtt = $(this).find('input[name="message_transport_type"]').val();
94                     at_least_one_exists = 1; // Only one template has to be filled in for form to be valid
95                     alert( MSG_EMPTY_TITLE_AND_CONTENT.format( mtt ) );
96                     are_valid = 0;
97                 } else if ( title.length > 0 && content.length > 0 ) {
98                     at_least_one_exists = 1;
99                 }
100             });
102             if ( ! at_least_one_exists ) {
103                 // No templates were filled out
104                 alert( MSG_EMPTY_TEMPLATES );
105                 return false;
106             }
108             if ( ! are_valid ){
109                 return false;
110             }
112             // Test if code already exists in DB
113             if( editing == 1 ){ // This is an edit operation
114                 // We don't need to check for an existing Code
115             } else {
116                 var new_lettercode = $("#code").val();
117                 var new_branchcode = $("#branch").val();
118                 var code_check = checkCodes( new_lettercode, new_branchcode );
119                 if( code_check.responseJSON.letters.length > 0 ){
120                     return false;
121                 }
122             }
123             form.submit();
124         }
125     });
127     var sms_limit = 160;
128     $(".content_sms").on("keyup", function(){
129         var length = $(this).val().length;
130         var sms_counter = ("#sms_counter_" + $(this).data('lang'));
131         $(sms_counter).html(length + "/" + sms_limit + _(" characters"));
132         if ( length  > sms_limit ) {
133             $(sms_counter).css("color", "red");
134         } else {
135             $(sms_counter).css("color", "black");
136         }
137     });
139     $( ".transport-types" ).accordion({
140         collapsible: true,
141         active: parseInt( $("#section").val(), 10),
142         animate: 200,
143         activate: function() {
144             var active = $( ".transport-types" ).accordion( "option", "active" );
145             if( active === false ){
146                 $("#section").val("");
147             } else {
148                 $("#section").val( active );
149             }
150         }
151     });
153     $(".insert").on("click",function(){
154         var containerid = $(this).data("containerid");
155         insertValueQuery( containerid );
156     });
158     $("#saveandcontinue").on("click",function(e){
159         e.preventDefault();
160         $("#redirect").val("just_save");
161         $("#submit_form").click();
162     });
164     $("#tabs").tabs();
166     $("body").on("click", ".preview_template", function(e){
167         e.preventDefault();
168         var mtt = $(this).data("mtt");
169         var lang = $(this).data("lang");
171         var code = $("#code").val();
172         var content = $("#content_"+mtt+"_"+lang).val();
173         var title = $("#title_"+mtt+"_"+lang).val();
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     });
188     $("#preview_template").on("hidden.bs.modal", function(){
189         $("#preview_template_label").html("");
190         $("#preview_template .modal-body").html( modal_loading );
191     });
193     function insertValueQuery(containerid) {
194         var fieldset = $("#" + containerid);
195         var myQuery = $(fieldset).find('textarea[name="content"]');
196         var myListBox = $(fieldset).find('select[name="SQLfieldname"]');
198         if($(myListBox).find('option').length > 0) {
199             $(myListBox).find('option').each( function (){
200                 if ( $(this).attr('selected') && $(this).val().length > 0 ) {
201                     $(myQuery).insertAtCaret("<<" + $(this).val() + ">>");
202                 }
203             });
204         }
205     }