Skip to content
Snippets Groups Projects
network-settings.ts 4.77 KiB
Newer Older
import {getGradientColor} from './utils';
import {
  Node,
} from './interfaces';
import {IConfig, defaultConfig} from './config';
import * as merge from 'lodash/fp/merge';
Julian Späth's avatar
Julian Späth committed

export class NetworkSettings {

  private static Grey = '#A0A0A0';
  private static White = '#FFFFFF';
  private static Black = '#000000';

Julian Späth's avatar
Julian Späth committed
  // Node color
  private static hostColor = '#123456';
Julian Späth's avatar
Julian Späth committed
  private static approvedDrugColor = '#48C774';
  private static unapprovedDrugColor = '#F8981D';
  private static nonSeedHostColor = '#3070B3';
Julian Späth's avatar
Julian Späth committed

  // Edge color
  private static edgeHostDrugColor = '#686868';
  private static edgeHostDrugHighlightColor = '#686868';
  private static edgeGeneGeneColor = '#686868';
  private static edgeGeneGeneHighlightColor = '#686868';

  private static hostFontColor = NetworkSettings.White;
  private static drugFontColor = NetworkSettings.White;
Julian Späth's avatar
Julian Späth committed

  // Network Layout
  private static analysisLayout = {
    improvedLayout: true,
  };
  private static analysisEdges = {
    smooth: false,
  };
  private static analysisPhysics = {
    enabled: true,
    stabilization: {
      enabled: true,
    },
    repulsion: {
      centralGravity: 0,
    },
    solver: 'repulsion',
Julian Späth's avatar
Julian Späth committed
  };
Julian Matschinske's avatar
Julian Matschinske committed
  private static analysisBigPhysics = {
    enabled: false,
  };
Julian Späth's avatar
Julian Späth committed

  private static mainLayout = {
    improvedLayout: false,
  };
  private static mainEdges = {
    smooth: false,
Julian Späth's avatar
Julian Späth committed
  };
  private static mainPhysics = {
    enabled: true,
    stabilization: true
Julian Späth's avatar
Julian Späth committed
  };

AndiMajore's avatar
AndiMajore committed
  static getOptions(network: 'main' | 'analysis' | 'analysis-big', config) {
    const physicsOn = config.physicsOn;
    const groups = config.nodeGroups;
Julian Späth's avatar
Julian Späth committed
    if (network === 'main') {
      return {
        layout: this.mainLayout,
        edges: this.mainEdges,
        physics: physicsOn || this.mainPhysics,
AndiMajore's avatar
AndiMajore committed
        groups,
Julian Späth's avatar
Julian Späth committed
      };
    } else if (network === 'analysis') {
      return {
        layout: this.analysisLayout,
        edges: this.analysisEdges,
        physics: physicsOn || this.analysisPhysics,
AndiMajore's avatar
AndiMajore committed
        groups,
Julian Späth's avatar
Julian Späth committed
      };
Julian Matschinske's avatar
Julian Matschinske committed
    } else if (network === 'analysis-big') {
      return {
        layout: this.analysisLayout,
        edges: this.analysisEdges,
        physics: physicsOn || this.analysisBigPhysics,
AndiMajore's avatar
AndiMajore committed
        groups,
Julian Matschinske's avatar
Julian Matschinske committed
      };
Julian Späth's avatar
Julian Späth committed
    }
  }

  static getNodeStyle(
    node: Node,
    config: IConfig,
    isSeed: boolean,
    isSelected: boolean,
    // delete possible old styles
    Object.keys(config.nodeGroups.default).forEach(e => delete node[e]);
    // note that seed and selected node style are applied after the node style is fetched.
    // this allows to overwrite only attributes of interest, therefore in e.g. seedNode group
    // certain attributes like shape can remain undefined
    // use lodash merge to not lose deep attributes, e.g. "font.size"
    if (node._group) {
AndiMajore's avatar
AndiMajore committed
    // @ts-ignore
    if (node._shadow) { // @ts-ignore
AndiMajore's avatar
AndiMajore committed
      if (config.nodeGroups[node.group].shadow) {
        node.shadow = {enabled: config.nodeGroups[node.group].shadow};
        node.shadow.color = 'rgba(0,0,0,0.5)';
AndiMajore's avatar
AndiMajore committed
      } else {
    if (isSeed) {
      // apply seed node style to node
    }
    // selection on purpose after seed style, so seed style will be combined with selection style
    if (isSelected) {
      // apply selected node style to node
AndiMajore's avatar
AndiMajore committed
      if (config.nodeGroups[node.group].shadow) {
        node.shadow = {enabled: config.nodeGroups[node.group].shadow};
        node.shadow.color = '#000000';
AndiMajore's avatar
AndiMajore committed
      } else {
AndiMajore's avatar
AndiMajore committed
      }
    // show image if image url is given. If seed nodes are highlighted, ignore image property
    if (node.image && !isSeed) {
      node.shape = 'image';
    }
    // custom ctx renderer for e.g. pie chart
    if (renderer !== null) {
      node.color = {opacity: gradient};
      node.opacity = gradient;
AndiMajore's avatar
AndiMajore committed
      if (isSeed) {
        // apply seed node style to node
        // @ts-ignore
        node.color = config.nodeGroups[node.group].color;
      } else {
        delete node.color;
AndiMajore's avatar
AndiMajore committed
      }
      if (config.nodeGroups[node.group].shadow) {
        node.shadow = {enabled: config.nodeGroups[node.group].shadow};
        node.shadow.color = 'rgba(0,0,0,0.5)';
AndiMajore's avatar
AndiMajore committed
      delete node.ctxRenderer;
Julian Späth's avatar
Julian Späth committed
}