all files / model/ NodeSelection.js

57.69% Statements 30/52
31.03% Branches 9/29
56.25% Functions 9/16
57.69% Lines 30/52
1 statement, 4 branches Ignored     
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              63×   63× 63× 63× 63× 63× 63× 63×     63×     63×     63×   63× 63× 63× 63× 63×   63× 63×                       216×               93×       44×       33×       20×       10×       24×                                                                                                         22×                      
import { isString } from '../util'
import Selection from './Selection'
import Coordinate from './Coordinate'
 
class NodeSelection extends Selection {
 
  constructor(containerId, nodeId, mode, reverse, surfaceId) {
    super()
 
    Eif (arguments.length === 1) {
      let data = arguments[0]
      containerId = data.containerId
      nodeId = data.nodeId
      mode = data.mode
      reverse = data.reverse
      surfaceId = data.surfaceId
    }
 
    Iif (!isString(containerId)) {
      throw new Error("'containerId' is mandatory.");
    }
    Iif (!isString(nodeId)) {
      throw new Error("'nodeId' is mandatory.");
    }
    mode = mode || "full"
 
    this.containerId = containerId;
    this.nodeId = nodeId;
    this.mode = mode;
    this.reverse = Boolean(reverse);
    this.surfaceId = surfaceId;
 
    this.start = new Coordinate([nodeId], 0)
    this.end = new Coordinate([nodeId], 1)
  }
 
  equals(other) {
    return (
      super.equals(other) &&
      this.nodeId === other.nodeId &&
      this.mode === other.mode
    )
  }
 
  isNodeSelection() {
    return true;
  }
 
  getType() {
    return 'node';
  }
 
  getNodeId() {
    return this.nodeId;
  }
 
  isFull() {
    return this.mode === 'full';
  }
 
  isBefore() {
    return this.mode === 'before';
  }
 
  isAfter() {
    return this.mode === 'after';
  }
 
  isCollapsed() {
    return this.mode !== 'full';
  }
 
  toJSON() {
    return {
      type: 'node',
      nodeId: this.nodeId,
      mode: this.mode,
      reverse: this.reverse,
      containerId: this.containerId,
      surfaceId: this.surfaceId
    };
  }
 
  toString() {
    /* istanbul ignore next */
    return [
      "NodeSelection(",
      this.containerId, ".", this.nodeId, ", ",
      this.mode, ", ",
      (this.reverse?", reverse":""),
      (this.surfaceId?(", "+this.surfaceId):""),
      ")"
    ].join('');
  }
 
  collapse(direction) {
    if (direction === 'left') {
      if (this.isBefore()) {
        return this;
      } else {
        return new NodeSelection(this.containerId, this.nodeId, 'before', this.reverse, this.surfaceId);
      }
    } else if (direction === 'right') {
      if (this.isAfter()) {
        return this;
      } else {
        return new NodeSelection(this.containerId, this.nodeId, 'after', this.reverse, this.surfaceId);
      }
    } else {
      throw new Error("'direction' must be either 'left' or 'right'");
    }
  }
 
  _getCoordinate() {
    if (this.mode === 'before') {
      return new Coordinate([this.nodeId], 0);
    } else if (this.mode === 'after') {
      return new Coordinate([this.nodeId], 1);
    }
  }
 
  _clone() {
    return new NodeSelection(this);
  }
}
 
NodeSelection.prototype._isNodeSelection = true
 
NodeSelection.fromJSON = function(json) {
  return new NodeSelection(json);
}
 
// TODO: is this used?
NodeSelection._createFromCoordinate = function(coor) {
  var containerId = coor.containerId;
  var nodeId = coor.getNodeId();
  var mode = coor.offset === 0 ? 'before' : 'after';
  return new NodeSelection(containerId, nodeId, mode, false);
};
 
export default NodeSelection