# all files / candela/components/TrackerDash/ ResultTablePane.js

49.61% Statements 64/129
52% Branches 39/75
37.5% Functions 9/24
14.47% Lines 11/76
12 statements, 2 functions, 19 branches Ignored

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    1551×
    1×
    1×

    1×
    1×

    1×
    1×

    1×
    1×

    1×

     import _ from 'underscore';
    import $ from 'jquery';
    import * as d3 from 'd3';

    import ValueWidget from './ValueWidget';
    import { sanitizeSelector } from './utility.js';

    import tablePane from './templates/tablePane.jade';
    import VisComponent from '../../VisComponent';

    class ResultTablePane extends VisComponent {
      constructor (el, settings) {
        super(el);
        this.$el = $(this.el);

        this.results = settings.trendValuesByDataset;
        this.trends = settings.trends;
        this.trendMap = settings.trendMap;
        this.datasetMap = settings.datasetMap || {};
        this.trajectoryMap = settings.trajectoryMap || {};
        this.datasetLabelMap = settings.datasetLabelMap || {};
        this.producerLink = settings.producer_link || null;
        if (this.results === undefined) {
          return;
        }
        // Default sort order is alphabetical by dataset name.
        this.sortOrder = {
          dataset: true,
          order: 1
        };
      }

      render () {
        if (this.results === undefined) {
          return;
        }
        const resultsByDatasetIdThenTrend = {};
        _.each(this.results, _.bind(function (result) {
          result.dataset_id_selector = sanitizeSelector(result.dataset);
          this.datasetMap[result.dataset_id_selector] = this.datasetMap[result.dataset];
          this.trajectoryMap[result.dataset_id_selector] = this.trajectoryMap[result.dataset];
          this.datasetLabelMap[result.dataset_id_selector] = this.datasetLabelMap[result.dataset];
          if (!_.has(resultsByDatasetIdThenTrend, result.dataset_id_selector)) {
            resultsByDatasetIdThenTrend[result.dataset_id_selector] = {};
          }
          if (Array.isArray(result.current)) {
            resultsByDatasetIdThenTrend[result.dataset_id_selector][result.trend] = d3.median(result.current);
          } else {
            resultsByDatasetIdThenTrend[result.dataset_id_selector][result.trend] = result.current;
          }
        }, this));

        this.results.sort((_.bind(function () {
          if (!this.sortOrder.dataset) {
            return _.bind(function (a, b) {
              // Sort all datasets by the selected trend column and direction
              const trendA = resultsByDatasetIdThenTrend[a.dataset_id_selector][this.sortOrder.trend];
              const trendB = resultsByDatasetIdThenTrend[b.dataset_id_selector][this.sortOrder.trend];
              if (trendB === undefined) {
                return -1;
              }
              if (trendA === undefined) {
                return 1;
              }
              return this.sortOrder.order * (trendA - trendB);
            }, this);
          } else {
            return _.bind(function (a, b) {
              if (this.sortOrder.order > 0) {
                return a.dataset.localeCompare(b.dataset);
              } else {
                return b.dataset.localeCompare(a.dataset);
              }
            }, this);
          }
        }, this))());

        // The order of the datasets determines the order the rows are printed.
        const resultsByDatasetId = _.groupBy(this.results, function (result) {
          return result.dataset_id_selector;
        });

        this.$el.html(tablePane({
          resultsByDatasetId: resultsByDatasetId,
          trends: this.trends,
          datasetMap: this.datasetMap,
          trajectoryMap: this.trajectoryMap,
          datasetLabelMap: this.datasetLabelMap,
          producerLink: this.producerLink,
          sortOrder: this.sortOrder
        })).promise().done(_.bind(function () {
          _.each(this.results, function (result) {
            const trend = this.trendMap[result.trend];
            const resultDivSelector = `#${result.dataset_id_selector}-${trend.id_selector}`;
            // Render value widgets.
            const el = $(`${resultDivSelector}-valueWidget-svg`).get(0);
            let valueWidget = new ValueWidget(el, {
              result: result,
              trend: trend
            });
            valueWidget.render();

            // Activate callback for valueWidget if specified.
            if (typeof result.callback === 'function') {
              $(resultDivSelector)
                .css('cursor', 'pointer')
                .click(result.callback);
            } else if (result.link) {
              $(resultDivSelector)
                .css('cursor', 'pointer')
                .click(function () {
                  if (result.link) {
                    window.location = result.link;
                  }
                });
            }
          }, this);

          _.each(this.datasetMap, function (value, key) {
            if (typeof value === 'function') {
              $('#' + key + '-link').click(value);
            }
          });

          _.each(this.trajectoryMap, function (value, key) {
            if (typeof value === 'function') {
              $('#' + key + '-trajectory-link').click(value);
            }
          });

          _.each(this.trends, function (trend) {
            // Order by the trend column clicked.
            $('#' + trend.id_selector + '-trend-col-header').click(_.bind(function () {
              this.sortOrder = {
                trend: trend.name,
                order: this.sortOrder.order * -1
              };
              this.render();
            }, this));
          }, this);

          $('#dataset-col-header').click(_.bind(function () {
            this.sortOrder = {
              dataset: true,
              order: this.sortOrder.order * -1
            };
            this.render();
          }, this));
        }, this));
      }
    }

    export default ResultTablePane;