// Chops off the last string segment per delimiter. If no delimiter is found in the string then the original string is // returned instead. The following attributes are accepted: // string = Required. The string to chop. // delimiter = Optional. The delimiter used to chop up the string. Defaults to '_'. function chop_last(string, delimiter) { var chopped = string; if (delimiter == undefined) {delimiter = '_';} var endIndex = string.lastIndexOf(delimiter); if (endIndex > 1) {chopped = string.slice(0, endIndex);} return chopped; } // Increments a number embedded in the text by one. If no number is found then the original text is returned. function increment_text(text) { var match = text.match(/\d+/); if (match != null) { var number = new Number(match); var new_number = number + 1; text = text.replace(number, new_number); } return text; }; // Suffixes text with a number. Accepts the following arguments: // text = Required. The text to suffix. // number = Optional. The number to be suffixed to the text. Defaults to 1. // delimiter = Optional. The delimiter between the text and number. Defaults to '_'. function suffix_num_to_text(text, number, delimiter) { if (text != null) { if (number == undefined) { number = 1; }; if (delimiter == undefined) { delimiter = '_'; }; return text + delimiter + number; }; }; // Increments the input field ID number by one so ActiveRecord can save new record attributes. function increment_input_id(input) { id = $(input).attr("id"); id = increment_text(id); $(input).attr("id", id); }; // Increments the input field name number by one so ActiveRecord can save new record attributes. function increment_input_name(input) { name = $(input).attr("name"); name = increment_text(name); $(input).attr("name", name); }; // Generates a hidden input field based off original input field data that instructs ActiveRecord to delete // a record based on the ID of the input field. function generate_destroy_input(input) { $(input).attr("id", chop_last($(input).attr("id")) + "__delete"); $(input).attr("name", chop_last($(input).attr("name"), '[') + "[_delete]"); $(input).attr("type", "hidden"); $(input).attr("value", 1); return input; } // UJS $(document).ready(function(){ // Nested New $("a.nested-new").click(function(){ var id = $(this).attr("id"); var parent_id = '#' + chop_last(id); var clone = $(parent_id + " tr:last").clone(true); var clone_id = clone.attr("id"); // Clone for new action. if (clone_id.length > 4 && clone_id.substr(0, 4) == "new_") { clone_id = clone_id.slice(4); clone_id = suffix_num_to_text(clone_id); clone.attr("id", clone_id); } // Clone for edit action. else { clone.attr("id", increment_text(clone_id)); } $(parent_id).append(clone); // Ensure all cloned input fields are unique. $.each($('#' + clone.attr("id") + " :input"), function(){ increment_input_id(this); increment_input_name(this); $(this).attr("value", ''); return this; }); // Ensure default event does not fire. return false; }); // Nested Destroy $("a.nested-destroy").click(function(){ try { var id = $(this).attr("id"); var parent_id = chop_last(id); if (parent_id != id) { parent_id = '#' + parent_id; $(parent_id).prepend(generate_destroy_input($(parent_id + " input:first").clone())); $(parent_id).animate({backgroundColor: "#FF0000"}, 500); $(parent_id).fadeOut(500); } else { throw "Invalid ID"; } } catch (e) { alert("Error: " + e + ". Check that parent ID is defined and/or the link ID includes parent ID as part of the link ID."); } // Ensure default event does not fire. return false; }); // Destroy $("a.destroy").click(function(){ var result = confirm("Are you sure you want to delete this?"); if (result) { try { var id = $(this).attr("id"); var parent_id = chop_last(id); if (parent_id != id) { parent_id = '#' + parent_id; $(parent_id).animate({backgroundColor: "#FF0000"}, 500); $(parent_id).fadeOut(500); } else { throw "Invalid ID"; } // Finally, call the destroy action. $.post($(this).attr("href"), "_method=delete"); } catch (e) { alert("Error: " + e + ". Check that parent ID is defined and/or the link ID includes parent ID as part of the link ID."); } } // Ensure default event does not fire. return false; }); });