From 3404655cb850d954dc9a1a60dee2ce927dea9ed3 Mon Sep 17 00:00:00 2001 From: Nick Date: Wed, 9 Oct 2013 14:27:35 -0400 Subject: [PATCH 1/2] Addition of a 'new_id' to the callback object when adding new forms (to allow for tabbing, etc.) --- vendor/assets/javascripts/jquery_nested_form.js | 4 ++-- vendor/assets/javascripts/prototype_nested_form.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/vendor/assets/javascripts/jquery_nested_form.js b/vendor/assets/javascripts/jquery_nested_form.js index b9225b14..ea932837 100644 --- a/vendor/assets/javascripts/jquery_nested_form.js +++ b/vendor/assets/javascripts/jquery_nested_form.js @@ -51,8 +51,8 @@ var field = this.insertFields(content, assoc, link); // bubble up event upto document (through form) field - .trigger({ type: 'nested:fieldAdded', field: field }) - .trigger({ type: 'nested:fieldAdded:' + assoc, field: field }); + .trigger({ type: 'nested:fieldAdded', field: field, new_id: new_id }) + .trigger({ type: 'nested:fieldAdded:' + assoc, field: field, new_id: new_id }); return false; }, newId: function() { diff --git a/vendor/assets/javascripts/prototype_nested_form.js b/vendor/assets/javascripts/prototype_nested_form.js index 26733a69..c1cc48a6 100644 --- a/vendor/assets/javascripts/prototype_nested_form.js +++ b/vendor/assets/javascripts/prototype_nested_form.js @@ -48,8 +48,8 @@ document.observe('click', function(e, el) { } else { field = el.insert({ before: content }); } - field.fire('nested:fieldAdded', {field: field}); - field.fire('nested:fieldAdded:' + assoc, {field: field}); + field.fire('nested:fieldAdded', {field: field, new_id: new_id}); + field.fire('nested:fieldAdded:' + assoc, {field: field, new_id: new_id}); return false; } }); From 55916d91f7d65f759e160363667cf15e3432786c Mon Sep 17 00:00:00 2001 From: Nick Date: Mon, 14 Oct 2013 16:59:00 -0400 Subject: [PATCH 2/2] Modifications to support new 'tabled' idea --- vendor/assets/javascripts/jquery_nested_form.js | 9 +++++++-- vendor/assets/javascripts/prototype_nested_form.js | 10 ++++++++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/vendor/assets/javascripts/jquery_nested_form.js b/vendor/assets/javascripts/jquery_nested_form.js index b9225b14..a10637d1 100644 --- a/vendor/assets/javascripts/jquery_nested_form.js +++ b/vendor/assets/javascripts/jquery_nested_form.js @@ -14,8 +14,11 @@ // Make the context correct by replacing with the generated ID // of each of the parent objects - var context = ($(link).closest('.fields').closestChild('input, textarea, select').eq(0).attr('name') || '').replace(/\[[a-z_]+\]$/, ''); - + if($(link).data('tabled')){ + var context = ($(link).closest('table').closestChild('input, textarea, select').eq(0).attr('name') || '').replace(/\[[a-z_]+\]$/, ''); + } else { + var context = ($(link).closest('.fields').closestChild('input, textarea, select').eq(0).attr('name') || '').replace(/\[[a-z_]+\]$/, ''); + } // If the parent has no inputs we need to strip off the last pair var current = content.match(new RegExp('\\[([a-z_]+)\\]\\[new_' + assoc + '\\]'))[1]; if (current) { @@ -62,6 +65,8 @@ var target = $(link).data('target'); if (target) { return $(content).appendTo($(target)); + } else if ($(link).data('tabled')) { + return $(content).insertBefore($(link).closest('tr')) } else { return $(content).insertBefore(link); } diff --git a/vendor/assets/javascripts/prototype_nested_form.js b/vendor/assets/javascripts/prototype_nested_form.js index 26733a69..40177bed 100644 --- a/vendor/assets/javascripts/prototype_nested_form.js +++ b/vendor/assets/javascripts/prototype_nested_form.js @@ -8,8 +8,12 @@ document.observe('click', function(e, el) { // Make the context correct by replacing with the generated ID // of each of the parent objects - var context = (el.getOffsetParent('.fields').firstDescendant().readAttribute('name') || '').replace(/\[[a-z_]+\]$/, ''); - + var context; + if (el.readAttribute('data-tabled')) { + context = (el.getOffsetParent('table').firstDescendant().readAttribute('name') || '').replace(/\[[a-z_]+\]$/, ''); + } else { + context = (el.getOffsetParent('.fields').firstDescendant().readAttribute('name') || '').replace(/\[[a-z_]+\]$/, ''); + } // If the parent has no inputs we need to strip off the last pair var current = content.match(new RegExp('\\[([a-z_]+)\\]\\[new_' + assoc + '\\]'))[1]; if (current) { @@ -45,6 +49,8 @@ document.observe('click', function(e, el) { var field; if (target) { field = $$(target)[0].insert(content); + } else if (el.readAttribute('data-tabled')) { + field = el.getOffsetParent('tr').insert({ before: content }) } else { field = el.insert({ before: content }); }