<%= form_for @tag_definition, :html => {:class => 'form-horizontal'} do |f| %> <%= f.hidden_field :id %>
<%= f.label :object_type, :class => 'col-sm-2 control-label' do %> Object type <%= ''.html_safe %> <% end %>
<% (@tag_definition.applicable_object_types || [:account]).each_with_index do |object_type, index| %>
<%= f.select "applicable_object_types[#{index}]", object_types, {:selected => object_type, :id => "object_type_#{index}"}, :class => 'form-control tag-definition-select' %> > <%= ''.html_safe %>
<% end %>
<%= f.label :name, 'Name', :class => 'col-sm-2 control-label' %>
<%= f.text_field :name, :class => 'form-control' %>
<%= f.label :description, 'Description', :class => 'col-sm-2 control-label' %>
<%= f.text_field :description, :class => 'form-control' %>
<%= submit_tag 'Save', :class => 'btn btn-default' %>
<% end %> <%= javascript_tag do %> var all_object_types = []; $( document ).ready(function() { initialize(); function initialize(){ all_object_types = $.map($("#object_types>div:last").find("select option"), function(e, i) { return $(e).val(); }); } $(".tag-definition-select").on("change",function(){ refresh_options(); }); }); function get_used_object_types(){ var used_object_types = $.map($('#object_types>div'), function(e, i) { return $(e).find("select").val(); }); return used_object_types; } function refresh_options(){ remove_used_object_types(); // restore missing types var used_object_types = get_used_object_types(); var available_object_types = all_object_types.filter(function(type){ return used_object_types.indexOf(type) < 0; }); $('#object_types>div').each(function(){ var container = this; var object_types = $.map($(container).find("select option"), function(option, i) { return $(option).val(); }); $.each(available_object_types, function(idx, type){ if (object_types.indexOf(type) < 0) { $(container).find("select").append(''); } }); sort_object_types(container); }); } function remove_used_object_types(){ var used_object_types = get_used_object_types(); for (var i = 0; i < used_object_types.length; i++){ $("#object_types>div").each(function(){ var selected = $(this).find("select").val(); if (selected != used_object_types[i]) { $(this).find("select").find("option[value='" + used_object_types[i] + "']").remove(); } }); } } function new_object_type() { var used_object_types = get_used_object_types(); var max_idx = $.map($("[id^=delete_object_type_]"), function(e, i) { return e.id.split("delete_object_type_")[1] } ).sort(function(a, b) { return b - a })[0]; var row = $("#object_types>div:last").clone(true); var row_idx = (parseInt(max_idx) + 1).toString(); row.attr("id", "object_type_line_" + row_idx); row.children("select").attr("id", "object_type_" + row_idx); for (var i = 0; i < used_object_types.length; i++){ row.children("select").find("option[value='" + used_object_types[i] + "']").remove(); } row.children("select").attr('name', ($(row.children("select")).attr('name')).replace(max_idx,row_idx)); row.children("a").attr("id", "delete_object_type_" + row_idx); row.children("a").css("display", "inline-block"); /* Attach row into dom */ if (row.children("select").has("option").length > 0){ row.insertAfter("#object_types>div:last"); } remove_used_object_types(); } function delete_object_type(obj) { var idx = obj.id.split("delete_object_type_")[1]; if ( idx > 0 ){ var object_type = $("#object_type_line_" + idx).find("select").val(); $("#object_type_line_" + idx).remove(); $("#object_types>div").each(function(){ $(this).find("select").append(''); sort_object_types(this); }); } } function sort_object_types(e){ var selected = $(e).find("select").val(); var options = $(e).find("select option"); options.detach().sort(function(a,b) { var at = $(a).text(); var bt = $(b).text(); return (at > bt)?1:((at < bt)?-1:0); }); options.appendTo($(e).find("select")); $(e).find("select").val(selected); } <% end %>