all files / model/data/ PropertyIndex.js

57.58% Statements 19/33
38.89% Branches 7/18
66.67% Functions 10/15
56.25% Lines 18/32
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                1067×   1067× 1067×                                                               6312×                       5297× 5297× 5297×   5297× 5297×                         175× 175× 175×   175× 175×                         420×                                   420×       1067×       1067×                  
import isArray from '../../util/isArray'
import forEach from '../../util/forEach'
import TreeIndex from '../../util/TreeIndex'
import NodeIndex from './NodeIndex'
 
class PropertyIndex extends NodeIndex {
 
  constructor(property) {
    super()
 
    this._property = property || 'id'
    this.index = new TreeIndex()
  }
 
  /**
    Get all indexed nodes for a given path.
 
    @param {Array<String>} path
    @returns A node or an object with ids and nodes as values.
   */
  get(path) {
    return this.index.get(path) || {}
  }
 
  /**
    Collects nodes recursively.
 
    @returns An object with ids as keys and nodes as values.
   */
  getAll(path) {
    return this.index.getAll(path)
  }
 
  /**
    Check if a node should be indexed.
 
    Used internally only. Override this in subclasses to achieve a custom behavior.
 
    @private
    @param {Node}
    @returns {Boolean} true if the given node should be added to the index.
   */
  select(node) { // eslint-disable-line
    return true
  }
 
  /**
    Called when a node has been created.
 
    Override this in subclasses for customization.
 
    @private
    @param {Node} node
   */
  create(node) {
    var values = node[this._property]
    Eif (!isArray(values)) {
      values = [values]
    }
    forEach(values, function(value) {
      this.index.set([value, node.id], node)
    }.bind(this))
  }
 
  /**
   * Called when a node has been deleted.
   *
   * Override this in subclasses for customization.
   *
   * @private
   * @param {model/data/Node} node
   */
  delete(node) {
    var values = node[this._property]
    Eif (!isArray(values)) {
      values = [values]
    }
    forEach(values, function(value) {
      this.index.delete([value, node.id])
    }.bind(this))
  }
 
  /**
    Called when a property has been updated.
 
    Override this in subclasses for customization.
 
    @private
    @param {Node} node
   */
  update(node, path, newValue, oldValue) {
    Eif (!this.select(node) || path[1] !== this._property) return
    var values = oldValue
    if (!isArray(values)) {
      values = [values]
    }
    forEach(values, function(value) {
      this.index.delete([value, node.id])
    }.bind(this))
    values = newValue
    if (!isArray(values)) {
      values = [values]
    }
    forEach(values, function(value) {
      this.index.set([value, node.id], node)
    }.bind(this))
  }
 
  set(node, path, newValue, oldValue) {
    this.update(node, path, newValue, oldValue)
  }
 
  _clear() {
    this.index.clear()
  }
 
  _initialize(data) {
    forEach(data.getNodes(), function(node) {
      if (this.select(node)) {
        this.create(node)
      }
    }.bind(this))
  }
}
 
 
export default PropertyIndex