All files / src SSVGPropertyAuto.js

87.71% Statements 50/57
69.23% Branches 36/52
100% Functions 6/6
87.5% Lines 49/56

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                                12x   12x 12x 12x 12x     12x   12x   12x       12x       12x 14x               12x 6x 6x     6x 6x 2x 2x     2x 2x     4x 2x     2x                   45x   6x 6x         11x   28x                 28x     28x       28x 28x   28x           56x 56x 37x 7x 7x   37x     37x 5x   37x 37x   19x 9x 9x     10x 10x           188x      
// @ts-check
 
import { PropertyType, default as SSVGProperty } from './SSVGProperty';
import { isNil } from './utils';
import { parseInitial } from './parser';
 
/**
 * @typedef {import('./SSVGState').default} SSVGState
 */
 
export default class SSVGPropertyAuto extends SSVGProperty {
  /**
   * @param {SSVGState} ssvg
   * @param {Element} element
   */
  constructor(ssvg, element) {
    super(ssvg, element);
 
    this._itype = PropertyType.auto; // will be updated
    this.min = Number.parseFloat(element.getAttribute('min') || '0');
    this.max = Number.parseFloat(element.getAttribute('max') || '1');
    this._irange = { min: this.min, max: this.max };
 
    /** @type {any} */
    this._value; /* eslint-disable-line no-unused-expressions *//* jshint ignore:line */
 
    this._parseInitial(element.getAttribute('initial'));
 
    Iif (Number.isNaN(this.min)) {
      throw new SyntaxError('invalid min value for number property: ' +
        (element.getAttribute('min') || ''));
    }
    else Iif (Number.isNaN(this.max)) {
      throw new SyntaxError('invalid max value for number property: ' +
        (element.getAttribute('max') || ''));
    }
    for (let i = 0; i < this.relations.length; ++i) {
      this.relations[i].setInitial(this);
    }
  }
 
  /**
   * @param  {string?=} value
   */
  _parseInitial(value) {
    if (isNil(value)) {
      this._value = this._irange.min;
      this._itype = PropertyType.number;
    }
    else {
      this._value = parseInitial(value);
      if (Number.isFinite(this._value)) {
        this._itype = PropertyType.number;
        Iif (this._value < this.min) {
          this.min = this._value;
        }
        else Eif (this._value > this.max) {
          this.max = this._value;
        }
      }
      else if (this._value === true || this._value === false) {
        this._itype = PropertyType.boolean;
      }
      else {
        this._itype = PropertyType.enum;
      }
    }
  }
 
  /**
   * @param {any?=} [value]
   * @return {number}
   */
  normalize(value) {
    switch (this._itype) {
    case PropertyType.boolean:
      Eif (isNil(value)) {
        return this._value ? 1 : 0;
      }
      return value ? 1 : 0;
    case PropertyType.enum:
    default:
      return 0;
    case PropertyType.number:
      return this._normalizeNumber(value);
    }
  }
 
  /**
   * @param {number?=} [value]
   * @return {number}
   */
  _normalizeNumber(value) {
    Iif (this.min === this.max) {
      return this.min;
    }
    else Iif (!isNil(value)) {
      // @ts-ignore
      return (Number.parseFloat(value) - this.min) / (this.max - this.min);
    }
    else Eif (isNil(this._normalized)) {
      this._normalized = (this._value - this.min) / (this.max - this.min);
    }
    return this._normalized;
  }
 
 
  /** @param {number|any} value */
  set value(value) {
    this._normalized = null;
    if (Number.isFinite(value)) {
      if (this._itype !== PropertyType.number) {
        this.min = this._irange.min;
        this.max = this._irange.max;
      }
      Iif (value < this.min) {
        this.min = value;
      }
      else if (value > this.max) {
        this.max = value;
      }
      this._itype = PropertyType.number;
      super.value = value;
    }
    else if (value === true || value === false) {
      this._itype = PropertyType.boolean;
      super.value = value;
    }
    else {
      this._itype = PropertyType.enum;
      super.value = value;
    }
  }
 
  /** @return {number|any} */
  get value() {
    return super.value;
  }
}