aboutsummaryrefslogtreecommitdiff
// Generated by CoffeeScript 1.8.0
var add_trait_data, assemble_into_json, back_to_collections, collection_click, collection_list, color_by_trait, create_trait_data_csv, get_this_trait_vals, get_trait_data, process_traits, selected_traits, submit_click, this_trait_data, trait_click,
  __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };

this_trait_data = null;

selected_traits = {};

$('#collections_list').attr("style", "width: 100%;");
$('#trait_table').dataTable( {
    "drawCallback": function( settings ) {
         $('#trait_table tr').click(function(event) {
             if (event.target.type !== 'checkbox') {
                 $(':checkbox', this).trigger('click');
             }
         });
    },
    "columns": [
        { "type": "natural", "width": "3%" },
        { "type": "natural", "width": "8%" },
        { "type": "natural", "width": "20%" },
        { "type": "natural", "width": "25%" },
        { "type": "natural", "width": "25%" },
        { "type": "natural", "width": "15%" }
    ],
    "columnDefs": [ {
        "targets": 0,
        "orderable": false
    } ],
    "order": [[1, "asc" ]],
    "sDom": "RZtr",
    "iDisplayLength": -1,
    "autoWidth": true,
    "bDeferRender": true,
    "bSortClasses": false,
    "paging": false,
    "orderClasses": true
} );

if ( ! $.fn.DataTable.isDataTable( '#collection_table' ) ) {
  $('#collection_table').dataTable( {
      "createdRow": function ( row, data, index ) {
          if ($('td', row).eq(2).text().length > 40) {
              $('td', row).eq(2).text($('td', row).eq(2).text().substring(0, 40));
              $('td', row).eq(2).text($('td', row).eq(2).text() + '...')
          }
          if ($('td', row).eq(4).text().length > 50) {
              $('td', row).eq(4).text($('td', row).eq(4).text().substring(0, 50));
              $('td', row).eq(4).text($('td', row).eq(4).text() + '...')
          }
      },
      "columnDefs": [ {
          "targets": 0,
          "orderable": false
      } ],
      "order": [[1, "asc" ]],
      "sDom": "ZRtr",
      "iDisplayLength": -1,
      "autoWidth": true,
      "bSortClasses": false,
      "paging": false,
      "orderClasses": true
  } );
}

collection_click = function() {
  var this_collection_url;
  this_collection_url = $(this).find('.collection_name').prop("href");
  this_collection_url += "&json";
  collection_list = $("#collections_holder").html();
  return $.ajax({
    dataType: "json",
    url: this_collection_url,
    success: process_traits
  });
};

submit_click = function() {
  var covariates_string = "";
  var covariates_as_set = new Set();
  $(".selected-covariates:first option").each(function() {
    if ($(this).val() != ""){
      covariates_as_set.add($(this).val() + "," + $(this).text());
    }
  });
  $('#collections_holder').find('input[type=checkbox]:checked').each(function() {
    var this_dataset, this_trait;
    this_trait = $(this).parents('tr').find('.trait').text();
    this_trait_display = $(this).parents('tr').find('.trait').data("display_name");
    this_description = $(this).parents('tr').find('.description').text();
    this_dataset = $(this).parents('tr').find('.dataset').data("dataset");
    this_covariate_display_string = this_trait_display
    if (this_covariate_display_string.length > 50) {
      this_covariate_display_string = this_covariate_display_string.substring(0, 45) + "..."
    }
    covariates_as_set.add(this_trait + ":" + this_dataset + "," + this_covariate_display_string)
  });

  covariates_as_list = Array.from(covariates_as_set)

  // Removed the starting "No covariates selected" option before adding options for each covariate
  if (covariates_as_list.length > 0){
    $(".selected-covariates option[value='']").each(function() {
      $(this).remove();
    });
  }

  $(".selected-covariates option").each(function() {
    $(this).remove();
  });

  covariate_list_for_form = []
  $.each(covariates_as_list, function (index, value) {
    option_value = value.split(",")[0]
    option_text = value.split(",")[1]
    $(".selected-covariates").append($("<option/>", {
      value: option_value,
      text: option_text
    }))
    covariate_list_for_form.push(option_value)
  });

  $("input[name=covariates]").val(covariate_list_for_form.join(","));

  cofactor_count = $(".selected-covariates:first option").length;
  if (cofactor_count > 10){
    $(".selected-covariates").attr("size", 10);
  } else {
    $(".selected-covariates").attr("size", cofactor_count);
  }

  return $.colorbox.close();
};

trait_click = function() {
  var dataset, this_trait_url, trait;
  trait = $(this).parent().find('.trait').text();
  dataset = $(this).parent().find('.dataset').data("dataset");
  $("input[name=covariates]").val(trait + ":" + dataset)
  $(".selected-covariates").text(trait)
  return $.colorbox.close();
};

add_trait_data = function(trait_data, textStatus, jqXHR) {
  var trait_name, trait_sample_data;
  trait_name = trait_data[0];
  trait_sample_data = trait_data[1];
  selected_traits[trait_name] = trait_sample_data;
  return console.log("selected_traits:", selected_traits);
};

get_trait_data = function(trait_data, textStatus, jqXHR) {
  var sample, samples, this_trait_vals, trait_sample_data, vals, _i, _len;
  trait_sample_data = trait_data[1];
  samples = $('input[name=allsamples]').val().split(" ");
  vals = [];
  for (_i = 0, _len = samples.length; _i < _len; _i++) {
    sample = samples[_i];
    if (__indexOf.call(Object.keys(trait_sample_data), sample) >= 0) {
      vals.push(parseFloat(trait_sample_data[sample]));
    } else {
      vals.push(null);
    }
  }
  if ($('input[name=samples]').length < 1) {
    $('#hidden_inputs').append('<input type="hidden" name="samples" value="[' + samples.toString() + ']" />');
  }
  $('#hidden_inputs').append('<input type="hidden" name="vals" value="[' + vals.toString() + ']" />');
  this_trait_vals = get_this_trait_vals(samples);
  return color_by_trait(trait_sample_data);
};

get_this_trait_vals = function(samples) {
  var sample, this_trait_vals, this_val, this_vals_json, _i, _len;
  this_trait_vals = [];
  for (_i = 0, _len = samples.length; _i < _len; _i++) {
    sample = samples[_i];
    this_val = parseFloat($("input[name='value:" + sample + "']").val());
    if (!isNaN(this_val)) {
      this_trait_vals.push(this_val);
    } else {
      this_trait_vals.push(null);
    }
  }
  this_vals_json = '[' + this_trait_vals.toString() + ']';
  return this_trait_vals;
};

assemble_into_json = function(this_trait_vals) {
  var json_data, json_ids, num_traits, samples;
  num_traits = $('input[name=vals]').length;
  samples = $('input[name=samples]').val();
  json_ids = samples;
  json_data = '[' + this_trait_vals;
  $('input[name=vals]').each((function(_this) {
    return function(index, element) {
      return json_data += ',' + $(element).val();
    };
  })(this));
  json_data += ']';
  return [json_ids, json_data];
};

color_by_trait = function(trait_sample_data, textStatus, jqXHR) {
  console.log('in color_by_trait:', trait_sample_data);
  return root.bar_chart.color_by_trait(trait_sample_data);
};

process_traits = function(trait_data, textStatus, jqXHR) {
  var the_html, trait, _i, _len;
  the_html = "<button id='back_to_collections' class='btn btn-inverse btn-small'>";
  the_html += "<i class='icon-white icon-arrow-left'></i> Back </button>";
  the_html += "    <button id='submit_cofactors' class='btn btn-primary btn-small submit'> Submit </button>";
  the_html += "<table id='collection_table' style='padding-top: 10px;' class='table table-hover'>";
  the_html += "<thead><tr><th></th><th>Record</th><th>Data Set</th><th>Description</th></tr></thead>";
  the_html += "<tbody>";
  for (_i = 0, _len = trait_data.length; _i < _len; _i++) {
    trait = trait_data[_i];
    the_html += "<tr class='trait_line'>";
    the_html += "<td class='select_trait'><input type='checkbox' name='selectCheck' class='checkbox edit_sample_checkbox'></td>";
    if ("abbreviation" in trait) {
        the_html += "<td class='trait' data-display_name='" + trait.name + " - " + trait.abbreviation + "'>" + trait.name + "</td>";
    } else if ("symbol" in trait) {
      the_html += "<td class='trait' data-display_name='" + trait.name + " - " + trait.symbol + "'>" + trait.name + "</td>";
    } else {
      the_html += "<td class='trait' data-display_name='" + trait.name + "'>" + trait.name + "</td>";
    }
    the_html += "<td class='dataset' data-dataset='" + trait.dataset + "'>" + trait.dataset_name + "</td>";
    the_html += "<td class='description'>" + trait.description + "</td>";
  }
  the_html += "</tbody>";
  the_html += "</table>";
  the_html += "<script type='text/javascript' src='/static/new/javascript/get_covariates_from_collection.js'></script>"
  $("#collections_holder").html(the_html);
  return $('#collections_holder').colorbox.resize();
};

back_to_collections = function() {
  console.log("collection_list:", collection_list);
  $("#collections_holder").html(collection_list);
  $(document).on("click", ".collection_line", collection_click);
  return $('#collections_holder').colorbox.resize();
};

$(".collection_line").on("click", collection_click);
$(".submit").on("click", submit_click);
$(".trait").on("click", trait_click);
$("#back_to_collections").on("click", back_to_collections);