# all files / candela/components/LineUp/ index.js

88.18% Statements 179/203
72.88% Branches 86/118
82.61% Functions 19/23
83.69% Lines 118/141
9 statements, 12 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
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    3111×
    1×
    1×
    1×

    1×
    1×
    1×
    1×

    1×

    1×

    1×
    1×

    1×
    1×

    1×
    1×

    1×
    1×

    1×
    1×
    1×

    3×
    3×

    3×

    3×
    3×

    3×
    3×
    3×

    1×
    1×
    1×
    1×

    1×
    1×
    1×
    3×
    1×
    2×
    2×

    2×
    1×

    1×
    1×

    1×
    1×
    1×
    1×
    3×
    1×
    2×

    2×
    2×
    1×

    1×

    1×
    1×
    1×
    1×
    1×
    1×
    1×
    1×
    1×

    1×
    1×
    1×
    1×

    1×

    1×
    1×
    1×
    1×

    1×
    1×
    1×
    1×
    1×
    1×
    1×
    1×
    4×
    1×

    1×

    1×
    1×

    1×

    1×
    1×
    1×
    1×

    1×
    1×

    1×

    1×

    1×

    1×
    3×
    3×
    3×

    3×
    3×
    1×

    3×

    3×
    4×

    3×

    3×

    3×
    2×

    1×

    1×
    1×

    1×
    3×

    1×
    1×

     import $ from 'jquery';
    import datalib from 'datalib';
    import d3 from 'd3';
    import VisComponent from '../../VisComponent';
    import { type } from 'datalib';
    import LineUpJS from 'LineUpJS/src/main.js';
    import 'LineUpJS/dist/style.css';
    import './index.styl';
    import 'font-awesome-webpack';

    export default class LineUp extends VisComponent {
      static get options () {
        return [
          {
            name: 'data',
            type: 'table',
            format: 'objectlist'
          },
          {
            name: 'fields',
            type: 'string_list',
            format: 'string_list',
            domain: {
              mode: 'field',
              from: 'data',
              fieldTypes: ['string', 'date', 'number', 'integer', 'boolean']
            }
          },
          {
            name: 'stacked',
            type: 'boolean',
            format: 'boolean',
            optional: true
          },
          {
            name: 'histograms',
            type: 'boolean',
            format: 'boolean',
            optional: true
          },
          {
            name: 'animation',
            type: 'boolean',
            format: 'boolean',
            optional: true
          }
        ];
      }

      constructor (el, options) {
        super(el);

        let width = options.width || 800;
        let height = options.height || 600;

        el.style.width = width + 'px';
        el.style.height = height + 'px';

        this.options = options;
        this.lineUpConfig = {
          interaction: {
            tooltips: false
          },
          renderingOptions: {
            animation: options.animation !== undefined ? options.animation : true,
            histograms: options.histograms !== undefined ? options.histograms : true,
            stacked: options.stacked !== undefined ? options.stacked : false
          },
          body: {
            mode: 'separate',
            rowPadding: 0
          },
          /* false to prevent removing columns and other actions */
          manipulative: true
        };
        this.lineupInstances = {};
        this.lineupColumns = {};
        this.lineupRankWidth = 50;  /* from the LineUpJS code */
      }

      /* Get the width of a column.  If the user has changed the width, scale based
      * on that activity.
      *
      * @param name: name of the lineup.  Used for user settings.
      * @param col: column specification.
      * @param fixed: minimum width for a column.
      */
      lineupGetColumnWidth (name, col, fixed) {
        let width = col.width || 0;
        let colname = col.column || col.type;
        /* Get the column scaling based on the user settings, if available */
        if (Ithis.lineupColumns && this.lineupColumns[name] && this.lineupColumns[name][colname]) {
          width = this.lineupColumns[name][colname] + fixed;
        }
        let colWidth = width < fixed ? 0 : (width - fixed);
        if (Icol === 'rank') {  /* defined in LineUp */
          colWidth = this.lineupRankWidth;
        }
        col.widthBasis = colWidth;
        col.widthFixed = fixed;
        return col.widthBasis;
      }

      /* Adjust the width of the columns in lineup to (a) use the available space,
      * and (b) use the weights selected by the user.
      *
      * @param elem: the element where the lineup is placed.
      * @param name: the name used for this lineup.  Used for tracking user widths.
      * @param spec: the specification for the lineup.  Modified.
      * @param fixed: fixed width used in each column.
      * @returns: relative scale of lineup to available space.
      */
      createLineupAdjustWidth (elem, name, spec, fixed) {
        let rankWidth = 0;
        let total = 0;
        let count = 0;
        let c1, c2;
        /* The final width value of 30 is to leave room for a scroll bar. */
        let width = $(elem)[0].getBoundingClientRect().width - fixed * 2 - 30;
        let col = spec.dataspec.layout.primary;
        for (c1 = 0; c1 < col.length; c1 += 1) {
          if (col[c1].children) {
            for (c2 = 0; c2 < col[c1].children.length; c2 += 1) {
              count += 1;
              total += this.lineupGetColumnWidth(name, col[c1].children[c2], fixed);
            }
          } else {
            if (col[c1].type === 'rank') { /* LineUp wants this to be 50 */
              rankWidth = this.lineupRankWidth + fixed;
              continue;
            }
            count += 1;
            total += this.lineupGetColumnWidth(name, col[c1], fixed);
          }
        }
        let avail = width - count * fixed - rankWidth;
        avail -= count + (rankWidth ? 1 : 0); // I'm not sure why this is necessary
        let scale = avail / total;
        for (c1 = 0; c1 < col.length; c1 += 1) {
          if (col[c1].children) {
            for (c2 = 0; c2 < col[c1].children.length; c2 += 1) {
              col[c1].children[c2].width = fixed + col[c1].children[c2].widthBasis * scale;
            }
          } else {
            col[c1].width = fixed + col[c1].widthBasis * scale;
            if (col[c1].type === 'rank') { /* LineUp wants this to be fixed */
              col[c1].width = this.lineupRankWidth + fixed;
            }
          }
        }
        return scale;
      }

      /* Create or recreate a lineup control.
      *
      * @param elem: selector to the parent div wrapper for the control.
      * @param name: name of the control.
      * @param desc: column description.
      * @param dataset: dataset to load.
      * @param lineupObj: old lineup object to replace.
      * @param sort: if specified, sort by this column.
      * @param selectCallback: if present, bind 'selected' to this function.
      * @returns: a lineup control object.
      */
      createLineup (elem, name, desc, dataset, lineupObj, sort, selectCallback) {
        let spec = {};
        spec.name = name;
        spec.dataspec = desc;
        delete spec.dataspec.file;
        delete spec.dataspec.separator;
        spec.dataspec.data = dataset;
        spec.storage = LineUpJS.createLocalStorage(dataset, LineUpJS.deriveColors(desc.columns));
        let config = ((lineupObj ? lineupObj.config : $.extend({}, this.lineUpConfig)) || {});
        if (I!config.renderingOptions) {
          config.renderingOptions = {};
        }
        let oldAnimation = config.renderingOptions.animation;
        config.renderingOptions.animation = false;
        let columnFixed = 5;
        let scale = this.createLineupAdjustWidth(elem, name, spec, columnFixed);
        /* Always recreate the control */
        $(elem).empty();
        /* Lineup takes a d3 element */
        lineupObj = LineUpJS.create(spec.storage, d3.select(elem), this.lineUpConfig);
        lineupObj.restore(desc);
        config = lineupObj.config;
        lineupObj.header.dragHandler.on('dragend.lineupWidget', (evt) => {
          this.lineupDragColumnEnd(name, evt);
        });
        lineupObj['column-scale'] = scale;
        lineupObj['column-fixed'] = columnFixed;
        lineupObj['lineup-key'] = name;
        $(elem).attr('lineup-key', name);
        if (Esort) {
          let sortColumn;
          $.each(lineupObj.data.getRankings(), function (ridx, ranking) {
            $.each(ranking.flatColumns, function (cidx, column) {
              if (column.label === sort) {
                sortColumn = column.id;
              }
            });
          });
          lineupObj.sortBy(sortColumn !== undefined ? sortColumn : sort);
        }
        lineupObj.changeRenderingOption('animation', oldAnimation);
        let fixTooltips = function () {
          for (var i = 0; i < desc.columns.length; i += 1) {
            if (desc.columns[i].description) {
              let label = (desc.columns[i].label || desc.columns[i].column);
              $('title', $(elem + ' .lu-header text.headerLabel:contains("' + label + '")').parent()).text(label + ': ' + desc.columns[i].description);
            }
          }
        };
        if (IselectCallback) {
          lineupObj.on('selectionChanged.lineupWidget', null);
          lineupObj.on('selectionChanged.lineupWidget', function (row) {
            selectCallback(dataset[row]);
          });
        }
        /* Try twice to work around some issues */
        window.setTimeout(fixTooltips, 1);
        window.setTimeout(fixTooltips, 1000);
        this.lineupInstances[name] = lineupObj;
        return lineupObj;
      }

      /* After a column is resized in lineup, record the size it became relative to
       * the scaling we are using.
       *
       * @param name: name of the lineup record we have adjusted.
       */
      lineupDragColumnEnd (name) {
        let c1, c2;
        if (!this.lineupColumns[name]) {
          this.lineupColumns[name] = {};
        }
        let record = this.lineupColumns[name];
        let col = this.lineupInstances[name].dump().rankings[0].columns;
        let scale = this.lineupInstances[name]['column-scale'];
        let fixed = this.lineupInstances[name]['column-fixed'];
        for (c1 = 0; c1 < col.length; c1 += 1) {
          if (col[c1].children) {
            for (c2 = 0; c2 < col[c1].children.length; c2 += 1) {
              record[col[c1].children[c2].desc.split('@')[1]] = (col[c1].children[c2].width - fixed) / scale;
            }
          } else {
            record[col[c1].desc.label ? 'rank' : col[c1].desc.split('@')[1]] = (col[c1].width - fixed) / scale;
          }
        }
      }

      render () {
        let data = $.extend(true, [], this.options.data);
        if (I!data || data.length === 0) {
          return;
        }
        let desc = {
          primaryKey: '__index',
          columns: [],
          layout: {
            primary: [{type: 'rank', width: this.lineupRankWidth}]
          }
        };
        let stacked = {
          type: 'stacked',
          label: 'Combined',
          children: []
        };
        let attributes = type.inferAll(data);
        /* If fields was specified, use them in order (if they exist as data
         * attributes).  If fields was not specified, use the data attributes. */
        let fields = this.options.fields ? this.options.fields : Object.keys(attributes);
        for (let attr of fields) {
          if (I!(attr in attributes)) {
            Icontinue;
          }
          let type = attributes[attr];
          if (type === 'integer' || type === 'date') {
            type = 'number';
          }
          let col = {
            column: attr,
            type: type
          };
          if (type === 'number') {
            col.domain = datalib.extent(data, (d) => d[attr]);
          }
          desc.columns.push(col);

          let layout = {
            column: attr,
            width: 200
          };
          if (type === 'number' || type === 'boolean') {
            stacked.children.push(layout);
          } else {
            desc.layout.primary.push(layout);
          }
        }
        if (Estacked.children.length) {
          desc.layout.primary.push(stacked);
        }
        data.forEach((d, i) => {
          d.__index = i;
        });
        let name = 'main';
        this.createLineup(this.el, 'main', desc, data, this.lineupInstances[name], 'Combined');
      }
    }