9bd660ad3b304efddbd4a5290c68865a96deeb15
[srvgit] / 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']").attr("value");
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     multiCopyControl.hide();
58     $("#add_multiple_copies").on("click",function(e){
59         e.preventDefault;
60         addMultipleBlock.toggle();
61         addSingleBlock.toggle();
62         multiCopyControl.toggle();
63         $('body,html').animate({ scrollTop: $('body').height() }, 100);
64     });
65     $("#cancel_add_multiple").on("click",function(e){
66         e.preventDefault();
67         addMultipleBlock.toggle();
68         addSingleBlock.toggle();
69         multiCopyControl.toggle();
70     });
71
72     var saveAsTemplateControl = $("#save_as_template_span");
73     var saveTemplateBlock = $("#savetemplate");
74     saveAsTemplateControl.hide();
75     $("#save_as_template").on("click",function(e){
76         e.preventDefault;
77         saveTemplateBlock.toggle();
78         saveAsTemplateControl.toggle();
79         $('#template_name').focus();
80     });
81     $("#cancel_save_as_template").on("click",function(e){
82         e.preventDefault();
83         saveTemplateBlock.toggle();
84         saveAsTemplateControl.toggle();
85     });
86
87     $("#template_id").on("change", function() {
88         if ( $(this).find(":selected").data("owner") ) {
89             $("#delete_template_submit").removeAttr("disabled");
90         } else {
91             $("#delete_template_submit").attr("disabled", "disabled");
92         }
93     });
94     $("#template_id").change(); // Trigger to enable delete button if patron's template is in use
95     $("#replace_template_id").on("change", function() {
96         if ( $(this).find(":selected").val() > 0 ) {
97             $("#template_name_block").hide();
98         } else {
99             $("#template_name_block").show();
100         }
101     });
102
103     // Add new item to an item group
104     if ( has_item_groups ) {
105         $('#item-group-add-or-create-form-description-block').hide();
106         $('#item-group-add-or-create-form-no-add').attr('selected', 'selected' );
107
108         $('#item-group-add-or-create-form-select').on('change', function(){
109             if ( $(this).val() == 'create' ) {
110                 $('#item-group-add-or-create-form-description')
111                     .addClass('required')
112                     .attr( 'required', 'required' );
113                 $('#item-group-add-or-create-form-description-block').show();
114             } else {
115                 $('#item-group-add-or-create-form-description')
116                     .removeClass('required')
117                     .removeAttr('required');
118                 $('#item-group-add-or-create-form-description-block').hide();
119             }
120         });
121     }
122
123     $('#item-group-add-or-create-form-select').on('change', function() {
124         if ( ! $('input.items-enumchron').val() ) {
125             let item_group_selector = '#item-group-' + $(this).val();
126             let enumchron = $(item_group_selector).val();
127             $('input.items-enumchron').val( enumchron );
128         }
129     });
130 });
131
132 function CheckTemplateForm(f) {
133     if ( $('#replace_template_id').val() == "0" &&  $('#template_name').val() == "" ) {
134         alert(MSG_TEMPLATE_NAME_REQUIRED);
135         return false;
136     } else {
137         return true;
138     }
139 }
140
141 function Check(f) {
142     var total_mandatory = CheckMandatorySubfields(f);
143     var total_important = CheckImportantSubfields(f);
144     var alertString2;
145     if (total_mandatory==0) {
146         // Explanation about this line:
147         // In case of limited edition permission, we have to prevent user from modifying some fields.
148         // But there is no such thing as readonly attribute for select elements.
149         // So we use disabled instead. But disabled prevent values from being passed through the form at submit.
150         // So we "un-disable" the elements just before submitting.
151         // That's a bit clumsy, and if someone comes up with a better solution, feel free to improve that.
152         $("select.input_marceditor").prop('disabled', false);
153     } else {
154         alertString2 = MSG_FORM_NOT_SUBMITTED;
155         alertString2 += "\n------------------------------------------------------------------------------------\n";
156         alertString2 += "\n- " + MSG_MANDATORY_FIELDS_EMPTY.format(total_mandatory);
157     }
158     if(total_important > 0){
159         if( !alertString2 ){
160             alertString2 = "";
161         }
162         alertString2 += "\n\n " + MSG_IMPORTANT_FIELDS_EMPTY.format(total_important);
163         alertString2 += "\n\n " + MSG_CONFIRM_SAVE;
164     }
165     if(alertString2){
166         if(total_mandatory){
167              alert(alertString2);
168         }else{
169             var a = confirm(alertString2);
170             if( a ){
171                 return true;
172             }
173         }
174         return false;
175     }
176     return true;
177 }
178
179 function CheckMultipleAdd(f) {
180
181     if (!f || isNaN(f) || !parseInt(f) == f || f <= 0) {
182         alert( MSG_ENTER_NUM_ITEMS );
183         return false;
184     }
185     // Add a soft-limit of 99 with a reminder about potential data entry error
186     if (f>99) {
187         return confirm( MSG_ADD_MULTIPLE_ITEMS.format(f));
188     }
189 }
190
191 function Dopop(link,i) {
192     var defaultvalue=document.forms[0].field_value[i].value;
193     var newin=window.open(link+"&result=" + defaultvalue,"valuebuilder",'width=500,height=400,toolbar=false,scrollbars=yes');
194 }
195
196 function confirm_deletion() {
197     return confirm( MSG_CONFIRM_DELETE_ITEM );
198 }