All files / src SSVGState.js

93.93% Statements 62/66
80% Branches 16/20
100% Functions 8/8
93.84% Lines 61/65

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              1x                         21x   21x   21x   21x 21x 21x   21x   21x 21x   21x   21x   21x   21x         19x     19x     19x   19x 35x     35x     19x 7x               21x   20x     21x 66x 66x 37x 37x   29x 9x 9x                   170x   170x 358x     358x 170x   358x   170x 170x               3x   3x 3x       3x 3x     3x 3x             173x 130x   43x 100x 100x               173x 173x           5x 5x 17x       17x     5x 1x   5x      
// @ts-check
 
import SSVGTransition from './SSVGTransition';
import { createProperty } from './SSVGPropertyFactory';
import SSVGComputed from './SSVGComputed';
 
import d from 'debug';
const debug = d('ssvg');
 
/**
 * @typedef {import('./SSVGEngine').default} SSVGEngine
 * @typedef {import('./SSVGProperty').default} SSVGProperty
 */
 
export default class SSVGState {
  /**
   * @param {SSVGEngine} engine
   * @param {Element} state
   */
  constructor(engine, state) {
    debug('loading state');
    /** @type {{ [name: string]: SSVGProperty }} */
    this.properties = {};
    /** @type {SSVGComputed[]} */
    this.computed = [];
    /** @type {ssvg.StateElement} */
    this.elt = /** @type {any} */ (state);
    this.svg = engine.svg;
    this.selection = engine.selection;
    /** @type {CustomEvent<{ state: ssvg.$State | null }>} */
    this._evt = new CustomEvent('state', { 'state': null });
 
    this.defaultTransition = new SSVGTransition();
    this.defaultTransition.setDefaults();
 
    this._parseState(state);
    // $FlowIgnore: we're extending it !
    this.elt.updateState = this.updateState.bind(this);
    // $FlowIgnore: we're extending it !
    this.elt.setState = this.setState.bind(this);
    // $FlowIgnore: we're extending it !
    this.elt.getState = this.getState.bind(this);
  }
 
  disconnect() {
    // @ts-ignore: unmounting
    delete this.elt.updateState;
 
    // @ts-ignore: unmounting
    delete this.elt.setState;
 
    // @ts-ignore: unmounting
    delete this.elt.getState;
 
    for (const key in this.properties) {
      Iif (!this.properties.hasOwnProperty(key)) {
        continue;
      }
      this.properties[key].disconnect();
    }
 
    for (const computed of this.computed) {
      computed.disconnect();
    }
  }
 
  /**
   * @param {Element} state
   */
  _parseState(state) {
    if (state.firstElementChild &&
        (state.firstElementChild.nodeName === 'transition')) {
      this.defaultTransition.parseTransition(state.firstElementChild);
    }
 
    for (let i = 0; i < state.children.length; ++i) {
      var children = state.children[i];
      if (children.tagName === 'property') {
        const prop = createProperty(this, children);
        this.properties[prop.name] = prop;
      }
      else if (children.tagName === 'computed') {
        const prop = createProperty(this, children);
        this.computed.push(new SSVGComputed(prop, children));
      }
    }
  }
 
  /**
   * @param {ssvg.$State} state
   */
  updateState(state) {
    /** @type {ssvg.$State} */
    var event = {};
 
    for (const key in this.properties) {
      Iif (!this.properties.hasOwnProperty(key)) {
        continue;
      }
      else if (state.hasOwnProperty(key)) {
        this.properties[key].value = state[key];
      }
      event[key] = this.properties[key].value;
    }
    this._updateComputed(event);
    this._emitEvent(event);
  }
 
  /**
   * @param {ssvg.$State} state
   */
  setState(state) {
    /** @type {ssvg.$State} */
    const event = {};
 
    for (const key in this.properties) {
      Iif (!this.properties.hasOwnProperty(key)) {
        continue;
      }
 
      this.properties[key].value = state[key];
      event[key] = this.properties[key].value;
    }
 
    this._updateComputed(event);
    this._emitEvent(event);
  }
 
  /**
   * @param {ssvg.$State} event
   */
  _updateComputed(event) {
    if (this.computed.length === 0) {
      return;
    }
    for (const computed of this.computed) {
      computed.compute(event);
      event[computed.property.name] = computed.property.value;
    }
  }
 
  /**
   * @param {ssvg.$State} state
   */
  _emitEvent(state) {
    this._evt.state = state;
    this.elt.dispatchEvent(this._evt);
  }
 
  /** @return {ssvg.$State} */
  getState() {
    /** @type {ssvg.$State} */
    var ret = {};
    for (const key in this.properties) {
      Iif (!this.properties.hasOwnProperty(key)) {
        continue;
      }
 
      ret[key] = this.properties[key].value;
    }
 
    for (const computed of this.computed) {
      ret[computed.property.name] = computed.property.value;
    }
    return ret;
  }
}