All files / src SSVGRelationElement.js

80% Statements 52/65
65.51% Branches 38/58
100% Functions 8/8
81.66% Lines 49/60

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                        1x             1x                                           84x 84x 84x 84x 84x     84x 84x           84x 84x 84x   84x               84x 84x 54x     30x   30x                     84x 84x                               64x 7x   57x 57x 53x   4x   4x                       64x 64x   28x 28x               36x 36x     36x   34x     34x               2x     2x   2x 2x 2x     2x       2x 2x 2x       4x     4x 2x     2x        
// @ts-check
 
import { TType, makeRange, parseTRange, parseTValue } from './parser/values';
import { isCSS } from './CSS';
import { default as SSVGTransition } from './SSVGTransition';
 
/**
 * @typedef {import('./SSVGState').default} SSVGState
 * @typedef {import('./SSVGProperty').default} SSVGProperty
 */
 
/** @enum {number} */
export const AttributeType = {
  XML: 0,
  CSS: 1,
  AUTO: 2
};
 
/** @enum {number} */
export const CalcMode = {
  linear: 0,
  discrete: 1
};
 
export default class SSVGRelationElement {
  /*::
  ssvg: SSVGState
  querySelector: string
  attributeName: ?string
  attributeType: $Values<typeof AttributeType>
  values: $TRange
  keyTimes: ?Array<number>
  calcMode: $Values<typeof CalcMode>
  transition: SSVGTransition
  selection: d3$Selection
  */
  /**
   * @param {SSVGState} ssvg
   * @param {Element} element
   */
  constructor(ssvg, element) {
    this.ssvg = ssvg;
    this.querySelector = element.getAttribute('query-selector') || '';
    this.selection = this.ssvg.selection.selectAll(this.querySelector);
    this.attributeName = element.getAttribute('attribute-name');
    this.attributeType = this._parseAttrType(element);
 
    /** @type {ssvg.$TRange} */
    this.values; /* eslint-disable-line no-unused-expressions *//* jshint ignore:line */
    Iif (!(this._parseValues(element) ||
          this._parseFrom(element) ||
          this._parseBy(element))) {
      throw new SyntaxError('at least values/from or by must be set');
    }
 
    this.keyTimes = this._parseKeyTimes(element);
    this.calcMode = this._parseCalcMode(element);
    this.transition = new SSVGTransition(element, ssvg.defaultTransition);
 
    this.element = element;
  }
 
  /**
   * @param {Element} element
   * @return {AttributeType}
   */
  _parseAttrType(element) {
    const type = element.getAttribute('attribute-type');
    if (!type || type === 'auto') {
      return (this.attributeName && isCSS(this.attributeName)) ?
        AttributeType.CSS : AttributeType.XML;
    }
    else if (type in AttributeType) {
      // @ts-ignore
      return AttributeType[type];
    }
    else E{
      throw new SyntaxError('invalid attribute-type: ' + type);
    }
  }
 
  /**
   * @param {Element} elt
   */
  _parseKeyTimes(elt) {
    const keyTimes = elt.getAttribute('key-times');
    Eif (!keyTimes) { return null; }
    var range = parseTRange(keyTimes);
    if (!range) { return null; }
    if ((range.type !== 'number') ||
        (range.values[0] !== 0) ||
        (range.values[range.values.length - 1] !== 1)) {
      throw new SyntaxError('invalid keyTimes value :' + keyTimes);
    }
    return range.values;
  }
 
  /**
   * @param {Element} elt
   * @return {CalcMode}
   */
  _parseCalcMode(elt) {
    if (this.values && !TType[this.values.type].canInterpolate) {
      return CalcMode.discrete;
    }
    const calcMode = elt.getAttribute('calc-mode');
    if (!calcMode) {
      return CalcMode.linear;
    }
    else if (calcMode in CalcMode) {
      // @ts-ignore
      return CalcMode[calcMode];
    }
    else E{
      throw new SyntaxError('invalid calcMode: ' + calcMode);
    }
  }
 
  /**
   * @param {Element} elt
   * @return {boolean}
   */
  _parseValues(elt) {
    const values = parseTRange(elt.getAttribute('values') || '');
    if (!values) { return false; }
 
    this.values = values;
    return true;
  }
 
  /**
   * @param {Element} elt
   * @return {boolean}
   */
  _parseFrom(elt) {
    const from = elt.getAttribute('from');
    const to = elt.getAttribute('to');
 
    /* not yet a number */
    if (!to) { return false; }
 
    this.values = makeRange(
      parseTValue(from ? from : this._getCurrentValue()),
      parseTValue(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 = parseTValue(this._getCurrentValue());
    const to = parseTValue(by);
    Iif (!to) {
      throw new SyntaxError('failed to parse by value');
    }
    else Iif (!current) {
      throw new SyntaxError('failed to parse retrieve current value');
    }
    // @ts-ignore
    to.value += current.value;
    this.values = makeRange(current, to);
    return true;
  }
 
  _getCurrentValue() {
    Iif (!this.attributeName) {
      return this.selection.text() || '';
    }
    else if (this.attributeType === AttributeType.CSS) {
      return this.selection.style(this.attributeName) || '';
    }
    else {
      return this.selection.attr(this.attributeName) || '';
    }
  }
}