From 71f444b35fcaf4216e0414223e425ea950844cfb Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Mon, 8 Jun 2020 16:00:48 +0200 Subject: [PATCH] Bug 25728: Create AV when adding a new item We do a bit of refactoring to make the code reusable. Test plan: Same as the first patch but when adding/editing an item QA note: There is a warning from the QA tools FAIL koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/additem.tt FAIL js_in_body A [% Asset.css("css/addbiblio.css") | $raw %] -[% INCLUDE 'select2.inc' %] - diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/addbiblio.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/addbiblio.tt index 8de093209d..4f4fc5901c 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/addbiblio.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/addbiblio.tt @@ -5,6 +5,10 @@ Koha › Cataloging › [% IF ( biblionumber ) %]Editing [% title | html %] (Record number [% biblionumber | html %])[% ELSE %]Add MARC record[% END %] [% INCLUDE 'doc-head-close.inc' %] [% Asset.js("lib/hc-sticky.js") | $raw %] + +[% INCLUDE 'select2.inc' %] [% Asset.js("js/cataloging.js") | $raw %] [% INCLUDE 'strings.inc' %] [% Asset.js("js/browser.js") | $raw %] @@ -482,75 +486,6 @@ [% Asset.css("css/addbiblio.css") | $raw %] -[% INCLUDE 'select2.inc' %] - - @@ -964,47 +899,7 @@ [%# End of fields for fast cataloging %] - + [% INCLUDE 'modals/cataloguing_create_av.inc' %] diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/additem.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/additem.tt index 5ae41036a5..e05b55f2ef 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/additem.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/additem.tt @@ -8,11 +8,14 @@ [% INCLUDE 'doc-head-close.inc' %] [% Asset.css("css/addbiblio.css") | $raw %] [% INCLUDE 'datatables.inc' %] + +[% INCLUDE 'select2.inc' %] [% Asset.js("js/cataloging.js") | $raw %] [% INCLUDE 'columns_settings.inc' %] [% INCLUDE 'strings.inc' %] [% Asset.js("js/browser.js") | $raw %] -[% INCLUDE 'select2.inc' %] [% INCLUDE 'calendar.inc' %] [% INCLUDE 'str/cataloging_additem.inc' %] [% Asset.js("js/cataloging_additem.js") | $raw %] @@ -160,7 +163,11 @@ [% IF ( mv.readonly || ite.IS_RETURN_CLAIM ) %] + [% IF mv.category AND CAN_user_parameters_manage_auth_values %] + + [% END %] [% END %] [% FOREACH aval IN mv.values %] [% IF aval == mv.default %] @@ -266,6 +273,9 @@ + + [% INCLUDE 'modals/cataloguing_create_av.inc' %] + diff --git a/koha-tmpl/intranet-tmpl/prog/js/cataloging.js b/koha-tmpl/intranet-tmpl/prog/js/cataloging.js index c74d953979..3c4d6fbe87 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/cataloging.js +++ b/koha-tmpl/intranet-tmpl/prog/js/cataloging.js @@ -515,9 +515,73 @@ function CheckImportantSubfields(p){ return total; } - $(document).ready(function() { +$(document).ready(function() { $("input.input_marceditor, input.indicator").addClass('noEnterSubmit'); $(document).ajaxSuccess(function() { $("input.input_marceditor, input.indicator").addClass('noEnterSubmit'); }); + + if ( CAN_user_parameters_manage_auth_values ) { + var current_select2; + $('.subfield_line select[data-category!=""]').select2({ + tags: true, + createTag: function (tag) { + return { + id: tag.term, + text: tag.term, + newTag: true + }; + }, + templateResult: function(state) { + if (state.newTag) { + return state.text + " " + __("(select to create)"); + } + return state.text; + } + }).on("select2:select", function(e) { + if(e.params.data.newTag){ + + var category = $(this).data("category"); + $("#avCreate #new_av_category").html(category); + $("#avCreate input[name='category']").val(category); + $("#avCreate input[name='value']").val(e.params.data.text); + $("#avCreate input[name='description']").val(e.params.data.text); + $('#avCreate').modal({show:true}); + + $(current_select2).val($(current_select2).find("option:first").val()).trigger('change'); + + current_select2 = this; + + } + }).on("select2:clear", function () { + $(this).on("select2:opening.cancelOpen", function (evt) { + evt.preventDefault(); + + $(this).off("select2:opening.cancelOpen"); + }); + }); + + $("#add_new_av").on("submit", function(){ + var data = { + category: $(this).find('input[name="category"]').val(), + value: $(this).find('input[name="value"]').val(), + description: $(this).find('input[name="description"]').val(), + opac_description: $(this).find('input[name="opac_description"]').val(), + }; + $.ajax({ + type: "POST", + url: "/api/v1/authorised_values", + data:JSON.stringify(data), + success: function(response) { + $('#avCreate').modal('hide'); + + $(current_select2).append(''); + }, + error: function(err) { + $("#avCreate .error").html(_("Something went wrong, maybe the value already exists?")) + } + }); + return false; + }); + } }); -- 2.11.0