Bug 34571: Remove use of "onclick" for ExpandField in cataloguing editors
[koha.git] / koha-tmpl / intranet-tmpl / prog / js / cataloging.js
1 /* global __ */
2 /* exported openAuth ExpandField CloneField CloneSubfield UnCloneField CloneItemSubfield CheckMandatorySubfields */
3
4 /*
5  * Unified file for catalogue edition
6  */
7
8 /* Functions developed for addbiblio.tt and authorities.tt */
9
10 // returns the fieldcode based upon tag div id
11 function getFieldCode(tagDivId){
12     // format : tag_<tagnumber>_...
13     return tagDivId.substr(3+1,3);
14 }
15
16 //returns the field and subfieldcode based upon subfield div id
17 function getFieldAndSubfieldCode(subfieldDivId){
18     // format : subfield<tagnumber><subfieldnumber>...
19     return subfieldDivId.substr(8,3+1);
20 }
21
22 //returns the subfieldcode based upon subfieldid writing
23 function getSubfieldCode(tagsubfieldid){
24     // 3 : tag +3 : tagnumber +4 : number of _ +8 subfield -1 begins at 0
25     return tagsubfieldid.substr(3+3+4+8-1,1);
26 }
27
28 // Take the base of tagsubfield information (removing the subfieldcodes and subfieldindexes)
29 // returns the filter
30 function getTagInputnameFilter(tagsubfieldid){
31     var tagsubfield=tagsubfieldid.substr(0,tagsubfieldid.lastIndexOf("_"));
32     var tagcode=tagsubfield.substr(tagsubfield.lastIndexOf("_"));
33     tagsubfield=tagsubfield.substr(0,tagsubfield.lastIndexOf("_"));
34     tagsubfield=tagsubfield.substr(0,tagsubfield.lastIndexOf("_"));
35     tagsubfield=tagsubfield+"_."+tagcode;
36     return tagsubfield;
37 }
38
39 // if source is "auth", we are editing an authority otherwise it is a biblio
40 function openAuth(tagsubfieldid,authtype,source) {
41     // let's take the base of tagsubfield information (removing the indexes and the codes
42     var element=document.getElementById(tagsubfieldid);
43     var tagsubfield=getTagInputnameFilter(tagsubfieldid);
44     var elementsubfcode=getSubfieldCode(element.name);
45     var mainmainstring=element.value;
46     var mainstring = new Array();
47
48     var ul = element.closest('ul');
49     var inputs = ul ? ul.getElementsByTagName('input') : element.parentNode.getElementsByTagName('input');
50     for (var myindex =0; myindex<inputs.length;myindex++){
51         if (inputs[myindex].name && inputs[myindex].name.match(tagsubfield)){
52             var subfieldcode=getSubfieldCode(inputs[myindex].name);
53             if (isNaN(parseInt(subfieldcode)) && inputs[myindex].value != "" && subfieldcode!=elementsubfcode){
54                 mainstring.push(inputs[myindex].value);
55             }
56         }
57     }
58     mainstring = mainstring.join(' ');
59     window.open("../authorities/auth_finder.pl?source="+source+"&authtypecode="+authtype+"&index="+tagsubfieldid+"&value_mainstr="+encodeURIComponent(mainmainstring)+"&value_main="+encodeURIComponent(mainstring), "_blank",'width=700,height=550,toolbar=false,scrollbars=yes');
60 }
61
62 function ExpandField() {
63     let index = this.dataset.field_id;
64     var original = document.getElementById(index); //original <li>
65     var lis = original.getElementsByTagName('li');
66     for(var i=0,lislen = lis.length ; i<lislen ; i++){   // foreach li
67         if(lis[i].hasAttribute('id') == 0 ) {continue; } // li element is specific to Select2
68         if(lis[i].getAttribute('id').match(/^subfield/)){  // if it s a subfield
69             if (!lis[i].style.display) {
70                 // first time => show all subfields
71                 lis[i].style.display = 'flex';
72             } else if (lis[i].style.display == 'none') {
73                 // show
74                 lis[i].style.display = 'flex';
75             } else {
76                 // hide
77                 lis[i].style.display = 'none';
78             }
79         }
80     }
81     return false;
82 }
83
84 var current_select2;
85 var Select2Utils = {
86     removeSelect2: function(selects) {
87         if ($.fn.select2) {
88             $(selects).each(function(){
89                 $(this).select2('destroy');
90             });
91         }
92     },
93
94     initSelect2: function(selects) {
95         if ($.fn.select2) {
96             if ( window.auth_values_creation === undefined || ! auth_values_creation ) {
97                 $(selects).select2().on("select2:clear", function () {
98                     $(this).on("select2:opening.cancelOpen", function (evt) {
99                         evt.preventDefault();
100                         $(this).off("select2:opening.cancelOpen");
101                     });
102                 });
103             } else {
104                 $(selects).each(function(){
105                     if ( !$(this).data("category") ) {
106                         $(this).select2().on("select2:clear", function () {
107                             $(this).on("select2:opening.cancelOpen", function (evt) {
108                                 evt.preventDefault();
109                                 $(this).off("select2:opening.cancelOpen");
110                             });
111                         });
112                     } else {
113                         $(this).select2({
114                             tags: true,
115                             createTag: function (tag) {
116                                 return {
117                                     id: tag.term,
118                                     text: tag.term,
119                                     newTag: true
120                                 };
121                             },
122                             templateResult: function(state) {
123                                 if (state.newTag) {
124                                     return state.text + " " + __("(select to create)");
125                                 }
126                                 return state.text;
127                             }
128                         }).on("select2:select", function(e) {
129                             if(e.params.data.newTag){
130                                 current_select2 = this;
131                                 var category = $(this).data("category");
132                                 $("#avCreate #new_av_category").html(category);
133                                 $("#avCreate input[name='category']").val(category);
134                                 $("#avCreate input[name='value']").val('');
135                                 $("#avCreate input[name='description']").val(e.params.data.text);
136
137                                 $(this).val($(this).find("option:first").val()).trigger('change');
138                                 $('#avCreate').modal({show:true});
139                             }
140                         }).on("select2:clear", function () {
141                             $(this).on("select2:opening.cancelOpen", function (evt) {
142                                 evt.preventDefault();
143
144                                 $(this).off("select2:opening.cancelOpen");
145                             });
146                         });
147                     }
148                 });
149             }
150         }
151     }
152 };
153
154 /**
155  * To clone a field
156  * @param hideMarc '0' for false, '1' for true
157  * @param advancedMARCEditor '0' for false, '1' for true
158  */
159 function CloneField(index, hideMarc, advancedMARCEditor) {
160     var original = document.getElementById(index); //original <li>
161     Select2Utils.removeSelect2($(original).find('select'));
162
163     var clone = original.cloneNode(true);
164     var new_key = CreateKey();
165     var new_id  = original.getAttribute('id')+new_key;
166
167     clone.setAttribute('id',new_id); // setting a new id for the parent li
168
169     var divs = Array.from(clone.getElementsByTagName('li')).concat(Array.from(clone.getElementsByTagName('div')));
170
171     // if hide_marc, indicators are hidden fields
172     // setting a new name for the new indicator
173     for(var i=0; i < 2; i++) {
174         var indicator = clone.getElementsByTagName('input')[i];
175         indicator.setAttribute('name',indicator.getAttribute('name')+new_key);
176     }
177
178     // settings all subfields
179     var divslen = divs.length;
180     for( i=0; i < divslen ; i++ ){      // foreach div/li
181         if( divs[i].getAttribute("id") && divs[i].getAttribute("id").match(/^subfield/)){  // if it s a subfield
182
183             // set the attribute for the new 'li' subfields
184             divs[i].setAttribute('id',divs[i].getAttribute('id')+new_key);
185
186             var inputs   = divs[i].getElementsByTagName('input');
187             var id_input = "";
188             var olddiv;
189             var oldcontrol;
190
191             for( j = 0 ; j < inputs.length ; j++ ) {
192                 if(inputs[j].getAttribute("id") && inputs[j].getAttribute("id").match(/^tag_/) ){
193                     inputs[j].value = "";
194
195                     //Remove the color added by the automatic linker
196                     $(inputs[j]).removeClass("matching_authority_field no_matching_authority_field");
197                 }
198             }
199             var textareas = divs[i].getElementsByTagName('textarea');
200             for( j = 0 ; j < textareas.length ; j++ ) {
201                 if(textareas[j].getAttribute("id") && textareas[j].getAttribute("id").match(/^tag_/) ){
202                     textareas[j].value = "";
203                 }
204             }
205             // Remove the status icons added by the automatic linker
206             $(divs[i]).find('.subfield_status').remove();
207             if( inputs.length > 0 ){
208                 inputs[0].setAttribute('id',inputs[0].getAttribute('id')+new_key);
209                 inputs[0].setAttribute('name',inputs[0].getAttribute('name')+new_key);
210
211                 try {
212                     id_input = inputs[1].getAttribute('id')+new_key;
213                     inputs[1].setAttribute('id',id_input);
214                     inputs[1].setAttribute('name',inputs[1].getAttribute('name')+new_key);
215                 } catch(e) {
216                     try{ // it s a select if it is not an input
217                         var selects = divs[i].getElementsByTagName('select');
218                         id_input = selects[0].getAttribute('id')+new_key;
219                         selects[0].setAttribute('id',id_input);
220                         selects[0].setAttribute('name',selects[0].getAttribute('name')+new_key);
221                     }catch(e2){ // it is a textarea if it s not a select or an input
222                         var textareas = divs[i].getElementsByTagName('textarea');
223                         if( textareas.length > 0 ){
224                             id_input = textareas[0].getAttribute('id')+new_key;
225                             textareas[0].setAttribute('id',id_input);
226                             textareas[0].setAttribute('name',textareas[0].getAttribute('name')+new_key);
227                         }
228                     }
229                 }
230                 if( $(inputs[1]).hasClass('framework_plugin') ) {
231                     olddiv= original.getElementsByTagName('li')[i];
232                     oldcontrol= olddiv.getElementsByTagName('input')[1];
233                     AddEventHandlers( oldcontrol,inputs[1],id_input );
234                 }
235             }
236             // when cloning a subfield, re set its label too.
237             try {
238                 var labels = divs[i].getElementsByTagName('label');
239                 labels[0].setAttribute('for', id_input);
240             }
241             catch(e) {
242                 // do nothing if label does not exist.
243             }
244
245             // setting its '+' and '-' buttons
246             try {
247                 var anchors = divs[i].getElementsByTagName('a');
248                 for (var j = 0; j < anchors.length; j++) {
249                     if(anchors[j].getAttribute('class') == 'buttonPlus'){
250                         anchors[j].setAttribute('onclick',"CloneSubfield('" + divs[i].getAttribute('id') + "','" + advancedMARCEditor + "'); return false;");
251                     } else if (anchors[j].getAttribute('class') == 'buttonMinus') {
252                         anchors[j].setAttribute('onclick',"UnCloneField('" + divs[i].getAttribute('id') + "'); return false;");
253                     }
254                 }
255             }
256             catch(e){
257                 // do nothig if ButtonPlus & CloneButtonPlus don t exist.
258             }
259
260             // button ...
261             var spans=0;
262             try {
263                 spans = divs[i].getElementsByTagName('a');
264             } catch(e) {
265                 // no spans
266             }
267             if(spans){
268                 var buttonDot;
269                 if(!CloneButtonPlus){ // it s impossible to have  + ... (buttonDot AND buttonPlus)
270                     buttonDot = spans[0];
271                     if(buttonDot){
272                         // 2 possibilities :
273                         try{
274                             if( $(buttonDot).hasClass('framework_plugin') ) {
275                                 olddiv= original.getElementsByTagName('li')[i];
276                                 oldcontrol= olddiv.getElementsByTagName('a')[0];
277                                 AddEventHandlers(oldcontrol,buttonDot,id_input);
278                             }
279                             try {
280                                 // do not copy the script section.
281                                 var script = spans[0].getElementsByTagName('script')[0];
282                                 spans[0].removeChild(script);
283                             } catch(e) {
284                                 // do nothing if there is no script
285                             }
286                         } catch(e){
287                             //
288                         }
289                     }
290                 }
291             }
292
293         } else { // it's a indicator div
294             if ( divs[i].getAttribute("id") && divs[i].getAttribute('id').match(/^div_indicator/)) {
295
296                 // setting a new id for the indicator div
297                 divs[i].setAttribute('id',divs[i].getAttribute('id')+new_key);
298
299                 inputs = divs[i].getElementsByTagName('input');
300                 inputs[0].setAttribute('id',inputs[0].getAttribute('id')+new_key);
301                 inputs[1].setAttribute('id',inputs[1].getAttribute('id')+new_key);
302
303                 var CloneButtonPlus;
304                 try {
305                     anchors = divs[i].getElementsByTagName('a');
306                     for ( j = 0; j < anchors.length; j++) {
307                         if (anchors[j].getAttribute('class') == 'buttonPlus') {
308                             anchors[j].setAttribute('onclick',"CloneField('" + new_id + "','" + hideMarc + "','" + advancedMARCEditor + "'); return false;");
309                         } else if (anchors[j].getAttribute('class') == 'buttonMinus') {
310                             anchors[j].setAttribute('onclick',"UnCloneField('" + new_id + "'); return false;");
311                         } else if (anchors[j].getAttribute('class') == 'expandfield') {
312                             anchors[j].setAttribute('data-field_id',new_id);
313                         }
314                     }
315                 }
316                 catch(e){
317                     // do nothig CloneButtonPlus doesn't exist.
318                 }
319
320             }
321         }
322     }
323
324     // insert this line on the page
325     original.parentNode.insertBefore(clone,original.nextSibling);
326
327     $("ul.sortable_subfield", clone).sortable();
328
329     Select2Utils.initSelect2($(original).find('select'));
330     Select2Utils.initSelect2($(clone).find('select'));
331 }
332
333
334 /**
335  * To clone a subfield
336  * @param index
337  * @param advancedMARCEditor '0' for false, '1' for true
338  */
339 function CloneSubfield(index, advancedMARCEditor){
340     var original = document.getElementById(index); //original <div>
341     Select2Utils.removeSelect2($(original).find('select'));
342     var clone = original.cloneNode(true);
343     var new_key = CreateKey();
344     // set the attribute for the new 'li' subfields
345     var inputs     = clone.getElementsByTagName('input');
346     var selects    = clone.getElementsByTagName('select');
347     var textareas  = clone.getElementsByTagName('textarea');
348     var linkid;
349     var oldcontrol;
350
351     // input
352     var id_input = "";
353     for(var i=0,len=inputs.length; i<len ; i++ ){
354         id_input = inputs[i].getAttribute('id')+new_key;
355         inputs[i].setAttribute('id',id_input);
356         inputs[i].setAttribute('name',inputs[i].getAttribute('name')+new_key);
357         if(inputs[i].getAttribute("id") && inputs[i].getAttribute("id").match(/^tag_/) ){
358             inputs[i].value = "";
359         }
360         linkid = id_input;
361     }
362
363     // Plugin input
364     if( $(inputs[1]).hasClass('framework_plugin') ) {
365         oldcontrol= original.getElementsByTagName('input')[1];
366         AddEventHandlers( oldcontrol, inputs[1], linkid );
367     }
368
369     // select
370     for(i=0,len=selects.length; i<len ; i++ ){
371         id_input = selects[i].getAttribute('id')+new_key;
372         selects[i].setAttribute('id',selects[i].getAttribute('id')+new_key);
373         selects[i].setAttribute('name',selects[i].getAttribute('name')+new_key);
374         linkid = id_input;
375     }
376
377     // textarea
378     for( i=0,len=textareas.length; i<len ; i++ ){
379         id_input = textareas[i].getAttribute('id')+new_key;
380         textareas[i].setAttribute('id',textareas[i].getAttribute('id')+new_key);
381         textareas[i].setAttribute('name',textareas[i].getAttribute('name')+new_key);
382         if(textareas[i].getAttribute("id") && textareas[i].getAttribute("id").match(/^tag_/) ){
383             textareas[i].value = "";
384         }
385         linkid = id_input;
386     }
387
388     // Handle click event on buttonDot for plugin
389     var links  = clone.getElementsByTagName('a');
390     if( $(links[0]).hasClass('framework_plugin') ) {
391         oldcontrol= original.getElementsByTagName('a')[0];
392         AddEventHandlers( oldcontrol, links[0], linkid );
393     }
394
395     if(advancedMARCEditor == '0') {
396         // when cloning a subfield, reset its label too.
397         var label = clone.getElementsByTagName('label')[0];
398         if( label ){
399             label.setAttribute('for',id_input);
400         }
401     }
402
403     // setting a new id for the parent div
404     var new_id  = original.getAttribute('id')+new_key;
405     clone.setAttribute('id',new_id);
406
407     try {
408         var anchors = clone.getElementsByTagName('a');
409         if(anchors.length){
410             for( i = 0 ,len = anchors.length ; i < len ; i++){
411                 if(anchors[i].getAttribute('class') == 'buttonPlus'){
412                     anchors[i].setAttribute('onclick',"CloneSubfield('" + new_id + "','" + advancedMARCEditor + "'); return false;");
413                 } else if (anchors[i].getAttribute('class') == 'buttonMinus') {
414                     anchors[i].setAttribute('onclick',"UnCloneField('" + new_id + "'); return false;");
415                 }
416             }
417         }
418     }
419     catch(e){
420         // do nothig if ButtonPlus & CloneButtonPlus don't exist.
421     }
422     // insert this line on the page
423     original.parentNode.insertBefore(clone,original.nextSibling);
424
425     //Restablish select2 for the cloned elements.
426     Select2Utils.initSelect2($(original).find('select'));
427     Select2Utils.initSelect2($(clone).find('select'));
428
429     // delete data of cloned subfield
430     clone.querySelectorAll('input.input_marceditor').value = "";
431 }
432
433 function AddEventHandlers (oldcontrol, newcontrol, newinputid ) {
434 // This function is a helper for CloneField and CloneSubfield.
435 // It adds the event handlers from oldcontrol to newcontrol.
436 // newinputid is the id attribute of the cloned controlling input field
437 // Note: This code depends on the jQuery data for events; this structure
438 // is moved to _data as of jQuery 1.8.
439     var ev= $(oldcontrol).data('events');
440     if(typeof ev != 'undefined') {
441         $.each(ev, function(prop,val) {
442             $.each(val, function(prop2,val2) {
443                 $(newcontrol).off( val2.type );
444                 $(newcontrol).on( val2.type, {id: newinputid}, val2.handler );
445             });
446         });
447     }
448 }
449
450 /**
451  * This function removes or clears unwanted subfields
452  */
453 function UnCloneField(index) {
454     var original = document.getElementById(index);
455     var canUnclone = false;
456     if ($(original).hasClass("tag")) {
457         // unclone a field, check if there will remain one field
458         var fieldCode = getFieldCode(index);
459         // tag divs with id begining with original field code
460         var cloneFields = $('.tag[id^="tag_'+fieldCode+'"]');
461         if (cloneFields.length > 1) {
462             canUnclone = true;
463         }
464     } else {
465         // unclone a subfield, check if there will remain one subfield
466         var subfieldCode = getFieldAndSubfieldCode(index);
467         // subfield divs of same field with id begining with original field and subfield field code
468         var cloneSubfields = $(original).parent().children('.subfield_line[id^="subfield'+subfieldCode+'"]');
469         if (cloneSubfields.length > 1) {
470             canUnclone = true;
471         }
472     }
473     if (canUnclone) {
474         // remove clone
475         original.parentNode.removeChild(original);
476     } else {
477         // clear inputs, but don't delete
478         $(":input.input_marceditor", original).each(function(){
479             // thanks to http://www.learningjquery.com/2007/08/clearing-form-data for
480             // hint about clearing selects correctly
481             var type = this.type;
482             var tag = this.tagName.toLowerCase();
483             if (type == 'text' || type == 'password' || tag == 'textarea') {
484                 this.value = "";
485             } else if (type == 'checkbox' || type == 'radio') {
486                 this.checked = false;
487             } else if (tag == 'select') {
488                 this.selectedIndex = -1;
489                 // required for Select2 to be able to update its control
490                 $(this).trigger('change');
491             }
492         });
493         $(":input.indicator", original).val("");
494     }
495 }
496
497 /**
498  * This function create a random number
499  */
500 function CreateKey(){
501     return parseInt(Math.random() * 100000);
502 }
503
504 /* Functions developed for additem.tt */
505
506 /**
507  * To clone a subfield.<br>
508  * @param original subfield div to clone
509  */
510 function CloneItemSubfield(original){
511     Select2Utils.removeSelect2($(original).find('select'));
512     var clone = original.cloneNode(true);
513     var new_key = CreateKey();
514
515     // set the attribute for the new 'li' subfields
516     var inputs     = clone.getElementsByTagName('input');
517     var selects    = clone.getElementsByTagName('select');
518     var textareas  = clone.getElementsByTagName('textarea');
519
520     // input (except hidden type)
521     var id_input = "";
522     for(var i=0,len=inputs.length; i<len ; i++ ){
523         if (inputs[i].getAttribute('type') != 'hidden') {
524             id_input = inputs[i].getAttribute('id')+new_key;
525             inputs[i].setAttribute('id',id_input);
526         }
527     }
528
529     // select
530     for( i=0,len=selects.length; i<len ; i++ ){
531         id_input = selects[i].getAttribute('id')+new_key;
532         selects[i].setAttribute('id',selects[i].getAttribute('id')+new_key);
533     }
534
535     // textarea
536     for( i=0,len=textareas.length; i<len ; i++ ){
537         id_input = textareas[i].getAttribute('id')+new_key;
538         textareas[i].setAttribute('id',textareas[i].getAttribute('id')+new_key);
539     }
540
541     // when cloning a subfield, reset its label too.
542     var label = clone.getElementsByTagName('label')[0];
543     label.setAttribute('for',id_input);
544
545     // setting a new if for the parent div
546     var new_id = original.getAttribute('id')+new_key;
547     clone.setAttribute('id',new_id);
548
549     // Don't clone "RegEx". We don't handle it for repeatable subfields
550     var links = clone.getElementsByTagName('a');
551     for( i = 0 ,len = links.length ; i < len ; i++){
552         if( $(links[i]).hasClass('field_regex') ) {
553             $(links[i]).remove();
554         }
555     }
556
557     // insert this line on the page
558     original.parentNode.insertBefore(clone,original.nextSibling);
559     Select2Utils.initSelect2($(original).find('select'));
560     Select2Utils.initSelect2($(clone).find('select'));
561 }
562
563 /**
564  * Check mandatory subfields of a cataloging form and adds <code>missing</code> class to those who are empty.<br>
565  * @param p the parent object of subfields to check
566  * @return the number of empty mandatory subfields
567  */
568 function CheckMandatorySubfields(p){
569     var total = 0;
570     $(p).find(".subfield_line input[name='mandatory'][value='1']").each(function(){
571         var editor = $(this).siblings(".input_marceditor");
572         if ( !editor.length ) { // Deal with date inputs
573             editor = $(this).siblings(".flatpickr_wrapper").find(".input_marceditor");
574         }
575         if (!editor.val()) {
576             editor.addClass("missing");
577             total++;
578         }
579     });
580     return total;
581 }
582
583 function CheckImportantSubfields(p){
584     var total = 0;
585     $(p).find(".subfield_line input[name='important'][value='1']").each(function(i){
586         var editor = $(this).siblings(".input_marceditor");
587         if ( !editor.length ) { // Deal with date inputs
588             editor = $(this).siblings(".flatpickr_wrapper").find(".input_marceditor");
589         }
590         if (!editor.val()) {
591             editor.addClass("missing");
592             total++;
593         }
594     });
595     return total;
596 }
597
598 $(document).ready(function() {
599     $("input.input_marceditor, input.indicator").addClass('noEnterSubmit');
600     $(document).ajaxSuccess(function() {
601         $("input.input_marceditor, input.indicator").addClass('noEnterSubmit');
602     });
603
604     if ( window.editor === undefined ) { // TODO This does not work with the advanced editor
605         Select2Utils.initSelect2($('.subfield_line select[data-category=""]')); // branches, itemtypes and cn_source
606         Select2Utils.initSelect2($('.subfield_line select[data-category!=""]'));
607     }
608
609     $("#avCreate").on("hidden.bs.modal", function(){
610         add_new_av.resetForm(); /* resets form state for jQuery Validate plugin */
611         $("#add_new_av")[0].reset();
612         $(".avCreate_error").hide();
613     });
614
615     var add_new_av = $("#add_new_av").validate({
616         submitHandler: function(form) {
617             var category         = form.category.value;
618             var value            = form.value.value;
619             var description      = form.description.value;
620             var opac_description = form.opac_description.value;
621
622             var data = "category="+encodeURIComponent(category)
623                 +"&value="+encodeURIComponent(value)
624                 +"&description="+encodeURIComponent(description)
625                 +"&opac_description="+encodeURIComponent(opac_description);
626             $.ajax({
627                 type: "POST",
628                 url: "/cgi-bin/koha/svc/authorised_values",
629                 data: data,
630                 success: function(response) {
631                     $('#avCreate').modal('hide');
632
633                     $(current_select2).append('<option selected value="'+response.value+'">'+response.description+'</option>');
634                     $("#avCreate").modal("hide");
635                 },
636                 error: function() {
637                     $(".avCreate_error").html(__("Something went wrong. Maybe the value already exists?")).show();
638                 }
639             });
640             return false;
641         }
642     });
643
644 });