Skip to content

Commit

Permalink
Merge pull request #285 from ccancellieri/284-multiple_text
Browse files Browse the repository at this point in the history
#284 Now the add buttow will point to the right component even if we have …
  • Loading branch information
wardi authored Mar 29, 2021
2 parents a8f86cd + 30512e9 commit d441d2d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 36 deletions.
44 changes: 24 additions & 20 deletions ckanext/scheming/assets/js/scheming-multiple-text.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,35 @@
var scheming_multiple_text_init_done = false;
this.ckan.module('scheming-multiple-text', function($, _) {
return {
initialize: function() {
var container = this;
var $this = $(this.el);
var $template = $this.children('div[name="multiple-template"]');
var template = $template.html();
$template.remove();
MultipleText = {

multiple_add: function(field_name){
var fieldset = $('fieldset[name='+field_name+']');
let list = fieldset.find('ol')
let items = list.find('li')
var copy = items.last().clone();
let input = copy.find('input');
input.val('');
list.append(copy);
input.focus();
},
initialize: function() {
if (!scheming_multiple_text_init_done) {
$(document).on('click', 'a[name="multiple-add"]', function(e) {
var $copy = $(template);
$(this).closest('.scheming-fieldset').find('.multiple-text-group').append($copy);
$copy.hide().show(100);
$copy.find('input').focus();
e.preventDefault();
});

$(document).on('click', 'a[name="multiple-remove"]', function(e) {
var $curr = $(this).closest('.multiple-text-field');
$curr.hide(100, function() {
$curr.remove();
});
e.preventDefault();
var list = $(this).closest('ol').find('li');
if (list.length != 1){
var $curr = $(this).closest('.multiple-text-field');
$curr.hide(100, function() {
$curr.remove();
});
e.preventDefault();
}
else{
list.first().find('input').val('');
}
});
scheming_multiple_text_init_done = true;
}
}
};
return MultipleText;
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{% include 'scheming/snippets/multiple_text_asset.html' %}

{% import 'macros/form.html' as form %}

{% macro multiple_text(element, error) %}
Expand All @@ -16,9 +17,6 @@
{% endmacro %}

{%- set values = data.get(field.field_name, []) or ([''] * field.get('form_blanks', 1)) -%}
{%- if values is string -%} {# single value posted in form containing errors #}
{%- set values = [values] -%}
{%- endif -%}

{% call form.input_block(
'field-' + field.field_name,
Expand All @@ -27,7 +25,7 @@
field.classes if 'classes' in field else ['control-medium'],
dict({"class": "form-control"}, **(field.get('form_attrs', {}))),
is_required=h.scheming_field_required(field)) %}
<fieldset name="scheming-multiple-text" class="scheming-fieldset" data-module="scheming-multiple-text">
<fieldset name="{{ field.field_name }}" class="scheming-fieldset" data-module="scheming-multiple-text">
<ol class="multiple-text-group">
{%- for element in values -%}
{{ multiple_text(element, errors[field.field_name] if loop.index == 1 else "") }}
Expand All @@ -42,19 +40,8 @@
{% endif %}

<div class="control-medium">
<a href="javascript:;" name="multiple-add" class="btn btn-link"
<a href="javascript:MultipleText.multiple_add('{{ field.field_name }}');" class="btn btn-link"
>{% block add_button_text %}<i class="fa fa-plus" aria-hidden="true"></i> {{ _('Add') }}{% endblock %}</a>

{% set help_text = h.scheming_language_text(field.help_text) %}
{% if help_text %}
<div class="info-block mrgn-tp-md">
{{ help_text }}
</div>
{% endif %}
</div>

<div name="multiple-template" style="display:none">{{ multiple_text("") }}</div>
</fieldset>
{% endcall %}


0 comments on commit d441d2d

Please sign in to comment.