<commit_before><%
  resource_text ||= text_for_resource(resource)

  assay_assets = resource.assay_assets.where(asset_type: 'Sample').includes(:asset)
  associated_samples_json = assay_assets.map do |aa|
    { title: aa.asset.title, id: aa.asset_id,
      direction: { value: aa.direction, text: direction_name(aa.direction) } }
  end.to_json
-%>

<%= folding_panel("Samples", assay_assets.blank?, :body_options => {:id => 'associate_sample_fold_content'},
                  :help_text => "Here you associate Samples in SEEK to this #{resource_text}.") do %>

    <p>The following Samples are involved in this <%= resource_text -%>:</p>

    <%= associations_list_group('sample_to_list', 'direction.value', associated_samples_json) do %>
        <div class="row">
          <div class="col-md-4">
            <h3>Incoming</h3>
            <%= associations_list('incoming-sample-list', 'associations/sample', '',
                                  empty_text: 'No incoming samples',
                                  'data-multilist-group-value' => AssayAsset::Direction::INCOMING) %>
          </div>
          <div class="col-md-4">
            <h3>Outgoing</h3>
            <%= associations_list('outgoing-sample-list', 'associations/sample', '',
                                  empty_text: 'No outgoing samples',
                                  'data-multilist-group-value' => AssayAsset::Direction::OUTGOING) %>
          </div>
          <div class="col-md-4">
            <h3>No direction</h3>
            <%= associations_list('other-sample-list', 'associations/sample', '',
                                  empty_text: 'No other samples',
                                  'data-multilist-group-value' => AssayAsset::Direction::NODIRECTION) %>
          </div>
        </div>
    <% end %>

    <hr/>

    <%= association_selector('sample_to_list', 'Add individual samples', 'Select samples') do %>
        <label>Select samples</label>
        <%= filterable_association_select(filter_samples_path(:assay_id => resource.id), :multiple => true) do %>
            <%= render :partial => 'samples/association_preview', :collection => Sample.limit(20),
                       :locals => { :existing => resource.samples }
            %>
        <% end %>

        <hr/>

        <div class="form-group">
          <label>Select sample direction</label>
          <%= select_tag(:direction, direction_options, :class => 'form-control', 'data-role' => 'seek-association-common-field') %>
        </div>
    <% end %>

    <%= button_link_to('Add samples from data file', 'add', '#', 'data-toggle' => 'modal', 'data-target' => "#AddSamplesFromDataFileModal") %>
    <%= modal(id: 'AddSamplesFromDataFileModal', size: 'xl', 'data-role' => 'seek-wizard') do %>
        <%= modal_header('Add samples from data file') %>
        <%= modal_body do %>
            <div data-role="seek-wizard-nav"></div>
            <div data-role="seek-wizard-step" data-step-name="Select data file" id="samples-select-data-file">
              <label>Select data file</label>
              <%= filterable_association_select(filter_data_files_path) do %>
                  <%= render :partial => 'data_files/association_preview',
                             :collection => DataFile.with_extracted_samples.all_authorized_for('view').select { |d| d.extracted_samples.any? }.first(20),
                             :locals => { :existing => resource.samples }
                  %>
              <% end %>
            </div>
            <div data-role="seek-wizard-step" data-step-name="Select samples">
              <label>Select samples</label>
              <div id="data-file-samples-table">
                <%= image('spinner') %>
              </div>
            </div>
            <div data-role="seek-wizard-step" data-step-name="Select sample direction">
              <div class="form-group">
                <label>Select sample direction</label>
                <%= select_tag(:direction, direction_options, :class => 'form-control', 'data-role' => 'seek-association-common-field') %>
              </div>
            </div>
        <% end %>
        <%= modal_footer do %>
            <a data-role="seek-wizard-next-btn" href="#" class="pull-right btn btn-default">Next</a>
            <a data-role="seek-wizard-prev-btn" href="#" class="pull-left btn btn-default">Previous</a>
            <%= confirm_association_button('Add samples', id: 'add-batch-sample-btn', style: 'display: none',
                                           class: 'btn btn-primary pull-right', 'data-dismiss' => 'modal') %>
        <% end %>
    <% end %>
    <%= button_link_to('Clear all samples', 'destroy', '#', id: 'clear-all-samples-btn') %>
<% end %>

<script>
  $j(document).ready(function () {
    var samplesList = $j('#sample_to_list').data('associationList');
    var batchSamplesForm = new Associations.Form(samplesList, $j('#AddSamplesFromDataFileModal'));
    $j('#AddSamplesFromDataFileModal :input[data-role="seek-association-common-field"]').each(function () {
      $j(this).data('attributeName', this.name);
      this.name = '';
      batchSamplesForm.commonFieldElements.push($j(this));
    });

    var samplesTable;

    function initSamplesTable() {
      samplesTable = Samples.initTable($j('#data-file-samples-table'), true);
      // When at least one sample is selected, unlock step 3
      var handleRowSelect = function ( e, dt, type, indexes ) {
        var selectedRows = samplesTable.rows({ selected: true });

        batchSamplesForm.selectedItems = [];
        selectedRows.every(function () {
          batchSamplesForm.selectedItems.push({
            id: this.data()[0],
            title: this.data()[1]
          });
        });

        if(selectedRows.count() > 0) {
          wizard.step(3).unlock();
        } else {
          wizard.step(3).lock();
        }
      };
      samplesTable.on('select', handleRowSelect);
      samplesTable.on('deselect', handleRowSelect);
    }

    initSamplesTable();

    $j('#add-batch-sample-btn').click(function () {
      batchSamplesForm.submit();
    });
    $j('#clear-all-samples-btn').click(function () {
      if(confirm("Are you sure you wish to remove all samples from this assay?")) {
        samplesList.removeAll();
      }
      return false;
    });

    // Wizard steps
    var wizard = new Wizards.Wizard($j('#AddSamplesFromDataFileModal'));

    // On final step, show the confirm button
    wizard.step(3).onShow = function () {
      $j('#add-batch-sample-btn').show();
    };
    wizard.step(3).onHide = function () {
      $j('#add-batch-sample-btn').hide();
    };

    // Reset the wizard when the modal is closed
    $j('#AddSamplesFromDataFileModal').on('hide.bs.modal', function () {
      samplesTable.rows().deselect();
      wizard.reset();
    });

    $j('#samples-select-data-file .selectable[data-role="seek-association-candidate"]').click(function () {
      var candidate = $j(this);
      candidate.spinner('add');
      $j.ajax('/data_files/'+$j(this).data('associationId')+'/samples_table', {
            success: function (data) {
              $j('#data-file-samples-table').html(data);
              initSamplesTable();
              wizard.step(2).unlock();
              wizard.gotoStep(2);
              candidate.spinner('remove');
            }
          }
      );

      return false;
    })
  });

</script>
<commit_msg>Fixed samples association via table not working<commit_after><%
  resource_text ||= text_for_resource(resource)

  assay_assets = resource.assay_assets.where(asset_type: 'Sample').includes(:asset)
  associated_samples_json = assay_assets.map do |aa|
    { title: aa.asset.title, id: aa.asset_id,
      direction: { value: aa.direction, text: direction_name(aa.direction) } }
  end.to_json
-%>

<%= folding_panel("Samples", assay_assets.blank?, :body_options => {:id => 'associate_sample_fold_content'},
                  :help_text => "Here you associate Samples in SEEK to this #{resource_text}.") do %>

    <p>The following Samples are involved in this <%= resource_text -%>:</p>

    <%= associations_list_group('sample_to_list', 'direction.value', associated_samples_json) do %>
        <div class="row">
          <div class="col-md-4">
            <h3>Incoming</h3>
            <%= associations_list('incoming-sample-list', 'associations/sample', '',
                                  empty_text: 'No incoming samples',
                                  'data-multilist-group-value' => AssayAsset::Direction::INCOMING) %>
          </div>
          <div class="col-md-4">
            <h3>Outgoing</h3>
            <%= associations_list('outgoing-sample-list', 'associations/sample', '',
                                  empty_text: 'No outgoing samples',
                                  'data-multilist-group-value' => AssayAsset::Direction::OUTGOING) %>
          </div>
          <div class="col-md-4">
            <h3>No direction</h3>
            <%= associations_list('other-sample-list', 'associations/sample', '',
                                  empty_text: 'No other samples',
                                  'data-multilist-group-value' => AssayAsset::Direction::NODIRECTION) %>
          </div>
        </div>
    <% end %>

    <hr/>

    <%= association_selector('sample_to_list', 'Add individual samples', 'Select samples') do %>
        <label>Select samples</label>
        <%= filterable_association_select(filter_samples_path(:assay_id => resource.id), :multiple => true) do %>
            <%= render :partial => 'samples/association_preview', :collection => Sample.limit(20),
                       :locals => { :existing => resource.samples }
            %>
        <% end %>

        <hr/>

        <div class="form-group">
          <label>Select sample direction</label>
          <%= select_tag(:direction, direction_options, :class => 'form-control', 'data-role' => 'seek-association-common-field') %>
        </div>
    <% end %>

    <%= button_link_to('Add samples from data file', 'add', '#', 'data-toggle' => 'modal', 'data-target' => "#AddSamplesFromDataFileModal") %>
    <%= modal(id: 'AddSamplesFromDataFileModal', size: 'xl', 'data-role' => 'seek-wizard') do %>
        <%= modal_header('Add samples from data file') %>
        <%= modal_body do %>
            <div data-role="seek-wizard-nav"></div>
            <div data-role="seek-wizard-step" data-step-name="Select data file" id="samples-select-data-file">
              <label>Select data file</label>
              <%= filterable_association_select(filter_data_files_path) do %>
                  <%= render :partial => 'data_files/association_preview',
                             :collection => DataFile.with_extracted_samples.all_authorized_for('view').select { |d| d.extracted_samples.any? }.first(20),
                             :locals => { :existing => resource.samples }
                  %>
              <% end %>
            </div>
            <div data-role="seek-wizard-step" data-step-name="Select samples">
              <label>Select samples</label>
              <div id="data-file-samples-table">
                <%= image('spinner') %>
              </div>
            </div>
            <div data-role="seek-wizard-step" data-step-name="Select sample direction">
              <div class="form-group">
                <label>Select sample direction</label>
                <%= select_tag(:direction, direction_options, :class => 'form-control', 'data-role' => 'seek-association-common-field') %>
              </div>
            </div>
        <% end %>
        <%= modal_footer do %>
            <a data-role="seek-wizard-next-btn" href="#" class="pull-right btn btn-default">Next</a>
            <a data-role="seek-wizard-prev-btn" href="#" class="pull-left btn btn-default">Previous</a>
            <%= confirm_association_button('Add samples', id: 'add-batch-sample-btn', style: 'display: none',
                                           class: 'btn btn-primary pull-right', 'data-dismiss' => 'modal') %>
        <% end %>
    <% end %>
    <%= button_link_to('Clear all samples', 'destroy', '#', id: 'clear-all-samples-btn') %>
<% end %>

<script>
  $j(document).ready(function () {
    var samplesList = $j('#sample_to_list').data('associationList');
    var batchSamplesForm = new Associations.Form(samplesList, $j('#AddSamplesFromDataFileModal'));
    $j('#AddSamplesFromDataFileModal :input[data-role="seek-association-common-field"]').each(function () {
      $j(this).data('attributeName', this.name);
      this.name = '';
      batchSamplesForm.commonFieldElements.push($j(this));
    });

    var samplesTable;

    function initSamplesTable() {
      samplesTable = Samples.initTable($j('#data-file-samples-table'), true);
      // When at least one sample is selected, unlock step 3
      var handleRowSelect = function ( e, dt, type, indexes ) {
        var selectedRowData = samplesTable.rows({ selected: true }).data();

        batchSamplesForm.selectedItems = [];
        for(var i = 0; i < selectedRowData.length; i++) {
          batchSamplesForm.selectedItems.push({
            id: selectedRowData[i].id,
            title: selectedRowData[i].title
          });
        }

        if(selectedRowData.length > 0) {
          wizard.step(3).unlock();
        } else {
          wizard.step(3).lock();
        }
      };
      samplesTable.on('select', handleRowSelect);
      samplesTable.on('deselect', handleRowSelect);
    }

    initSamplesTable();

    $j('#add-batch-sample-btn').click(function () {
      batchSamplesForm.submit();
    });
    $j('#clear-all-samples-btn').click(function () {
      if(confirm("Are you sure you wish to remove all samples from this assay?")) {
        samplesList.removeAll();
      }
      return false;
    });

    // Wizard steps
    var wizard = new Wizards.Wizard($j('#AddSamplesFromDataFileModal'));

    // On final step, show the confirm button
    wizard.step(3).onShow = function () {
      $j('#add-batch-sample-btn').show();
    };
    wizard.step(3).onHide = function () {
      $j('#add-batch-sample-btn').hide();
    };

    // Reset the wizard when the modal is closed
    $j('#AddSamplesFromDataFileModal').on('hide.bs.modal', function () {
      samplesTable.rows().deselect();
      wizard.reset();
    });

    $j('#samples-select-data-file .selectable[data-role="seek-association-candidate"]').click(function () {
      var candidate = $j(this);
      candidate.spinner('add');
      $j.ajax('/data_files/'+$j(this).data('associationId')+'/samples_table', {
            success: function (data) {
              $j('#data-file-samples-table').html(data);
              initSamplesTable();
              wizard.step(2).unlock();
              wizard.gotoStep(2);
              candidate.spinner('remove');
            }
          }
      );

      return false;
    })
  });

</script>

{'fraction_non_alphanumeric': 0.16300268096514745, 'fraction_numerical': 0.0030160857908847183, 'mean_word_length': 2.306959219858156, 'pattern_counts': {'":': 0, '<': 160, '<?xml version=': 0, '>': 209, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5385680', 'n_tokens_mistral': 4502, 'n_tokens_neox': 4236, 'n_words': 758}