From a4e42d0334fc97f3e6910a9518d86b0397090b31 Mon Sep 17 00:00:00 2001 From: carlo cancellieri Date: Tue, 23 Mar 2021 10:51:17 +0100 Subject: [PATCH 1/4] Now the add buttow will point to the right component even if we have multiple instances of multiple_text --- .../assets/js/scheming-multiple-text.js | 48 ++++++++++--------- .../scheming/form_snippets/multiple_text.html | 5 +- 2 files changed, 29 insertions(+), 24 deletions(-) diff --git a/ckanext/scheming/assets/js/scheming-multiple-text.js b/ckanext/scheming/assets/js/scheming-multiple-text.js index 43d4429e..a6e299b8 100644 --- a/ckanext/scheming/assets/js/scheming-multiple-text.js +++ b/ckanext/scheming/assets/js/scheming-multiple-text.js @@ -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(); +this.ckan.module('scheming-multiple-text-custom', function($, _) { + 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(); + $(document).on('click', 'a[name="multiple-remove-custom"]', function(e) { + 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; }); diff --git a/ckanext/scheming/templates/scheming/form_snippets/multiple_text.html b/ckanext/scheming/templates/scheming/form_snippets/multiple_text.html index c9f57580..21c4db5c 100644 --- a/ckanext/scheming/templates/scheming/form_snippets/multiple_text.html +++ b/ckanext/scheming/templates/scheming/form_snippets/multiple_text.html @@ -1,4 +1,5 @@ {% include 'scheming/snippets/multiple_text_asset.html' %} + {% import 'macros/form.html' as form %} {% macro multiple_text(element, error) %} @@ -27,7 +28,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)) %} -
+
    {%- for element in values -%} {{ multiple_text(element, errors[field.field_name] if loop.index == 1 else "") }} @@ -42,7 +43,7 @@ {% endif %}
    - {% block add_button_text %} {{ _('Add') }}{% endblock %} {% set help_text = h.scheming_language_text(field.help_text) %} From 45937f56e1b9957a52b1b6a030f0f02117dd3cb8 Mon Sep 17 00:00:00 2001 From: carlo cancellieri Date: Tue, 23 Mar 2021 22:08:22 +0100 Subject: [PATCH 2/4] missing remove field fix --- .../scheming/form_snippets/multiple_text.html | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/ckanext/scheming/templates/scheming/form_snippets/multiple_text.html b/ckanext/scheming/templates/scheming/form_snippets/multiple_text.html index 21c4db5c..265849c4 100644 --- a/ckanext/scheming/templates/scheming/form_snippets/multiple_text.html +++ b/ckanext/scheming/templates/scheming/form_snippets/multiple_text.html @@ -17,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, @@ -43,19 +40,8 @@ {% endif %}
    - {% block add_button_text %} {{ _('Add') }}{% endblock %} - - {% set help_text = h.scheming_language_text(field.help_text) %} - {% if help_text %} -
    - {{ help_text }} -
    - {% endif %}
    - -
    {{ multiple_text("") }}
{% endcall %} - - From 88fa2a21fc43ed1fe00889ae87040dcd3d0936f0 Mon Sep 17 00:00:00 2001 From: carlo cancellieri Date: Fri, 26 Mar 2021 15:18:28 +0100 Subject: [PATCH 3/4] now -custom has been properly removed, this should let 'remove' working angain --- ckanext/scheming/assets/js/scheming-multiple-text.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ckanext/scheming/assets/js/scheming-multiple-text.js b/ckanext/scheming/assets/js/scheming-multiple-text.js index a6e299b8..fb273d0f 100644 --- a/ckanext/scheming/assets/js/scheming-multiple-text.js +++ b/ckanext/scheming/assets/js/scheming-multiple-text.js @@ -1,5 +1,5 @@ var scheming_multiple_text_init_done = false; -this.ckan.module('scheming-multiple-text-custom', function($, _) { +this.ckan.module('scheming-multiple-text', function($, _) { MultipleText = { multiple_add: function(field_name){ From 30512e977a1708143eff3241a3c2b3fb86fc9469 Mon Sep 17 00:00:00 2001 From: carlo cancellieri Date: Fri, 26 Mar 2021 15:27:56 +0100 Subject: [PATCH 4/4] now -custom has been properly removed, this should let 'remove' working angain --- ckanext/scheming/assets/js/scheming-multiple-text.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ckanext/scheming/assets/js/scheming-multiple-text.js b/ckanext/scheming/assets/js/scheming-multiple-text.js index fb273d0f..2b024a97 100644 --- a/ckanext/scheming/assets/js/scheming-multiple-text.js +++ b/ckanext/scheming/assets/js/scheming-multiple-text.js @@ -14,7 +14,7 @@ this.ckan.module('scheming-multiple-text', function($, _) { }, initialize: function() { if (!scheming_multiple_text_init_done) { - $(document).on('click', 'a[name="multiple-remove-custom"]', function(e) { + $(document).on('click', 'a[name="multiple-remove"]', function(e) { var list = $(this).closest('ol').find('li'); if (list.length != 1){ var $curr = $(this).closest('.multiple-text-field');