All files / src SSVGTransform.js

84.5% Statements 60/71
63.04% Branches 29/46
93.33% Functions 14/15
85.71% Lines 54/63

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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188                                          11x     11x     11x       11x 11x 11x 7x     11x 11x   11x 11x 33x       11x 81x 11x 33x   11x   11x                     11x 11x   7x 7x               4x 4x   4x   2x         2x               2x     2x   2x 2x 2x     2x         2x 2x 2x     2x               11x 11x 11x                               14168x 14168x 14168x 54248x               110x 110x 110x                 54358x       54358x 54358x 54358x               4x     4x 4x              
// @ts-check
 
import { scaleLinear } from 'd3-scale';
 
import { AttributeType, CalcMode } from './SSVGRelationElement';
import { default as SSVGRelation, scaleThreshold } from './SSVGRelation';
import { normalizeTransform, parseTTransform, parseTTransformRange } from './parser/transform';
import { isNil } from './utils';
 
/**
 * @typedef {import('./SSVGState').default} SSVGState
 * @typedef {import('./SSVGProperty').default} SSVGProperty
 */
 
 
export default class SSVGTransform extends SSVGRelation {
  /**
   * @param {SSVGState} ssvg
   * @param {Element} element
   */
  constructor(ssvg, element) {
    super(ssvg, element);
 
    /** @type {ssvg.$TransformRange[]} */
    this.transforms; /* eslint-disable-line no-unused-expressions *//* jshint ignore:line */
 
    /** @type {number[]} */
    this.keyTimes; /* eslint-disable-line no-unused-expressions *//* jshint ignore:line */
  }
 
  init() {
    this.animName = 'ssvg-trans-' + this.id;
    this.canInterpolate = true;
    if (isNil(this.attributeName)) {
      this.attributeName = 'transform';
    }
 
    const steps = (this.transforms.length > 0) ? this.transforms[0].args.length : 0;
    Eif (!this.keyTimes) {
      /** @type {number[]} */
      this.keyTimes = [];
      for (let i = 0; i < steps; ++i) {
        this.keyTimes.push(i / (steps - 1));
      }
    }
 
    const values = [];
    const mapper = function(/** @type {number} */ step, /** @type {ssvg.$TransformRange} */range) { return range.args[step]; };
    for (let step = 0; step < steps; ++step) {
      values.push(this.transforms.map(mapper.bind(null, step)));
    }
    this.scale = /** @type {any} */ (scaleLinear(this.keyTimes, values));
 
    Iif (this.calcMode === CalcMode.discrete) {
      const toScale = scaleThreshold(this.keyTimes, this.keyTimes);
      this.quantize = (/** @type {SSVGProperty} */ prop, /** @type {number?=} */ t) => toScale(prop.normalize(t));
    }
  }
 
  /**
   * @param {Element} elt
   * @return {boolean}
   */
  _parseValues(elt) {
    const values = parseTTransformRange(elt.getAttribute('values') || '');
    if (!values) { return false; }
 
    this.transforms = normalizeTransform(values);
    return true;
  }
 
  /**
   * @param {Element} elt
   * @return {boolean}
   */
  _parseFrom(elt) {
    const from = elt.getAttribute('from');
    const to = elt.getAttribute('to');
 
    if (!to) { return false; }
 
    this.transforms = normalizeTransform([
      parseTTransform(from ? from : this._getCurrentValue()) || [],
      // @ts-ignore
      parseTTransform(to)
    ]);
    return true;
  }
 
  /**
   * @param {Element} elt
   * @return {boolean}
   */
  _parseBy(elt) {
    const by = elt.getAttribute('by');
 
    /* not yet a number */
    Iif (!by) { return false; }
 
    const current = this._getCurrentValue();
    const to = parseTTransform(by);
    Iif (!to) {
      throw new SyntaxError('failed to parse by value');
    }
    this.transforms = normalizeTransform([
      // @ts-ignore
      current ? parseTTransform(current) : [],
      to
    ]);
    for (const transform of this.transforms) {
      for (let i = 0; i < transform.args[0].length; ++i) {
        transform.args[1][i] += transform.args[0][i];
      }
    }
    return true;
  }
 
  /**
   * @param {Element} elt
   * @return {CalcMode}
   */
  _parseCalcMode(elt) {
    const calcMode = elt.getAttribute('calc-mode');
    if (!calcMode) {
      return CalcMode.linear;
    }
    else Eif (calcMode in CalcMode) {
      // @ts-ignore
      return CalcMode[calcMode];
    }
    else {
      throw new SyntaxError('invalid calcMode: ' + calcMode);
    }
  }
 
  /**
   * @brief computed target value from normalized input
   * @param  {number} n
   */
  strict(n) {
    this._last = n;
    const values = this.scale(n);
    return this.transforms.map(// @ts-ignore
      (tr, idx) => this.transformToString(tr, values[idx])).join(" ");
  }
 
  /**
   * @brief pre-computed target value
   * @param  {any} value
   */
  direct(value /*: any */) {
    this._last = value;
    return this.transforms.map(
      (tr, idx) => this.transformToString(tr, value[idx])).join(" ");
  }
 
  /**
   * @param {ssvg.$TransformRange} trRange
   * @param {number[]} values
   * @return {string}
   */
  transformToString(trRange, values) {
    Iif (values.length === 0) {
      return trRange.transform + "()";
    }
 
    return trRange.transform + "(" + values.map((value, idx) => {
      const unit = trRange.units[idx];
      return unit ? (value.toString() + unit) : value.toString();
    }).join(',') + ")";
  }
 
  /**
   * @return {string}
   */
  _getCurrentValue() {
    Iif (this.attributeName === '') {
      return this.selection.text() || '';
    }
    else if (!this.attributeName || (this.attributeType === AttributeType.XML)) {
      return this.selection.attr(this.attributeName || "transform") || '';
    }
    else E{
      return this.selection.style(this.attributeName) || '';
    }
  }
}