all files / model/ NodeIndex.js

50% Statements 11/22
0% Branches 0/2
42.86% Functions 6/14
50% Lines 11/22
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                                                                                          32×                                     725× 725×                 654× 654× 654×               1450×                                                              
import { forEach } from '../util'
 
/**
  Index for Nodes.
 
  Node indexes are first-class citizens in {@link model/data/Data}.
  I.e., they are updated after each operation, and before any other listener is notified.
 
  @abstract
 
 */
class NodeIndex {
 
  /**
    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 no-unused-vars
    throw new Error('This method is abstract.')
  }
 
  /**
    Called when a node has been created.
 
    @param {Node} node
   */
  create(node) { // eslint-disable-line no-unused-vars
    throw new Error('This method is abstract.')
  }
 
  /**
    Called when a node has been deleted.
 
    @param {model/data/Node} node
   */
  delete(node) { // eslint-disable-line no-unused-vars
    throw new Error('This method is abstract.')
  }
 
  set(node, path, newValue, oldValue) {
    this.update(node, path, newValue, oldValue)
  }
 
  /**
    Called when a property has been updated.
 
    @private
    @param {Node} node
   */
  update(node, path, newValue, oldValue) { // eslint-disable-line no-unused-vars
    throw new Error('This method is abstract.')
  }
 
  /**
    Reset the index using a Data instance.
 
    @private
   */
  reset(data) {
    this._clear()
    this._initialize(data)
  }
 
  /**
    Clone this index.
 
    @return A cloned NodeIndex.
   */
  clone() {
    var NodeIndexClass = this.constructor
    var clone = new NodeIndexClass()
    return clone
  }
 
  _clear() {
    throw new Error('This method is abstract')
  }
 
  _initialize(data) {
    forEach(data.getNodes(), function(node) {
      if (this.select(node)) {
        this.create(node)
      }
    }.bind(this))
  }
}
 
/**
  Create a new NodeIndex using the given prototype as mixin.
 
  @param {Object} prototype
  @returns {NodeIndex} A customized NodeIndex.
 */
NodeIndex.create = function(prototype) {
  var index = Object.assign(new NodeIndex(), prototype)
  index.clone = function() {
    return NodeIndex.create(prototype)
  }
  return index
}
 
/**
  Create a filter to filter nodes by type.
 
  @param {String} type
  @returns {function}
 */
NodeIndex.filterByType = function(type) {
  return function(node) {
    return node.isInstanceOf(type)
  }
}
 
export default NodeIndex