MiraitSystems/enju_trunk

View on GitHub
app/views/agents/_form.html.erb

Summary

Maintainability
Test Coverage
<%#= TODO: for app/assets/javascripts/agent.js -- start --- %>

<script>
  $(document).ready(function(){
    // check full_name_alternative
    <% unless @agent.full_name_alternative.blank? %>
      if ("<%= @agent.full_name_alternative.gsub(/[ \s\t]+$/o, "").gsub(/^[ \s\t]+/o, "") -%>" != "")
        $("#full_name_alternative").show();
    <%- end -%> 
    // check address2
    var input_address2 = false;
    "<%- if @agent.zip_code_2 -%>"
      if ("<%=@agent.zip_code_2.gsub(/[ \s\t]+$/o, "").gsub(/^[ \s\t]+/o, "") -%>" != "")
        input_address2 = true;
    "<%- end -%>"
    "<%- if @agent.address_2 -%>"
      if ("<%=@agent.address_2.gsub(/[ \s\t]+$/o, "").gsub(/^[ \s\t]+/o, "") -%>" != "")
        input_address2 = true;
    "<%- end -%>"
    "<%- if @agent.telephone_number_2 -%>"
      if ("<%=@agent.telephone_number_2.gsub(/[ \s\t]+$/o, "").gsub(/^[ \s\t]+/o, "") -%>" != "")
        input_address2 = true;
    "<%- end -%>"
    "<%- if @agent.extelephone_number_2 -%>"
      if ("<%=@agent.extelephone_number_2.gsub(/[ \s\t]+$/o, "").gsub(/^[ \s\t]+/o, "") -%>" != "")
        input_address2 = true;
    "<%- end -%>"
    "<%- if @agent.fax_number_2 -%>"
      if ("<%=@agent.fax_number_2.gsub(/[ \s\t]+$/o, "").gsub(/^[ \s\t]+/o, "") -%>" != "")
        input_address2 = true;
    "<%- end -%>"
    "<%- if @agent.address_2_note -%>"
      if ("<%=@agent.address_2_note.gsub(/[ \s\t]+$/o, "").gsub(/^[ \s\t]+/o, "") -%>" != "")
        input_address2 = true;
    "<%- end -%>"
    if (input_address2 == true)
      $("#address_2").show();
    // check agent_type
    change_layout();
    // change agent_type
    $("select#agent_agent_type_id").change(function(){
      change_layout();
    });
  });

  $(document).keydown(function(e) {
    $("input[type=text]").keypress(function(ev) {
      if ((ev.which && ev.which === 13) ||
        (ev.keyCode && ev.keyCode === 13)) {
        return false;
      } else {
        return true;
      }
    });
  });
  var ItemField = {
    currentNumber : 0,
    itemTemplate : ''
                 + '<input class="resorce_title" id="agent_agent_aliases_attributes___count___full_name" name="agent[agent_aliases_attributes][__count__][full_name]" size="20" type="text" value="" placeholder=<%= t('activerecord.attributes.agent.full_name') %> /> '
                 + '<input class="resorce_title" id="agent_agent_aliases_attributes___count___full_name_transcription" name="agent[agent_aliases_attributes][__count__][full_name_transcription]" size="20" type="text" value="" placeholder=<%= t('activerecord.attributes.agent.full_name_transcription') %> /> '
                 + '<input class="resorce_title" id="agent_agent_aliases_attributes___count___full_name_alternative" name="agent[agent_aliases_attributes][__count__][full_name_alternative]" size="20" type="text" value="" placeholder=<%= t('activerecord.attributes.agent.full_name_alternative') %> /> '
                 + '<input type="button" value="+" onClick="ItemField.add();" /> '
                 + '<input type="button" value="-" onClick="ItemField.remove();" />',
    itemDeleteTemplate : ''
                 + '<input class="resorce_title" id="agent_agent_aliases_attributes___count___full_name" name="agent[agent_aliases_attributes][__count__][full_name]" size="20" type="hidden" value="" /> '
                 + '<input class="resorce_title" id="agent_agent_aliases_attributes___count___full_name_transcription" name="agent[agent_aliases_attributes][__count__][full_name_transcription]" size="20" type="hidden" value="" /> '
                 + '<input class="resorce_title" id="agent_agent_aliases_attributes___count___full_name_alternative" name="agent[agent_aliases_attributes][__count__][full_name_alternative]" size="20" type="hidden" value="" /> ',
    add : function () {
      this.currentNumber++;
      var field = document.getElementById('SF' + this.currentNumber);
      var replaceCount = this.currentNumber + <%= @countalias -%>;
      var newItem = this.itemTemplate.replace(/__count__/mg, replaceCount);
      field.innerHTML = newItem;

      var nextNumber = this.currentNumber + 1;
      var new_area = document.createElement("div");
      new_area.setAttribute("id", "SF" + nextNumber);
      new_area.setAttribute("class", "field");
      field.appendChild(new_area);
    },
    remove : function () {
      if ( this.currentNumber == 0) { return; }

      var field = document.getElementById('SF' + this.currentNumber);
      field.removeChild(field.lastChild);
      field.innerHTML = '';
      this.currentNumber--;
    },
    alias_remove : function (span_id) {
      var field = document.getElementById("agent_alias" + span_id);
      var newItem = this.itemDeleteTemplate.replace(/__count__/mg, span_id);
      field.innerHTML = newItem;
    }
  };
  
  /* 人物・団体の種類毎に表示・非表示を切り替える */
  function change_layout() {
    agent_type_id = $("select#agent_agent_type_id option:selected").val();
    $("#agent_identifier").hide();
    $("#keyperson_1_field").hide();
    $("#keyperson_2_field").hide();
    $("#corporate_type_field").hide();
    $("#name_person").hide();
    $("#full_name_sub").hide();
    $("#agent_alias").hide();
    $("#title").hide();
    $("#language_id").hide();
    $("#country_id").hide();
    $("#url").hide();
    $("#required_role_id").hide();
    $("#address").hide();
    $("#telephone_number_1").hide();
    $("#extelephone_number_1").hide();
    $("#fax_number_1").hide();
    $("#email").hide();
    $("#zip_code_1").hide();
    $("#address_1").hide();
    $("#address_1_note").hide();
    $("div.agent_full_name_note").hide();
    $("label[for='agent_agent_identifier']").text('<%= t('agent.agent_identifier') %>');
    $("label[for='agent_conference_title']").text('<%= t('agent.conference.title') %>');
    $("label[for='agent_date_of_birth']").text('<%= t('activerecord.attributes.agent.date_of_establishment') %>');
    $("label[for='agent_date_of_death']").text('<%= t('activerecord.attributes.agent.date_of_dissolution') %>');
    $("label[for='agent_full_name']").text('<%= t('activerecord.attributes.agent.full_name') %>');
    $("label[for='agent_full_name_transcription']").text('<%= t('activerecord.attributes.agent..full_name_transcription') %>');
    $("label[for='agent_place']").text('<%= t('activerecord.attributes.agent.place') %>');
    switch (agent_type_id){
      /* 会議 */
      case "<%= AgentType.find_by_name('Conference').try(:id) %>":
        $("label[for='agent_full_name']").text('<%= t('agent.conference.full_name') %>');
        $("#title").show();
        $("label[for='agent_date_of_birth']").text('<%= t('agent.conference.birth_date') %>');
        $("label[for='agent_date_of_death']").text('<%= t('agent.conference.death_date') %>');
        $("label[for='agent_place']").text('<%= t('agent.conference.place') %>');
        $("input#agent_agent_identifier").val("");
        $("input#agent_last_name").val("");
        $("input#agent_last_name_transcription").val("");
        $("input#agent_first_name").val("");
        $("input#agent_first_name_transcription").val("");
        $("input#agent_full_name_transcription").val("");
        $("input#agent_full_name_alternative").val("");
        $.each($("#agent_alias").find('div'), function(i){
          $("input#agent_agent_aliases_attributes_" + i + "_full_name").val("");
          $("input#agent_agent_aliases_attributes_" + i + "_full_name_transcription").val("");
          $("input#agent_agent_aliases_attributes_" + i + "_full_name_alternative").val("");
        });
        $("select#agent_language_id").val("");
        $("select#agent_country_id").val("");
        $("input#agent_url").val("");
        $("select#agent_required_role_id").val("");
        $("#agent_keyperson_1").val("");
        $("#agent_keyperson_2").val("");
        $("input#agent_telephone_number_1").val("");
        $("input#agent_extelephone_number_1").val("");
        $("input#agent_fax_number_1").val("");
        $("input#agent_email").val("");
        $("input#agent_zip_code_1").val("");
        $("textarea#agent_address_1").val("");
        $("textarea#agent_address_1_note").val("");
        if ($('#address_2').is(':visible')){
          $('#address_2').toggle();
        }
        $("input#agent_telephone_number_2").val("");
        $("input#agent_extelephone_number_2").val("");
        $("input#agent_fax_number_2").val("");
        $("input#agent_email_2").val("");
        $("input#agent_zip_code_2").val("");
        $("textarea#agent_address_2").val("");
        $("textarea#agent_address_2_note").val("");
        break;
      /* 担当窓口 */
      case "<%= AgentType.find_by_name('Contact').try(:id) %>":
        $("#agent_identifier").show();
        $("#full_name_sub").show();
        $("#keyperson_1_field").show();
        $("#agent_alias").show();
        $("#keyperson_2_field").show();
        $("input#agent_title").val("");
        $("input#agent_last_name").val("");
        $("input#agent_last_name_transcription").val("");
        $("input#agent_first_name").val("");
        $("input#agent_first_name_transcription").val("");
        $("label[for='agent_agent_identifier']").text('<%= t('agent.contact.agent_identifier') %>');
        $("label[for='agent_full_name']").text('<%= t('agent.contact.full_name') %>');
        $("label[for='agent_full_name_transcription']").text('<%= t('agent.contact.full_name_transcription') %>');
        $("#language_id").show();
        $("#country_id").show();
        $("#url").show();
        $("#required_role_id").show();
        $("#address").show();
        $("#telephone_number_1").show();
        $("#extelephone_number_1").show();
        $("#fax_number_1").show();
        $("#email").show();
        $("#zip_code_1").show();
        $("#address_1").show();
        $("#address_1_note").show();
        break;
      /* 人物 */
      case "<%= AgentType.find_by_name('Person').try(:id) %>":
        $("#agent_identifier").show();
        $("#name_person").show();
        $("#full_name_sub").show();
        $("#agent_alias").show();
        $("div.agent_full_name_note").show();
        $("input#agent_title").val("");
        $("label[for='agent_date_of_birth']").text('<%= t('activerecord.attributes.agent.date_of_birth') %>');
        $("label[for='agent_date_of_death']").text('<%= t('activerecord.attributes.agent.date_of_death') %>');
        $("#language_id").show();
        $("#country_id").show();
        $("#url").show();
        $("#required_role_id").show();
        $("#agent_keyperson_1").val("");
        $("#agent_keyperson_2").val("");
        $("#address").show();
        $("#telephone_number_1").show();
        $("#extelephone_number_1").show();
        $("#fax_number_1").show();
        $("#email").show();
        $("#zip_code_1").show();
        $("#address_1").show();
        $("#address_1_note").show();
        break;
      /* 上記に該当しないもの */
      default:
        $("#agent_identifier").show();
        $("#corporate_type_field").show();
        $("#full_name_sub").show();
        $("#agent_alias").show();
        $("input#agent_title").val("");
        $("input#agent_last_name").val("");
        $("input#agent_last_name_transcription").val("");
        $("input#agent_first_name").val("");
        $("input#agent_first_name_transcription").val("");
        $("#language_id").show();
        $("#country_id").show();
        $("#url").show();
        $("#required_role_id").show();
        $("#agent_keyperson_1").val("");
        $("#agent_keyperson_2").val("");
        $("#address").show();
        $("#telephone_number_1").show();
        $("#extelephone_number_1").show();
        $("#fax_number_1").show();
        $("#email").show();
        $("#zip_code_1").show();
        $("#address_1").show();
        $("#address_1_note").show();
    }
  }
</script>

<%#= TODO: for app/assets/javascripts/agent.js -- end --- %>

<%= render 'page/required_field' %>
<%= simple_form_for(@agent) do |f| -%>
  <%= f.error_messages -%>

  <div class="field">
    <%= f.label t('activerecord.models.agent_type') -%>
    <%= f.select(:agent_type_id, @agent_types.collect{|p| [p.display_name.localize, p.id]}) -%>
  </div>

  <div class="field" id="agent_identifier">
    <%= f.label :agent_identifier %><br />
    <%= f.text_field :agent_identifier, :class => 'date_field' %>
  </div>

  <% if corporate_types %>
    <div class="field" id="corporate_type_field">
      <%= f.label :corporate_type %><br />
      <%= f.select :corporate_type_id, corporate_types.collect{|p| [p.keyname.localize, p.id]} %>
    </div>
  <% end %>

  <div class="field">
    <span id="name_person">
      <%- if SystemConfiguration.get("family_name_first") == true -%>
        <%= render 'agents/form_family_name_first', :f => f -%>
      <%- else -%>
        <%= render 'agents/form_family_name_last', :f => f -%>
      <%- end -%>
    </span>
    <%= f.label :full_name -%><br />
    <div id="annotation" class="agent_full_name_note"><%= t('activerecord.attributes.agent.full_name_note') -%><br /></div>
    <%= f.text_field :full_name, :class => 'resource_title' -%><br />
    <span id="full_name_sub">
      <%= f.label :full_name_transcription -%><br />
      <%= f.text_field :full_name_transcription, :class => 'resource_title' -%><br />
      <%= link_to_function t('activerecord.attributes.agent.full_name_alternative'), "$('#full_name_alternative').toggle()" -%>
      <span id="full_name_alternative" style="display: none">
        <br />
        <%= f.text_field :full_name_alternative, :class => 'resource_title' -%>
      </span>
    </span>
  </div>

  <div class="field" id="agent_alias">
    <%= f.label :agent_alias -%><br />

    <% i = 0 %>
    <%= f.fields_for :agent_aliases do |aliasf| %>
      <span id="<%= 'agent_alias' + i.to_s -%>">
        <%= aliasf.text_field :full_name , :class => 'resorce_title', :size => '20', :placeholder => t('activerecord.attributes.agent.full_name') %>
        <%= aliasf.text_field :full_name_transcription , :class => 'resorce_title', :size => '20', :placeholder => t('activerecord.attributes.agent.full_name_transcription') %>
        <%= aliasf.text_field :full_name_alternative , :class => 'resorce_title', :size => '20', :placeholder => t('activerecord.attributes.agent.full_name_alternative') %>
        <%= button_to_function '+', 'ItemField.add()' -%>
        <br />
      </span>
      <% i += 1 %>
    <% end %>

    <div id="SF1" class="field">
    </div>
  </div>

  <div class="field" id="title">
    <%= f.label :conference_title -%><br />
    <%= f.text_field :title, :class => 'resource_title' -%>
  </div>

  <div class="field">
    <%= f.label :date_of_birth -%><br />
    <%= f.text_field :birth_date, :class => 'date_field' -%>
  </div>

  <% unless @user %>
    <div class="field">
      <%= f.label :date_of_death -%><br />
      <%= f.text_field :death_date, :class => 'date_field' -%>
    </div>
  <% end %>

  <div class="field" id="language_id">
    <%= f.label t('activerecord.models.language') -%>
    <%= f.select(:language_id, @languages.collect{|l| [l.display_name.localize, l.id]}) -%>
  </div>

  <div class="field" id="country_id">
    <%= f.label t('activerecord.models.country') -%>
    <%= f.select(:country_id, @countries.collect{|c| [c.display_name.localize, c.id]}) -%>
  </div>

  <% if @places %>
    <div class="field">
      <%= f.label :place %>
      <%= f.select(:place_id, @places.collect{ |p| [p.term, p.id] }) %>
    </div>
  <% end %>

  <div class="field" id="url">
    <%= f.label :url -%><br />
    <%= f.url_field :url, :class => 'resource_url' -%>
  </div>

  <div class="field" id="required_role_id">
    <%= f.label t('role.required_role') -%>
    <%= f.select(:required_role_id, @roles.collect{|r| [r.display_name.localize, r.id]}) -%>
  </div>

  <div class="field" id='keyperson_1_field'>
    <%= f.label :keyperson_1 %><br />
    <%= f.text_field :keyperson_1, :class => 'date_field' %>
  </div>

  <div class="field" id='keyperson_2_field'>
    <%= f.label :keyperson_2 %><br />
    <%= f.text_field :keyperson_2, :class => 'date_field' %>
  </div>

  <div class="actions">
    <%= f.submit %>
  </div>

  <div id="address">
    <hr />
  </div>

  <div class="field" id="telephone_number_1">
    <%= f.label :telephone_number_1 -%><br />
    <%= f.phone_field :telephone_number_1, :class => 'resource_telephone_number' -%>
    <%= f.select :telephone_number_1_type_id, {t('activerecord.attributes.agent.home_phone')=>1, t('activerecord.attributes.agent.fax')=>2, t('activerecord.attributes.agent.mobile_phone')=>3, t('activerecord.attributes.agent.company_phone')=>4}, include_blank: true -%>
  </div>

  <div class="field" id="extelephone_number_1">
    <%= f.label :extelephone_number_1 -%><br />
    <%= f.phone_field :extelephone_number_1, :class => 'resource_telephone_number' -%>
    <%= f.select :extelephone_number_1_type_id, {t('activerecord.attributes.agent.home_phone')=>1, t('activerecord.attributes.agent.fax')=>2, t('activerecord.attributes.agent.mobile_phone')=>3, t('activerecord.attributes.agent.company_phone')=>4}, include_blank: true -%>
  </div>

  <div class="field" id="fax_number_1">
    <%= f.label :fax_number_1 -%><br />
    <%= f.phone_field :fax_number_1, :class => 'resource_telephone_number' -%>
    <%= f.select :fax_number_1_type_id, {t('activerecord.attributes.agent.home_phone')=>1, t('activerecord.attributes.agent.fax')=>2, t('activerecord.attributes.agent.mobile_phone')=>3, t('activerecord.attributes.agent.company_phone')=>4}, include_blank: true -%>
  </div>

  <div class="field" id="email">
    <%= f.label :email -%><br />
    <%= f.email_field :email, :class => 'resource_email'  -%>
  </div>

  <div id="address_1">
  <div class="field" id="zip_code_1">
    <%= f.label :zip_code_1 -%><br />
    <%= f.text_field :zip_code_1, :class => 'resource_zip_code' -%>
  </div>

  <div class="field" id="address_1">
    <%= f.label :address_1 -%><br />
    <%= f.text_area :address_1, :class => 'resource_textarea' -%>
  </div>

  <div class="field" id="address_1_note">
    <%= f.label :address_1_note -%><br />
    <%= f.text_area :address_1_note, :class => 'resource_textarea' -%>
  </div>

  <div class="field"><%= link_to_function t('agent.other_address'), "$('#address_2').toggle()" -%></div>
  </div>

  <div id="address_2" style="display: none">
  <hr />
  <div class="field" id="telephone_number_2">
    <%= f.label :telephone_number_2 -%><br />
    <%= f.phone_field :telephone_number_2, :class => 'resource_telephone_number' -%>
    <%= f.select :telephone_number_2_type_id, {t('activerecord.attributes.agent.home_phone')=>1, t('activerecord.attributes.agent.fax')=>2, t('activerecord.attributes.agent.mobile_phone')=>3, t('activerecord.attributes.agent.company_phone')=>4}, include_blank: true -%>
  </div>

  <div class="field" id="extelephone_number_2">
    <%= f.label :extelephone_number_2 -%><br />
    <%= f.phone_field :extelephone_number_2, :class => 'resource_telephone_number' -%>
    <%= f.select :extelephone_number_2_type_id, {t('activerecord.attributes.agent.home_phone')=>1, t('activerecord.attributes.agent.fax')=>2, t('activerecord.attributes.agent.mobile_phone')=>3, t('activerecord.attributes.agent.company_phone')=>4}, include_blank: true -%>
  </div>

  <div class="field" id="fax_number_2">
    <%= f.label :fax_number_2 -%><br />
    <%= f.phone_field :fax_number_2, :class => 'resource_telephone_number' -%>
    <%= f.select :fax_number_2_type_id, {t('activerecord.attributes.agent.home_phone')=>1, t('activerecord.attributes.agent.fax')=>2, t('activerecord.attributes.agent.mobile_phone')=>3, t('activerecord.attributes.agent.company_phone')=>4}, include_blank: true -%>
  </div>

  <div class="field" id="email_2">
    <%= f.label :email_2 -%><br />
    <%= f.email_field :email_2, :class => 'resource_email'  -%>
  </div>

  <div class="field" id="zip_code_2">
    <%= f.label :zip_code_2 -%><br />
    <%= f.text_field :zip_code_2, :class => 'resource_zip_code' -%>
  </div>

  <div class="field" id="address_2">
    <%= f.label :address_2 -%><br />
    <%= f.text_area :address_2, :class => 'resource_textarea' -%>
  </div>

  <div class="field" id="address_2_note">
    <%= f.label :address_2_note -%><br />
    <%= f.text_area :address_2_note, :class => 'resource_textarea' -%>
  </div>

  </div>

  <hr />
  <div class="field">
    <%= f.label :note -%>
    <%- if @agent.note_update_at -%>
      &nbsp;<%= t('agent.last_update_at') -%>: <%= l(@agent.note_update_at) -%>
    <%- end -%>
    <%- if @agent.note_update_by -%>
      &nbsp;<%= t('agent.last_update_by') -%>: <%= @agent.note_update_by -%>
    <%- end -%>
    <%- if @agent.note_update_library -%>
      (<%= @agent.note_update_library-%>)
    <%- end -%>
    <br />
    <%= f.text_area :note, :class => 'resource_textarea' -%>
  </div>

  <div class="actions">
    <%= hidden_field_tag 'work_id', @work.id if @work -%>
    <%= hidden_field_tag 'expression_id', @expression.id if @expression -%>
    <%= hidden_field_tag 'manifestation_id', @manifestation.id if @manifestation -%>
    <%= hidden_field_tag 'item_id', @item.id if @item -%>
    <%= f.hidden_field :user_username -%>
    <%= f.submit %>
  </div>
<%- end -%>