Projekt

Obecné

Profil

Stáhnout (3.5 KB) Statistiky
| Větev: | Revize:
1
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
2

    
3
function findParent(_ref, cb) {
4
  var parentPath = _ref.parentPath;
5

    
6
  if (parentPath == null) {
7
    throw new Error("node is root");
8
  }
9

    
10
  var currentPath = parentPath;
11

    
12
  while (cb(currentPath) !== false) {
13
    // Hit the root node, stop
14
    // $FlowIgnore
15
    if (currentPath.parentPath == null) {
16
      return null;
17
    } // $FlowIgnore
18

    
19

    
20
    currentPath = currentPath.parentPath;
21
  }
22

    
23
  return currentPath.node;
24
}
25

    
26
function insertBefore(context, newNode) {
27
  return insert(context, newNode);
28
}
29

    
30
function insertAfter(context, newNode) {
31
  return insert(context, newNode, 1);
32
}
33

    
34
function insert(_ref2, newNode) {
35
  var node = _ref2.node,
36
      inList = _ref2.inList,
37
      parentPath = _ref2.parentPath,
38
      parentKey = _ref2.parentKey;
39
  var indexOffset = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
40

    
41
  if (!inList) {
42
    throw new Error('inList' + " error: " + ("insert can only be used for nodes that are within lists" || "unknown"));
43
  }
44

    
45
  if (!(parentPath != null)) {
46
    throw new Error('parentPath != null' + " error: " + ("Can not remove root node" || "unknown"));
47
  }
48

    
49
  // $FlowIgnore
50
  var parentList = parentPath.node[parentKey];
51
  var indexInList = parentList.findIndex(function (n) {
52
    return n === node;
53
  });
54
  parentList.splice(indexInList + indexOffset, 0, newNode);
55
}
56

    
57
function remove(_ref3) {
58
  var node = _ref3.node,
59
      parentKey = _ref3.parentKey,
60
      parentPath = _ref3.parentPath;
61

    
62
  if (!(parentPath != null)) {
63
    throw new Error('parentPath != null' + " error: " + ("Can not remove root node" || "unknown"));
64
  }
65

    
66
  // $FlowIgnore
67
  var parentNode = parentPath.node; // $FlowIgnore
68

    
69
  var parentProperty = parentNode[parentKey];
70

    
71
  if (Array.isArray(parentProperty)) {
72
    // $FlowIgnore
73
    parentNode[parentKey] = parentProperty.filter(function (n) {
74
      return n !== node;
75
    });
76
  } else {
77
    // $FlowIgnore
78
    delete parentNode[parentKey];
79
  }
80

    
81
  node._deleted = true;
82
}
83

    
84
function stop(context) {
85
  context.shouldStop = true;
86
}
87

    
88
function replaceWith(context, newNode) {
89
  // $FlowIgnore
90
  var parentNode = context.parentPath.node; // $FlowIgnore
91

    
92
  var parentProperty = parentNode[context.parentKey];
93

    
94
  if (Array.isArray(parentProperty)) {
95
    var indexInList = parentProperty.findIndex(function (n) {
96
      return n === context.node;
97
    });
98
    parentProperty.splice(indexInList, 1, newNode);
99
  } else {
100
    // $FlowIgnore
101
    parentNode[context.parentKey] = newNode;
102
  }
103

    
104
  context.node._deleted = true;
105
  context.node = newNode;
106
} // bind the context to the first argument of node operations
107

    
108

    
109
function bindNodeOperations(operations, context) {
110
  var keys = Object.keys(operations);
111
  var boundOperations = {};
112
  keys.forEach(function (key) {
113
    boundOperations[key] = operations[key].bind(null, context);
114
  });
115
  return boundOperations;
116
}
117

    
118
function createPathOperations(context) {
119
  // $FlowIgnore
120
  return bindNodeOperations({
121
    findParent: findParent,
122
    replaceWith: replaceWith,
123
    remove: remove,
124
    insertBefore: insertBefore,
125
    insertAfter: insertAfter,
126
    stop: stop
127
  }, context);
128
}
129

    
130
export function createPath(context) {
131
  var path = _extends({}, context); // $FlowIgnore
132

    
133

    
134
  Object.assign(path, createPathOperations(path)); // $FlowIgnore
135

    
136
  return path;
137
}
(5-5/9)