2011-02-09 21:03:53 +00:00
|
|
|
$(document).ready(function() {
|
|
|
|
|
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-07-19 09:53:04 +00:00
|
|
|
$('.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 + ']');
|
2011-04-24 22:21:13 +00:00
|
|
|
|
2011-02-09 21:03:53 +00:00
|
|
|
if (new_content == content) {
|
2011-04-24 12:36:50 +00:00
|
|
|
regexp_braced = new RegExp('\\[new_' + assocs + '\\]', 'g');
|
2011-04-24 22:21:13 +00:00
|
|
|
regexp_underscord = new RegExp('_new_' + assocs + '_', 'g');
|
2011-02-09 21:03:53 +00:00
|
|
|
new_content = content.replace(regexp_braced, '[' + new_id + ']');
|
|
|
|
}
|
2011-07-19 09:53:04 +00:00
|
|
|
|
2011-04-24 22:21:13 +00:00
|
|
|
new_content = new_content.replace(regexp_underscord, newcontent_underscord);
|
|
|
|
|
2011-07-19 12:49:59 +00:00
|
|
|
if (insertionNode){
|
2011-03-03 18:22:23 +00:00
|
|
|
insertionNode = $(insertionNode);
|
2011-07-19 12:49:59 +00:00
|
|
|
} else {
|
2011-03-03 18:22:23 +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
|
|
|
|
2011-07-19 12:49:59 +00:00
|
|
|
if (insertionPosition == 'after'){
|
2011-04-13 10:08:25 +00:00
|
|
|
insertionNode.after(contentNode);
|
2011-07-19 12:49:59 +00:00
|
|
|
} else {
|
2011-07-19 09:53:04 +00:00
|
|
|
insertionNode.before(contentNode);
|
2011-07-19 12:49:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (insertionCallback){
|
2011-04-13 10:08:25 +00:00
|
|
|
insertionCallback.call(contentNode);
|
2011-07-19 12:49:59 +00:00
|
|
|
}
|
2011-02-09 21:03:53 +00:00
|
|
|
});
|
|
|
|
|
2011-07-19 09:53:04 +00:00
|
|
|
$('.remove_fields.dynamic').live('click', function(e) {
|
|
|
|
e.preventDefault();
|
2011-02-09 21:03:53 +00:00
|
|
|
$(this).closest(".nested-fields").remove();
|
|
|
|
});
|
|
|
|
|
2011-07-19 09:53:04 +00:00
|
|
|
$('.remove_fields.existing').live('click', function(e) {
|
|
|
|
e.preventDefault();
|
2011-02-09 21:03:53 +00:00
|
|
|
$(this).prev("input[type=hidden]").val("1");
|
|
|
|
$(this).closest(".nested-fields").hide();
|
|
|
|
});
|
|
|
|
|
2011-07-19 09:53:04 +00:00
|
|
|
});
|