<commit_before><script>
  var agentSlect2options = {
    multiple: false,
    minimumInputLength: 1,
    width: "250px",
    allowClear: true,
    placeholder: <%= raw t("agent.search_placeholder").to_json %>,
    formatNoMatches: function(term) {
      return "'" + term + "' " + <%= raw t("agent.search_nomatch").to_json %>;
    },
    formatSearching: function() {
      return <%= raw t("agent.search_searching").to_json %>;
    },
    formatInputTooShort: function(term, minLength){
      return <%= raw t("agent.search_placeholder").to_json %>;
    },
    ajax: {
      url: "/agents/search_name.json",
      dataType: 'json',
      data: function (term, page) {
          return {search_phrase: term,
          };
      },
      results: function (data, page) {
          return {results: data};
      },
    },
    initSelection: function(element, callback) {
      var id = $(element).val();
      if (id != "" && id == parseInt(id)) {
        $.ajax("/agents/search_name.json?agent_id=" + id, {
            dataType: "json"
       }).done(function(data) { callback(data); });
      }
      else{
        callback({ id: id, text: id });
      }
    },
    <% unless SystemConfiguration.get("add_only_exist_agent") %>
      createSearchChoice: function(term, data) {
        if ($(data).filter(function() {
          return this.text.localeCompare(term)===0; }).length===0) {
          return {id:term, text:term};
        }
      },
    <% end %>
  };

  var agentTypeSlect2options = {
    width: "250px",
    matcher: function(term, text, opt) {
      return text.toUpperCase().indexOf(term.toUpperCase())==0
          || opt.attr("alt").toUpperCase().indexOf(term.toUpperCase())==0;
    },
  };

  $(document).ready(function() {
    $(".creator, .contributor, .publisher").select2(agentSlect2options);
    $("select[name$='[type_id]']").select2(agentTypeSlect2options);
  });

  /**
   * select2 選択時の値代入
   * 
   */
  $(document)
    .on("select2-selecting select2-removed", ".creator", function(e){
      var targetId = e.target.id
      var targetNumber = targetId.match(/_(\d+)_/);
      if (e.type == 'select2-removed') {
          $("#creators_" + targetNumber[1] + "_full_name_transcription").val('');
      }
      else if(e.val == parseInt(e.val)){
          $("#creators_" + targetNumber[1] + "_id").val(e.object.id);
          $("#creators_" + targetNumber[1] + "_full_name").val(e.object.text);
          $("#creators_" + targetNumber[1] + "_full_name_transcription").val(e.object.full_name_transcription);
      }
      else{
          $("#creators_" + targetNumber[1] + "_id").val('');
          $("#creators_" + targetNumber[1] + "_full_name").val(e.object.id);
      }
    })
    .on("select2-selecting select2-removed", ".contributor", function(e){
      var targetId = e.target.id
      var targetNumber = targetId.match(/_(\d+)_/);
      if(e.type == 'select2-removed'){
          $("#contributors_" + targetNumber[1] + "_full_name_transcription").val('');
      }
      else if(e.val == parseInt(e.val)){
          $("#contributors_" + targetNumber[1] + "_id").val(e.object.id);
          $("#contributors_" + targetNumber[1] + "_full_name").val(e.object.text);
          $("#contributors_" + targetNumber[1] + "_full_name_transcription").val(e.object.full_name_transcription);
      }
      else{
          $("#contributors_" + targetNumber[1] + "_id").val('');
          $("#contributors_" + targetNumber[1] + "_full_name").val(e.object.id);
      }
    })
    .on("select2-selecting select2-removed", ".publisher", function(e){
      var targetId = e.target.id
      var targetNumber = targetId.match(/_(\d+)_/);
      if(e.type == 'select2-removed'){
          $("#publishers_" + targetNumber[1] + "_full_name_transcription").val('');
      }
      else if(e.val == parseInt(e.val)){
          $("#publishers_" + targetNumber[1] + "_id").val(e.object.id);
          $("#publishers_" + targetNumber[1] + "_full_name").val(e.object.text);
          $("#publishers_" + targetNumber[1] + "_full_name_transcription").val(e.object.full_name_transcription);
      }
      else{
          $("#publishers_" + targetNumber[1] + "_id").val('');
          $("#publishers_" + targetNumber[1] + "_full_name").val(e.object.id);
      }
    });

  /**
   * 著者 select2
   * 
   */
  var ItemFieldCreatorSelect2 = {
    currentNumber : <%= raw (@creators.size).to_json %>,
    actionTemplate : ''
      + '<%= button_to_function t('page.add_line'), "ItemFieldCreatorSelect2.add()" -%>'
      + '<%= button_to_function t('page.del_line'), "ItemFieldCreatorSelect2.remove(__count__)" -%>',
    add : function () {
      var newField = $("<div/>").attr("id", "CF" + this.currentNumber).attr("class", 'row2');
      
      var idFieldId = $("div[id ^= 'CF']:first").find("input[id $= 'id']").attr('id');
      var cloneId = this.cloneField(idFieldId);
      var actionButton = this.actionTemplate.replace(/__count__/mg, this.currentNumber);
      var div1 = $("<div/>");
      var div2 = $("<div/>");

      <% if SystemConfiguration.get("use_agent_type") %>
        var typeFieldId = $("div[id ^= 'CF']:first").find("select[id $= 'type_id']").attr('id');
        var cloneCreatorType = this.cloneFieldWithoutValue(typeFieldId);
        div1.append(cloneCreatorType);
        cloneCreatorType.select2(agentTypeSlect2options);
      <% end %>
      
      <% if SystemConfiguration.get("add_only_exist_agent") %>
        div2.append(cloneId).append(actionButton);
      <% else %>
        var nameFieldId = $("div[id ^= 'CF']:first").find("input[id $= 'full_name']").attr('id');
        var transcriptionFieldId = $("div[id ^= 'CF']:first").find("input[id $= 'full_name_transcription']").attr('id');
        var cloneName = this.cloneField(nameFieldId);
        var cloneFullNameTranscription = this.cloneField(transcriptionFieldId);
        div2.append(cloneId).append(cloneName).append(cloneFullNameTranscription).append(actionButton);
      <% end %>
      cloneId.select2(agentSlect2options);
      newField.append(div1).append(div2);
      $("#creator_field > #creators").append(newField);

      this.currentNumber += 1;
    },
    cloneField : function (fieldId) {
      return $('#' + fieldId).clone().attr({
          'id' : $('#' + fieldId).attr('id').replace(/_\d+_/, '_' + this.currentNumber + '_'),
          'name' : $('#' + fieldId).attr('name').replace(/\[\d+\]/, '[' + this.currentNumber + ']'),
      }).val('');
    },
    cloneFieldWithoutValue : function (fieldId) {
      return $('#' + fieldId).clone().attr({
          'id' : $('#' + fieldId).attr('id').replace(/_\d+_/, '_' + this.currentNumber + '_'),
          'name' : $('#' + fieldId).attr('name').replace(/\[\d+\]/, '[' + this.currentNumber + ']'),
      });
    },
    remove : function (div_num) {
      if($("div[id ^= 'CF']").length == 1) this.add();
      removeDivField('CF', div_num);
    }
  }

  /**
   * 協力者 select2
   * 
   */
  var ItemFieldContributorSelect2 = {
    currentNumber : <%= raw (@contributors.size).to_json %>,
    actionTemplate : ''
      + '<%= button_to_function t('page.add_line'), "ItemFieldContributorSelect2.add()" -%>'
      + '<%= button_to_function t('page.del_line'), "ItemFieldContributorSelect2.remove(__count__)" -%>',
    add : function () {
      var newField = $("<div/>").attr("id", "RF" + this.currentNumber).attr("class", 'row2');
      
      var idFieldId = $("div[id ^= 'RF']:first").find("input[id $= 'id']").attr('id');
      var cloneId = this.cloneField(idFieldId);
      var actionButton = this.actionTemplate.replace(/__count__/mg, this.currentNumber);
      var div1 = $("<div/>");
      var div2 = $("<div/>");

      <% if SystemConfiguration.get("use_agent_type") %>
        var typeFieldId = $("div[id ^= 'RF']:first").find("select[id $= 'type_id']").attr('id');
        var cloneContributorType = this.cloneFieldWithoutValue(typeFieldId);
        div1.append(cloneContributorType);
        cloneContributorType.select2(agentTypeSlect2options);
      <% end %>
      
      <% if SystemConfiguration.get("add_only_exist_agent") %>
        div2.append(cloneId).append(actionButton);
      <% else %>
        var nameFieldId = $("div[id ^= 'RF']:first").find("input[id $= 'full_name']").attr('id');
        var transcriptionFieldId = $("div[id ^= 'RF']:first").find("input[id $= 'full_name_transcription']").attr('id');
        var cloneName = this.cloneField(nameFieldId);
        var cloneFullNameTranscription = this.cloneField(transcriptionFieldId);
        div2.append(cloneId).append(cloneName).append(cloneFullNameTranscription).append(actionButton);
      <% end %>
      cloneId.select2(agentSlect2options);
      newField.append(div1).append(div2);
      $("#contributor_field > #contributors").append(newField);

      this.currentNumber += 1;
    },
    cloneField : function (fieldId) {
      return $('#' + fieldId).clone().attr({
          'id' : $('#' + fieldId).attr('id').replace(/_\d+_/, '_' + this.currentNumber + '_'),
          'name' : $('#' + fieldId).attr('name').replace(/\[\d+\]/, '[' + this.currentNumber + ']'),
      }).val('');
    },
    cloneFieldWithoutValue : function (fieldId) {
      return $('#' + fieldId).clone().attr({
          'id' : $('#' + fieldId).attr('id').replace(/_\d+_/, '_' + this.currentNumber + '_'),
          'name' : $('#' + fieldId).attr('name').replace(/\[\d+\]/, '[' + this.currentNumber + ']'),
      });
    },
    remove : function (div_num) {
      if($("div[id ^= 'RF']").length == 1) this.add();
      removeDivField('RF', div_num);
    }
  }

  /**
   * 出版者 select2
   * 
   */
  var ItemFieldPublisherSelect2 = {
    currentNumber : <%= raw (@publishers.size).to_json %>,
    actionTemplate : ''
      + '<%= button_to_function t('page.add_line'), "ItemFieldPublisherSelect2.add()" -%>'
      + '<%= button_to_function t('page.del_line'), "ItemFieldPublisherSelect2.remove(__count__)" -%>',
    add : function () {
      var newField = $("<div/>").attr("id", "PF" + this.currentNumber).attr("class", 'row2');
      
      var idFieldId = $("div[id ^= 'PF']:first").find("input[id $= 'id']").attr('id');
      var cloneId = this.cloneField(idFieldId);
      var actionButton = this.actionTemplate.replace(/__count__/mg, this.currentNumber);
      var div1 = $("<div/>");
      var div2 = $("<div/>");

      <% if SystemConfiguration.get("use_agent_type") %>
        var typeFieldId = $("div[id ^= 'PF']:first").find("select[id $= 'type_id']").attr('id');
        var clonePublisherType = this.cloneFieldWithoutValue(typeFieldId);
        div1.append(clonePublisherType);
        clonePublisherType.select2(agentTypeSlect2options);
      <% end %>
      
      <% if SystemConfiguration.get("add_only_exist_agent") %>
        div2.append(cloneId).append(actionButton);
      <% else %>
        var nameFieldId = $("div[id ^= 'PF']:first").find("input[id $= 'full_name']").attr('id');
        var transcriptionFieldId = $("div[id ^= 'PF']:first").find("input[id $= 'full_name_transcription']").attr('id');
        var cloneName = this.cloneField(nameFieldId);
        var cloneFullNameTranscription = this.cloneField(transcriptionFieldId);
        div2.append(cloneId).append(cloneName).append(cloneFullNameTranscription).append(actionButton);
      <% end %>
      cloneId.select2(agentSlect2options);
      newField.append(div1).append(div2);
      $("#publisher_field > #publishers").append(newField);

      this.currentNumber += 1;
    },
    cloneField : function (fieldId) {
      return $('#' + fieldId).clone().attr({
          'id' : $('#' + fieldId).attr('id').replace(/_\d+_/, '_' + this.currentNumber + '_'),
          'name' : $('#' + fieldId).attr('name').replace(/\[\d+\]/, '[' + this.currentNumber + ']'),
      }).val('');
    },
    cloneFieldWithoutValue : function (fieldId) {
      return $('#' + fieldId).clone().attr({
          'id' : $('#' + fieldId).attr('id').replace(/_\d+_/, '_' + this.currentNumber + '_'),
          'name' : $('#' + fieldId).attr('name').replace(/\[\d+\]/, '[' + this.currentNumber + ']'),
      });
    },
    remove : function (div_num) {
      if($("div[id ^= 'PF']").length == 1) this.add();
      removeDivField('PF', div_num);
    }
  }
</script>

<div class="row" id="creator_field">
  <div><strong><%= t('agent.creator') %></strong></div>
  <div id="creators">
    <% @creators.each_with_index do |creator, index| -%>
      <div id="CF<%= index %>" class="row2">
        <% if SystemConfiguration.get("use_agent_type") %>
          <%= select2_tag(
            "creators_#{index}_type_id", 
            "creators[][type_id]", 
            @create_types,
            creator[:type_id], 
            :width => 250, :select_attribute => :name, :display_attribute => :display_name 
          ) %><br />
        <% end %>
        <% if SystemConfiguration.get("add_only_exist_agent") %>
          <%= text_field_tag "creators[][id]", creator[:id], :id => "creators_#{index}_id", :class => "creator" %>
        <% else %>
          <%= text_field_tag "creators[][id]", creator[:id], :id => "creators_#{index}_id", :class => "creator" %>
          <%= hidden_field_tag "creators[][full_name]", creator[:full_name], :id => "creators_#{index}_full_name" %>
          <%= text_field_tag "creators[][full_name_transcription]", creator[:full_name_transcription],
              :id => "creators_#{index}_full_name_transcription", :class => 'medium_string', 
              :placeholder => t("activerecord.attributes.agent.full_name_transcription") 
          %>
        <% end %>
        <%= button_to_function t('page.add_line'), "ItemFieldCreatorSelect2.add()" -%>
        <%= button_to_function t('page.del_line'), "ItemFieldCreatorSelect2.remove(#{index})" -%>
      </div>
    <% end %>
  </div>
</div>

<div class="row" id="contributor_field">
  <div><strong><%= t('agent.contributor') %></strong></div>
  <div id="contributors">
    <% @contributors.each_with_index do |contributor, index| -%>
      <div id="RF<%= index %>" class="row2">
        <% if SystemConfiguration.get("use_agent_type") %>
          <%= select2_tag(
            "contributors_#{index}_type_id", 
            "contributors[][type_id]", 
            @realize_types,
            contributor[:type_id], 
            :width => 250, :select_attribute => :name, :display_attribute => :display_name 
          ) %><br />
        <% end %>
        <% if SystemConfiguration.get("add_only_exist_agent") %>
          <%= text_field_tag "contributors[][id]", contributor[:id], :id => "contributors_#{index}_id", :class => "contributor" %>
        <% else %>
            <%= text_field_tag "contributors[][id]", contributor[:id], :id => "contributors_#{index}_id", :class => "contributor" %>
            <%= hidden_field_tag "contributors[][full_name]", contributor[:full_name], :id => "contributors_#{index}_full_name" %>
            <%= text_field_tag "contributors[][full_name_transcription]", contributor[:full_name_transcription],
              :id => "contributors_#{index}_full_name_transcription", :class => 'medium_string',
              :placeholder => t("activerecord.attributes.agent.full_name_transcription") 
            %>
        <% end %>
        <%= button_to_function t('page.add_line'), "ItemFieldContributorSelect2.add()" -%>
        <%= button_to_function t('page.del_line'), "ItemFieldContributorSelect2.remove(#{index})" -%>
      </div>
    <% end %>
  </div>
</div>

<div class="row" id="publisher_field">
  <div><strong><%= t('agent.publisher') %></strong></div>
  <div id="publishers">
    <% @publishers.each_with_index do |publisher, index| -%>
      <div id="PF<%= index %>" class="row2">
        <% if SystemConfiguration.get("use_agent_type") %>
          <%= select2_tag(
            "publishers_#{index}_type_id", 
            "publishers[][type_id]", 
            @produce_types,
            publisher[:type_id], 
            :width => 250, :select_attribute => :name, :display_attribute => :display_name 
          ) %><br />
        <% end %>
        <% if SystemConfiguration.get("add_only_exist_agent") %>
          <%= text_field_tag "publishers[][id]", publisher[:id], :id => "publishers_#{index}_id", :class => "publisher" %>
        <% else %>
            <%= text_field_tag "publishers[][id]", publisher[:id], :id => "publishers_#{index}_id", :class => "publisher" %>
            <%= hidden_field_tag "publishers[][full_name]", publisher[:full_name], :id => "publishers_#{index}_full_name" %>
            <%= text_field_tag "publishers[][full_name_transcription]", publisher[:full_name_transcription],
              :id => "publishers_#{index}_full_name_transcription", :class => 'medium_string',
              :placeholder => t("activerecord.attributes.agent.full_name_transcription") 
            %>
        <% end %>
        <%= button_to_function t('page.add_line'), "ItemFieldPublisherSelect2.add()" -%>
        <%= button_to_function t('page.del_line'), "ItemFieldPublisherSelect2.remove(#{index})" -%>
      </div>
    <% end %> 
  </div>
</div>
<commit_msg>_form_agent_field の select2 修正: type_id にマッチさせると不要なフィールドに適用されるため<commit_after><script>
  var agentSlect2options = {
    multiple: false,
    minimumInputLength: 1,
    width: "250px",
    allowClear: true,
    placeholder: <%= raw t("agent.search_placeholder").to_json %>,
    formatNoMatches: function(term) {
      return "'" + term + "' " + <%= raw t("agent.search_nomatch").to_json %>;
    },
    formatSearching: function() {
      return <%= raw t("agent.search_searching").to_json %>;
    },
    formatInputTooShort: function(term, minLength){
      return <%= raw t("agent.search_placeholder").to_json %>;
    },
    ajax: {
      url: "/agents/search_name.json",
      dataType: 'json',
      data: function (term, page) {
          return {search_phrase: term,
          };
      },
      results: function (data, page) {
          return {results: data};
      },
    },
    initSelection: function(element, callback) {
      var id = $(element).val();
      if (id != "" && id == parseInt(id)) {
        $.ajax("/agents/search_name.json?agent_id=" + id, {
            dataType: "json"
       }).done(function(data) { callback(data); });
      }
      else{
        callback({ id: id, text: id });
      }
    },
    <% unless SystemConfiguration.get("add_only_exist_agent") %>
      createSearchChoice: function(term, data) {
        if ($(data).filter(function() {
          return this.text.localeCompare(term)===0; }).length===0) {
          return {id:term, text:term};
        }
      },
    <% end %>
  };

  var agentTypeSlect2options = {
    width: "250px",
    matcher: function(term, text, opt) {
      return text.toUpperCase().indexOf(term.toUpperCase())==0
          || opt.attr("alt").toUpperCase().indexOf(term.toUpperCase())==0;
    },
  };

  $(document).ready(function() {
    $(".creator, .contributor, .publisher").select2(agentSlect2options);
    $("select[name $= 'create_type_id']").select2(agentTypeSlect2options);
    $("select[name $= 'realize_type_id']").select2(agentTypeSlect2options);
    $("select[name $= 'produce_type_id']").select2(agentTypeSlect2options);
  });

  /**
   * select2 選択時の値代入
   * 
   */
  $(document)
    .on("select2-selecting select2-removed", ".creator", function(e){
      var targetId = e.target.id
      var targetNumber = targetId.match(/_(\d+)_/);
      if (e.type == 'select2-removed') {
          $("#creators_" + targetNumber[1] + "_full_name_transcription").val('');
      }
      else if(e.val == parseInt(e.val)){
          $("#creators_" + targetNumber[1] + "_id").val(e.object.id);
          $("#creators_" + targetNumber[1] + "_full_name").val(e.object.text);
          $("#creators_" + targetNumber[1] + "_full_name_transcription").val(e.object.full_name_transcription);
      }
      else{
          $("#creators_" + targetNumber[1] + "_id").val('');
          $("#creators_" + targetNumber[1] + "_full_name").val(e.object.id);
      }
    })
    .on("select2-selecting select2-removed", ".contributor", function(e){
      var targetId = e.target.id
      var targetNumber = targetId.match(/_(\d+)_/);
      if(e.type == 'select2-removed'){
          $("#contributors_" + targetNumber[1] + "_full_name_transcription").val('');
      }
      else if(e.val == parseInt(e.val)){
          $("#contributors_" + targetNumber[1] + "_id").val(e.object.id);
          $("#contributors_" + targetNumber[1] + "_full_name").val(e.object.text);
          $("#contributors_" + targetNumber[1] + "_full_name_transcription").val(e.object.full_name_transcription);
      }
      else{
          $("#contributors_" + targetNumber[1] + "_id").val('');
          $("#contributors_" + targetNumber[1] + "_full_name").val(e.object.id);
      }
    })
    .on("select2-selecting select2-removed", ".publisher", function(e){
      var targetId = e.target.id
      var targetNumber = targetId.match(/_(\d+)_/);
      if(e.type == 'select2-removed'){
          $("#publishers_" + targetNumber[1] + "_full_name_transcription").val('');
      }
      else if(e.val == parseInt(e.val)){
          $("#publishers_" + targetNumber[1] + "_id").val(e.object.id);
          $("#publishers_" + targetNumber[1] + "_full_name").val(e.object.text);
          $("#publishers_" + targetNumber[1] + "_full_name_transcription").val(e.object.full_name_transcription);
      }
      else{
          $("#publishers_" + targetNumber[1] + "_id").val('');
          $("#publishers_" + targetNumber[1] + "_full_name").val(e.object.id);
      }
    });

  /**
   * 著者 select2
   * 
   */
  var ItemFieldCreatorSelect2 = {
    currentNumber : <%= raw (@creators.size).to_json %>,
    actionTemplate : ''
      + '<%= button_to_function t('page.add_line'), "ItemFieldCreatorSelect2.add()" -%>'
      + '<%= button_to_function t('page.del_line'), "ItemFieldCreatorSelect2.remove(__count__)" -%>',
    add : function () {
      var newField = $("<div/>").attr("id", "CF" + this.currentNumber).attr("class", 'row2');
      
      var idFieldId = $("div[id ^= 'CF']:first").find("input[id $= 'id']").attr('id');
      var cloneId = this.cloneField(idFieldId);
      var actionButton = this.actionTemplate.replace(/__count__/mg, this.currentNumber);
      var div1 = $("<div/>");
      var div2 = $("<div/>");

      <% if SystemConfiguration.get("use_agent_type") %>
        var typeFieldId = $("div[id ^= 'CF']:first").find("select[id $= 'type_id']").attr('id');
        var cloneCreatorType = this.cloneFieldWithoutValue(typeFieldId);
        div1.append(cloneCreatorType);
        cloneCreatorType.select2(agentTypeSlect2options);
      <% end %>
      
      <% if SystemConfiguration.get("add_only_exist_agent") %>
        div2.append(cloneId).append(actionButton);
      <% else %>
        var nameFieldId = $("div[id ^= 'CF']:first").find("input[id $= 'full_name']").attr('id');
        var transcriptionFieldId = $("div[id ^= 'CF']:first").find("input[id $= 'full_name_transcription']").attr('id');
        var cloneName = this.cloneField(nameFieldId);
        var cloneFullNameTranscription = this.cloneField(transcriptionFieldId);
        div2.append(cloneId).append(cloneName).append(cloneFullNameTranscription).append(actionButton);
      <% end %>
      cloneId.select2(agentSlect2options);
      newField.append(div1).append(div2);
      $("#creator_field > #creators").append(newField);

      this.currentNumber += 1;
    },
    cloneField : function (fieldId) {
      return $('#' + fieldId).clone().attr({
          'id' : $('#' + fieldId).attr('id').replace(/_\d+_/, '_' + this.currentNumber + '_'),
          'name' : $('#' + fieldId).attr('name').replace(/\[\d+\]/, '[' + this.currentNumber + ']'),
      }).val('');
    },
    cloneFieldWithoutValue : function (fieldId) {
      return $('#' + fieldId).clone().attr({
          'id' : $('#' + fieldId).attr('id').replace(/_\d+_/, '_' + this.currentNumber + '_'),
          'name' : $('#' + fieldId).attr('name').replace(/\[\d+\]/, '[' + this.currentNumber + ']'),
      });
    },
    remove : function (div_num) {
      if($("div[id ^= 'CF']").length == 1) this.add();
      removeDivField('CF', div_num);
    }
  }

  /**
   * 協力者 select2
   * 
   */
  var ItemFieldContributorSelect2 = {
    currentNumber : <%= raw (@contributors.size).to_json %>,
    actionTemplate : ''
      + '<%= button_to_function t('page.add_line'), "ItemFieldContributorSelect2.add()" -%>'
      + '<%= button_to_function t('page.del_line'), "ItemFieldContributorSelect2.remove(__count__)" -%>',
    add : function () {
      var newField = $("<div/>").attr("id", "RF" + this.currentNumber).attr("class", 'row2');
      
      var idFieldId = $("div[id ^= 'RF']:first").find("input[id $= 'id']").attr('id');
      var cloneId = this.cloneField(idFieldId);
      var actionButton = this.actionTemplate.replace(/__count__/mg, this.currentNumber);
      var div1 = $("<div/>");
      var div2 = $("<div/>");

      <% if SystemConfiguration.get("use_agent_type") %>
        var typeFieldId = $("div[id ^= 'RF']:first").find("select[id $= 'type_id']").attr('id');
        var cloneContributorType = this.cloneFieldWithoutValue(typeFieldId);
        div1.append(cloneContributorType);
        cloneContributorType.select2(agentTypeSlect2options);
      <% end %>
      
      <% if SystemConfiguration.get("add_only_exist_agent") %>
        div2.append(cloneId).append(actionButton);
      <% else %>
        var nameFieldId = $("div[id ^= 'RF']:first").find("input[id $= 'full_name']").attr('id');
        var transcriptionFieldId = $("div[id ^= 'RF']:first").find("input[id $= 'full_name_transcription']").attr('id');
        var cloneName = this.cloneField(nameFieldId);
        var cloneFullNameTranscription = this.cloneField(transcriptionFieldId);
        div2.append(cloneId).append(cloneName).append(cloneFullNameTranscription).append(actionButton);
      <% end %>
      cloneId.select2(agentSlect2options);
      newField.append(div1).append(div2);
      $("#contributor_field > #contributors").append(newField);

      this.currentNumber += 1;
    },
    cloneField : function (fieldId) {
      return $('#' + fieldId).clone().attr({
          'id' : $('#' + fieldId).attr('id').replace(/_\d+_/, '_' + this.currentNumber + '_'),
          'name' : $('#' + fieldId).attr('name').replace(/\[\d+\]/, '[' + this.currentNumber + ']'),
      }).val('');
    },
    cloneFieldWithoutValue : function (fieldId) {
      return $('#' + fieldId).clone().attr({
          'id' : $('#' + fieldId).attr('id').replace(/_\d+_/, '_' + this.currentNumber + '_'),
          'name' : $('#' + fieldId).attr('name').replace(/\[\d+\]/, '[' + this.currentNumber + ']'),
      });
    },
    remove : function (div_num) {
      if($("div[id ^= 'RF']").length == 1) this.add();
      removeDivField('RF', div_num);
    }
  }

  /**
   * 出版者 select2
   * 
   */
  var ItemFieldPublisherSelect2 = {
    currentNumber : <%= raw (@publishers.size).to_json %>,
    actionTemplate : ''
      + '<%= button_to_function t('page.add_line'), "ItemFieldPublisherSelect2.add()" -%>'
      + '<%= button_to_function t('page.del_line'), "ItemFieldPublisherSelect2.remove(__count__)" -%>',
    add : function () {
      var newField = $("<div/>").attr("id", "PF" + this.currentNumber).attr("class", 'row2');
      
      var idFieldId = $("div[id ^= 'PF']:first").find("input[id $= 'id']").attr('id');
      var cloneId = this.cloneField(idFieldId);
      var actionButton = this.actionTemplate.replace(/__count__/mg, this.currentNumber);
      var div1 = $("<div/>");
      var div2 = $("<div/>");

      <% if SystemConfiguration.get("use_agent_type") %>
        var typeFieldId = $("div[id ^= 'PF']:first").find("select[id $= 'type_id']").attr('id');
        var clonePublisherType = this.cloneFieldWithoutValue(typeFieldId);
        div1.append(clonePublisherType);
        clonePublisherType.select2(agentTypeSlect2options);
      <% end %>
      
      <% if SystemConfiguration.get("add_only_exist_agent") %>
        div2.append(cloneId).append(actionButton);
      <% else %>
        var nameFieldId = $("div[id ^= 'PF']:first").find("input[id $= 'full_name']").attr('id');
        var transcriptionFieldId = $("div[id ^= 'PF']:first").find("input[id $= 'full_name_transcription']").attr('id');
        var cloneName = this.cloneField(nameFieldId);
        var cloneFullNameTranscription = this.cloneField(transcriptionFieldId);
        div2.append(cloneId).append(cloneName).append(cloneFullNameTranscription).append(actionButton);
      <% end %>
      cloneId.select2(agentSlect2options);
      newField.append(div1).append(div2);
      $("#publisher_field > #publishers").append(newField);

      this.currentNumber += 1;
    },
    cloneField : function (fieldId) {
      return $('#' + fieldId).clone().attr({
          'id' : $('#' + fieldId).attr('id').replace(/_\d+_/, '_' + this.currentNumber + '_'),
          'name' : $('#' + fieldId).attr('name').replace(/\[\d+\]/, '[' + this.currentNumber + ']'),
      }).val('');
    },
    cloneFieldWithoutValue : function (fieldId) {
      return $('#' + fieldId).clone().attr({
          'id' : $('#' + fieldId).attr('id').replace(/_\d+_/, '_' + this.currentNumber + '_'),
          'name' : $('#' + fieldId).attr('name').replace(/\[\d+\]/, '[' + this.currentNumber + ']'),
      });
    },
    remove : function (div_num) {
      if($("div[id ^= 'PF']").length == 1) this.add();
      removeDivField('PF', div_num);
    }
  }
</script>

<div class="row" id="creator_field">
  <div><strong><%= t('agent.creator') %></strong></div>
  <div id="creators">
    <% @creators.each_with_index do |creator, index| -%>
      <div id="CF<%= index %>" class="row2">
        <% if SystemConfiguration.get("use_agent_type") %>
          <%= select2_tag(
            "creators_#{index}_type_id", 
            "creators[][type_id]", 
            @create_types,
            creator[:type_id], 
            :width => 250, :select_attribute => :name, :display_attribute => :display_name 
          ) %><br />
        <% end %>
        <% if SystemConfiguration.get("add_only_exist_agent") %>
          <%= text_field_tag "creators[][id]", creator[:id], :id => "creators_#{index}_id", :class => "creator" %>
        <% else %>
          <%= text_field_tag "creators[][id]", creator[:id], :id => "creators_#{index}_id", :class => "creator" %>
          <%= hidden_field_tag "creators[][full_name]", creator[:full_name], :id => "creators_#{index}_full_name" %>
          <%= text_field_tag "creators[][full_name_transcription]", creator[:full_name_transcription],
              :id => "creators_#{index}_full_name_transcription", :class => 'medium_string', 
              :placeholder => t("activerecord.attributes.agent.full_name_transcription") 
          %>
        <% end %>
        <%= button_to_function t('page.add_line'), "ItemFieldCreatorSelect2.add()" -%>
        <%= button_to_function t('page.del_line'), "ItemFieldCreatorSelect2.remove(#{index})" -%>
      </div>
    <% end %>
  </div>
</div>

<div class="row" id="contributor_field">
  <div><strong><%= t('agent.contributor') %></strong></div>
  <div id="contributors">
    <% @contributors.each_with_index do |contributor, index| -%>
      <div id="RF<%= index %>" class="row2">
        <% if SystemConfiguration.get("use_agent_type") %>
          <%= select2_tag(
            "contributors_#{index}_type_id", 
            "contributors[][type_id]", 
            @realize_types,
            contributor[:type_id], 
            :width => 250, :select_attribute => :name, :display_attribute => :display_name 
          ) %><br />
        <% end %>
        <% if SystemConfiguration.get("add_only_exist_agent") %>
          <%= text_field_tag "contributors[][id]", contributor[:id], :id => "contributors_#{index}_id", :class => "contributor" %>
        <% else %>
            <%= text_field_tag "contributors[][id]", contributor[:id], :id => "contributors_#{index}_id", :class => "contributor" %>
            <%= hidden_field_tag "contributors[][full_name]", contributor[:full_name], :id => "contributors_#{index}_full_name" %>
            <%= text_field_tag "contributors[][full_name_transcription]", contributor[:full_name_transcription],
              :id => "contributors_#{index}_full_name_transcription", :class => 'medium_string',
              :placeholder => t("activerecord.attributes.agent.full_name_transcription") 
            %>
        <% end %>
        <%= button_to_function t('page.add_line'), "ItemFieldContributorSelect2.add()" -%>
        <%= button_to_function t('page.del_line'), "ItemFieldContributorSelect2.remove(#{index})" -%>
      </div>
    <% end %>
  </div>
</div>

<div class="row" id="publisher_field">
  <div><strong><%= t('agent.publisher') %></strong></div>
  <div id="publishers">
    <% @publishers.each_with_index do |publisher, index| -%>
      <div id="PF<%= index %>" class="row2">
        <% if SystemConfiguration.get("use_agent_type") %>
          <%= select2_tag(
            "publishers_#{index}_type_id", 
            "publishers[][type_id]", 
            @produce_types,
            publisher[:type_id], 
            :width => 250, :select_attribute => :name, :display_attribute => :display_name 
          ) %><br />
        <% end %>
        <% if SystemConfiguration.get("add_only_exist_agent") %>
          <%= text_field_tag "publishers[][id]", publisher[:id], :id => "publishers_#{index}_id", :class => "publisher" %>
        <% else %>
            <%= text_field_tag "publishers[][id]", publisher[:id], :id => "publishers_#{index}_id", :class => "publisher" %>
            <%= hidden_field_tag "publishers[][full_name]", publisher[:full_name], :id => "publishers_#{index}_full_name" %>
            <%= text_field_tag "publishers[][full_name_transcription]", publisher[:full_name_transcription],
              :id => "publishers_#{index}_full_name_transcription", :class => 'medium_string',
              :placeholder => t("activerecord.attributes.agent.full_name_transcription") 
            %>
        <% end %>
        <%= button_to_function t('page.add_line'), "ItemFieldPublisherSelect2.add()" -%>
        <%= button_to_function t('page.del_line'), "ItemFieldPublisherSelect2.remove(#{index})" -%>
      </div>
    <% end %> 
  </div>
</div>

{'fraction_non_alphanumeric': 0.2025489967335511, 'fraction_numerical': 0.007145356976201587, 'mean_word_length': 3.0986134353334926, 'pattern_counts': {'":': 0, '<': 247, '<?xml version=': 0, '>': 319, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6196671', 'n_tokens_mistral': 11497, 'n_tokens_neox': 11064, 'n_words': 1544}