All files / src SSVGProperty.js

96% Statements 24/25
75% Branches 9/12
87.5% Functions 7/8
95.83% Lines 23/24

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                      1x                         46x 46x 46x     46x   46x 84x 84x 64x   20x 11x   9x 9x           42x 81x           11x         64x         9x         539x         267x     224x 224x 732x                      
// @ts-check
 
import SSVGRelation from './SSVGRelation';
import SSVGTransform from './SSVGTransform';
import SSVGDirect from './SSVGDirect';
 
/**
 * @typedef {import('./SSVGState').default} SSVGState
 */
 
/** @type {{ [key: string]: number }} */
export const PropertyType = {
  boolean: 0,
  number: 1,
  enum: 2,
  auto: 3
};
 
export default class SSVGProperty {
  /**
   * @param {SSVGState} ssvg
   * @param {Element} element
   */
  constructor(ssvg, element) {
    this.ssvg = ssvg;
    this.name = element.getAttribute('name') || '';
    this.type = PropertyType[element.getAttribute('type') || 'boolean'];
 
    /** @type {SSVGRelation[]} */
    this.relations = [];
 
    for (let i = 0; i < element.children.length; ++i) {
      const relation = element.children[i];
      if (relation.tagName === 'relation') {
        this.makeRelation(relation);
      }
      else if (relation.tagName === 'transform') {
        this.makeTransform(relation);
      }
      else Eif (relation.tagName === 'direct') {
        this.makeDirect(relation);
      }
    }
  }
 
  disconnect() {
    for (const relation of this.relations) {
      relation.disconnect();
    }
  }
 
  /** @param {Element} relation */
  makeTransform(relation /*: Element */) {
    this.relations.push(new SSVGTransform(this.ssvg, relation));
  }
 
  /** @param {Element} relation */
  makeRelation(relation /*: Element */) {
    this.relations.push(new SSVGRelation(this.ssvg, relation));
  }
 
  /** @param {Element} relation */
  makeDirect(relation /*: Element */) {
    this.relations.push(new SSVGDirect(this.ssvg, relation));
  }
 
  /** @return {any} */
  get value() {
    return this._value;
  }
 
  /** @param {any} value */
  set value(value /*: any */) {
    if (value !== this._value) {
 
      /* FIXME: check for changes */
      this._value = value;
      for (const relation of this.relations) {
        relation.update(this);
      }
    }
  }
 
  /**
   * @param  {any} [value]
   * @return {number}
   */ // eslint-disable-next-line @typescript-eslint/no-unused-vars
  normalize(value) { return 0; } // jshint ignore:line
}