Bug 35695: Remove useless JS from cataloging_additem.js
[koha.git] / koha-tmpl / intranet-tmpl / prog / js / cataloging_additem.js
1 /* global KOHA searchid biblionumber frameworkcode popup op LABEL_EDIT_ITEM LABEL_DELETE_ITEM MSG_FORM_NOT_SUBMITTED MSG_MANDATORY_FIELDS_EMPTY MSG_ADD_MULTIPLE_ITEMS MSG_ENTER_NUM_ITEMS MSG_CONFIRM_DELETE_ITEM MSG_CONFIRM_ADD_ITEM columns_settings CheckMandatorySubfields CheckMultipleAdd */
2
3 var browser = KOHA.browser(searchid, parseInt(biblionumber, 10));
4 browser.show();
5
6 $(document).ready(function(){
7
8     // Remove the onclick event defined in browser.js,
9     // otherwise the deletion confirmation will not work correctly
10     $('a[href*="biblionumber="]').off('click');
11
12     if( popup && op != 'saveitem' ){
13         window.close();
14     }
15
16     $("fieldset.rows input, fieldset.rows select").addClass("noEnterSubmit");
17     /* Inline edit/delete links */
18     var biblionumber = $("input[name='biblionumber']").val();
19     $("tr.editable").each(function(){
20         $(this).find("td:not(:first)").on('click', function(){
21             var rowid = $(this).parent().attr("id");
22             var num_rowid = rowid.replace("row","");
23             $(".linktools").remove();
24             var edit_link = $('<a href="/cgi-bin/koha/cataloguing/additem.pl?op=edititem&frameworkcode=' + frameworkcode + '&biblionumber=' + biblionumber + '&itemnumber=' + num_rowid + '&searchid=' + searchid + '#edititem"></a>');
25             $(edit_link).text( LABEL_EDIT_ITEM );
26             var delete_link = $('<a href="/cgi-bin/koha/cataloguing/additem.pl?op=delitem&frameworkcode=' + frameworkcode + '&biblionumber=' + biblionumber + '&itemnumber=' + num_rowid + '&searchid=' + searchid + '"></a>');
27             $(delete_link).text( LABEL_DELETE_ITEM );
28             $(delete_link).on('click', function() {
29                 return confirm_deletion();
30             });
31             var tools_node = $('<span class="linktools"></span>');
32             $(tools_node).append(edit_link);
33             $(tools_node).append(delete_link);
34             $(this).append(tools_node);
35         });
36     });
37
38     $("#addnewitem").click(function(){
39         if ( confirm( MSG_CONFIRM_ADD_ITEM ) ){
40             window.location.href = "/cgi-bin/koha/cataloguing/additem.pl?biblionumber=" + biblionumber;
41         }
42     });
43
44     // Skip the first column
45     table_settings['columns'].unshift( { cannot_be_toggled: "1" } );
46
47     var itemst = KohaTable("itemst", {
48         'bPaginate': false,
49         'bInfo': false,
50         "bAutoWidth": false,
51         "bKohaColumnsUseNames": true
52     }, table_settings);
53
54     var multiCopyControl = $("#add_multiple_copies_span");
55     var addMultipleBlock = $("#addmultiple");
56     var addSingleBlock = $("#addsingle");
57     var saveAsTemplateControl = $("#save_as_template_span");
58     var saveTemplateBlock = $("#savetemplate");
59
60     multiCopyControl.hide();
61     $("#add_multiple_copies").on("click",function(e){
62         e.preventDefault;
63         addMultipleBlock.toggle();
64         saveTemplateBlock.toggle();
65         addSingleBlock.toggle();
66         multiCopyControl.toggle();
67         $('body,html').animate({ scrollTop: $('body').height() }, 100);
68     });
69     $("#cancel_add_multiple").on("click",function(e){
70         e.preventDefault();
71         addMultipleBlock.toggle();
72         saveTemplateBlock.toggle();
73         addSingleBlock.toggle();
74         multiCopyControl.toggle();
75     });
76
77     saveAsTemplateControl.hide();
78     $("#save_as_template").on("click",function(e){
79         e.preventDefault;
80         saveTemplateBlock.toggle();
81         saveAsTemplateControl.toggle();
82         $('#template_name').focus();
83     });
84     $("#cancel_save_as_template").on("click",function(e){
85         e.preventDefault();
86         saveTemplateBlock.toggle();
87         saveAsTemplateControl.toggle();
88     });
89
90     $("#template_id").on("change", function() {
91         let option = $(this).find(":selected");
92
93         if ( option.data("editor") ) {
94             $("#delete_template_submit").removeAttr("disabled");
95         } else {
96             $("#delete_template_submit").attr("disabled", "disabled");
97         }
98
99         if ( option.val() != 0 ) {
100             $("#load_template_submit").removeAttr("disabled");
101         } else {
102             $("#load_template_submit").attr("disabled", "disabled");
103         }
104     });
105     $("#template_id").change(); // Trigger to enable delete button if patron's template is in use
106     $("#replace_template_id").on("change", function() {
107         let selected = $(this).find(":selected");
108         if ( selected.val() > 0 ) {
109             $("#template_name_block").hide();
110             $("#template_is_shared").prop("checked", selected.data("shared"));
111         } else {
112             $("#template_name_block").show();
113             $("#template_is_shared").prop("checked", false);
114         }
115     });
116
117     // Add new item to an item group
118     if ( has_item_groups ) {
119         $('#item-group-add-or-create-form-description-block').hide();
120         $('#item-group-add-or-create-form-no-add').attr('selected', 'selected' );
121
122         $('#item-group-add-or-create-form-select').on('change', function(){
123             if ( $(this).val() == 'create' ) {
124                 $('#item-group-add-or-create-form-description')
125                     .addClass('required')
126                     .attr( 'required', 'required' );
127                 $('#item-group-add-or-create-form-description-block').show();
128             } else {
129                 $('#item-group-add-or-create-form-description')
130                     .removeClass('required')
131                     .removeAttr('required');
132                 $('#item-group-add-or-create-form-description-block').hide();
133             }
134         });
135     }
136
137 });
138
139 function CheckTemplateForm(f) {
140     if ( $('#replace_template_id').val() == "0" &&  $('#template_name').val() == "" ) {
141         alert(__("Template name is required."));
142         return false;
143     } else {
144         return true;
145     }
146 }
147
148 function Check(f) {
149     var total_mandatory = CheckMandatorySubfields(f);
150     var total_important = CheckImportantSubfields(f);
151     var alertString2;
152     if (total_mandatory==0) {
153         // Explanation about this line:
154         // In case of limited edition permission, we have to prevent user from modifying some fields.
155         // But there is no such thing as readonly attribute for select elements.
156         // So we use disabled instead. But disabled prevent values from being passed through the form at submit.
157         // So we "un-disable" the elements just before submitting.
158         // That's a bit clumsy, and if someone comes up with a better solution, feel free to improve that.
159         $("select.input_marceditor").prop('disabled', false);
160     } else {
161         alertString2 = MSG_FORM_NOT_SUBMITTED;
162         alertString2 += "\n------------------------------------------------------------------------------------\n";
163         alertString2 += "\n- " + MSG_MANDATORY_FIELDS_EMPTY.format(total_mandatory);
164     }
165     if(total_important > 0){
166         if( !alertString2 ){
167             alertString2 = "";
168         }
169         alertString2 += "\n\n " + MSG_IMPORTANT_FIELDS_EMPTY.format(total_important);
170         alertString2 += "\n\n " + MSG_CONFIRM_SAVE;
171     }
172     if(alertString2){
173         if(total_mandatory){
174              alert(alertString2);
175         }else{
176             var a = confirm(alertString2);
177             if( a ){
178                 return true;
179             }
180         }
181         return false;
182     }
183     return true;
184 }
185
186 function CheckMultipleAdd(f) {
187
188     if (!f || isNaN(f) || !parseInt(f) == f || f <= 0) {
189         alert( MSG_ENTER_NUM_ITEMS );
190         return false;
191     }
192     // Add a soft-limit of 99 with a reminder about potential data entry error
193     if (f>99) {
194         return confirm( MSG_ADD_MULTIPLE_ITEMS.format(f));
195     }
196 }
197
198 function Dopop(link,i) {
199     var defaultvalue=document.forms[0].field_value[i].value;
200     var newin=window.open(link+"&result=" + defaultvalue,"valuebuilder",'width=500,height=400,toolbar=false,scrollbars=yes');
201 }
202
203 function confirm_deletion() {
204     return confirm( MSG_CONFIRM_DELETE_ITEM );
205 }