;(function($){ $(document).ready(function() { var multiField = { $fieldWrappers: $('.multi-field-wrapper'), init: function() { var $removeBtns = this.$fieldWrappers.find('.remove-button'); $removeBtns.bind('click', this.onRemoveBtnClick); this.$fieldWrappers.each(this.initWrapper); }, initWrapper: function() { var self = multiField, $wrapper = $(this); self.makeSortable($wrapper); self.appendAddButton($wrapper); self.updateFields($wrapper); }, makeSortable: function( $wrapper ) { var self = multiField; $wrapper.sortable({ axis: "y", containment: "parent", handle: '.handle', update: self.onSortUpdate, start: self.onSortStart }); }, updateFields: function( $wrapper ) { var self = multiField, $fieldGroups = $wrapper.find('li.fields'); $fieldGroups.each(function(key, value) { var $group = $(this), $fields = $group.find('[name]'), $removeBtn = $group.find('.remove-button'), $checkedFields = $fields.filter(':checked'); $fields.each(function(){ var $this = $(this), checked = $this.prop('checked'); // Update the order values on the field // Order values are determined by the name value self.setOrderVal($this, key); //Update the id of the field and any data values self.setIdVal($this, key); }); // Sorting unchecks the radio boxes // Loop the cached checked items and recheck them if(self.$checkedFields != undefined) { self.$checkedFields.each(function(){ var self = multiField, $this = $(this); self.setCheckedState($this); }); } // Update remove button display state if(key == 0) { $removeBtn.addClass('disabled'); } else { $removeBtn.removeClass('disabled'); } }); }, setIdVal: function( $field, key ) { var self = multiField, pattern = /_\d+$/, replace = '_' + key, type = $field.attr('type'), $uploadBtn = $field.siblings('.button-upload'); if(type == 'radio') { pattern = /_(\d+)_(\d+)$/; replace = function(match, p1, p2) { return '_' + key + '_' + p2; } } var newId = self.updateAttributeWithPattern($field, 'id', pattern, replace); // Modify the upload field data attribute if($uploadBtn.length > 0) { $uploadBtn.data('field', newId); } // Modify parent labels for radios and checkboxs $field.parents('label').attr('for', newId); }, setOrderVal: function( $field, key ) { var self = multiField, pattern = /[\d]+/; self.updateAttributeWithPattern($field, 'name', pattern, key); }, setCheckedState: function( $field ) { $field.prop('checked', true); $field.attr('checked', 'checked'); }, updateAttributeWithPattern: function($field, attr, pattern, replace) { var old = $field.attr(attr), newVal = old.replace(pattern, replace); $field.attr(attr, newVal); return newVal; }, cacheCheckedFields: function($wrapper) { var self = multiField; self.$checkedFields = $wrapper.find(':checked'); }, appendAddButton: function( $wrapper ) { var self = multiField; $addButton = $('