-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy patharrow.js
394 lines (338 loc) · 14.5 KB
/
arrow.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/**
* timeline-arrows
* https://github.com/javdome/timeline-arrows
*
* Class to easily draw lines to connect items in the vis Timeline module.
*
* @version 4.6.0
* @date 2024-03-24
*
* @copyright (c) Javi Domenech (javdome@gmail.com)
*
*
* @license
* timeline-arrows is dual licensed under both
*
* 1. The Apache 2.0 License
* http://www.apache.org/licenses/LICENSE-2.0
*
* and
*
* 2. The MIT License
* http://opensource.org/licenses/MIT
*
* timeline-arrows may be distributed under either license.
*/
// @ts-check
/**
* @typedef {(number | string)} VisIdType Timeline view item id. Equivalent to vis.IdType.
*/
/**
* @typedef {(number | string)} ArrowIdType arrow id.
*/
/**
* @typedef ArrowSpec Arrow specification
* @property {ArrowIdType} id arrow id
* @property {VisIdType} id_item_1 start timeline item id
* @property {VisIdType} id_item_2 end timeline item id
* @property {string} [title] optional arrow title
*/
/**
* @typedef ArrowOptions Arrow configuration options
* @property {boolean} [followRelationships] if true, arrows can point backwards and will follow the relationships set in the data
* @property {(el: SVGPathElement, title: string) => string } [tooltipConfig] if arrows have a `title` property, the default behavior will add a title attribute that shows on hover. However, you might not want to use the title attribute, but instead your own tooltip configuration.
This method takes two arguments, `el` - the arrow - and `title` - the content of the `title` property set in the arrow data.
* @property {string} [color] arrow color
* @property {number} [strokeWidth] arrow thickness in pixels
* @property {boolean} [hideWhenItemsNotVisible] if true, arrows will be hidden when both items is not visible due to timeline zoom.
*/
/** Arrow set for a vis.js Timeline. */
export default class Arrow {
/**
* Creates arrows.
* @param {*} timeline timeline object
* @param {ArrowSpec[]} dependencies arrows
* @param {ArrowOptions} [options]
*/
constructor(timeline, dependencies, options) {
this._svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
this._timeline = timeline;
/** @private @type {boolean | undefined} if true, arrows can point backwards and will follow the relationships set in the data */
this._followRelationships = options?.followRelationships;
/** @private @type {((el: SVGPathElement, title: string) => string) | undefined } */
this._tooltipConfig = options?.tooltipConfig;
/** @private @type {string} color */
this._arrowsColor = options?.color ? options.color : "#9c0000"
/** @private @type {number} arrow thickness in pixels */
this._arrowsStrokeWidth = options?.strokeWidth ?? 3;
/** @private @type {boolean} if true, arrows will be hidden when both items is not visible due to timeline zoom */
this._hideWhenItemsNotVisible = options?.hideWhenItemsNotVisible ?? true;
/** @private @type {SVGMarkerElement} */
this._arrowHead = document.createElementNS(
"http://www.w3.org/2000/svg",
"marker"
);
/** @private @type {SVGPathElement} */
this._arrowHeadPath = document.createElementNS(
"http://www.w3.org/2000/svg",
"path"
);
this._dependency = [...dependencies];
/** @private @type {SVGPathElement[]} */
this._dependencyPath = [];
/** @private @type {string} */
this._arrowHeadId = `arrowhead-${Math.random().toString(36).substring(2)}`;
this._initialize();
}
_initialize() {
//Configures the SVG layer and add it to timeline
this._svg.style.position = "absolute";
this._svg.style.top = "0px";
this._svg.style.height = "100%";
this._svg.style.width = "100%";
this._svg.style.display = "block";
this._svg.style.zIndex = "1"; // Should it be above or below? (1 for above, -1 for below)
this._svg.style.pointerEvents = "none"; // To click through, if we decide to put it above other elements.
this._timeline.dom.center.appendChild(this._svg);
//Configure the arrowHead
this._arrowHead.setAttribute("id", this._arrowHeadId);
this._arrowHead.setAttribute("viewBox", "-10 -5 10 10");
this._arrowHead.setAttribute("refX", "-7");
this._arrowHead.setAttribute("refY", "0");
this._arrowHead.setAttribute("markerUnits", "strokeWidth");
this._arrowHead.setAttribute("markerWidth", "3");
this._arrowHead.setAttribute("markerHeight", "3");
this._arrowHead.setAttribute("orient", "auto-start-reverse");
//Configure the path of the arrowHead (arrowHeadPath)
this._arrowHeadPath.setAttribute("d", "M 0 0 L -10 -5 L -7.5 0 L -10 5 z");
this._arrowHeadPath.style.fill = this._arrowsColor;
this._arrowHead.appendChild(this._arrowHeadPath);
this._svg.appendChild(this._arrowHead);
//Create paths for the started dependency array
for (let i = 0; i < this._dependency.length; i++) {
this._createPath();
}
//NOTE: We hijack the on "changed" event to draw the arrows.
this._timeline.on("changed", () => {
this._drawDependencies();
});
}
/** @private */
_createPath(){
//Add a new path to array dependencyPath and to svg
let somePath = document.createElementNS(
"http://www.w3.org/2000/svg",
"path"
);
somePath.setAttribute("d", "M 0 0");
somePath.style.stroke = this._arrowsColor;
somePath.style.strokeWidth = this._arrowsStrokeWidth + "px";
somePath.style.fill = "none";
somePath.style.pointerEvents = "auto";
this._dependencyPath.push(somePath);
this._svg.appendChild(somePath);
}
/** @private */
_drawDependencies() {
//Create paths for the started dependency array
for (let i = 0; i < this._dependency.length; i++) {
this._drawArrows(this._dependency[i], i);
}
}
/**
* @private
* @param {ArrowSpec} dep arrow specification
* @param {number} index arrow index
*/
_drawArrows(dep, index) {
//Checks if both items exist
//if( (typeof this._timeline.itemsData._data[dep.id_item_1] !== "undefined") && (typeof this._timeline.itemsData._data[dep.id_item_2] !== "undefined") ) {
const bothItemsExist = (this._timeline.itemsData.get(dep.id_item_1) !== null) && (this._timeline.itemsData.get(dep.id_item_2) !== null);
//Checks if at least one item is visible in screen
let oneItemVisible = false; //Iniciamos a false
//Checks if the groups of items are both visible
let groupOf_1_isVisible = false; //Iniciamos a false
let groupOf_2_isVisible = false; //Iniciamos a false
if (bothItemsExist) {
const visibleItems = this._timeline.getVisibleItems();
for (let k = 0; k < visibleItems.length ; k++) {
if (dep.id_item_1 == visibleItems[k]) oneItemVisible = true;
if (dep.id_item_2 == visibleItems[k]) oneItemVisible = true;
}
let groupOf_1 = this._timeline.itemsData.get(dep.id_item_1).group; //let groupOf_1 = items.get(dep.id_item_1).group;
let groupOf_2 = this._timeline.itemsData.get(dep.id_item_2).group; //let groupOf_2 = items.get(dep.id_item_2).group;
if ( this._timeline.groupsData.get(groupOf_1) ) groupOf_1_isVisible = true;
if ( this._timeline.groupsData.get(groupOf_2) ) groupOf_2_isVisible = true;
// If groups are null then they are not visible.
if (groupOf_1 == null){
groupOf_1_isVisible = false;
}
if (groupOf_2 == null){
groupOf_2_isVisible = false;
}
}
if ( (groupOf_1_isVisible && groupOf_2_isVisible) && (oneItemVisible || !this._hideWhenItemsNotVisible) && (bothItemsExist)) {
var item_1 = this._getItemPos(this._timeline.itemSet.items[dep.id_item_1]);
var item_2 = this._getItemPos(this._timeline.itemSet.items[dep.id_item_2]);
if (!this._followRelationships && item_2.mid_x < item_1.mid_x) {
[item_1, item_2] = [item_2, item_1]; // As demo, we put an arrow between item 0 and item1, from the one that is more on left to the one more on right.
}
var curveLen = item_1.height * 2; // Length of straight Bezier segment out of the item.
if (this._followRelationships && item_2.mid_x < item_1.mid_x) {
item_2.right += 10; // Space for the arrowhead.
this._dependencyPath[index].setAttribute("marker-start", `url(#${this._arrowHeadId})`);
this._dependencyPath[index].setAttribute("marker-end", "");
this._dependencyPath[index].setAttribute(
"d",
"M " +
item_2.right +
" " +
item_2.mid_y +
" C " +
(item_2.right + curveLen) +
" " +
item_2.mid_y +
" " +
(item_1.left - curveLen) +
" " +
item_1.mid_y +
" " +
item_1.left +
" " +
item_1.mid_y
);
} else {
item_2.left -= 10; // Space for the arrowhead.
this._dependencyPath[index].setAttribute("marker-end", `url(#${this._arrowHeadId})`);
this._dependencyPath[index].setAttribute("marker-start", "");
this._dependencyPath[index].setAttribute(
"d",
"M " +
item_1.right +
" " +
item_1.mid_y +
" C " +
(item_1.right + curveLen) +
" " +
item_1.mid_y +
" " +
(item_2.left - curveLen) +
" " +
item_2.mid_y +
" " +
item_2.left +
" " +
item_2.mid_y
);
}
// Adding the title if property title has been added in the dependency
if (dep.hasOwnProperty("title")) {
this._tooltipConfig
? this._tooltipConfig(this._dependencyPath[index], dep.title ?? '')
: this._dependencyPath[index].innerHTML = "<title>" + dep.title + "</title>";
}
} else {
this._dependencyPath[index].setAttribute("marker-end", "");
this._dependencyPath[index].setAttribute("d", "M 0 0");
}
}
/** @private Función que recibe in Item y devuelve la posición en pantalla del item. */
_getItemPos (item) {
let left_x = item.left;
let top_y;
if (this._timeline.options.orientation.item == "top") {
top_y = item.parent.top + item.top;
} else {
top_y = item.parent.top + item.parent.height - item.top - item.height;
}
return {
left: left_x,
top: top_y,
right: left_x + item.width,
bottom: top_y + item.height,
mid_x: left_x + item.width / 2,
mid_y: top_y + item.height / 2,
width: item.width,
height: item.height
}
}
/**
* Adds arrow between two timeline items.
* @param {ArrowSpec} dep item dependency
*/
addArrow(dep) {
this._dependency.push(dep);
this._createPath();
this._timeline.redraw();
}
/**
* Get arrow by ID.
* @param {ArrowIdType} id arrow ID
* @returns {ArrowSpec | null} arrow spec, or null
*/
getArrow(id) {
return this._dependency.find(dep => dep.id === id) ?? null;
}
/**
* Get all Id arrows.
*
* @return {(ArrowIdType)[]} list of id arrows
*/
getIdArrows() {
return this._dependency.map(dep => dep.id);
}
/**
* Finds arrow with the given id and removes it.
* Función que recibe el id de una flecha y la elimina.
* @param {ArrowIdType} id arrow id
*/
removeArrow(id) {
const index = this._dependency.findIndex(dep => dep.id === id);
if (index >= 0) {
//var list = document.getElementsByTagName("path"); //FALTA QUE ESTA SELECCION LA HAGA PARA EL DOM DEL TIMELINE INSTANCIADO!!!!
const list = document.querySelectorAll("#" + this._timeline.dom.container.id + " path");
this._dependency.splice(index, 1); //Elimino del array dependency
this._dependencyPath.splice(index, 1); //Elimino del array dependencyPath
list[index + 1].parentNode?.removeChild(list[index + 1]); //Lo elimino del dom
}
}
/**
* Finds all arrows related to one view item and removes them all.
* Función que recibe el id de un item y elimina la flecha.
* @param {VisIdType} id view item id
* @returns {(ArrowIdType)[]} list of removed arrow ids
*/
removeItemArrows(id) {
let listOfRemovedArrows = [];
for (let i = 0; i < this._dependency.length; i++) {
if ( (this._dependency[i].id_item_1 == id) || (this._dependency[i].id_item_2 == id) ) {
listOfRemovedArrows.push(this._dependency[i].id);
this.removeArrow(this._dependency[i].id);
i--;
}
}
return listOfRemovedArrows;
}
/**
* Removes the arrows between item 1 and item 2.
* @param {VisIdType} itemId1 item id
* @param {VisIdType} itemId2 item id
* @returns {(ArrowIdType)[]} id of the removed arrow
*/
removeArrowsBetweenItems(itemId1, itemId2) {
let listOfRemovedArrows = [];
let ArrowsToDelete = this._dependency.filter(dep => (dep.id_item_1 == itemId1 && dep.id_item_2 == itemId2) )
ArrowsToDelete.forEach(dep => {
listOfRemovedArrows.push(dep.id);
this.removeArrow(dep.id)
})
return listOfRemovedArrows
}
/**
* For backward compatibility
* @deprecated use the removeItemArrows method instead.
*/
removeArrowbyItemId(id) {
this.removeItemArrows(id);
}
}