datatracker/ietf/static/js/ipr-edit.js
Jennifer Richards de62fd72d9
fix: Allow additional draft forms to be added to IPR disclosure edit form (#4952)
* chore: Remove commented-out template content

* fix: Correctly number newly added draft_form inputs on IPR edit form

* fix: Initialize select2 when adding a new field instance
2023-01-06 15:24:08 -06:00

72 lines
2.4 KiB
JavaScript

$(document)
.ready(function () {
var form = $(".ipr-form");
$('.draft-add-row')
.on("click", function () {
var template = form.find('.draft-row.template');
var el = template.clone(true)
.removeClass("template d-none");
var totalField = $('#id_iprdocrel_set-TOTAL_FORMS');
var total = +totalField.val();
el.find("*[for*=iprdocrel], *[id*=iprdocrel], *[name*=iprdocrel]")
.not(".d-none")
.each(function () {
var x = $(this);
["for", "id", "name"].forEach(function (at) {
var val = x.attr(at);
if (val && val.match("iprdocrel")) {
x.attr(at, val.replace('__prefix__', total.toString()));
}
});
});
++total;
totalField.val(total);
template.before(el);
el.find('.select2-field').each((index, element) => setupSelect2Field($(element)));
});
function updateRevisions() {
if ($(this)
.hasClass("template"))
return;
var selectbox = $(this)
.find('[name$="document"]');
if (selectbox.val()) {
var name = selectbox.select2("data")[0]
.text;
var prefix = name.toLowerCase()
.substring(0, 3);
if (prefix == "rfc" || prefix == "bcp" || prefix == "std")
$(this)
.find('[name$=revisions]')
.val("")
.hide();
else
$(this)
.find('[name$=revisions]')
.show();
}
}
form.on("change", ".select2-field", function () {
$(this)
.closest(".draft-row")
.each(updateRevisions);
});
// add a little bit of delay to let the select2 box have time to do its magic
// FIXME: this should be done after a select2 event fires!
// See https://select2.org/programmatic-control/events
setTimeout(function () {
form.find(".draft-row")
.each(updateRevisions);
}, 10);
});