capibara 1.3.68 → 1.3.71

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/util/utils.js CHANGED
@@ -85,7 +85,7 @@ export default {
85
85
  indexSelectedDay(list, props) {
86
86
  let index = -1;
87
87
  list.forEach((l, i) => {
88
- if (l.date == props.date && l.item.id == props.item.id) {
88
+ if (l.date == props.date && this.getNodeKey(l.item) == this.getNodeKey(props.item)) {
89
89
  index = i;
90
90
  }
91
91
  })
@@ -107,29 +107,42 @@ export default {
107
107
  * @param {item} item - Item a buscar en Array
108
108
  * @return {Integer} Posicion, default -1
109
109
  */
110
- getPositionByItem(config, item, typeNode = "child") {
111
- let position = -1;
112
- let finalPosition = -1;
113
-
114
- config.data.forEach(d => {
115
- if (d.isHidden) return;
116
- position++;
117
- if (d.id == item.id && typeNode == "parent") {
118
- finalPosition = position;
119
- }
120
- d.children.forEach(c => {
121
- if (c.isHidden) return;
122
- if (!d.isClosed) {
123
- position++;
124
- if (typeNode == "child" && c.id == item.id) {
125
- finalPosition = position;
126
- }
110
+ getNodeKey(item) {
111
+ return String((item && item.type) || "node") + ":" + String(item && item.id);
112
+ },
113
+
114
+ /**
115
+ * Lista canónica de filas del árbol. Mantiene la compatibilidad con los
116
+ * árboles parent -> child y permite consumidores con más profundidad.
117
+ */
118
+ flattenTree(data, options = {}) {
119
+ const rows = [];
120
+ const tabKey = options.tabKey;
121
+ const visibleOnly = options.visibleOnly !== false;
122
+
123
+ const isVisibleInTab = (item) => {
124
+ if (!tabKey || !item || !item.visibleInTabs) return true;
125
+ return item.visibleInTabs.indexOf(tabKey) !== -1;
126
+ };
127
+
128
+ const walk = (items, parent, ancestors, depth) => {
129
+ (items || []).forEach((item) => {
130
+ if (!item || item.isHidden || !isVisibleInTab(item)) return;
131
+ rows.push({ item, parent, ancestors, depth, key: this.getNodeKey(item) });
132
+ if (item.children && item.children.length && (!visibleOnly || !item.isClosed)) {
133
+ walk(item.children, item, ancestors.concat([item]), depth + 1);
127
134
  }
128
135
  });
136
+ };
129
137
 
130
- });
138
+ walk(data || [], null, [], 0);
139
+ return rows;
140
+ },
131
141
 
132
- return finalPosition;
142
+ getPositionByItem(config, item, typeNode = "child") {
143
+ const rows = this.flattenTree(config.data);
144
+ const key = this.getNodeKey(item);
145
+ return rows.findIndex((row) => row.key === key && (typeNode === "child" || row.depth === 0));
133
146
  },
134
147
 
135
148
 
@@ -147,27 +160,9 @@ export default {
147
160
  * @return {Object} Item encontrado
148
161
  */
149
162
  getItemByEvent(config, evt, $table) {
150
- let item = null;
151
- let actualPosition = 0;
152
163
  let pos = this.eventToPosition(config, evt, $table);
153
- let position = pos.y;
154
- config.data.forEach(d => {
155
- if (d.isHidden) return;
156
- if (actualPosition == position ) {
157
- item = d;
158
- }
159
- actualPosition++;
160
- if (!d.isClosed) {
161
- d.children.forEach(c => {
162
- if (c.isHidden) return;
163
- if (actualPosition == position ) {
164
- item = c;
165
- }
166
- actualPosition++;
167
- });
168
- }
169
- });
170
- return item;
164
+ const rows = this.flattenTree(config.data);
165
+ return rows[pos.y] ? rows[pos.y].item : null;
171
166
  },
172
167
 
173
168
 
@@ -197,20 +192,15 @@ export default {
197
192
  * @return {Object} item o {item, parent}
198
193
  */
199
194
  getItemById(config, id, typeNode = "child", withParent = false) {
200
- let item = null;
201
- let parent = null;
202
-
203
- config.data.forEach(d => {
204
- if (d.id == id && typeNode == "parent") {
205
- item = d;
206
- }
207
- d.children.forEach(c => {
208
- if (c.id == id && typeNode == "child") {
209
- item = c;
210
- parent = d;
211
- }
212
- });
195
+ const rows = this.flattenTree(config.data, { visibleOnly: false });
196
+ const isStructuralType = typeNode === "child" || typeNode === "parent";
197
+ let found = rows.find((row) => {
198
+ if (row.item.id != id) return false;
199
+ if (!isStructuralType) return row.item.type === typeNode;
200
+ return typeNode === "child" || row.depth === 0;
213
201
  });
202
+ let item = found ? found.item : null;
203
+ let parent = found ? found.parent : null;
214
204
 
215
205
  if (withParent) {
216
206
  return { item, parent }
@@ -476,13 +466,7 @@ export default {
476
466
  let diff_time = end.getTime() - start.getTime();
477
467
  let days = diff_time / dateUtils.dayInMilliseconds;
478
468
 
479
- config.data.forEach(d => {
480
- if (d.isHidden) return;
481
- rows++;
482
- if (d.children && !d.isClosed) {
483
- rows += d.children.filter(c => !c.isHidden).length;
484
- }
485
- });
469
+ rows = this.flattenTree(config.data).length;
486
470
 
487
471
  let height = rows * h;
488
472
  let width = days * w;
@@ -0,0 +1,29 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import utils from "../src/util/utils";
3
+
4
+ describe("árbol de Capibara", () => {
5
+ const valve = { id: 7, type: "valvula", label: "Válvula 7" };
6
+ const sector = { id: 7, type: "sector", label: "Sector 7", children: [valve] };
7
+ const equipo = { id: 1, type: "equipo", label: "Equipo", children: [sector] };
8
+ const config = { data: [equipo] };
9
+
10
+ it("distingue nodos de distinto tipo aunque compartan ID", () => {
11
+ expect(utils.getNodeKey(sector)).toBe("sector:7");
12
+ expect(utils.getNodeKey(valve)).toBe("valvula:7");
13
+ expect(utils.indexSelectedDay([{ date: "2026-09-14", item: sector }], { date: "2026-09-14", item: valve })).toBe(-1);
14
+ });
15
+
16
+ it("aplana todos los niveles y respeta el colapso", () => {
17
+ expect(utils.flattenTree(config.data).map((row) => row.key)).toEqual(["equipo:1", "sector:7", "valvula:7"]);
18
+ sector.isClosed = true;
19
+ expect(utils.flattenTree(config.data).map((row) => row.key)).toEqual(["equipo:1", "sector:7"]);
20
+ expect(utils.flattenTree(config.data, { visibleOnly: false }).map((row) => row.key)).toEqual(["equipo:1", "sector:7", "valvula:7"]);
21
+ });
22
+
23
+ it("encuentra una válvula anidada y conserva su padre inmediato", () => {
24
+ const found = utils.getItemById(config, 7, "valvula", true);
25
+ expect(found.item).toBe(valve);
26
+ expect(found.parent).toBe(sector);
27
+ expect(utils.getItemById(config, 7, "sector")).toBe(sector);
28
+ });
29
+ });