-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactions.js
278 lines (257 loc) · 10.9 KB
/
actions.js
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// получить селектор из связи
// get selector by selected
// .getSelector(selected/selector: Document) => String|undefined
Shuttler.Selection.prototype.getSelector = function(selected) {
if (this.isSelected(selected))
return selected[this._options.selectedField].selector;
else if (this.isSelector(selected))
return selected._id;
else return undefined;
};
// найти все пути от сорса/таргета
// find all paths from source/target
// .findPathsBy(directions: 'sources'/'targets', object: Document, handler: .call(this, path: Document, direction: 'source'/'target'/'link', graph: Mongo.Collection)) => this
Shuttler.Selection.prototype.eachPathsBy = function(directions, object, handler) {
var selection = this;
lodash.each(this._paths, function(_path) {
lodash.each(_path.directions[directions], function(direction) {
if (
direction == 'link' // object can be a path
&& object.Ref().collection == _path.graph._ref // object in path collection
) {
handler.call(this, object, direction, _path.graph);
} else { // find path as path.(source/target)
_path.graph.find(object.Ref('_'+direction)).forEach(function(path) {
handler.call(selection, path, direction, _path.graph);
});
}
});
});
return this;
};
// найти все выделенные от сорса/таргета
// find all selected from source/target
// .eachSelectedBy(side: 'source'/'target', object: Document, handler: .call(this, selected: Document)) => this
Shuttler.Selection.prototype.eachSelectedBy = function(side, object, handler) {
var selection = this;
selection._selects.find(
lodash.merge(
object.Ref('_'+selection._sides[side]),
this._options.selectedQuery.call(this)
)
).forEach(function(selected) {
handler.call(selection, selected);
});
return this;
};
// найти все селекторы от сорса/таргета
// find all selectors from source/target
// .eachSelectorBy(side: 'source'/'target', object: Document, handler: .call(this, selector: Document)) => this
Shuttler.Selection.prototype.eachSelectorBy = function(side, object, handler) {
var selection = this;
selection._selects.find(
lodash.merge(
object.Ref('_'+selection._sides[side]),
this._options.selectorQuery.call(this)
)
).forEach(function(selector) {
handler.call(selection, selector);
});
return this;
};
// найти все сорсы/таргеты пути (согласно directions)
// find all sources/targets of path
// .eachDirectionsOfPath(directions: 'sources'/'targets', path: Document, handler: .call(this, object: Document, path: Document, graph: Mongo.Collection)) => this
Shuttler.Selection.prototype.eachDirectionsOfPath = function(directions, path, handler) {
var selection = this;
lodash.each(this._paths[path.Ref().collection].directions[directions], function(direction) {
handler.call(selection, Shuttler.getDocumentByDirection(direction, path), path, selection._paths[path.Ref().collection]);
});
return this;
};
// выделить от правила
// insert first selected to selector
// .selectBySelector(selector: Document) => this
Shuttler.Selection.prototype.selectBySelector = function(selector) {
if (this._selects.checkRestrictions('selection', selector[this._sides.target](), selector, undefined, undefined, selector._id, this)) {
this._selects.insert({
['_'+this._sides.target]: selector['_'+this._sides.target],
['_'+this._sides.source]: selector['_'+this._sides.source],
[this._options.selectedField]: { selector: selector._id }
});
}
return this;
};
// выделить от выделения
// select from selected
// .selectBySelected(selected: Document) => this
Shuttler.Selection.prototype.selectBySelected = function(selected) {
var selection = this;
this.selectFromSourceBySelected(selected[this._sides.target](), selected);
return this;
};
// выделить от сорса по всем путям конкретным выделением
// find all paths from source and insert to each path.targets selected
// .selectFromSourceBySelected(source: Document, selected: Document) => this
Shuttler.Selection.prototype.selectFromSourceBySelected = function(source, selected) {
var selection = this;
this.eachPathsBy('sources', source, function(path, pathDirection, graph) {
lodash.each(this._paths[graph._ref].directions.targets, function(direction) {
selection.selectTargetInPathBySelected(
Shuttler.getDocumentByDirection(direction, path),
path, Shuttler.getRefByDirection(pathDirection, path),
selected
);
});
});
return this;
};
// выделить от сорса по всем путям всеми доступными выделениями
// find all paths from source and insert to each path.targets each source selection
// .selectFromSource(source: Document) => this
Shuttler.Selection.prototype.selectFromSource = function(source) {
this.eachSelectedBy(this._sides.target, source, function(selected) {
this.selectFromSourceBySelected(source, selected);
});
return this;
};
// выделить по конкретному пути конкретным выделением
// find all path.targets and insert to it selected
// .selectInPathBySelected(path: Document, from: Document|Ref, selected: Document) => this
Shuttler.Selection.prototype.selectInPathBySelected = function(path, from, selected) {
var selection = this;
var _path = this._paths[path.Ref().collection];
this.eachDirectionsOfPath('targets', path, function(target) {
selection.selectTargetInPathBySelected(target, path, from, selected);
});
return this;
};
// выделить всё по конкретному пути всеми доступными выделениями
// find all path.targets and insert to it each path.sources selection
// .selectInPath(path: Document) => this
Shuttler.Selection.prototype.selectInPath = function(path) {
var selection = this;
this.eachDirectionsOfPath('targets', path, function(target) {
this.selectTargetInPath(target, path);
});
return this;
};
// выделить таргет по конкретному пути конкретным выделением
// insert selected to target
// .selectTargetInPathBySelected(target: Document, path: Document, from: Document|Ref, selected: Document) => this
Shuttler.Selection.prototype.selectTargetInPathBySelected = function(target, path, from, selected) {
var from = Shuttler.Ref.new(from);
var selection = this;
var selector = selection.getSelector(selected);
if (this._selects.checkRestrictions('selection', target, selected, from, path, selector, this)) {
this._selects.insert({
['_'+this._sides.target]: target.Ref(),
['_'+this._sides.source]: selected['_'+this._sides.source],
[this._options.selectedField]: {
from: from,
path: path.Ref(),
prev: selected._id,
selector: selector
}
});
}
return this;
};
// выделить таргет по конкретному пути всеми доступными выделениями
// insert each path.sources selection to target
// .selectTargetInPath(target: Document, path: Document) => this
Shuttler.Selection.prototype.selectTargetInPath = function(target, path) {
var selection = this;
this.eachDirectionsOfPath('sources', path, function(source) {
this.eachSelectedBy('target', source, function(selected) {
this.selectTargetInPathBySelected(target, path, source, selected);
});
});
return this;
};
// выделить таргет по всем путям всеми доступными выделениями
// выделить таргет от селекторов на нём
// find all paths to target and insert to target each path.sources selection
// find all selectors on target and insert selected
// .selectTarget(target: Document) => this
Shuttler.Selection.prototype.selectTarget = function(target) {
var selection = this;
this.eachPathsBy('targets', target, function(path) {
this.selectTargetInPath(target, path);
});
this.eachSelectorBy('target', target, function(selector) {
this.selectBySelector(selector);
});
return this;
};
// сдвинуть все селекты определенного селектора определённой стороной
// reselectBySelector(side: 'source'|'target', selector: Document) => this
Shuttler.Selection.prototype.reselectBySelector = function(side, selector) {
var selection = this;
this._selects.update({ [this._options.selectedField+'.selector']: selector._id }, {
$set: selector[selection._sides[side]]().Ref('_'+selection._sides[side])
}, { multi: true });
return this;
};
// убрать от правила
// remove first selected from selector
// .unselectBySelector(selector: Document) => this
Shuttler.Selection.prototype.unselectBySelector = function(selector) {
this._selects.remove({
[this._options.selectedField+'.selector']: selector._id,
[this._options.selectedField+'.prev']: { $exists: false },
[this._options.selectedField+'.path']: { $exists: false },
[this._options.selectedField+'.from']: { $exists: false },
'_source.id': selector._source.id, '_source.collection': selector._source.collection,
'_target.id': selector._target.id, '_target.collection': selector._target.collection
});
return this;
};
// убрать всех кто есть на этом пути
// clear path from any selected
// .unselectInPath(path: Document|Ref) => this
Shuttler.Selection.prototype.unselectInPath = function(path) {
var selection = this;
var path = Shuttler.Ref.soft(path);
var pathRef = path.Ref(this._options.selectedField+'.path');
var query = pathRef;
query.$or = [];
lodash.each(this._paths[pathRef[this._options.selectedField+'.path.collection']].directions.sources, function(source) {
query.$or.push({
[selection._options.selectedField+'.from.id']: path['_'+source].id,
[selection._options.selectedField+'.from.collection']: path['_'+source].collection
});
});
this._selects.remove(query);
return this;
};
// убрать всех от выделения
// remove all with .prev: selected._id
// .unselectByPrevSelected(selected: Document) => this
Shuttler.Selection.prototype.unselectByPrevSelected = function(selected) {
var selection = this;
this._selects.remove({
[this._options.selectedField+'.selector']: selected[this._options.selectedField].selector,
[this._options.selectedField+'.prev']: selected._id
});
return this;
};
// убрать все выделения
// clear target from any selected
// .unselectTarget(target: Document) => this
Shuttler.Selection.prototype.unselectTarget = function(target) {
this.eachSelectedBy('target', target, function(selected) {
this._selects.remove(selected._id);
});
return this;
};
// проверка на соответствие условиям выделения
// sheck with query
// .isSelected(selected: Document) => Boolean
Shuttler.Selection.prototype.isSelected = function(selected) {
return this._options.isSelected(selected);
};
// .isSelector(selector: Document) => Boolean
Shuttler.Selection.prototype.isSelector = function(selector) {
return this._options.isSelector(selector);
};