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

(-)a/koha-tmpl/intranet-tmpl/prog/js/cataloging.js (-80 / +77 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
                }).on("select2:clear", function () {
123
                    $(this).on("select2:opening.cancelOpen", function (evt) {
124
                        evt.preventDefault();
125
126
                        $(this).off("select2:opening.cancelOpen");
127
                    });
128
                });
95
            }
129
            }
96
        }
130
        }
97
    }
131
    }
Lines 104-110 var Select2Utils = { Link Here
104
 */
138
 */
105
function CloneField(index, hideMarc, advancedMARCEditor) {
139
function CloneField(index, hideMarc, advancedMARCEditor) {
106
    var original = document.getElementById(index); //original <li>
140
    var original = document.getElementById(index); //original <li>
107
    Select2Utils.removeSelect2(original);
141
    Select2Utils.removeSelect2($(original).find('select'));
108
142
109
    var clone = original.cloneNode(true);
143
    var clone = original.cloneNode(true);
110
    var new_key = CreateKey();
144
    var new_key = CreateKey();
Lines 262-269 function CloneField(index, hideMarc, advancedMARCEditor) { Link Here
262
296
263
    $("ul.sortable_subfield", clone).sortable();
297
    $("ul.sortable_subfield", clone).sortable();
264
298
265
    Select2Utils.initSelect2(original);
299
    Select2Utils.initSelect2($(original).find('select'));
266
    Select2Utils.initSelect2(clone);
300
    Select2Utils.initSelect2($(clone).find('select'));
267
}
301
}
268
302
269
303
Lines 274-280 function CloneField(index, hideMarc, advancedMARCEditor) { Link Here
274
 */
308
 */
275
function CloneSubfield(index, advancedMARCEditor){
309
function CloneSubfield(index, advancedMARCEditor){
276
    var original = document.getElementById(index); //original <div>
310
    var original = document.getElementById(index); //original <div>
277
    Select2Utils.removeSelect2(original);
311
    Select2Utils.removeSelect2($(original).find('select'));
278
    var clone = original.cloneNode(true);
312
    var clone = original.cloneNode(true);
279
    var new_key = CreateKey();
313
    var new_key = CreateKey();
280
    // set the attribute for the new 'li' subfields
314
    // set the attribute for the new 'li' subfields
Lines 357-364 function CloneSubfield(index, advancedMARCEditor){ Link Here
357
    original.parentNode.insertBefore(clone,original.nextSibling);
391
    original.parentNode.insertBefore(clone,original.nextSibling);
358
392
359
    //Restablish select2 for the cloned elements.
393
    //Restablish select2 for the cloned elements.
360
    Select2Utils.initSelect2(original);
394
    Select2Utils.initSelect2($(original).find('select'));
361
    Select2Utils.initSelect2(clone);
395
    Select2Utils.initSelect2($(clone).find('select'));
362
396
363
    // delete data of cloned subfield
397
    // delete data of cloned subfield
364
    clone.querySelectorAll('input.input_marceditor').value = "";
398
    clone.querySelectorAll('input.input_marceditor').value = "";
Lines 442-448 function CreateKey(){ Link Here
442
 * @param original subfield div to clone
476
 * @param original subfield div to clone
443
 */
477
 */
444
function CloneItemSubfield(original){
478
function CloneItemSubfield(original){
445
    Select2Utils.removeSelect2(original);
479
    Select2Utils.removeSelect2($(original).find('select'));
446
    var clone = original.cloneNode(true);
480
    var clone = original.cloneNode(true);
447
    var new_key = CreateKey();
481
    var new_key = CreateKey();
448
482
Lines 482-489 function CloneItemSubfield(original){ Link Here
482
516
483
    // insert this line on the page
517
    // insert this line on the page
484
    original.parentNode.insertBefore(clone,original.nextSibling);
518
    original.parentNode.insertBefore(clone,original.nextSibling);
485
    Select2Utils.initSelect2(original);
519
    Select2Utils.initSelect2($(original).find('select'));
486
    Select2Utils.initSelect2(clone);
520
    Select2Utils.initSelect2($(clone).find('select'));
487
}
521
}
488
522
489
/**
523
/**
Lines 522-587 $(document).ready(function() { Link Here
522
    });
556
    });
523
557
524
    if ( window.editor === undefined ) { // TODO This does not work with the advanced editor
558
    if ( window.editor === undefined ) { // TODO This does not work with the advanced editor
525
        if ( CAN_user_parameters_manage_auth_values ) {
559
        Select2Utils.initSelect2($('.subfield_line select[data-category=""]')); // branches, itemtypes and cn_source
526
            var current_select2;
560
        Select2Utils.initSelect2($('.subfield_line select[data-category!=""]'));
527
            $('.subfield_line select[data-category!=""]').select2({
528
                tags: true,
529
                createTag: function (tag) {
530
                    return {
531
                        id: tag.term,
532
                        text: tag.term,
533
                        newTag: true
534
                    };
535
                },
536
                templateResult: function(state) {
537
                    if (state.newTag) {
538
                        return state.text + " " + __("(select to create)");
539
                    }
540
                }
541
            }).on("select2:select", function(e) {
542
                if(e.params.data.newTag){
543
544
                    var category = $(this).data("category");
545
                    $("#avCreate #new_av_category").html(category);
546
                    $("#avCreate input[name='category']").val(category);
547
                    $("#avCreate input[name='value']").val(e.params.data.text);
548
                    $("#avCreate input[name='description']").val(e.params.data.text);
549
                    $('#avCreate').modal({show:true});
550
551
                    $(current_select2).val($(current_select2).find("option:first").val()).trigger('change');
552
553
                    current_select2 = this;
554
                }
555
            }).on("select2:clear", function () {
556
                $(this).on("select2:opening.cancelOpen", function (evt) {
557
                    evt.preventDefault();
558
559
                    $(this).off("select2:opening.cancelOpen");
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
    }
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
    });
587
});
585
});
588
- 

Return to bug 25728