Bug 26922: Regression tests
[koha.git] / koha-tmpl / intranet-tmpl / prog / js / letter.js
blobc2e353bfe8a017d888f8e543ec00581d17d7e0b9
1 /* Variables defined in letter.tt: */
2 /* global _ module add_form copy_form no_op_set code interface theme KohaTable columns_settings */
4 var modal_loading = "<div id=\"loading\"><img src=\"" + interface + "/" + theme + "/img/spinner-small.gif\" alt=\"\" /> "+ __('Loading...') +"</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(__("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     });
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         "exportColumns": [0,1,2,3],
53     }, columns_settings);
55     if( no_op_set ){
56         $('#branch').change(function() {
57             $('#op').val("");
58             $('#selectlibrary').submit();
59         });
60         $('#newnotice').click(function() {
61             $('#op').val("add_form");
62             return true;
63         });
64     }
66     $("#newmodule").on("change",function(){
67         var branchcode;
68         if( $("#branch").val() === ""){
69             branchcode = "*";
70         } else {
71             branchcode = $("#branch").val();
72         }
73         window.location.href = "/cgi-bin/koha/tools/letter.pl?op=add_form&module=" + $(this).val() + "&branchcode=" + branchcode;
74     });
76     $("#submit_form").on("click",function(){
77         $("#add_notice").submit();
78     });
80     $("#add_notice").validate({
81         submitHandler: function(form){
82             var at_least_one_exists = 0;
83             var are_valid = 1;
84             $("fieldset.mtt").each( function(){
85                 var title = $(this).find('input[name="title"]').val();
86                 var content = $(this).find('textarea[name="content"]').val();
87                 if (
88                     ( title.length === 0 && content.length > 0 ) || ( title.length > 0 && content.length === 0 )
89                 ) {
90                     var mtt = $(this).find('input[name="message_transport_type"]').val();
91                     at_least_one_exists = 1; // Only one template has to be filled in for form to be valid
92                     alert(__("Please specify title and content for %s").format(mtt));
93                     are_valid = 0;
94                 } else if ( title.length > 0 && content.length > 0 ) {
95                     at_least_one_exists = 1;
96                 }
97             });
99             if ( ! at_least_one_exists ) {
100                 // No templates were filled out
101                 alert( __("Please fill at least one template.") );
102                 return false;
103             }
105             if ( ! are_valid ){
106                 return false;
107             }
109             // Test if code already exists in DB
110             if( editing == 1 ){ // This is an edit operation
111                 // We don't need to check for an existing Code
112             } else {
113                 var new_lettercode = $("#code").val();
114                 var new_branchcode = $("#branch").val();
115                 var code_check = checkCodes( new_lettercode, new_branchcode );
116                 if( code_check.responseJSON.letters.length > 0 ){
117                     return false;
118                 }
119             }
120             form.submit();
121         }
122     });
124     var sms_limit = 160;
125     $(".content_sms").on("keyup", function(){
126         var length = $(this).val().length;
127         var sms_counter = ("#sms_counter_" + $(this).data('lang'));
128         $(sms_counter).html(length + "/" + sms_limit + _(" characters"));
129         if ( length  > sms_limit ) {
130             $(sms_counter).css("color", "red");
131         } else {
132             $(sms_counter).css("color", "black");
133         }
134     });
136     $( ".transport-types" ).accordion({
137         collapsible: true,
138         active: parseInt( $("#section").val(), 10),
139         animate: 200,
140         activate: function() {
141             var active = $( ".transport-types" ).accordion( "option", "active" );
142             if( active === false ){
143                 $("#section").val("");
144             } else {
145                 $("#section").val( active );
146             }
147         }
148     });
150     $(".insert").on("click",function(){
151         var containerid = $(this).data("containerid");
152         insertValueQuery( containerid );
153     });
155     $("#saveandcontinue").on("click",function(e){
156         e.preventDefault();
157         $("#redirect").val("just_save");
158         $("#submit_form").click();
159     });
161     $("#tabs").tabs();
163     $("body").on("click", ".preview_template", function(e){
164         e.preventDefault();
165         var mtt = $(this).data("mtt");
166         var lang = $(this).data("lang");
168         var code = $("#code").val();
169         var content = $("#content_"+mtt+"_"+lang).val();
170         var title = $("#title_"+mtt+"_"+lang).val();
172         var is_html = $("#is_html_"+mtt+"_"+lang).val();
173         var page = $(this).attr("href");
174         var data_preview = $("#data_preview").val();
175         page += '?code='+encodeURIComponent(code);
176         page += '&title='+encodeURIComponent(title);
177         page += '&content='+encodeURIComponent(content);
178         page += '&data_preview='+encodeURIComponent(data_preview);
179         page += '&is_html='+encodeURIComponent(is_html);
180         $("#preview_template .modal-body").load(page + " .main");
181         $('#preview_template').modal('show');
182         $("#preview_template_button").attr("href", "/cgi-bin/koha/svc/letters/convert?module="+module+"&code="+code+"&mtt="+mtt+"&lang="+lang);
183     });
185     $("#preview_template").on("hidden.bs.modal", function(){
186         $("#preview_template_label").html("");
187         $("#preview_template .modal-body").html( modal_loading );
188     });
190     function insertValueQuery(containerid) {
191         var fieldset = $("#" + containerid);
192         var myQuery = $(fieldset).find('textarea[name="content"]');
193         var myListBox = $(fieldset).find('select[name="SQLfieldname"]');
195         if($(myListBox).find('option').length > 0) {
196             $(myListBox).find('option').each( function (){
197                 if ( $(this).attr('selected') && $(this).val().length > 0 ) {
198                     $(myQuery).insertAtCaret("<<" + $(this).val() + ">>");
199                 }
200             });
201         }
202     }