all files / model/ AnchorIndex.js

0% Statements 0/32
0% Branches 0/8
0% Functions 0/8
0% Lines 0/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                                                                                                                                         
import { filter, TreeIndex } from '../util'
import DocumentIndex from './DocumentIndex'
 
class AnchorIndex extends DocumentIndex {
 
  constructor(doc) {
    super()
 
    this.doc = doc
    this.byPath = new TreeIndex.Arrays()
    this.byId = {}
  }
 
  select(node) {
    return (node._isContainerAnnotation)
  }
 
  reset(data) {
    this.byPath.clear()
    this.byId = {}
    this._initialize(data)
  }
 
  get(path, containerName) {
    var anchors = this.byPath.getAll(path)
    if (containerName) {
      return filter(anchors, function(anchor) {
        return (anchor.containerId === containerName)
      })
    } else {
      // return a copy of the array
      return anchors.slice(0)
    }
  }
 
  create(containerAnno) {
    var startAnchor = containerAnno.getStartAnchor()
    var endAnchor = containerAnno.getEndAnchor()
    this.byPath.add(startAnchor.path, startAnchor)
    this.byPath.add(endAnchor.path, endAnchor)
    this.byId[containerAnno.id] = containerAnno
  }
 
  delete(containerAnno) {
    var startAnchor = containerAnno.getStartAnchor()
    var endAnchor = containerAnno.getEndAnchor()
    this.byPath.remove(startAnchor.path, startAnchor)
    this.byPath.remove(endAnchor.path, endAnchor)
    delete this.byId[containerAnno.id]
  }
 
  update(node, path, newValue, oldValue) {
    if (this.select(node)) {
      var anchor = null
      if (path[1] === 'startPath') {
        anchor = node.getStartAnchor()
      } else if (path[1] === 'endPath') {
        anchor = node.getEndAnchor()
      } else {
        return
      }
      this.byPath.remove(oldValue, anchor)
      this.byPath.add(anchor.path, anchor)
    }
  }
}
 
export default AnchorIndex