web-mojo 2.1.936 → 2.1.954
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/dist/admin.cjs.js +1 -1
- package/dist/admin.cjs.js.map +1 -1
- package/dist/admin.es.js +122 -229
- package/dist/admin.es.js.map +1 -1
- package/dist/auth.cjs.js +1 -1
- package/dist/auth.cjs.js.map +1 -1
- package/dist/auth.es.js +7 -7
- package/dist/auth.es.js.map +1 -1
- package/dist/charts.cjs.js +1 -1
- package/dist/charts.es.js +7 -7
- package/dist/chunks/ChatView-BKa78eKF.js +2 -0
- package/dist/chunks/ChatView-BKa78eKF.js.map +1 -0
- package/dist/chunks/{ChatView-DlSxjxah.js → ChatView-Dcz7LkwL.js} +89 -574
- package/dist/chunks/ChatView-Dcz7LkwL.js.map +1 -0
- package/dist/chunks/Collection-OP7c9Zyu.js +989 -0
- package/dist/chunks/Collection-OP7c9Zyu.js.map +1 -0
- package/dist/chunks/Collection-aQF5eOUH.js +2 -0
- package/dist/chunks/Collection-aQF5eOUH.js.map +1 -0
- package/dist/chunks/ContextMenu-DV_wsr9B.js +3 -0
- package/dist/chunks/ContextMenu-DV_wsr9B.js.map +1 -0
- package/dist/chunks/ContextMenu-ixMyAUGS.js +1171 -0
- package/dist/chunks/ContextMenu-ixMyAUGS.js.map +1 -0
- package/dist/chunks/DataView-CdDY9ijM.js +2 -0
- package/dist/chunks/{DataView-XJbTQ5q0.js.map → DataView-CdDY9ijM.js.map} +1 -1
- package/dist/chunks/{DataView-Vmjx4eCr.js → DataView-OUqaLmGB.js} +2 -2
- package/dist/chunks/{DataView-Vmjx4eCr.js.map → DataView-OUqaLmGB.js.map} +1 -1
- package/dist/chunks/{Dialog-D_rAf4gQ.js → Dialog-C2mRUxga.js} +8 -6
- package/dist/chunks/{Dialog-D_rAf4gQ.js.map → Dialog-C2mRUxga.js.map} +1 -1
- package/dist/chunks/Dialog-Cl6MN8if.js +2 -0
- package/dist/chunks/{Dialog-BinTQTfO.js.map → Dialog-Cl6MN8if.js.map} +1 -1
- package/dist/chunks/FormView-BSWaXDav.js +3 -0
- package/dist/chunks/{FormView-TPFsq8ZX.js.map → FormView-BSWaXDav.js.map} +1 -1
- package/dist/chunks/{FormView-CIriLDZY.js → FormView-HWvIdFkB.js} +10 -6
- package/dist/chunks/FormView-HWvIdFkB.js.map +1 -0
- package/dist/chunks/ListView-Dcz0Gs6D.js +492 -0
- package/dist/chunks/ListView-Dcz0Gs6D.js.map +1 -0
- package/dist/chunks/ListView-eXgn0F0-.js +2 -0
- package/dist/chunks/ListView-eXgn0F0-.js.map +1 -0
- package/dist/chunks/MetricsMiniChartWidget-BkTEO87S.js +2 -0
- package/dist/chunks/{MetricsMiniChartWidget-sONcM0pG.js.map → MetricsMiniChartWidget-BkTEO87S.js.map} +1 -1
- package/dist/chunks/{MetricsMiniChartWidget-BolRZ-Ja.js → MetricsMiniChartWidget-Y70IHFIe.js} +3 -3
- package/dist/chunks/{MetricsMiniChartWidget-BolRZ-Ja.js.map → MetricsMiniChartWidget-Y70IHFIe.js.map} +1 -1
- package/dist/chunks/PDFViewer-C0aMqGJL.js +2 -0
- package/dist/chunks/{PDFViewer-UBhinN8A.js.map → PDFViewer-C0aMqGJL.js.map} +1 -1
- package/dist/chunks/{PDFViewer-D6SKOl85.js → PDFViewer-DkbYnnoV.js} +3 -3
- package/dist/chunks/{PDFViewer-D6SKOl85.js.map → PDFViewer-DkbYnnoV.js.map} +1 -1
- package/dist/chunks/Page-CvbwEoLv.js +2 -0
- package/dist/chunks/{Page-CnvHhwLZ.js.map → Page-CvbwEoLv.js.map} +1 -1
- package/dist/chunks/{Page-B7L25Omb.js → Page-Deq4y2Kq.js} +2 -2
- package/dist/chunks/{Page-B7L25Omb.js.map → Page-Deq4y2Kq.js.map} +1 -1
- package/dist/chunks/Rest-BNYqGlnP.js +2 -0
- package/dist/chunks/Rest-BNYqGlnP.js.map +1 -0
- package/dist/chunks/{WebApp-El07OMHH.js → Rest-CS4jRCAs.js} +5 -1389
- package/dist/chunks/Rest-CS4jRCAs.js.map +1 -0
- package/dist/chunks/{TopNav-CPA884W7.js → TopNav-DXLRdU0o.js} +5 -5
- package/dist/chunks/{TopNav-CPA884W7.js.map → TopNav-DXLRdU0o.js.map} +1 -1
- package/dist/chunks/TopNav-GyvI31l2.js +2 -0
- package/dist/chunks/{TopNav-Dcmcic-i.js.map → TopNav-GyvI31l2.js.map} +1 -1
- package/dist/chunks/WebApp-BAadsDpO.js +2 -0
- package/dist/chunks/WebApp-BAadsDpO.js.map +1 -0
- package/dist/chunks/WebApp-BVGZC2rj.js +1388 -0
- package/dist/chunks/WebApp-BVGZC2rj.js.map +1 -0
- package/dist/css/web-mojo.css +2 -2
- package/dist/docit.cjs.js +1 -1
- package/dist/docit.cjs.js.map +1 -1
- package/dist/docit.es.js +12 -10
- package/dist/docit.es.js.map +1 -1
- package/dist/index.cjs.js +1 -1
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.es.js +120 -116
- package/dist/index.es.js.map +1 -1
- package/dist/lightbox.cjs.js +1 -1
- package/dist/lightbox.cjs.js.map +1 -1
- package/dist/lightbox.es.js +121 -121
- package/dist/lightbox.es.js.map +1 -1
- package/dist/map.cjs.js +2 -0
- package/dist/map.cjs.js.map +1 -0
- package/dist/map.es.js +188 -0
- package/dist/map.es.js.map +1 -0
- package/dist/timeline.cjs.js +2 -0
- package/dist/timeline.cjs.js.map +1 -0
- package/dist/timeline.es.js +225 -0
- package/dist/timeline.es.js.map +1 -0
- package/package.json +9 -1
- package/dist/chunks/ChatView-DlSxjxah.js.map +0 -1
- package/dist/chunks/ChatView-DnqrGXMC.js +0 -2
- package/dist/chunks/ChatView-DnqrGXMC.js.map +0 -1
- package/dist/chunks/ContextMenu-CE77rUmn.js +0 -2155
- package/dist/chunks/ContextMenu-CE77rUmn.js.map +0 -1
- package/dist/chunks/ContextMenu-KVxd0Kgd.js +0 -3
- package/dist/chunks/ContextMenu-KVxd0Kgd.js.map +0 -1
- package/dist/chunks/DataView-XJbTQ5q0.js +0 -2
- package/dist/chunks/Dialog-BinTQTfO.js +0 -2
- package/dist/chunks/FormView-CIriLDZY.js.map +0 -1
- package/dist/chunks/FormView-TPFsq8ZX.js +0 -3
- package/dist/chunks/MetricsMiniChartWidget-sONcM0pG.js +0 -2
- package/dist/chunks/PDFViewer-UBhinN8A.js +0 -2
- package/dist/chunks/Page-CnvHhwLZ.js +0 -2
- package/dist/chunks/TopNav-Dcmcic-i.js +0 -2
- package/dist/chunks/WebApp-El07OMHH.js.map +0 -1
- package/dist/chunks/WebApp-b9DQWz1d.js +0 -2
- package/dist/chunks/WebApp-b9DQWz1d.js.map +0 -1
|
@@ -0,0 +1,989 @@
|
|
|
1
|
+
import { E as EventEmitter, r as rest, M as MOJOUtils } from "./Rest-CS4jRCAs.js";
|
|
2
|
+
class Model {
|
|
3
|
+
constructor(data = {}, options = {}) {
|
|
4
|
+
this.endpoint = options.endpoint || this.constructor.endpoint || "";
|
|
5
|
+
this.id = data.id || null;
|
|
6
|
+
this.attributes = { ...data };
|
|
7
|
+
this._ = this.attributes;
|
|
8
|
+
this.originalAttributes = { ...data };
|
|
9
|
+
this.errors = {};
|
|
10
|
+
this.loading = false;
|
|
11
|
+
this.rest = rest;
|
|
12
|
+
this.options = {
|
|
13
|
+
idAttribute: "id",
|
|
14
|
+
timestamps: true,
|
|
15
|
+
...options
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
getContextValue(key) {
|
|
19
|
+
return this.get(key);
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Get attribute value with support for dot notation and pipe formatting
|
|
23
|
+
* @param {string} key - Attribute key with optional pipes (e.g., "name|uppercase")
|
|
24
|
+
* @returns {*} Attribute value, possibly formatted
|
|
25
|
+
*/
|
|
26
|
+
get(key) {
|
|
27
|
+
if (!key.includes(".") && !key.includes("|") && this[key] !== void 0) {
|
|
28
|
+
if (typeof this[key] === "function") {
|
|
29
|
+
return this[key]();
|
|
30
|
+
}
|
|
31
|
+
return this[key];
|
|
32
|
+
}
|
|
33
|
+
return MOJOUtils.getContextData(this.attributes, key);
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Set attribute value(s)
|
|
37
|
+
* @param {string|object} key - Attribute key or object of key-value pairs
|
|
38
|
+
* @param {*} value - Attribute value (if key is string)
|
|
39
|
+
* @param {object} options - Options (silent: true to not trigger change event)
|
|
40
|
+
*/
|
|
41
|
+
set(key, value, options = {}) {
|
|
42
|
+
const previousAttributes = JSON.parse(JSON.stringify(this.attributes));
|
|
43
|
+
let hasChanged = false;
|
|
44
|
+
if (typeof key === "object") {
|
|
45
|
+
for (const [attrKey, attrValue] of Object.entries(key)) {
|
|
46
|
+
hasChanged = this._setNestedAttribute(attrKey, attrValue) || hasChanged;
|
|
47
|
+
}
|
|
48
|
+
if (key.id !== void 0) {
|
|
49
|
+
this.id = key.id;
|
|
50
|
+
}
|
|
51
|
+
} else {
|
|
52
|
+
if (key === "id") {
|
|
53
|
+
this.id = value;
|
|
54
|
+
hasChanged = true;
|
|
55
|
+
} else {
|
|
56
|
+
hasChanged = this._setNestedAttribute(key, value);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
if (hasChanged && !options.silent) {
|
|
60
|
+
this.emit("change", this);
|
|
61
|
+
if (typeof key === "string") {
|
|
62
|
+
this.emit(`change:${key}`, value, this);
|
|
63
|
+
} else {
|
|
64
|
+
for (const [attr, val] of Object.entries(key)) {
|
|
65
|
+
const finalValue = this._getNestedValue(attr);
|
|
66
|
+
if (JSON.stringify(this._getNestedValue(attr, previousAttributes)) !== JSON.stringify(finalValue)) {
|
|
67
|
+
this.emit(`change:${attr}`, finalValue, this);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Set a nested attribute using dot notation
|
|
75
|
+
* @param {string} key - Attribute key (may contain dots)
|
|
76
|
+
* @param {*} value - Value to set
|
|
77
|
+
* @returns {boolean} - Whether the value changed
|
|
78
|
+
*/
|
|
79
|
+
_setNestedAttribute(key, value) {
|
|
80
|
+
if (!key.includes(".")) {
|
|
81
|
+
const oldValue2 = this.attributes[key];
|
|
82
|
+
this.attributes[key] = value;
|
|
83
|
+
this[key] = value;
|
|
84
|
+
return oldValue2 !== value;
|
|
85
|
+
}
|
|
86
|
+
const keys = key.split(".");
|
|
87
|
+
const topLevelKey = keys[0];
|
|
88
|
+
if (!this.attributes[topLevelKey] || typeof this.attributes[topLevelKey] !== "object") {
|
|
89
|
+
this.attributes[topLevelKey] = {};
|
|
90
|
+
}
|
|
91
|
+
if (!this[topLevelKey] || typeof this[topLevelKey] !== "object") {
|
|
92
|
+
this[topLevelKey] = {};
|
|
93
|
+
}
|
|
94
|
+
const oldValue = this._getNestedValue(key);
|
|
95
|
+
let attrTarget = this.attributes[topLevelKey];
|
|
96
|
+
let instanceTarget = this[topLevelKey];
|
|
97
|
+
for (let i = 1; i < keys.length - 1; i++) {
|
|
98
|
+
const currentKey = keys[i];
|
|
99
|
+
if (!attrTarget[currentKey] || typeof attrTarget[currentKey] !== "object") {
|
|
100
|
+
attrTarget[currentKey] = {};
|
|
101
|
+
}
|
|
102
|
+
if (!instanceTarget[currentKey] || typeof instanceTarget[currentKey] !== "object") {
|
|
103
|
+
instanceTarget[currentKey] = {};
|
|
104
|
+
}
|
|
105
|
+
attrTarget = attrTarget[currentKey];
|
|
106
|
+
instanceTarget = instanceTarget[currentKey];
|
|
107
|
+
}
|
|
108
|
+
const finalKey = keys[keys.length - 1];
|
|
109
|
+
attrTarget[finalKey] = value;
|
|
110
|
+
instanceTarget[finalKey] = value;
|
|
111
|
+
return JSON.stringify(oldValue) !== JSON.stringify(value);
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Get a nested value using dot notation
|
|
115
|
+
* @param {string} key - Attribute key (may contain dots)
|
|
116
|
+
* @param {object} source - Source object (defaults to this.attributes)
|
|
117
|
+
* @returns {*} - The nested value
|
|
118
|
+
*/
|
|
119
|
+
_getNestedValue(key, source = this.attributes) {
|
|
120
|
+
if (!key.includes(".")) {
|
|
121
|
+
return source[key];
|
|
122
|
+
}
|
|
123
|
+
const keys = key.split(".");
|
|
124
|
+
let current = source;
|
|
125
|
+
for (const k of keys) {
|
|
126
|
+
if (current == null || typeof current !== "object") {
|
|
127
|
+
return void 0;
|
|
128
|
+
}
|
|
129
|
+
current = current[k];
|
|
130
|
+
}
|
|
131
|
+
return current;
|
|
132
|
+
}
|
|
133
|
+
getData() {
|
|
134
|
+
return this.attributes;
|
|
135
|
+
}
|
|
136
|
+
getId() {
|
|
137
|
+
return this.id;
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Fetch model data from API with request deduplication and cancellation
|
|
141
|
+
* @param {object} options - Request options
|
|
142
|
+
* @param {number} options.debounceMs - Optional debounce delay in milliseconds
|
|
143
|
+
* @returns {Promise} Promise that resolves with REST response
|
|
144
|
+
*/
|
|
145
|
+
async fetch(options = {}) {
|
|
146
|
+
let url = options.url;
|
|
147
|
+
if (!url) {
|
|
148
|
+
const id = options.id || this.getId();
|
|
149
|
+
if (!id && this.options.requiresId !== false) {
|
|
150
|
+
throw new Error("Model: ID is required for fetching");
|
|
151
|
+
}
|
|
152
|
+
url = this.buildUrl(id);
|
|
153
|
+
}
|
|
154
|
+
const requestKey = JSON.stringify({ url, params: options.params });
|
|
155
|
+
if (options.debounceMs && options.debounceMs > 0) {
|
|
156
|
+
return this._debouncedFetch(requestKey, options);
|
|
157
|
+
}
|
|
158
|
+
if (this.currentRequest && this.currentRequestKey !== requestKey) {
|
|
159
|
+
console.info("Model: Cancelling previous request for new parameters");
|
|
160
|
+
this.abortController?.abort();
|
|
161
|
+
this.currentRequest = null;
|
|
162
|
+
}
|
|
163
|
+
if (this.currentRequest && this.currentRequestKey === requestKey) {
|
|
164
|
+
console.info("Model: Duplicate request in progress, returning existing promise");
|
|
165
|
+
return this.currentRequest;
|
|
166
|
+
}
|
|
167
|
+
const now = Date.now();
|
|
168
|
+
const minInterval = 100;
|
|
169
|
+
if (this.lastFetchTime && now - this.lastFetchTime < minInterval) {
|
|
170
|
+
console.info("Model: Rate limited, skipping fetch");
|
|
171
|
+
return this;
|
|
172
|
+
}
|
|
173
|
+
this.loading = true;
|
|
174
|
+
this.errors = {};
|
|
175
|
+
this.lastFetchTime = now;
|
|
176
|
+
this.currentRequestKey = requestKey;
|
|
177
|
+
this.abortController = new AbortController();
|
|
178
|
+
this.currentRequest = this._performFetch(url, options, this.abortController);
|
|
179
|
+
try {
|
|
180
|
+
const result = await this.currentRequest;
|
|
181
|
+
return result;
|
|
182
|
+
} catch (error) {
|
|
183
|
+
if (error.name === "AbortError") {
|
|
184
|
+
console.info("Model: Request was cancelled");
|
|
185
|
+
return this;
|
|
186
|
+
}
|
|
187
|
+
throw error;
|
|
188
|
+
} finally {
|
|
189
|
+
this.currentRequest = null;
|
|
190
|
+
this.currentRequestKey = null;
|
|
191
|
+
this.abortController = null;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Handle debounced fetch requests
|
|
196
|
+
* @param {string} requestKey - Unique key for this request
|
|
197
|
+
* @param {object} options - Fetch options
|
|
198
|
+
* @returns {Promise} Promise that resolves with REST response
|
|
199
|
+
*/
|
|
200
|
+
async _debouncedFetch(requestKey, options) {
|
|
201
|
+
if (this.debouncedFetchTimeout) {
|
|
202
|
+
clearTimeout(this.debouncedFetchTimeout);
|
|
203
|
+
}
|
|
204
|
+
this.cancel();
|
|
205
|
+
return new Promise((resolve, reject) => {
|
|
206
|
+
this.debouncedFetchTimeout = setTimeout(async () => {
|
|
207
|
+
try {
|
|
208
|
+
const result = await this.fetch({ ...options, debounceMs: 0 });
|
|
209
|
+
resolve(result);
|
|
210
|
+
} catch (error) {
|
|
211
|
+
reject(error);
|
|
212
|
+
}
|
|
213
|
+
}, options.debounceMs);
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Internal method to perform the actual fetch
|
|
218
|
+
* @param {string} url - API endpoint URL
|
|
219
|
+
* @param {object} options - Request options
|
|
220
|
+
* @param {AbortController} abortController - Controller for request cancellation
|
|
221
|
+
* @returns {Promise} Promise that resolves with REST response
|
|
222
|
+
*/
|
|
223
|
+
async _performFetch(url, options, abortController) {
|
|
224
|
+
try {
|
|
225
|
+
if (options.graph && (!options.params || !options.params.graph)) {
|
|
226
|
+
if (!options.params) options.params = {};
|
|
227
|
+
options.params.graph = options.graph;
|
|
228
|
+
}
|
|
229
|
+
const response = await this.rest.GET(url, options.params, {
|
|
230
|
+
signal: abortController.signal
|
|
231
|
+
});
|
|
232
|
+
if (response.success) {
|
|
233
|
+
if (response.data.status) {
|
|
234
|
+
this.originalAttributes = { ...this.attributes };
|
|
235
|
+
this.set(response.data.data);
|
|
236
|
+
this.errors = {};
|
|
237
|
+
} else {
|
|
238
|
+
this.errors = response.data;
|
|
239
|
+
}
|
|
240
|
+
} else {
|
|
241
|
+
this.errors = response.errors || {};
|
|
242
|
+
}
|
|
243
|
+
return response;
|
|
244
|
+
} catch (error) {
|
|
245
|
+
if (error.name === "AbortError") {
|
|
246
|
+
console.info("Model: Fetch was cancelled");
|
|
247
|
+
throw error;
|
|
248
|
+
}
|
|
249
|
+
this.errors = { fetch: error.message };
|
|
250
|
+
return {
|
|
251
|
+
success: false,
|
|
252
|
+
error: error.message,
|
|
253
|
+
status: error.status || 500
|
|
254
|
+
};
|
|
255
|
+
} finally {
|
|
256
|
+
this.loading = false;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* Save model to API (create or update)
|
|
261
|
+
* @param {object} data - Data to save to the model
|
|
262
|
+
* @param {object} options - Request options
|
|
263
|
+
* @returns {Promise} Promise that resolves with REST response
|
|
264
|
+
*/
|
|
265
|
+
async save(data, options = {}) {
|
|
266
|
+
const isNew = !this.id;
|
|
267
|
+
const method = isNew ? "POST" : "PUT";
|
|
268
|
+
const url = isNew ? this.buildUrl() : this.buildUrl(this.id);
|
|
269
|
+
this.loading = true;
|
|
270
|
+
this.errors = {};
|
|
271
|
+
try {
|
|
272
|
+
const response = await this.rest[method](url, data, options.params);
|
|
273
|
+
if (response.success) {
|
|
274
|
+
if (response.data.status) {
|
|
275
|
+
this.originalAttributes = { ...this.attributes };
|
|
276
|
+
this.set(response.data.data);
|
|
277
|
+
this.errors = {};
|
|
278
|
+
} else {
|
|
279
|
+
this.errors = response.data;
|
|
280
|
+
}
|
|
281
|
+
} else {
|
|
282
|
+
this.errors = response.errors || {};
|
|
283
|
+
}
|
|
284
|
+
return response;
|
|
285
|
+
} catch (error) {
|
|
286
|
+
return {
|
|
287
|
+
success: false,
|
|
288
|
+
error: error.message,
|
|
289
|
+
status: error.status || 500
|
|
290
|
+
};
|
|
291
|
+
} finally {
|
|
292
|
+
this.loading = false;
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
/**
|
|
296
|
+
* Delete model from API
|
|
297
|
+
* @param {object} options - Request options
|
|
298
|
+
* @returns {Promise} Promise that resolves with REST response
|
|
299
|
+
*/
|
|
300
|
+
async destroy(options = {}) {
|
|
301
|
+
if (!this.id) {
|
|
302
|
+
this.errors = { destroy: "Cannot destroy model without ID" };
|
|
303
|
+
return {
|
|
304
|
+
success: false,
|
|
305
|
+
error: "Cannot destroy model without ID",
|
|
306
|
+
status: 400
|
|
307
|
+
};
|
|
308
|
+
}
|
|
309
|
+
const url = this.buildUrl(this.id);
|
|
310
|
+
this.loading = true;
|
|
311
|
+
this.errors = {};
|
|
312
|
+
try {
|
|
313
|
+
const response = await this.rest.DELETE(url, options.params);
|
|
314
|
+
if (response.success) {
|
|
315
|
+
this.attributes = {};
|
|
316
|
+
this.originalAttributes = {};
|
|
317
|
+
this.id = null;
|
|
318
|
+
this.errors = {};
|
|
319
|
+
} else {
|
|
320
|
+
this.errors = response.errors || {};
|
|
321
|
+
}
|
|
322
|
+
return response;
|
|
323
|
+
} catch (error) {
|
|
324
|
+
this.errors = { destroy: error.message };
|
|
325
|
+
return {
|
|
326
|
+
success: false,
|
|
327
|
+
error: error.message,
|
|
328
|
+
status: error.status || 500
|
|
329
|
+
};
|
|
330
|
+
} finally {
|
|
331
|
+
this.loading = false;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
/**
|
|
335
|
+
* Check if model has been modified
|
|
336
|
+
* @returns {boolean} True if model has unsaved changes
|
|
337
|
+
*/
|
|
338
|
+
isDirty() {
|
|
339
|
+
return JSON.stringify(this.attributes) !== JSON.stringify(this.originalAttributes);
|
|
340
|
+
}
|
|
341
|
+
/**
|
|
342
|
+
* Get attributes that have changed since last save
|
|
343
|
+
* @returns {object} Object containing only changed attributes
|
|
344
|
+
*/
|
|
345
|
+
getChangedAttributes() {
|
|
346
|
+
const changed = {};
|
|
347
|
+
for (const [key, value] of Object.entries(this.attributes)) {
|
|
348
|
+
if (this.originalAttributes[key] !== value) {
|
|
349
|
+
changed[key] = value;
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
return changed;
|
|
353
|
+
}
|
|
354
|
+
/**
|
|
355
|
+
* Reset model to original state
|
|
356
|
+
*/
|
|
357
|
+
reset() {
|
|
358
|
+
this.attributes = { ...this.originalAttributes };
|
|
359
|
+
this._ = this.attributes;
|
|
360
|
+
this.errors = {};
|
|
361
|
+
}
|
|
362
|
+
/**
|
|
363
|
+
* Build URL for API requests
|
|
364
|
+
* @param {string|number} id - Optional ID to append to URL
|
|
365
|
+
* @returns {string} Complete API URL
|
|
366
|
+
*/
|
|
367
|
+
buildUrl(id = null) {
|
|
368
|
+
let url = this.endpoint;
|
|
369
|
+
if (id) {
|
|
370
|
+
url = url.endsWith("/") ? `${url}${id}` : `${url}/${id}`;
|
|
371
|
+
}
|
|
372
|
+
return url;
|
|
373
|
+
}
|
|
374
|
+
/**
|
|
375
|
+
* Convert model to JSON
|
|
376
|
+
* @returns {object} Model attributes as plain object
|
|
377
|
+
*/
|
|
378
|
+
toJSON() {
|
|
379
|
+
return {
|
|
380
|
+
id: this.id,
|
|
381
|
+
...this.attributes
|
|
382
|
+
};
|
|
383
|
+
}
|
|
384
|
+
/**
|
|
385
|
+
* Validate model attributes
|
|
386
|
+
* @returns {boolean} True if valid, false if validation errors exist
|
|
387
|
+
*/
|
|
388
|
+
validate() {
|
|
389
|
+
this.errors = {};
|
|
390
|
+
if (this.constructor.validations) {
|
|
391
|
+
for (const [field, rules] of Object.entries(this.constructor.validations)) {
|
|
392
|
+
this.validateField(field, rules);
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
return Object.keys(this.errors).length === 0;
|
|
396
|
+
}
|
|
397
|
+
/**
|
|
398
|
+
* Validate a single field
|
|
399
|
+
* @param {string} field - Field name
|
|
400
|
+
* @param {object|array} rules - Validation rules
|
|
401
|
+
*/
|
|
402
|
+
validateField(field, rules) {
|
|
403
|
+
const value = this.get(field);
|
|
404
|
+
const rulesArray = Array.isArray(rules) ? rules : [rules];
|
|
405
|
+
for (const rule of rulesArray) {
|
|
406
|
+
if (typeof rule === "function") {
|
|
407
|
+
const result = rule(value, this);
|
|
408
|
+
if (result !== true) {
|
|
409
|
+
this.errors[field] = result || `${field} is invalid`;
|
|
410
|
+
break;
|
|
411
|
+
}
|
|
412
|
+
} else if (typeof rule === "object") {
|
|
413
|
+
if (rule.required && (value === void 0 || value === null || value === "")) {
|
|
414
|
+
this.errors[field] = rule.message || `${field} is required`;
|
|
415
|
+
break;
|
|
416
|
+
}
|
|
417
|
+
if (rule.minLength && value && value.length < rule.minLength) {
|
|
418
|
+
this.errors[field] = rule.message || `${field} must be at least ${rule.minLength} characters`;
|
|
419
|
+
break;
|
|
420
|
+
}
|
|
421
|
+
if (rule.maxLength && value && value.length > rule.maxLength) {
|
|
422
|
+
this.errors[field] = rule.message || `${field} must be no more than ${rule.maxLength} characters`;
|
|
423
|
+
break;
|
|
424
|
+
}
|
|
425
|
+
if (rule.pattern && value && !rule.pattern.test(value)) {
|
|
426
|
+
this.errors[field] = rule.message || `${field} format is invalid`;
|
|
427
|
+
break;
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
// EventEmitter API: on, off, once, emit (from mixin).
|
|
433
|
+
/**
|
|
434
|
+
* Static method to create and fetch a model by ID
|
|
435
|
+
* @param {string|number} id - Model ID
|
|
436
|
+
* @param {object} options - Options
|
|
437
|
+
* @returns {Promise<RestModel>} Promise that resolves with fetched model
|
|
438
|
+
*/
|
|
439
|
+
static async find(id, options = {}) {
|
|
440
|
+
const model = new this({}, options);
|
|
441
|
+
await model.fetch({ id, ...options });
|
|
442
|
+
return model;
|
|
443
|
+
}
|
|
444
|
+
/**
|
|
445
|
+
* Static method to create a new model with data
|
|
446
|
+
* @param {object} data - Model data
|
|
447
|
+
* @param {object} options - Options
|
|
448
|
+
* @returns {RestModel} New model instance
|
|
449
|
+
*/
|
|
450
|
+
static create(data = {}, options = {}) {
|
|
451
|
+
return new this(data, options);
|
|
452
|
+
}
|
|
453
|
+
/**
|
|
454
|
+
* Cancel any active fetch request
|
|
455
|
+
* @returns {boolean} True if a request was cancelled, false if no active request
|
|
456
|
+
*/
|
|
457
|
+
cancel() {
|
|
458
|
+
if (this.currentRequest && this.abortController) {
|
|
459
|
+
console.info("Model: Manually cancelling active request");
|
|
460
|
+
this.abortController.abort();
|
|
461
|
+
return true;
|
|
462
|
+
}
|
|
463
|
+
if (this.debouncedFetchTimeout) {
|
|
464
|
+
clearTimeout(this.debouncedFetchTimeout);
|
|
465
|
+
this.debouncedFetchTimeout = null;
|
|
466
|
+
return true;
|
|
467
|
+
}
|
|
468
|
+
return false;
|
|
469
|
+
}
|
|
470
|
+
/**
|
|
471
|
+
* Check if model has an active fetch request
|
|
472
|
+
* @returns {boolean} True if fetch is in progress
|
|
473
|
+
*/
|
|
474
|
+
isFetching() {
|
|
475
|
+
return !!this.currentRequest;
|
|
476
|
+
}
|
|
477
|
+
async showError(message) {
|
|
478
|
+
await Dialog.alert(message, "Error", {
|
|
479
|
+
size: "md",
|
|
480
|
+
class: "text-danger"
|
|
481
|
+
});
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
Object.assign(Model.prototype, EventEmitter);
|
|
485
|
+
class Collection {
|
|
486
|
+
constructor(options = {}, data = null) {
|
|
487
|
+
if (Array.isArray(options)) {
|
|
488
|
+
data = options;
|
|
489
|
+
options = data || {};
|
|
490
|
+
} else {
|
|
491
|
+
data = data || options.data || [];
|
|
492
|
+
}
|
|
493
|
+
this.ModelClass = options.ModelClass || Model;
|
|
494
|
+
this.models = [];
|
|
495
|
+
this.loading = false;
|
|
496
|
+
this.errors = {};
|
|
497
|
+
this.meta = {};
|
|
498
|
+
this.rest = rest;
|
|
499
|
+
if (data) {
|
|
500
|
+
this.add(data);
|
|
501
|
+
}
|
|
502
|
+
this.params = {
|
|
503
|
+
start: 0,
|
|
504
|
+
size: options.size || 10,
|
|
505
|
+
...options.params
|
|
506
|
+
};
|
|
507
|
+
this.endpoint = options.endpoint || this.ModelClass.endpoint || "";
|
|
508
|
+
if (!this.endpoint) {
|
|
509
|
+
let tmp = new this.ModelClass();
|
|
510
|
+
this.endpoint = tmp.endpoint;
|
|
511
|
+
}
|
|
512
|
+
this.restEnabled = this.endpoint ? true : false;
|
|
513
|
+
if (options.restEnabled !== void 0) {
|
|
514
|
+
this.restEnabled = options.restEnabled;
|
|
515
|
+
}
|
|
516
|
+
this.options = {
|
|
517
|
+
parse: true,
|
|
518
|
+
reset: true,
|
|
519
|
+
preloaded: false,
|
|
520
|
+
...options
|
|
521
|
+
};
|
|
522
|
+
}
|
|
523
|
+
getModelName() {
|
|
524
|
+
return this.ModelClass.name;
|
|
525
|
+
}
|
|
526
|
+
/**
|
|
527
|
+
* Fetch collection data from API
|
|
528
|
+
* @param {object} additionalParams - Additional parameters to merge for this fetch only
|
|
529
|
+
* @returns {Promise} Promise that resolves with REST response
|
|
530
|
+
*/
|
|
531
|
+
async fetch(additionalParams = {}) {
|
|
532
|
+
const requestKey = JSON.stringify({ ...this.params, ...additionalParams });
|
|
533
|
+
if (this.currentRequest && this.currentRequestKey !== requestKey) {
|
|
534
|
+
console.info("Collection: Cancelling previous request for new parameters");
|
|
535
|
+
this.abortController?.abort();
|
|
536
|
+
this.currentRequest = null;
|
|
537
|
+
}
|
|
538
|
+
if (this.currentRequest && this.currentRequestKey === requestKey) {
|
|
539
|
+
console.info("Collection: Duplicate request in progress, returning existing promise");
|
|
540
|
+
return this.currentRequest;
|
|
541
|
+
}
|
|
542
|
+
const now = Date.now();
|
|
543
|
+
const minInterval = 100;
|
|
544
|
+
if (this.options.rateLimiting && this.lastFetchTime && now - this.lastFetchTime < minInterval) {
|
|
545
|
+
console.info("Collection: Rate limited, skipping fetch");
|
|
546
|
+
return { success: true, message: "Rate limited, skipping fetch", data: { data: this.toJSON() } };
|
|
547
|
+
}
|
|
548
|
+
if (!this.restEnabled) {
|
|
549
|
+
console.info("Collection: REST disabled, skipping fetch");
|
|
550
|
+
return { success: true, message: "REST disabled, skipping fetch", data: { data: this.toJSON() } };
|
|
551
|
+
}
|
|
552
|
+
if (this.options.preloaded && this.models.length > 0) {
|
|
553
|
+
console.info("Collection: Using preloaded data, skipping fetch");
|
|
554
|
+
return { success: true, message: "Using preloaded data, skipping fetch", data: { data: this.toJSON() } };
|
|
555
|
+
}
|
|
556
|
+
const url = this.buildUrl();
|
|
557
|
+
this.loading = true;
|
|
558
|
+
this.errors = {};
|
|
559
|
+
this.lastFetchTime = now;
|
|
560
|
+
this.currentRequestKey = requestKey;
|
|
561
|
+
this.abortController = new AbortController();
|
|
562
|
+
this.currentRequest = this._performFetch(url, additionalParams, this.abortController);
|
|
563
|
+
try {
|
|
564
|
+
const result = await this.currentRequest;
|
|
565
|
+
return result;
|
|
566
|
+
} catch (error) {
|
|
567
|
+
if (error.name === "AbortError") {
|
|
568
|
+
console.info("Collection: Request was cancelled");
|
|
569
|
+
return { success: false, error: "Request cancelled", status: 0 };
|
|
570
|
+
}
|
|
571
|
+
return {
|
|
572
|
+
success: false,
|
|
573
|
+
error: error.message,
|
|
574
|
+
status: error.status || 500
|
|
575
|
+
};
|
|
576
|
+
} finally {
|
|
577
|
+
this.currentRequest = null;
|
|
578
|
+
this.currentRequestKey = null;
|
|
579
|
+
this.abortController = null;
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
/**
|
|
583
|
+
* Internal method to perform the actual fetch
|
|
584
|
+
* @param {string} url - API endpoint URL
|
|
585
|
+
* @param {object} additionalParams - Additional parameters
|
|
586
|
+
* @param {AbortController} abortController - Controller for request cancellation
|
|
587
|
+
* @returns {Promise} Promise that resolves with REST response
|
|
588
|
+
*/
|
|
589
|
+
async _performFetch(url, additionalParams, abortController) {
|
|
590
|
+
const fetchParams = { ...this.params, ...additionalParams };
|
|
591
|
+
console.log("Fetching collection data from", url, fetchParams);
|
|
592
|
+
try {
|
|
593
|
+
this.emit("fetch:start");
|
|
594
|
+
const response = await this.rest.GET(url, fetchParams, {
|
|
595
|
+
signal: abortController.signal
|
|
596
|
+
});
|
|
597
|
+
if (response.success && response.data.status) {
|
|
598
|
+
const data = this.options.parse ? this.parse(response) : response.data;
|
|
599
|
+
if (this.options.reset || additionalParams.reset !== false) {
|
|
600
|
+
this.reset();
|
|
601
|
+
}
|
|
602
|
+
this.add(data, { silent: additionalParams.silent });
|
|
603
|
+
this.errors = {};
|
|
604
|
+
this.emit("fetch:success");
|
|
605
|
+
} else {
|
|
606
|
+
if (response.data && response.data.error) {
|
|
607
|
+
this.errors = response.data;
|
|
608
|
+
this.emit("fetch:error", { message: response.data.error, error: response.data });
|
|
609
|
+
} else {
|
|
610
|
+
this.errors = response.errors || {};
|
|
611
|
+
this.emit("fetch:error", { error: response.errors });
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
return response;
|
|
615
|
+
} catch (error) {
|
|
616
|
+
if (error.name === "AbortError") {
|
|
617
|
+
console.info("Collection: Fetch was cancelled");
|
|
618
|
+
return { success: false, error: "Request cancelled", status: 0 };
|
|
619
|
+
}
|
|
620
|
+
this.errors = { fetch: error.message };
|
|
621
|
+
this.emit("fetch:error", { message: error.message, error });
|
|
622
|
+
return {
|
|
623
|
+
success: false,
|
|
624
|
+
error: error.message,
|
|
625
|
+
status: error.status || 500
|
|
626
|
+
};
|
|
627
|
+
} finally {
|
|
628
|
+
this.loading = false;
|
|
629
|
+
this.emit("fetch:end");
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
/**
|
|
633
|
+
* Update collection parameters and optionally fetch new data
|
|
634
|
+
* @param {object} newParams - Parameters to update
|
|
635
|
+
* @param {boolean} autoFetch - Whether to automatically fetch after updating params
|
|
636
|
+
* @param {number} debounceMs - Optional debounce delay in milliseconds
|
|
637
|
+
* @returns {Promise} Promise that resolves with REST response if autoFetch=true, or collection if autoFetch=false
|
|
638
|
+
*/
|
|
639
|
+
async updateParams(newParams, autoFetch = false, debounceMs = 0) {
|
|
640
|
+
return await this.setParams({ ...this.params, ...newParams }, autoFetch, debounceMs);
|
|
641
|
+
}
|
|
642
|
+
async setParams(newParams, autoFetch = false, debounceMs = 0) {
|
|
643
|
+
this.params = newParams;
|
|
644
|
+
if (autoFetch && this.restEnabled) {
|
|
645
|
+
if (debounceMs > 0) {
|
|
646
|
+
if (this.debouncedFetchTimeout) {
|
|
647
|
+
clearTimeout(this.debouncedFetchTimeout);
|
|
648
|
+
}
|
|
649
|
+
this.cancel();
|
|
650
|
+
return new Promise((resolve, reject) => {
|
|
651
|
+
this.debouncedFetchTimeout = setTimeout(async () => {
|
|
652
|
+
try {
|
|
653
|
+
const result = await this.fetch();
|
|
654
|
+
resolve(result);
|
|
655
|
+
} catch (error) {
|
|
656
|
+
reject(error);
|
|
657
|
+
}
|
|
658
|
+
}, debounceMs);
|
|
659
|
+
});
|
|
660
|
+
} else {
|
|
661
|
+
return this.fetch();
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
return Promise.resolve(this);
|
|
665
|
+
}
|
|
666
|
+
/**
|
|
667
|
+
* Fetch a single model by ID
|
|
668
|
+
* @param {string|number} id - Model ID to fetch
|
|
669
|
+
* @param {object} options - Additional fetch options
|
|
670
|
+
* @returns {Promise<Model|null>} Promise that resolves with model instance or null if not found
|
|
671
|
+
*/
|
|
672
|
+
async fetchOne(id, options = {}) {
|
|
673
|
+
if (!id) {
|
|
674
|
+
console.warn("Collection: fetchOne requires an ID");
|
|
675
|
+
return null;
|
|
676
|
+
}
|
|
677
|
+
if (!this.restEnabled) {
|
|
678
|
+
console.info("Collection: REST disabled, cannot fetch single item");
|
|
679
|
+
return null;
|
|
680
|
+
}
|
|
681
|
+
try {
|
|
682
|
+
const model = new this.ModelClass({ id }, {
|
|
683
|
+
endpoint: this.endpoint,
|
|
684
|
+
collection: this
|
|
685
|
+
});
|
|
686
|
+
const response = await model.fetch(options);
|
|
687
|
+
if (response.success) {
|
|
688
|
+
if (options.addToCollection === true) {
|
|
689
|
+
const existingModel = this.get(model.id);
|
|
690
|
+
if (!existingModel) {
|
|
691
|
+
this.add(model, { silent: options.silent });
|
|
692
|
+
} else if (options.merge !== false) {
|
|
693
|
+
existingModel.set(model.attributes);
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
return model;
|
|
697
|
+
} else {
|
|
698
|
+
console.warn("Collection: fetchOne failed -", response.error || "Unknown error");
|
|
699
|
+
return null;
|
|
700
|
+
}
|
|
701
|
+
} catch (error) {
|
|
702
|
+
console.error("Collection: fetchOne error -", error.message);
|
|
703
|
+
return null;
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
/**
|
|
707
|
+
* Download collection data in a specified format
|
|
708
|
+
* @param {string} format - The format for the download (e.g., 'csv', 'json')
|
|
709
|
+
* @param {object} options - Download options
|
|
710
|
+
* @returns {Promise} Promise that resolves with the download result
|
|
711
|
+
*/
|
|
712
|
+
async download(format = "json", options = {}) {
|
|
713
|
+
if (!this.restEnabled) {
|
|
714
|
+
console.warn("Collection: REST is not enabled, cannot download from remote.");
|
|
715
|
+
return { success: false, message: "Remote downloads are not enabled for this collection." };
|
|
716
|
+
}
|
|
717
|
+
const url = this.buildUrl();
|
|
718
|
+
const downloadParams = { ...this.params };
|
|
719
|
+
delete downloadParams.start;
|
|
720
|
+
delete downloadParams.size;
|
|
721
|
+
downloadParams.download_format = format;
|
|
722
|
+
const filename = `export-${this.getModelName().toLowerCase()}.${format}`;
|
|
723
|
+
const contentTypes = {
|
|
724
|
+
json: "application/json",
|
|
725
|
+
csv: "text/csv"
|
|
726
|
+
};
|
|
727
|
+
const acceptHeader = contentTypes[format] || "*/*";
|
|
728
|
+
return this.rest.download(url, downloadParams, {
|
|
729
|
+
...options,
|
|
730
|
+
filename,
|
|
731
|
+
headers: { "Accept": acceptHeader }
|
|
732
|
+
});
|
|
733
|
+
}
|
|
734
|
+
/**
|
|
735
|
+
* Parse response data - override in subclasses for custom parsing
|
|
736
|
+
* @param {object} response - API response
|
|
737
|
+
* @returns {array} Array of model data objects
|
|
738
|
+
*/
|
|
739
|
+
parse(response) {
|
|
740
|
+
if (response.data && Array.isArray(response.data.data)) {
|
|
741
|
+
this.meta = {
|
|
742
|
+
size: response.data.size || 10,
|
|
743
|
+
start: response.data.start || 0,
|
|
744
|
+
count: response.data.count || 0,
|
|
745
|
+
status: response.data.status,
|
|
746
|
+
graph: response.data.graph,
|
|
747
|
+
...response.meta
|
|
748
|
+
};
|
|
749
|
+
return response.data.data;
|
|
750
|
+
}
|
|
751
|
+
if (Array.isArray(response.data)) {
|
|
752
|
+
return response.data;
|
|
753
|
+
}
|
|
754
|
+
return Array.isArray(response) ? response : [response];
|
|
755
|
+
}
|
|
756
|
+
/**
|
|
757
|
+
* Add model(s) to the collection
|
|
758
|
+
* @param {object|array} data - Model data or array of model data
|
|
759
|
+
* @param {object} options - Options for adding models
|
|
760
|
+
*/
|
|
761
|
+
add(data, options = {}) {
|
|
762
|
+
const modelsData = Array.isArray(data) ? data : [data];
|
|
763
|
+
const addedModels = [];
|
|
764
|
+
for (const modelData of modelsData) {
|
|
765
|
+
let model;
|
|
766
|
+
if (modelData instanceof this.ModelClass) {
|
|
767
|
+
model = modelData;
|
|
768
|
+
} else {
|
|
769
|
+
model = new this.ModelClass(modelData, {
|
|
770
|
+
endpoint: this.endpoint,
|
|
771
|
+
collection: this
|
|
772
|
+
});
|
|
773
|
+
}
|
|
774
|
+
const existingIndex = this.models.findIndex((m) => m.id === model.id);
|
|
775
|
+
if (existingIndex !== -1) {
|
|
776
|
+
if (options.merge !== false) {
|
|
777
|
+
this.models[existingIndex].set(model.attributes);
|
|
778
|
+
}
|
|
779
|
+
} else {
|
|
780
|
+
this.models.push(model);
|
|
781
|
+
addedModels.push(model);
|
|
782
|
+
}
|
|
783
|
+
}
|
|
784
|
+
if (!options.silent && addedModels.length > 0) {
|
|
785
|
+
this.emit("add", { models: addedModels, collection: this });
|
|
786
|
+
this.emit("update", { collection: this });
|
|
787
|
+
}
|
|
788
|
+
return addedModels;
|
|
789
|
+
}
|
|
790
|
+
/**
|
|
791
|
+
* Remove model(s) from the collection
|
|
792
|
+
* @param {Model|array|string|number} models - Model(s) to remove or ID(s)
|
|
793
|
+
* @param {object} options - Options
|
|
794
|
+
*/
|
|
795
|
+
remove(models, options = {}) {
|
|
796
|
+
const modelsToRemove = Array.isArray(models) ? models : [models];
|
|
797
|
+
const removedModels = [];
|
|
798
|
+
for (const model of modelsToRemove) {
|
|
799
|
+
let index = -1;
|
|
800
|
+
if (typeof model === "string" || typeof model === "number") {
|
|
801
|
+
index = this.models.findIndex((m) => m.id == model);
|
|
802
|
+
} else {
|
|
803
|
+
index = this.models.indexOf(model);
|
|
804
|
+
}
|
|
805
|
+
if (index !== -1) {
|
|
806
|
+
const removedModel = this.models.splice(index, 1)[0];
|
|
807
|
+
removedModels.push(removedModel);
|
|
808
|
+
}
|
|
809
|
+
}
|
|
810
|
+
if (!options.silent && removedModels.length > 0) {
|
|
811
|
+
this.emit("remove", { models: removedModels, collection: this });
|
|
812
|
+
this.emit("update", { collection: this });
|
|
813
|
+
}
|
|
814
|
+
return removedModels;
|
|
815
|
+
}
|
|
816
|
+
/**
|
|
817
|
+
* Reset the collection (remove all models)
|
|
818
|
+
* @param {array} models - Optional new models to set
|
|
819
|
+
* @param {object} options - Options
|
|
820
|
+
*/
|
|
821
|
+
reset(models = null, options = {}) {
|
|
822
|
+
const previousModels = [...this.models];
|
|
823
|
+
this.models = [];
|
|
824
|
+
if (models) {
|
|
825
|
+
this.add(models, { silent: true, ...options });
|
|
826
|
+
}
|
|
827
|
+
if (!options.silent) {
|
|
828
|
+
this.emit("reset", {
|
|
829
|
+
collection: this,
|
|
830
|
+
previousModels
|
|
831
|
+
});
|
|
832
|
+
}
|
|
833
|
+
return this;
|
|
834
|
+
}
|
|
835
|
+
/**
|
|
836
|
+
* Get model by ID
|
|
837
|
+
* @param {string|number} id - Model ID
|
|
838
|
+
* @returns {Model|undefined} Model instance or undefined
|
|
839
|
+
*/
|
|
840
|
+
get(id) {
|
|
841
|
+
return this.models.find((model) => model.id == id);
|
|
842
|
+
}
|
|
843
|
+
/**
|
|
844
|
+
* Get model by index
|
|
845
|
+
* @param {number} index - Model index
|
|
846
|
+
* @returns {Model|undefined} Model instance or undefined
|
|
847
|
+
*/
|
|
848
|
+
at(index) {
|
|
849
|
+
return this.models[index];
|
|
850
|
+
}
|
|
851
|
+
/**
|
|
852
|
+
* Get collection length
|
|
853
|
+
* @returns {number} Number of models in collection
|
|
854
|
+
*/
|
|
855
|
+
length() {
|
|
856
|
+
return this.models.length;
|
|
857
|
+
}
|
|
858
|
+
/**
|
|
859
|
+
* Check if collection is empty
|
|
860
|
+
* @returns {boolean} True if collection has no models
|
|
861
|
+
*/
|
|
862
|
+
isEmpty() {
|
|
863
|
+
return this.models.length === 0;
|
|
864
|
+
}
|
|
865
|
+
/**
|
|
866
|
+
* Find models matching criteria
|
|
867
|
+
* @param {function|object} criteria - Filter function or object with key-value pairs
|
|
868
|
+
* @returns {array} Array of matching models
|
|
869
|
+
*/
|
|
870
|
+
where(criteria) {
|
|
871
|
+
if (typeof criteria === "function") {
|
|
872
|
+
return this.models.filter(criteria);
|
|
873
|
+
}
|
|
874
|
+
if (typeof criteria === "object") {
|
|
875
|
+
return this.models.filter((model) => {
|
|
876
|
+
return Object.entries(criteria).every(([key, value]) => {
|
|
877
|
+
return model.get(key) === value;
|
|
878
|
+
});
|
|
879
|
+
});
|
|
880
|
+
}
|
|
881
|
+
return [];
|
|
882
|
+
}
|
|
883
|
+
/**
|
|
884
|
+
* Find first model matching criteria
|
|
885
|
+
* @param {function|object} criteria - Filter function or object with key-value pairs
|
|
886
|
+
* @returns {Model|undefined} First matching model or undefined
|
|
887
|
+
*/
|
|
888
|
+
findWhere(criteria) {
|
|
889
|
+
const results = this.where(criteria);
|
|
890
|
+
return results.length > 0 ? results[0] : void 0;
|
|
891
|
+
}
|
|
892
|
+
/**
|
|
893
|
+
* Iterate over each model in the collection
|
|
894
|
+
* @param {function} callback - Function to execute for each model (model, index, collection)
|
|
895
|
+
* @param {object} thisArg - Optional value to use as this when executing callback
|
|
896
|
+
* @returns {Collection} Returns the collection for chaining
|
|
897
|
+
*/
|
|
898
|
+
forEach(callback, thisArg) {
|
|
899
|
+
if (typeof callback !== "function") {
|
|
900
|
+
throw new TypeError("Callback must be a function");
|
|
901
|
+
}
|
|
902
|
+
this.models.forEach((model, index) => {
|
|
903
|
+
callback.call(thisArg, model, index, this);
|
|
904
|
+
});
|
|
905
|
+
return this;
|
|
906
|
+
}
|
|
907
|
+
/**
|
|
908
|
+
* Sort collection by comparator function
|
|
909
|
+
* @param {function|string} comparator - Comparison function or attribute name
|
|
910
|
+
* @param {object} options - Sort options
|
|
911
|
+
*/
|
|
912
|
+
sort(comparator, options = {}) {
|
|
913
|
+
if (typeof comparator === "string") {
|
|
914
|
+
const attr = comparator;
|
|
915
|
+
comparator = (a, b) => {
|
|
916
|
+
const aVal = a.get(attr);
|
|
917
|
+
const bVal = b.get(attr);
|
|
918
|
+
if (aVal < bVal) return -1;
|
|
919
|
+
if (aVal > bVal) return 1;
|
|
920
|
+
return 0;
|
|
921
|
+
};
|
|
922
|
+
}
|
|
923
|
+
this.models.sort(comparator);
|
|
924
|
+
if (!options.silent) {
|
|
925
|
+
this.trigger("sort", { collection: this });
|
|
926
|
+
}
|
|
927
|
+
return this;
|
|
928
|
+
}
|
|
929
|
+
/**
|
|
930
|
+
* Convert collection to JSON array
|
|
931
|
+
* @returns {array} Array of model JSON representations
|
|
932
|
+
*/
|
|
933
|
+
toJSON() {
|
|
934
|
+
return this.models.map((model) => model.toJSON());
|
|
935
|
+
}
|
|
936
|
+
/**
|
|
937
|
+
* Cancel any active fetch request
|
|
938
|
+
* @returns {boolean} True if a request was cancelled, false if no active request
|
|
939
|
+
*/
|
|
940
|
+
cancel() {
|
|
941
|
+
if (this.currentRequest && this.abortController) {
|
|
942
|
+
console.info("Collection: Manually cancelling active request");
|
|
943
|
+
this.abortController.abort();
|
|
944
|
+
return true;
|
|
945
|
+
}
|
|
946
|
+
return false;
|
|
947
|
+
}
|
|
948
|
+
/**
|
|
949
|
+
* Check if collection has an active fetch request
|
|
950
|
+
* @returns {boolean} True if fetch is in progress
|
|
951
|
+
*/
|
|
952
|
+
isFetching() {
|
|
953
|
+
return !!this.currentRequest;
|
|
954
|
+
}
|
|
955
|
+
/**
|
|
956
|
+
* Build URL for collection endpoint
|
|
957
|
+
* @returns {string} Collection API URL
|
|
958
|
+
*/
|
|
959
|
+
buildUrl() {
|
|
960
|
+
return this.endpoint;
|
|
961
|
+
}
|
|
962
|
+
// EventEmitter API: on, off, once, emit (from mixin).
|
|
963
|
+
/**
|
|
964
|
+
* Iterator support for for...of loops
|
|
965
|
+
*/
|
|
966
|
+
*[Symbol.iterator]() {
|
|
967
|
+
for (const model of this.models) {
|
|
968
|
+
yield model;
|
|
969
|
+
}
|
|
970
|
+
}
|
|
971
|
+
/**
|
|
972
|
+
* Static method to create collection from array data
|
|
973
|
+
* @param {function} ModelClass - Model class constructor
|
|
974
|
+
* @param {array} data - Array of model data
|
|
975
|
+
* @param {object} options - Collection options
|
|
976
|
+
* @returns {Collection} New collection instance
|
|
977
|
+
*/
|
|
978
|
+
static fromArray(ModelClass, data = [], options = {}) {
|
|
979
|
+
const collection = new this(ModelClass, options);
|
|
980
|
+
collection.add(data, { silent: true });
|
|
981
|
+
return collection;
|
|
982
|
+
}
|
|
983
|
+
}
|
|
984
|
+
Object.assign(Collection.prototype, EventEmitter);
|
|
985
|
+
export {
|
|
986
|
+
Collection as C,
|
|
987
|
+
Model as M
|
|
988
|
+
};
|
|
989
|
+
//# sourceMappingURL=Collection-OP7c9Zyu.js.map
|