Created
February 1, 2016 13:41
-
-
Save hvwaldow/031dbd5926d653068234 to your computer and use it in GitHub Desktop.
modifications to lcrnz_repeating
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Module for working with multiple field inputs. This will create | |
* a new field when the user clicks the 'add field' button. | |
* | |
*/ | |
this.ckan.module('lcrnz-repeating', function (jQuery, _) { | |
return { | |
options: { | |
/* The selector used for each custom field wrapper */ | |
fieldSelector: '.control-repeating' | |
}, | |
/* Initializes the module and attaches custom event listeners. This | |
* is called internally by ckan.module.initialize(). | |
* | |
* Returns nothing. | |
*/ | |
initialize: function () { | |
if (!jQuery('html').hasClass('ie7')) { | |
jQuery.proxyAll(this, /_on/); | |
// Create 'plus field' checkbox and add to first input container. | |
var firstFieldContainer = this.el.find(this.options.fieldSelector + ':first .controls'); | |
var checkbox = $('<label class="checkbox btn btn-success icon-plus"><input type="checkbox" id="add-field" /></label>'); | |
checkbox.on('change', ':checkbox', this._onChange); | |
checkbox.children(':checkbox').hide(); | |
$(firstFieldContainer).append(checkbox); | |
} | |
}, | |
/* Create a new field and appends it to the list. This currently works by | |
* cloning and erasing an existing input rather than using a template. | |
* In future using a template might be more appropriate. | |
* | |
* element - Another custom field element to wrap. | |
* | |
* Returns nothing. | |
*/ | |
newField: function (element) { | |
newEl = this.cloneField(element); | |
this.el.append(newEl); | |
}, | |
/* Clone the provided element, wipe its content and increment its | |
* `for`, `id` and `name` fields (if possible). | |
* | |
* current - A custom field to clone. | |
* | |
* Returns a newly created custom field element. | |
*/ | |
cloneField: function (current) { | |
return this.resetField(jQuery(current).clone()); | |
}, | |
/* Wipe the contents of the field provided and increment its `name`, `id` | |
* and `for` attributes. Also remove 'add' checkbox if necessary. | |
* | |
* field - A custom field to wipe. | |
* | |
* Returns the wiped element. | |
*/ | |
resetField: function (field) { | |
function increment(index, string) { | |
return (string || '').replace(/\d+/, function (int) { return 1 + parseInt(int, 10); }); | |
} | |
var input = field.find(':input'); | |
input.val('').attr('id', increment).attr('name', increment); | |
var label = field.find('label'); | |
label.text(increment).attr('for', increment); | |
field.find('.checkbox').remove(); | |
return field; | |
}, | |
/* Event handler called when the add checkbox is changed */ | |
_onChange: function (event) { | |
var lastFieldContainer = this.el.find(this.options.fieldSelector + ':last'); | |
this.newField(lastFieldContainer); | |
} | |
}; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% import 'macros/form.html' as form %} | |
{% resource 'eaw_vocabularies/lcrnz-repeating.js' %} | |
<div data-module="lcrnz-repeating"> | |
{%- set values = data.get(field.field_name, []) + [''] -%} | |
{%- for value in values -%} | |
<div class="control-repeating"> | |
{{ form.input(field.field_name + '-' ~ loop.index, | |
id='field-' + field.field_name + '-' ~ loop.index, | |
label=h.scheming_language_text(field.label) + ' ' ~ loop.index, | |
placeholder=h.scheming_language_text(field.form_placeholder), | |
value=value, | |
error=errors[field.field_name], | |
classes=['control-medium'], | |
is_required=h.scheming_field_required(field) | |
) }} | |
</div> | |
{%- endfor -%} | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment