<commit_before>/**
 * Created by <NAME> on 29.01.2016.
 */
/// <reference path="./tsd.d.ts" />

import {AView, EViewMode, IViewContext, ISelection, ViewWrapper} from './View';
import lineup = require('lineupjs');
import d3 = require('d3');
import idtypes = require('../caleydo_core/idtype');
import tables = require('../caleydo_core/table');
import ranges = require('../caleydo_core/range');
import plugins = require('../caleydo_core/plugin');
import dialogs = require('../caleydo_bootstrap_fontawesome/dialogs');
import cmds = require('./LineUpCommands');
import {saveNamedSet} from './storage';
import {showErrorModalDialog} from './Dialogs';
import {IDType} from '../caleydo_core/idtype';
import {EventHandler} from '../caleydo_core/event';

export function numberCol(col:string, rows:any[], label = col, visible = true, width = -1, selectedId = -1) {
  return {
    type: 'number',
    column: col,
    label: label,
    domain: d3.extent(rows, (d) => d[col]),
    color: '',
    visible: visible,
    width: width,
    selectedId: selectedId
  };
}

export function numberCol2(col:string, min:number, max:number, label = col, visible = true, width = -1, selectedId = -1) {
  return {
    type: 'number',
    column: col,
    label: label,
    domain: [min, max],
    color: '',
    visible: visible,
    width: width,
    selectedId: selectedId
  };
}


export function categoricalCol(col:string, categories:string[], label = col, visible = true, width = -1, selectedId = -1) {
  return {
    type: 'categorical',
    column: col,
    label: label,
    categories: categories,
    color: '',
    visible: visible,
    width: width,
    selectedId: selectedId
  };
}


export function stringCol(col:string, label = col, visible = true, width = -1, selectedId = -1) {
  return {
    type: 'string',
    column: col,
    label: label,
    color: '',
    visible: visible,
    width: width,
    selectedId: selectedId
  };
}

export function booleanCol(col:string, label = col, visible = true, width = -1, selectedId = -1) {
  return {
    type: 'boolean',
    column: col,
    label: label,
    color: '',
    visible: visible,
    width: width,
    selectedId: selectedId
  };
}

/**
 * Returns the all items that are not in the given two arrays
 * TODO improve performance of diff algorithm
 * @param array1
 * @param array2
 * @returns {any}
 */
function array_diff(array1, array2) {
  return array1.filter((elm) => array2.indexOf(elm) === -1);
}


export function useDefaultLayout(instance:any) {
  instance.data.deriveDefault();
  //insert selection column
  instance.data.insert(instance.data.getRankings()[0], 1, lineup.model.createSelectionDesc());
}

export function deriveCol(col:tables.IVector) {
  var r:any = {
    column: col.desc.name
  };
  const desc = <any>col.desc;
  if (desc.color) {
    r.color = desc.color;
  } else if (desc.cssClass) {
    r.cssClass = desc.cssClass;
  }
  var val = desc.value;
  switch (val.type) {
    case 'string':
      r.type = 'string';
      break;
    case 'categorical':
      r.type = 'categorical';
      r.categories = desc.categories;
      break;
    case 'real':
    case 'int':
      r.type = 'number';
      r.domain = val.range;
      break;
    default:
      r.type = 'string';
      break;
  }
  return r;
}


export abstract class ALineUpView2 extends AView {

  resolver:(d:any) => void;

  protected lineup:any;

  protected additionalScoreParameter: any = null;

  private initLineUpPromise;

  private config = {
    renderingOptions: {
      histograms: true
    },
    header: {
      rankingButtons: ($node:d3.Selection<any>) => {
        const rb = new LineUpRankingButtons(this.lineup, $node, this.idType, this.additionalScoreParameter);
        rb.on(LineUpRankingButtons.SAVE_NAMED_SET, (event, order, name, description) => {
          this.saveNamedSet(order, name, description);
        });
        rb.on(LineUpRankingButtons.ADD_SCORE_COLUMN, (event, scoreImpl, scorePlugin) => {
          this.addScoreColumn(scoreImpl, scorePlugin);
        });
        return rb;
      }
    },
    body: {}
  };

  protected idType:idtypes.IDType;

  /**
   * Stores the ranking data when collapsing columns on modeChange()
   * @type {any}
   */
  private dump:any = null;

  /**
   * DOM element with message when no data is available
   */
  protected $nodata;

  /**
   * DOM element for LineUp stats in parameter UI
   */
  private $params:d3.Selection<ViewWrapper>;

  private selectionHelper:LineUpSelectionHelper;

  protected idAccessor = (d) => d._id;

  private scoreAccessor = (row:any, id:string, desc:any) => {
    const row_id = this.idAccessor(row);
    return (desc.scores && typeof desc.scores[row_id] !== 'undefined') ? desc.scores[row_id] : (typeof desc.missingValue !== 'undefined' ? desc.missingValue : null);
  };

  constructor(context:IViewContext, protected selection: ISelection, parent:Element, private options?) {
    super(context, parent, options);

    this.$node.classed('lineup', true);

    this.context.ref.value.data = new Promise((resolve) => {
      this.resolver = resolve;
    });
    //context.graph.findOrAddObject()

    this.$nodata = this.$node.append('p')
      .classed('nodata', true)
      .classed('hidden', true)
      .text('No data found for selection and parameter.');
  }

  init() {
    super.init();
    //this.build(); // will be called from update()<commit_msg>Caleydo/targid2#308 restructure lineup categorical scores<commit_after>/**
 * Created by <NAME> on 29.01.2016.
 */
/// <reference path="./tsd.d.ts" />

import {AView, EViewMode, IViewContext, ISelection, ViewWrapper} from './View';
import lineup = require('lineupjs');
import d3 = require('d3');
import idtypes = require('../caleydo_core/idtype');
import tables = require('../caleydo_core/table');
import ranges = require('../caleydo_core/range');
import plugins = require('../caleydo_core/plugin');
import dialogs = require('../caleydo_bootstrap_fontawesome/dialogs');
import cmds = require('./LineUpCommands');
import {saveNamedSet} from './storage';
import {showErrorModalDialog} from './Dialogs';
import {IDType} from '../caleydo_core/idtype';
import {EventHandler} from '../caleydo_core/event';

export function numberCol(col:string, rows:any[], label = col, visible = true, width = -1, selectedId = -1) {
  return {
    type: 'number',
    column: col,
    label: label,
    domain: d3.extent(rows, (d) => d[col]),
    color: '',
    visible: visible,
    width: width,
    selectedId: selectedId
  };
}

export function numberCol2(col:string, min:number, max:number, label = col, visible = true, width = -1, selectedId = -1) {
  return {
    type: 'number',
    column: col,
    label: label,
    domain: [min, max],
    color: '',
    visible: visible,
    width: width,
    selectedId: selectedId
  };
}


export function categoricalCol(col:string, categories:(string|{label?: string, name: string, color?: string})[], label = col, visible = true, width = -1, selectedId = -1) {
  return {
    type: 'categorical',
    column: col,
    label: label,
    categories: categories,
    color: '',
    visible: visible,
    width: width,
    selectedId: selectedId
  };
}


export function stringCol(col:string, label = col, visible = true, width = -1, selectedId = -1) {
  return {
    type: 'string',
    column: col,
    label: label,
    color: '',
    visible: visible,
    width: width,
    selectedId: selectedId
  };
}

export function booleanCol(col:string, label = col, visible = true, width = -1, selectedId = -1) {
  return {
    type: 'boolean',
    column: col,
    label: label,
    color: '',
    visible: visible,
    width: width,
    selectedId: selectedId
  };
}

/**
 * Returns the all items that are not in the given two arrays
 * TODO improve performance of diff algorithm
 * @param array1
 * @param array2
 * @returns {any}
 */
function array_diff(array1, array2) {
  return array1.filter((elm) => array2.indexOf(elm) === -1);
}


export function useDefaultLayout(instance:any) {
  instance.data.deriveDefault();
  //insert selection column
  instance.data.insert(instance.data.getRankings()[0], 1, lineup.model.createSelectionDesc());
}

export function deriveCol(col:tables.IVector) {
  var r:any = {
    column: col.desc.name
  };
  const desc = <any>col.desc;
  if (desc.color) {
    r.color = desc.color;
  } else if (desc.cssClass) {
    r.cssClass = desc.cssClass;
  }
  var val = desc.value;
  switch (val.type) {
    case 'string':
      r.type = 'string';
      break;
    case 'categorical':
      r.type = 'categorical';
      r.categories = desc.categories;
      break;
    case 'real':
    case 'int':
      r.type = 'number';
      r.domain = val.range;
      break;
    default:
      r.type = 'string';
      break;
  }
  return r;
}


export abstract class ALineUpView2 extends AView {

  resolver:(d:any) => void;

  protected lineup:any;

  protected additionalScoreParameter: any = null;

  private initLineUpPromise;

  private config = {
    renderingOptions: {
      histograms: true
    },
    header: {
      rankingButtons: ($node:d3.Selection<any>) => {
        const rb = new LineUpRankingButtons(this.lineup, $node, this.idType, this.additionalScoreParameter);
        rb.on(LineUpRankingButtons.SAVE_NAMED_SET, (event, order, name, description) => {
          this.saveNamedSet(order, name, description);
        });
        rb.on(LineUpRankingButtons.ADD_SCORE_COLUMN, (event, scoreImpl, scorePlugin) => {
          this.addScoreColumn(scoreImpl, scorePlugin);
        });
        return rb;
      }
    },
    body: {}
  };

  protected idType:idtypes.IDType;

  /**
   * Stores the ranking data when collapsing columns on modeChange()
   * @type {any}
   */
  private dump:any = null;

  /**
   * DOM element with message when no data is available
   */
  protected $nodata;

  /**
   * DOM element for LineUp stats in parameter UI
   */
  private $params:d3.Selection<ViewWrapper>;

  private selectionHelper:LineUpSelectionHelper;

  protected idAccessor = (d) => d._id;

  private scoreAccessor = (row:any, id:string, desc:any) => {
    const row_id = this.idAccessor(row);
    var r = (desc.scores && typeof desc.scores[row_id] !== 'undefined') ? desc.scores[row_id] : (typeof desc.missingValue !== 'undefined' ? desc.missingValue : null);
    if (desc.type === 'categorical') {
      r = String(r); //even null values
    }
    return r;
  };

  constructor(context:IViewContext, protected selection: ISelection, parent:Element, private options?) {
    super(context, parent, options);

    this.$node.classed('lineup', true);

    this.context.ref.value.data = new Promise((resolve) => {
      this.resolver = resolve;
    });
    //context.graph.findOrAddObject()

    this.$nodata = this.$node.append('p')
      .classed('nodata', true)
      .classed('hidden', true)
      .text('No data found for selection and parameter.');
  }

  init() {
    super.init();
    //this.build(); // will be called from update()
{'fraction_non_alphanumeric': 0.145812448737811, 'fraction_numerical': 0.006561560193201495, 'mean_word_length': 3.1821646341463414, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 31, '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': '4573173', 'n_tokens_mistral': 3561, 'n_tokens_neox': 3480, 'n_words': 889}