Skip to content
This repository has been archived by the owner on Jan 25, 2023. It is now read-only.

Commit

Permalink
Use JQuery to extend forms automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-ball committed Apr 3, 2019
1 parent 295e687 commit 26c2d76
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
38 changes: 38 additions & 0 deletions static/js/forms.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* For fields repeated as list items */

function autoduplicate() {
var new_li = $( this ).clone( true );
$( new_li ).find( "label" ).attr( "for", function(index, value) {
return value.replace(/-(\d+)(-\w+)?$/, function(str, p1, p2, offset, s) {
if ( p2 ) {
return '-' + (Number(p1) + 1) + p2;
}
return '-' + (Number(p1) + 1);
});
});
$( new_li ).find( ":input" ).attr( "id", function(index, value) {
return value.replace(/-(\d+)(-\w+)?$/, function(str, p1, p2, offset, s) {
if ( p2 ) {
return '-' + (Number(p1) + 1) + p2;
}
return '-' + (Number(p1) + 1);
});
});
$( new_li ).find( ":input" ).attr( "name", function(index, value) {
return value.replace(/-(\d+)(-\w+)?$/, function(str, p1, p2, offset, s) {
if ( p2 ) {
return '-' + (Number(p1) + 1) + p2;
}
return '-' + (Number(p1) + 1);
});
});
$( new_li ).find( ":input" ).val(function() {return this.defaultValue});
$( new_li ).one( "change", autoduplicate );
$( this ).after( new_li );
}

$( ".form-list > li:last-child" )
.one( "change", autoduplicate );

$( "input[list='keyword-list']" ).parent().filter(":last-child")
.one( "change", autoduplicate );
1 change: 1 addition & 0 deletions templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,6 @@ <h1>{% if self.title() %}{{ self.title() }}{% else %}Metadata Standards Catalog{
</footer>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="{{ url_for('static', filename='js/bootstrap.min.js') }}"></script>
<script src="{{ url_for('static', filename='js/forms.js') }}"></script>
</body>
</html>
4 changes: 2 additions & 2 deletions templates/edit-metadata-scheme.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@
{% endfor %}
</datalist>
<div class="form-inline">
{% for f in form.keywords %}
{% for f in form.keywords -%}
<span class="{{ state(f) }}">{{ f(list="keyword-list", class="form-control form-control-narrow") }}</span>
{% endfor %}
{%- endfor %}
</div>
{% for f in form.keywords %}
{{ errors(f) }}
Expand Down

0 comments on commit 26c2d76

Please sign in to comment.