View | Details | Raw Unified | Return to bug 25728
Collapse All | Expand All

(-)a/koha-tmpl/intranet-tmpl/prog/js/cataloging.js (-80 / +71 lines)
Lines 77-97 function ExpandField(index) { Link Here
77
    }
77
    }
78
}
78
}
79
79
80
var current_select2;
80
var Select2Utils = {
81
var Select2Utils = {
81
    removeSelect2: function(element) {
82
    removeSelect2: function(selects) {
82
        if ($.fn.select2) {
83
        if ($.fn.select2) {
83
            var selects = element.getElementsByTagName('select');
84
            $(selects).each(function(){
84
            for (var i=0; i < selects.length; i++) {
85
                $(this).select2('destroy');
85
                $(selects[i]).select2('destroy');
86
            });
86
            }
87
        }
87
        }
88
    },
88
    },
89
89
90
    initSelect2: function(element) {
90
    initSelect2: function(selects) {
91
        if ($.fn.select2) {
91
        if ($.fn.select2) {
92
            var selects = element.getElementsByTagName('select');
92
            if ( ! CAN_user_parameters_manage_auth_values ) {
93
            for (var i=0; i < selects.length; i++) {
93
                $(selects).select2();
94
                $(selects[i]).select2();
94
            } else {
95
                $(selects).select2({
96
                    tags: true,
97
                    createTag: function (tag) {
98
                        return {
99
                            id: tag.term,
100
                            text: tag.term,
101
                            newTag: true
102
                        };
103
                    },
104
                    templateResult: function(state) {
105
                        if (state.newTag) {
106
                            return state.text + " " + "(select to create)";
107
                        }
108
                        return state.text;
109
                    }
110
                }).on("select2:select", function(e) {
111
                    if(e.params.data.newTag){
112
                        current_select2 = this;
113
                        var category = $(this).data("category");
114
                        $("#avCreate #new_av_category").html(category);
115
                        $("#avCreate input[name='category']").val(category);
116
                        $("#avCreate input[name='value']").val(e.params.data.text);
117
                        $("#avCreate input[name='description']").val(e.params.data.text);
118
119
                        $(this).val($(this).find("option:first").val()).trigger('change');
120
                        $('#avCreate').modal({show:true});
121
                    }
122
                });
95
            }
123
            }
96
        }
124
        }
97
    }
125
    }
Lines 104-110 var Select2Utils = { Link Here
104
 */
132
 */
105
function CloneField(index, hideMarc, advancedMARCEditor) {
133
function CloneField(index, hideMarc, advancedMARCEditor) {
106
    var original = document.getElementById(index); //original <li>
134
    var original = document.getElementById(index); //original <li>
107
    Select2Utils.removeSelect2(original);
135
    Select2Utils.removeSelect2($(original).find('select'));
108
136
109
    var clone = original.cloneNode(true);
137
    var clone = original.cloneNode(true);
110
    var new_key = CreateKey();
138
    var new_key = CreateKey();
Lines 262-269 function CloneField(index, hideMarc, advancedMARCEditor) { Link Here
262
290
263
    $("ul.sortable_subfield", clone).sortable();
291
    $("ul.sortable_subfield", clone).sortable();
264
292
265
    Select2Utils.initSelect2(original);
293
    Select2Utils.initSelect2($(original).find('select'));
266
    Select2Utils.initSelect2(clone);
294
    Select2Utils.initSelect2($(clone).find('select'));
267
}
295
}
268
296
269
297
Lines 274-280 function CloneField(index, hideMarc, advancedMARCEditor) { Link Here
274
 */
302
 */
275
function CloneSubfield(index, advancedMARCEditor){
303
function CloneSubfield(index, advancedMARCEditor){
276
    var original = document.getElementById(index); //original <div>
304
    var original = document.getElementById(index); //original <div>
277
    Select2Utils.removeSelect2(original);
305
    Select2Utils.removeSelect2($(original).find('select'));
278
    var clone = original.cloneNode(true);
306
    var clone = original.cloneNode(true);
279
    var new_key = CreateKey();
307
    var new_key = CreateKey();
280
    // set the attribute for the new 'li' subfields
308
    // set the attribute for the new 'li' subfields
Lines 357-364 function CloneSubfield(index, advancedMARCEditor){ Link Here
357
    original.parentNode.insertBefore(clone,original.nextSibling);
385
    original.parentNode.insertBefore(clone,original.nextSibling);
358
386
359
    //Restablish select2 for the cloned elements.
387
    //Restablish select2 for the cloned elements.
360
    Select2Utils.initSelect2(original);
388
    Select2Utils.initSelect2($(original).find('select'));
361
    Select2Utils.initSelect2(clone);
389
    Select2Utils.initSelect2($(clone).find('select'));
362
390
363
    // delete data of cloned subfield
391
    // delete data of cloned subfield
364
    clone.querySelectorAll('input.input_marceditor').value = "";
392
    clone.querySelectorAll('input.input_marceditor').value = "";
Lines 442-448 function CreateKey(){ Link Here
442
 * @param original subfield div to clone
470
 * @param original subfield div to clone
443
 */
471
 */
444
function CloneItemSubfield(original){
472
function CloneItemSubfield(original){
445
    Select2Utils.removeSelect2(original);
473
    Select2Utils.removeSelect2($(original).find('select'));
446
    var clone = original.cloneNode(true);
474
    var clone = original.cloneNode(true);
447
    var new_key = CreateKey();
475
    var new_key = CreateKey();
448
476
Lines 482-489 function CloneItemSubfield(original){ Link Here
482
510
483
    // insert this line on the page
511
    // insert this line on the page
484
    original.parentNode.insertBefore(clone,original.nextSibling);
512
    original.parentNode.insertBefore(clone,original.nextSibling);
485
    Select2Utils.initSelect2(original);
513
    Select2Utils.initSelect2($(original).find('select'));
486
    Select2Utils.initSelect2(clone);
514
    Select2Utils.initSelect2($(clone).find('select'));
487
}
515
}
488
516
489
/**
517
/**
Lines 522-587 $(document).ready(function() { Link Here
522
    });
550
    });
523
551
524
    if ( window.editor === undefined ) { // TODO This does not work with the advanced editor
552
    if ( window.editor === undefined ) { // TODO This does not work with the advanced editor
525
        $('.subfield_line select[data-category=""]').select2(); // branches, itemtypes and cn_source
553
        Select2Utils.initSelect2($('.subfield_line select[data-category=""]')); // branches, itemtypes and cn_source
526
554
        Select2Utils.initSelect2($('.subfield_line select[data-category!=""]'));
527
        if ( ! CAN_user_parameters_manage_auth_values ) {
528
            $('.subfield_line select[data-category!=""]').select2();
529
        } else {
530
            var current_select2;
531
            $('.subfield_line select[data-category!=""]').select2({
532
                tags: true,
533
                createTag: function (tag) {
534
                    return {
535
                        id: tag.term,
536
                        text: tag.term,
537
                        newTag: true
538
                    };
539
                },
540
                templateResult: function(state) {
541
                    if (state.newTag) {
542
                        return state.text + " " + "(select to create)";
543
                    }
544
                    return state.text;
545
                }
546
            }).on("select2:select", function(e) {
547
                if(e.params.data.newTag){
548
549
                    var category = $(this).data("category");
550
                    $("#avCreate #new_av_category").html(category);
551
                    $("#avCreate input[name='category']").val(category);
552
                    $("#avCreate input[name='value']").val(e.params.data.text);
553
                    $("#avCreate input[name='description']").val(e.params.data.text);
554
                    $('#avCreate').modal({show:true});
555
556
                    $(current_select2).val($(current_select2).find("option:first").val()).trigger('change');
557
558
                    current_select2 = this;
559
560
                }
561
            });
562
563
            $("#add_new_av").on("submit", function(){
564
                var data = {
565
                    category: $(this).find('input[name="category"]').val(),
566
                    value: $(this).find('input[name="value"]').val(),
567
                    description: $(this).find('input[name="description"]').val(),
568
                    opac_description: $(this).find('input[name="opac_description"]').val(),
569
                };
570
                $.ajax({
571
                    type: "POST",
572
                    url: "/api/v1/authorised_values",
573
                    data:JSON.stringify(data),
574
                    success: function(response) {
575
                        $('#avCreate').modal('hide');
576
577
                        $(current_select2).append('<option selected value="'+data['value']+'">'+data['description']+'</option>');
578
                    },
579
                    error: function(err) {
580
                        $("#avCreate .error").html(_("Something went wrong, maybe the value already exists?"))
581
                    }
582
                });
583
                return false;
584
            });
585
        }
586
    }
555
    }
556
557
    $("#add_new_av").on("submit", function(){
558
        var data = {
559
            category: $(this).find('input[name="category"]').val(),
560
            value: $(this).find('input[name="value"]').val(),
561
            description: $(this).find('input[name="description"]').val(),
562
            opac_description: $(this).find('input[name="opac_description"]').val(),
563
        };
564
        $.ajax({
565
            type: "POST",
566
            url: "/api/v1/authorised_values",
567
            data:JSON.stringify(data),
568
            success: function(response) {
569
                $('#avCreate').modal('hide');
570
571
                $(current_select2).append('<option selected value="'+data['value']+'">'+data['description']+'</option>');
572
            },
573
            error: function(err) {
574
                $("#avCreate .error").html(_("Something went wrong, maybe the value already exists?"))
575
            }
576
        });
577
        return false;
578
    });
587
});
579
});
588
- 

Return to bug 25728