From bbba9dc4c0353ec2c0990dec64c24e56bd6ffe9c Mon Sep 17 00:00:00 2001 From: nathanvda Date: Thu, 18 Oct 2012 07:28:29 +0200 Subject: [PATCH] Add the (to be) inserted or (to be) removed html to the event. The event gets an extra parameter: for `before-insert` and `after-insert` the inserted piece of html. For `before-remove` and `after-remove` the removed piece of html. This way one can add effects/animations if needed. The remove code has been refactored (less code duplication), and checks for a possible timeout, which can be set in the `before-remove` callback. --- app/assets/javascripts/cocoon.js | 46 +++++++++++++++----------------- 1 file changed, 22 insertions(+), 24 deletions(-) diff --git a/app/assets/javascripts/cocoon.js b/app/assets/javascripts/cocoon.js index 2418e6f..b4ec934 100644 --- a/app/assets/javascripts/cocoon.js +++ b/app/assets/javascripts/cocoon.js @@ -7,13 +7,6 @@ content.replace(reg_exp, with_str); } - function trigger_before_removal_callback(node) { - node.trigger('cocoon:before-remove'); - } - - function trigger_after_removal_callback(node) { - node.trigger('cocoon:after-remove'); - } $('.add_fields').live('click', function(e) { e.preventDefault(); @@ -51,33 +44,38 @@ var contentNode = $(new_content); - insertionNode.trigger('cocoon:before-insert'); + insertionNode.trigger('cocoon:before-insert', [contentNode]); // allow any of the jquery dom manipulation methods (after, before, append, prepend, etc) // to be called on the node. allows the insertion node to be the parent of the inserted // code and doesn't force it to be a sibling like after/before does. default: 'before' - insertionNode[insertionMethod](contentNode); + var addedContent = insertionNode[insertionMethod](contentNode); - insertionNode.trigger('cocoon:after-insert'); + insertionNode.trigger('cocoon:after-insert', [contentNode]); }); - $('.remove_fields.dynamic').live('click', function(e) { - var $this = $(this); - var trigger_node = $this.closest(".nested-fields").parent(); - trigger_before_removal_callback(trigger_node); - e.preventDefault(); - $this.closest(".nested-fields").remove(); - trigger_after_removal_callback(trigger_node); - }); - $('.remove_fields.existing').live('click', function(e) { + $('.remove_fields.dynamic, .remove_fields.existing').live('click', function(e) { var $this = $(this); - var trigger_node = $this.closest(".nested-fields").parent(); - trigger_before_removal_callback(trigger_node); + var node_to_delete = $this.closest(".nested-fields"); + var trigger_node = node_to_delete.parent(); + e.preventDefault(); - $this.prev("input[type=hidden]").val("1"); - $this.closest(".nested-fields").hide(); - trigger_after_removal_callback(trigger_node); + + trigger_node.trigger('cocoon:before-remove', [node_to_delete]); + + + var timeout = trigger_node.data('remove-timeout') || 0; + + setTimeout(function() { + if ($this.hasClass('dynamic')) { + $this.closest(".nested-fields").remove(); + } else { + $this.prev("input[type=hidden]").val("1"); + $this.closest(".nested-fields").hide(); + } + trigger_node.trigger('cocoon:after-remove', [node_to_delete]); + }, timeout); }); })(jQuery);