Small refactor cocoon.js

This commit is contained in:
gray 2011-07-19 12:53:04 +03:00
parent 9e83c9cfcd
commit 01ec1ff00b

View File

@ -2,62 +2,57 @@ $(document).ready(function() {
function replace_in_content(content, regexp_str, with_str) { function replace_in_content(content, regexp_str, with_str) {
reg_exp = new RegExp(regexp_str); reg_exp = new RegExp(regexp_str);
content.replace(reg_exp, with_str) content.replace(reg_exp, with_str);
} }
$('.add_fields').live('click', function(e) {
e.preventDefault();
var assoc = $(this).data('association'),
assocs = $(this).data('associations'),
content = $(this).data('template'),
insertionPosition = $(this).data('association-insertion-position'),
insertionNode = $(this).data('association-insertion-node'),
insertionCallback = $(this).data('insertion-callback'),
regexp_braced = new RegExp('\\[new_' + assoc + '\\]', 'g'),
regexp_underscord = new RegExp('_new_' + assoc + '_', 'g'),
new_id = new Date().getTime(),
newcontent_braced = '[' + new_id + ']',
newcontent_underscord = '_' + new_id + '_',
new_content = content.replace(regexp_braced, '[' + new_id + ']');
$('.add_fields').live('click', function() {
var assoc = $(this).attr('data-association');
var assocs = $(this).attr('data-associations');
var content = $(this).attr('data-template');
var insertionPosition = $(this).attr('data-association-insertion-position');
var insertionNode = $(this).attr('data-association-insertion-node');
var insertionCallback = $(this).data('insertion-callback');
var regexp_braced = new RegExp('\\[new_' + assoc + '\\]', 'g');
var regexp_underscord = new RegExp('_new_' + assoc + '_', 'g');
var new_id = new Date().getTime();
var newcontent_braced = '[' + new_id + ']';
var newcontent_underscord = '_' + new_id + '_';
var new_content = content.replace(regexp_braced, '[' + new_id + ']');
if (new_content == content) { if (new_content == content) {
regexp_braced = new RegExp('\\[new_' + assocs + '\\]', 'g'); regexp_braced = new RegExp('\\[new_' + assocs + '\\]', 'g');
regexp_underscord = new RegExp('_new_' + assocs + '_', 'g'); regexp_underscord = new RegExp('_new_' + assocs + '_', 'g');
new_content = content.replace(regexp_braced, '[' + new_id + ']'); new_content = content.replace(regexp_braced, '[' + new_id + ']');
} }
new_content = new_content.replace(regexp_underscord, newcontent_underscord); new_content = new_content.replace(regexp_underscord, newcontent_underscord);
if (insertionNode) { if (insertionNode)
insertionNode = $(insertionNode); insertionNode = $(insertionNode);
} else
else {
insertionNode = $(this).parent(); insertionNode = $(this).parent();
}
var contentNode = $(new_content); var contentNode = $(new_content);
if (insertionPosition == 'after'){ if (insertionPosition == 'after')
insertionNode.after(contentNode); insertionNode.after(contentNode);
} else { else
insertionNode.before(contentNode); insertionNode.before(contentNode);
}
if(insertionCallback){ if (insertionCallback)
insertionCallback.call(contentNode); insertionCallback.call(contentNode);
}
return false;
}); });
$('.remove_fields.dynamic').live('click', function() { $('.remove_fields.dynamic').live('click', function(e) {
e.preventDefault();
$(this).closest(".nested-fields").remove(); $(this).closest(".nested-fields").remove();
return false;
}); });
$('.remove_fields.existing').live('click', function() { $('.remove_fields.existing').live('click', function(e) {
e.preventDefault();
$(this).prev("input[type=hidden]").val("1"); $(this).prev("input[type=hidden]").val("1");
$(this).closest(".nested-fields").hide(); $(this).closest(".nested-fields").hide();
return false;
}); });
}); });