From 3833d9b1751e9e38c8dcf14abe2b642739b3bbee Mon Sep 17 00:00:00 2001 From: Gauthier Delacroix Date: Tue, 28 Aug 2012 19:08:57 +0200 Subject: [PATCH] Add relative node selection --- README.markdown | 13 +++++++++++++ app/assets/javascripts/cocoon.js | 7 ++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/README.markdown b/README.markdown index 68b9374..3d57b54 100644 --- a/README.markdown +++ b/README.markdown @@ -173,6 +173,7 @@ It takes four parameters: - association: the name of the association (plural) of which a new instance needs to be added (symbol or string). - html_options: extra html-options (see `link_to`) There are some special options, the first three allow to control the placement of the new link-data: + - `data-association-insertion-traversal` : the jquery traversal method to allow node selection relative to the link. `closest`, `next`, `children`, etc. Default: absolute selection - `data-association-insertion-node` : the jquery selector of the node - `data-association-insertion-method` : jquery method that inserts the new data. `before`, `after`, `append`, `prepend`, etc. Default: `before` - `data-association-insertion-position` : old method specifying where to insert new data. @@ -281,6 +282,18 @@ The `association-insertion-node` will determine where to add it. You can choose The `association-insertion-method` will determine where to add it in relation with the node. Any jQuery DOM Manipulation method can be set but we recommend sticking to any of the following: `before`, `after`, `append`, `prepend`. It is unknown at this time what others would do. +The `association-insertion-traversal` will allow node selection to be relative to the link. + +For example: + +````javascript +$(document).ready(function() { + $("#owner a.add_fields"). + data("association-insertion-method", 'append'). + data("association-insertion-traversal", 'closest'). + data("association-insertion-node", '#parent_table'); +}); +```` ### Partial diff --git a/app/assets/javascripts/cocoon.js b/app/assets/javascripts/cocoon.js index 17fd400..e1fe7d7 100644 --- a/app/assets/javascripts/cocoon.js +++ b/app/assets/javascripts/cocoon.js @@ -21,6 +21,7 @@ content = $this.data('template'), insertionMethod = $this.data('association-insertion-method') || $this.data('association-insertion-position') || 'before'; insertionNode = $this.data('association-insertion-node'), + insertionTraversal = $this.data('association-insertion-traversal'), regexp_braced = new RegExp('\\[new_' + assoc + '\\]', 'g'), regexp_underscord = new RegExp('_new_' + assoc + '_', 'g'), new_id = new Date().getTime(), @@ -37,7 +38,11 @@ new_content = new_content.replace(regexp_underscord, newcontent_underscord); if (insertionNode){ - insertionNode = insertionNode == "this" ? $this : $(insertionNode); + if (insertionTraversal){ + insertionNode = $this[insertionTraversal](insertionNode) + } else { + insertionNode = insertionNode == "this" ? $this : $(insertionNode); + } } else { insertionNode = $this.parent(); }