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