All files / src SSVGRelation.js

98.36% Statements 60/61
92.85% Branches 26/28
100% Functions 17/17
98.18% Lines 54/55

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                  11x 1050x                     84x 84x     84x     84x     84x     84x   84x 84x       64x 64x   64x 64x 157x   64x 7x         57x   57x 4x 70x           81x             75x 75x 75x   75x 8x   67x 46x     21x               681x   681x 681x 681x     681x   681x     681x 19x     19x   158x 425x       662x 1089474x     681x 82x   599x 124627x     200x                 1075486x 1075486x 1075486x               318x 318x       1x  
// @ts-check
 
import { TType } from './parser/values';
import { TransitionType } from './SSVGTransition';
 
import 'd3-transition'; // needed at least once to load plugins
 
import { scaleLinear } from 'd3-scale';
import { interpolateNumber } from 'd3-interpolate';
import { bisect } from 'd3-array';
import { AttributeType, CalcMode, default as SSVGRelationElement } from './SSVGRelationElement';
import SSVGFilter from './SSVGFilter';
 
/**
 * @typedef {import('./SSVGState').default} SSVGState
 * @typedef {import('./SSVGProperty').default} SSVGProperty
 */
 
/**
 * @template T=any
 * @param {number[]} keyTimes
 * @param {T[]} values
 * @return {(t: number) => T}
 */
export function scaleThreshold(keyTimes, values) {
  return function(t) {
    return values[bisect(keyTimes, t, 1, keyTimes.length) - 1];
  };
}
 
export default class SSVGRelation extends SSVGRelationElement {
  /**
   *
   * @param {SSVGState} ssvg
   * @param {Element} element
   */
  constructor(ssvg, element) {
    super(ssvg, element);
    this.id = ++SSVGRelation.id;
 
    /** @type {(t: number) => ssvg.$Value} */
    this.scale; /* eslint-disable-line no-unused-expressions *//* jshint ignore:line */
 
    /** @type {number[]|null} */
    this.keyTimes; /* eslint-disable-line no-unused-expressions *//* jshint ignore:line */
 
    /** @type {undefined|null|((prop: SSVGProperty, t?: number) => number)} */
    this.quantize; /* eslint-disable-line no-unused-expressions *//* jshint ignore:line */
 
    /** @type {ssvg.$Value} */
    this._last; /* eslint-disable-line no-unused-expressions *//* jshint ignore:line */
 
    this._filter = new SSVGFilter(element);
    this.init();
  }
 
  init() {
    this.animName = 'ssvg-rel-' + this.id;
    this.canInterpolate = TType[this.values.type].canInterpolate;
 
    Eif (!this.keyTimes) {
      this.keyTimes =
        this.values.values.map((val, idx) => idx / (this.values.values.length - 1));
    }
    if (!this.canInterpolate) {
      this.scale = scaleThreshold(
        this.keyTimes,
        this.values.values);
    }
    else {
      this.scale = scaleLinear(this.keyTimes, this.values.values);
 
      if (this.calcMode === CalcMode.discrete) {
        const toScale = scaleThreshold(this.keyTimes, this.keyTimes);
        this.quantize = (prop, t) => toScale(prop.normalize(t));
      }
    }
  }
 
  disconnect() {
    this.selection.interrupt(this.animName);
  }
 
  /**
   * @param {SSVGProperty} prop
   */
  setInitial(prop) {
    this.selection.interrupt(this.animName);
    const to = this.quantize ? this.quantize(prop) : prop.normalize();
    const value = (this.transition.type === TransitionType.direct) ?
      this.direct(this.scale(to)) : this.strict(to);
    if (!this.attributeName) {
      this.selection.text(value);
    }
    else if (this.attributeType === AttributeType.CSS) {
      this.selection.style(this.attributeName, value);
    }
    else {
      this.selection.attr(this.attributeName, value);
    }
  }
 
  /**
   * @param {SSVGProperty} prop
   */
  update(prop) {
    this.selection.interrupt(this.animName);
 
    const to = (this.quantize) ? this.quantize(prop) : prop.normalize();
    var t = this.selection.transition(this.animName);
    Iif (this.transition.delay) {
      t = t.delay(this.transition.delay);
    }
    t = t.ease(this.transition.timingFunction);
 
    t = t.duration(this.transition.duration);
    /** @type {(t: number) => any} */
    var factory;
    if (this.transition.type === TransitionType.direct) {
      const scaledTo = this.scale(to);
      // non-interpolatable direct transitions change value at half animation
      // duration
      const interpolator = (this.canInterpolate) ?
        scaleLinear([ 0, 1 ], [ this._last, scaledTo ]) :
        (/** @type {number} */t) => ((t >= 0.5) ? scaledTo : this._last);
      factory = (t) => this.direct(interpolator(t));
    }
    else {
      // @ts-ignore
      const interpolator = interpolateNumber(this._last, to);
      factory = (t) => this.strict(interpolator(t));
    }
 
    if (!this.attributeName) {
      t.textTween(() => factory);
    }
    else if (this.attributeType === AttributeType.CSS) {
      t.styleTween(this.attributeName, () => factory);
    }
    else {
      t.attrTween(this.attributeName, () => factory);
    }
  }
 
  /**
   * @brief computed target value from normalized input
   * @param {number} n
   */
  strict(n) {
    this._last = n;
    const value = this.scale(n);
    return this._filter.filter(value, this.values.unit);
  }
 
  /**
   * @brief pre-computed target value
   * @param {ssvg.$Value} value
   */
  direct(value) {
    this._last = value;
    return this._filter.filter(value, this.values.unit);
  }
}
 
SSVGRelation.id = 0;