2011-09-26 09:01:20 +00:00
|
|
|
(function($) {
|
2011-02-09 21:03:53 +00:00
|
|
|
|
2012-10-12 22:11:16 +00:00
|
|
|
var cocoon_element_counter = 0;
|
|
|
|
|
2011-04-24 22:21:13 +00:00
|
|
|
function replace_in_content(content, regexp_str, with_str) {
|
2011-07-19 09:53:04 +00:00
|
|
|
reg_exp = new RegExp(regexp_str);
|
|
|
|
content.replace(reg_exp, with_str);
|
2011-04-24 22:21:13 +00:00
|
|
|
}
|
|
|
|
|
2011-10-12 20:59:12 +00:00
|
|
|
|
2011-07-19 09:53:04 +00:00
|
|
|
$('.add_fields').live('click', function(e) {
|
|
|
|
e.preventDefault();
|
2011-12-06 20:03:25 +00:00
|
|
|
var $this = $(this),
|
|
|
|
assoc = $this.data('association'),
|
|
|
|
assocs = $this.data('associations'),
|
2012-08-29 08:33:40 +00:00
|
|
|
content = $this.data('association-insertion-template'),
|
2011-12-06 20:03:25 +00:00
|
|
|
insertionMethod = $this.data('association-insertion-method') || $this.data('association-insertion-position') || 'before';
|
|
|
|
insertionNode = $this.data('association-insertion-node'),
|
2012-08-28 17:08:57 +00:00
|
|
|
insertionTraversal = $this.data('association-insertion-traversal'),
|
2012-10-12 22:11:16 +00:00
|
|
|
regexp_braced = new RegExp('\\[new_' + assoc + '\\](.*?\\s)', 'g'),
|
|
|
|
regexp_underscord = new RegExp('_new_' + assoc + '_(\\w*)', 'g'),
|
|
|
|
new_id = new Date().getTime() + cocoon_element_counter++,
|
2011-12-06 20:03:25 +00:00
|
|
|
newcontent_braced = '[' + new_id + ']',
|
2011-07-19 09:53:04 +00:00
|
|
|
newcontent_underscord = '_' + new_id + '_',
|
2012-10-12 22:11:16 +00:00
|
|
|
new_content = content.replace(regexp_braced, '[' + new_id + ']$1');
|
2011-04-24 22:21:13 +00:00
|
|
|
|
2011-02-09 21:03:53 +00:00
|
|
|
if (new_content == content) {
|
2012-10-12 22:11:16 +00:00
|
|
|
regexp_braced = new RegExp('\\[new_' + assocs + '\\](.*?\\s)', 'g');
|
|
|
|
regexp_underscord = new RegExp('_new_' + assocs + '_(\\w*)', 'g');
|
|
|
|
new_content = content.replace(regexp_braced, '[' + new_id + ']$1');
|
2011-02-09 21:03:53 +00:00
|
|
|
}
|
2011-07-19 09:53:04 +00:00
|
|
|
|
2012-10-12 22:11:16 +00:00
|
|
|
new_content = new_content.replace(regexp_underscord, newcontent_underscord + "$1");
|
2011-04-24 22:21:13 +00:00
|
|
|
|
2011-07-19 12:49:59 +00:00
|
|
|
if (insertionNode){
|
2012-08-28 17:08:57 +00:00
|
|
|
if (insertionTraversal){
|
|
|
|
insertionNode = $this[insertionTraversal](insertionNode)
|
|
|
|
} else {
|
|
|
|
insertionNode = insertionNode == "this" ? $this : $(insertionNode);
|
|
|
|
}
|
2011-07-19 12:49:59 +00:00
|
|
|
} else {
|
2011-12-06 20:03:25 +00:00
|
|
|
insertionNode = $this.parent();
|
2011-07-19 12:49:59 +00:00
|
|
|
}
|
2011-04-13 10:08:25 +00:00
|
|
|
|
|
|
|
var contentNode = $(new_content);
|
2011-07-19 09:53:04 +00:00
|
|
|
|
2012-10-18 05:28:29 +00:00
|
|
|
insertionNode.trigger('cocoon:before-insert', [contentNode]);
|
2012-09-20 22:34:13 +00:00
|
|
|
|
2011-12-06 20:03:25 +00:00
|
|
|
// 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'
|
2012-10-18 05:28:29 +00:00
|
|
|
var addedContent = insertionNode[insertionMethod](contentNode);
|
2011-10-12 20:59:12 +00:00
|
|
|
|
2012-10-18 05:28:29 +00:00
|
|
|
insertionNode.trigger('cocoon:after-insert', [contentNode]);
|
2011-02-09 21:03:53 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
2012-10-18 05:28:29 +00:00
|
|
|
$('.remove_fields.dynamic, .remove_fields.existing').live('click', function(e) {
|
2011-12-06 20:03:25 +00:00
|
|
|
var $this = $(this);
|
2012-10-18 05:28:29 +00:00
|
|
|
var node_to_delete = $this.closest(".nested-fields");
|
|
|
|
var trigger_node = node_to_delete.parent();
|
|
|
|
|
2011-07-19 09:53:04 +00:00
|
|
|
e.preventDefault();
|
2012-10-18 05:28:29 +00:00
|
|
|
|
|
|
|
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);
|
2011-02-09 21:03:53 +00:00
|
|
|
});
|
|
|
|
|
2012-09-24 09:00:19 +00:00
|
|
|
})(jQuery);
|