Sha256: 709cf7f11207bc2230f4de9b52bd9c6ae645896c10450eccb97f67fdadca1a0a

Contents?: true

Size: 957 Bytes

Versions: 71

Compression:

Stored size: 957 Bytes

Contents

var ArrayFields = new Class({
	version: 0.1,

	// Implements: [Options, Events],

	initialize: function(_object, _options) {
		if ( ! _object) { return; }
		// this.setOptions(_options);
		var elem = _object;
		var template = elem.getElement('input').clone().erase('value');
		var currentNr = 0;
		
		new Element('span.add_field', {text: '+'})
			.inject(elem)
			.addEvent('click', function(e){
				var newInput = template.clone();
				var arrayI = newInput.get('name').match(/.+?\[(\d+)\].+/);
				if (arrayI && arrayI[1]) {
					currentNr += 1;
					newInput.set('name',
						newInput.get('name')
							.replace(/(.+?\[)\d+(\].+)/, '$1'+(parseInt(arrayI[1], 10)+currentNr)+'$2')
					);
				}
				newInput.inject(e.target, 'before');
			});
		
		elem.getParent('form').addEvent('submit', function(){
			elem.getElements('.array_fields').each(function(input){
				if (input.value) { return; }
				input.erase('name');
			});
			return true;
		});
	}
});

Version data entries

71 entries across 71 versions & 1 rubygems

Version Path
bhf-0.8.0 app/assets/javascripts/bhf/classes/ArrayFields.js
bhf-0.7.16 app/assets/javascripts/bhf/classes/ArrayFields.js
bhf-0.7.15 app/assets/javascripts/bhf/classes/ArrayFields.js
bhf-0.7.14 app/assets/javascripts/bhf/classes/ArrayFields.js
bhf-0.7.13 app/assets/javascripts/bhf/classes/ArrayFields.js
bhf-0.7.12 app/assets/javascripts/bhf/classes/ArrayFields.js
bhf-0.7.11 app/assets/javascripts/bhf/classes/ArrayFields.js
bhf-0.7.9 app/assets/javascripts/bhf/classes/ArrayFields.js
bhf-0.7.8 app/assets/javascripts/bhf/classes/ArrayFields.js
bhf-0.7.7 app/assets/javascripts/bhf/classes/ArrayFields.js
bhf-0.7.5 app/assets/javascripts/bhf/classes/ArrayFields.js
bhf-0.7.4 app/assets/javascripts/bhf/classes/ArrayFields.js
bhf-0.7.3 app/assets/javascripts/bhf/classes/ArrayFields.js
bhf-0.7.2 app/assets/javascripts/bhf/classes/ArrayFields.js
bhf-0.7.1 app/assets/javascripts/bhf/classes/ArrayFields.js
bhf-0.7.0.rc3 app/assets/javascripts/bhf/classes/ArrayFields.js
bhf-0.7.0.rc2 app/assets/javascripts/bhf/classes/ArrayFields.js
bhf-0.7.0.rc1 app/assets/javascripts/bhf/classes/ArrayFields.js
bhf-0.6.32 app/assets/javascripts/bhf/classes/ArrayFields.js
bhf-0.6.31 app/assets/javascripts/bhf/classes/ArrayFields.js