MiraitSystems/enju_trunk

View on GitHub
app/views/manifestations/_form_subject_field.html.erb

Summary

Maintainability
Test Coverage
<script>
  var ItemFieldSubjectSelect2 = {
    // Subject select2 options
    subjectSelect2options: {
      multiple: false,
      minimumInputLength: 1,
      width: "200px",
      allowClear: true,
      placeholder: <%= raw t("activerecord.attributes.subject.term").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: "/subjects/search_name.json",
        dataType: 'json',
        data: function (term, page) {
            var currentNumber = $(this).attr('id').match(/\d+/);
            var subjectTypeId = $('#subjects_' + currentNumber + '_subject_type_id').val();
            return {
              subject_type_id: subjectTypeId,
              search_phrase: term,
            };
        },
        results: function (data, page) {
            return {results: data};
        },
      },
      initSelection: function(element, callback) {
        var id = $(element).val();
        if (id != "") {
          $.ajax("/subjects/search_name.json?subject_id=" + id, {
              dataType: "json"
         }).done(function(data) { callback(data); });
        }
      },
      <% unless SystemConfiguration.get('add_only_exist_subject') %>
        createSearchChoice: function(term, data) {
          if ($(data).filter(function() {
            return this.text.localeCompare(term)===0; }).length===0) {
            return {id:term, text:term};
          }
        },
      <% end %>
    },
    // SubjectType select2 options
    subjectTypeSelect2options: {
      width: "200px",
      matcher: function(term, text, opt) {
        return text.toUpperCase().indexOf(term.toUpperCase())==0
            || opt.attr("alt").toUpperCase().indexOf(term.toUpperCase())==0;
      },
    },
    currentNumber : <%= raw (@subjects.size).to_json %>,
    actionTemplate : ''
      + '<%= button_to_function t('page.add_line'), "ItemFieldSubjectSelect2.add()" %>'
      + '<%= button_to_function t('page.del_line'), "ItemFieldSubjectSelect2.remove(__count__)" %>',
    add : function () {
      var newField = $("<div/>").attr("id", "SUBJECT" + this.currentNumber).attr("class", 'row2');
      var termFieldId = $("div[id ^= 'SUBJECT']:first").find("input[id $= 'subject_id']").attr('id');
      var cloneTerm = this.cloneField(termFieldId);
      var actionButton = this.actionTemplate.replace(/__count__/mg, this.currentNumber);
      <% if SystemConfiguration.get('attributes.subject.subject_type_id') %>
        var typeFieldId = $("div[id ^= 'SUBJECT']:first").find("select[id $= 'subject_type_id']").attr('id');
      <% else %>
        var typeFieldId = $("div[id ^= 'SUBJECT']:first").find("input[id $= 'subject_type_id']").attr('id');
      <% end %>
      var cloneType = this.cloneField(typeFieldId);
      <% if SystemConfiguration.get('attributes.subject.term_transcription') %>
        var termTranscriptionFieldId = $("div[id ^= 'SUBJECT']:first").find("input[id $= 'term_transcription']").attr('id');
        var cloneTermTranscription = this.cloneField(termTranscriptionFieldId);
      <% end %>

      newField.append(cloneType)
      <% if SystemConfiguration.get('attributes.subject.subject_type_id') %>
        newField..append("<br />")
      <% end %>
      newField.append(cloneTerm);
      <% if SystemConfiguration.get('attributes.subject.term_transcription') %>
        newField.append(cloneTermTranscription);
      <% end %>
      newField.append(actionButton);
      
      // select2の設定
      <% if SystemConfiguration.get('attributes.subject.subject_type_id') %>
        cloneType.select2(this.subjectTypeSelect2options);
      <% end %>
      cloneTerm.val('');
      cloneTerm.select2(ItemFieldSubjectSelect2.subjectSelect2options);
      <% if SystemConfiguration.get('attributes.subject.term_transcription') %>
        cloneTermTranscription.val('');
      <% end %>

      $("#subjects").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 + ']'),
      });
    },
    remove : function (trNum) {
      if($("div[id ^= 'SUBJECT']").length == 1) this.add();
      removeDivField('SUBJECT', trNum)
    }
  }

  $(document).ready(function() {
    <% if SystemConfiguration.get('attributes.subject.subject_type_id') %>
      $("select[id $= 'subject_type_id']").select2(ItemFieldSubjectSelect2.subjectTypeSelect2options);
    <% end %>
    $("input[id $= 'subject_id']").each(function() {
      $(this).select2(ItemFieldSubjectSelect2.subjectSelect2options).select2('val', [$(this).attr('value')]);
    });
    //$("input[id $= 'subject_id']").select2(ItemFieldSubjectSelect2.subjectSelect2options).select2('val', [this.val]);
  });
  $(document).on("select2-selecting select2-removed", ".subject", function(e){
    var targetId = e.target.id;
    var targetNumber = targetId.match(/_(\d+)_/);
    if (e.type == 'select2-removed'){
      <% if SystemConfiguration.get('attributes.subject.term_transcription') %>
        $("#subjects_" + targetNumber[1] + "_term_transcription").val('');
      <% end %>
    }
    else if(e.val == parseInt(e.val)){
      $("#subjects_" + targetNumber[1] + "_id").val(e.object.id);
      $("#subjects_" + targetNumber[1] + "_term").val(e.object.text);
      <% if SystemConfiguration.get('attributes.subject.term_transcription') %>
        $("#subjects_" + targetNumber[1] + "_term_transcription").val(e.object.term_transcription);
      <% end %>
    }
    else{
      $("#subjects_" + targetNumber[1] + "_id").val("");
      $("#subjects_" + targetNumber[1] + "_term").val(e.object.id);
      <% if SystemConfiguration.get('attributes.subject.term_transcription') %>
        $("#subjects_" + targetNumber[1] + "_term_transcription").val(e.object.term_transcription);
      <% end %>
    }
  });
</script>

<div class="row">
  <div><%= f.label t('activerecord.attributes.manifestation.subject') %></div>
  <div id="subjects">
    <% @subjects.each_with_index do |subject, index| -%>
      <div id="SUBJECT<%= index %>" class="row2">
        <% if SystemConfiguration.get('attributes.subject.subject_type_id') %>
          <%= select2_tag("subjects_#{index}_subject_type_id",
                          "subjects[][subject_type_id]",
                          @subject_types,
                          subject[:subject_type_id],
                          :width => 200, :select_attribute => :id, :display_attribute => :display_name) %>
          <br />
        <% else %>
           <%= hidden_field_tag "subjects_#{index}_subject_type_id", subject[:subject_type_id] || 1 %>
        <% end %>
        <%= text_field_tag "subjects[][subject_id]", subject[:id], :id => "subjects_#{index}_subject_id", :class => "subject" %>
        <% if SystemConfiguration.get('attributes.subject.term_transcription') %>
          <%= text_field_tag "subjects[][term_transcription]", 
            subject[:term_transcription], 
            :id => "subjects_#{index}_term_transcription", 
            :value => "aaa",
            :placeholder => t("activerecord.attributes.subject.term_transcription"),
            :style => 'width: 200px;' %>
        <% end %>
        <%= button_to_function t('page.add_line'), "ItemFieldSubjectSelect2.add()" -%>
        <%= button_to_function t('page.del_line'), "ItemFieldSubjectSelect2.remove(#{index})" -%>
      </div>
    <% end %>
  </div>
</div>