<commit_before>/** Component for showing the queried index data as a table */

modulejs.define( "data-table-view", [
  "lib/lodash",
  "lib/jquery",
  "constants",
  "preferences",
  "aspects",
  "values"
], function(
  _,
  $,
  Constants,
  Preferences,
  Aspects,
  Values
) {
  "use strict";

  var DataTableView = function() {
    this._tableViewCompact = false;
    this.bindEvents();
  };

  _.extend( DataTableView.prototype, {
    bindEvents: function() {
      $(".c-change-table-view").on( "click", _.bind( this.onChangeTableView, this ) );
    },

    showQueryResults: function( qr ) {
      var aspects = this.preferences().aspects();
      var data = this.formulateData( qr, aspects );
      var columns = this.formulateColumns( aspects );

      googleAnalyticsDataTablesWorkaround();

      $(".c-results").removeClass( "js-hidden" );

      // this should be necessary, but the destroy:true attrib on DataTables is not working
      if ($("#results-table.dataTable").length > 0) {
        $("#results-table").DataTable().destroy( false );
        $("#results-table").children().remove();
      }

      $("#results-table").DataTable( {
        data: data,
        columns: columns,
        searching: false
      } );
    },

    preferences: function() {
      return new Preferences();
    },

    formulateData: function( qr, aspects ) {
      if (this._tableViewCompact) {
        return this.formulateDataWide( qr, aspects );
      }
      else {
        return this.formulateDataNarrow( qr, aspects );
      }
    },

    formulateDataNarrow: function( qr, aspects ) {
      var rowSets = _.map( qr.results(), function( result ) {
        var values = result.valuesFor( aspects );
        var zValues = _.zip( aspects, values );

        return _.map( zValues, function( zv ) {
          return [
            result.period(),
            formatAspectNameLong( zv[0], " " ),
            zv[1] ? Values.formatValue( zv[0], zv[1] ) : null
          ];
        } );
      } );

      return _.flatten( rowSets );
    },

    formulateDataWide: function( qr, aspects ) {
      return _.map( qr.results(), function( result ) {
        var values = result.valuesFor( aspects );
        var formattedValues = _.map( _.zip( aspects, values ), function( pair ) {
          return Values.formatValue.apply( null, pair );
        } );

        return [result.period()].concat( formattedValues );
      } );
    },

    formulateColumns: function( aspects ) {
      return this._tableViewCompact ? this.formulateColumnsWide( aspects ) : this.formulateColumnsNarrow();
    },

    formulateColumnsNarrow: function() {
      return [
        {title: "Date"},
        {title: "Measure"},
        {title: "Value", type: "num-fmt", className: "text-right"}
      ];
    },

    formulateColumnsWide: function( aspects ) {
      var aspectColumns = _.map( aspects, function( aspect ) {
        return {title: formatAspectNameShort( aspect ), className: "text-right", type: "num-fmt"};
      } );

      return [{title: "Date"}].concat( aspectColumns );
    },

    // event handlers

    onChangeTableView: function() {
      this._tableViewCompact = !this._tableViewCompact;

      var buttonLabel = this._tableViewCompact ?
        "Return to narrow table view" :
        "Switch to compact table view";
      $(".c-change-table-view span").text( buttonLabel );

      $(".c-results-table-container").toggleClass( "c-results-table--compact", this._tableViewCompact );

      $("body").trigger( Constants.EVENT_PREFERENCES_CHANGE );
    }

  } );


  var formatAspectNameShort = function( aspectName, sep ) {
    sep = sep || "<br />";<commit_msg>Workaround for issue #15. The root problem here is that we have to remove the container node to work around a Jquery.datatables refresh problem. I've added a deferred call to scroll the node back into view, but this is not a very elegant solution.<commit_after>/** Component for showing the queried index data as a table */

modulejs.define( "data-table-view", [
  "lib/lodash",
  "lib/jquery",
  "constants",
  "preferences",
  "aspects",
  "values"
], function(
  _,
  $,
  Constants,
  Preferences,
  Aspects,
  Values
) {
  "use strict";

  var DataTableView = function() {
    this._tableViewCompact = false;
    this.bindEvents();
  };

  _.extend( DataTableView.prototype, {
    bindEvents: function() {
      $(".c-change-table-view").on( "click", _.bind( this.onChangeTableView, this ) );
    },

    showQueryResults: function( qr ) {
      var aspects = this.preferences().aspects();
      var data = this.formulateData( qr, aspects );
      var columns = this.formulateColumns( aspects );

      googleAnalyticsDataTablesWorkaround();

      $(".c-results").removeClass( "js-hidden" );

      // this should be necessary, but the destroy:true attrib on DataTables is not working
      if ($("#results-table.dataTable").length > 0) {
        $("#results-table").DataTable().destroy( false );
        $("#results-table").children().remove();
      }

      $("#results-table").DataTable( {
        data: data,
        columns: columns,
        searching: false
      } );

      if (this.afterTableViewCallback) {
        this.afterTableViewCallback();
        this.afterTableViewCallback = null;
      }
    },

    preferences: function() {
      return new Preferences();
    },

    formulateData: function( qr, aspects ) {
      if (this._tableViewCompact) {
        return this.formulateDataWide( qr, aspects );
      }
      else {
        return this.formulateDataNarrow( qr, aspects );
      }
    },

    formulateDataNarrow: function( qr, aspects ) {
      var rowSets = _.map( qr.results(), function( result ) {
        var values = result.valuesFor( aspects );
        var zValues = _.zip( aspects, values );

        return _.map( zValues, function( zv ) {
          return [
            result.period(),
            formatAspectNameLong( zv[0], " " ),
            zv[1] ? Values.formatValue( zv[0], zv[1] ) : null
          ];
        } );
      } );

      return _.flatten( rowSets );
    },

    formulateDataWide: function( qr, aspects ) {
      return _.map( qr.results(), function( result ) {
        var values = result.valuesFor( aspects );
        var formattedValues = _.map( _.zip( aspects, values ), function( pair ) {
          return Values.formatValue.apply( null, pair );
        } );

        return [result.period()].concat( formattedValues );
      } );
    },

    formulateColumns: function( aspects ) {
      return this._tableViewCompact ? this.formulateColumnsWide( aspects ) : this.formulateColumnsNarrow();
    },

    formulateColumnsNarrow: function() {
      return [
        {title: "Date"},
        {title: "Measure"},
        {title: "Value", type: "num-fmt", className: "text-right"}
      ];
    },

    formulateColumnsWide: function( aspects ) {
      var aspectColumns = _.map( aspects, function( aspect ) {
        return {title: formatAspectNameShort( aspect ), className: "text-right", type: "num-fmt"};
      } );

      return [{title: "Date"}].concat( aspectColumns );
    },

    // event handlers

    onChangeTableView: function() {
      this._tableViewCompact = !this._tableViewCompact;

      var buttonLabel = this._tableViewCompact ?
        "Return to narrow table view" :
        "Switch to compact table view";
      $(".c-change-table-view span").text( buttonLabel );

      $(".c-results-table-container").toggleClass( "c-results-table--compact", this._tableViewCompact );

      $("body").trigger( Constants.EVENT_PREFERENCES_CHANGE );
      this.afterTableViewCallback = function() {
        _.defer( function() {
          $(".c-results-table-container").get(0).scrollIntoView();
        } );
      };
    }

  } );


  var formatAspectNameShort = function( aspectName, sep ) {
    sep = sep || "<br />";
{'fraction_non_alphanumeric': 0.14258385410024232, 'fraction_numerical': 0.0016579517918632828, 'mean_word_length': 2.508724832214765, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '3004302', 'n_tokens_mistral': 2349, 'n_tokens_neox': 2286, 'n_words': 545}