@guebbit/vue-toolkit 1.0.0

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.
@@ -0,0 +1,238 @@
1
+ import { computed, ref } from 'vue';
2
+ export const useStructureDataManagement = (
3
+ // The identification parameter of the item (READONLY and not exported)
4
+ identifiers = "id",
5
+ // Delimiter for multiple identifiers
6
+ delimiter = "|") => {
7
+ /**
8
+ *
9
+ * @param itemData
10
+ * @param customIdentifiers - if specified, it will create a key using these identifiers instead of the default ones
11
+ */
12
+ const createIdentifier = (itemData, customIdentifiers) => {
13
+ const _identifiers = customIdentifiers ?? identifiers;
14
+ if (Array.isArray(_identifiers))
15
+ return _identifiers.map((key) => itemData[key]).join(delimiter);
16
+ return itemData[identifier];
17
+ };
18
+ /**
19
+ * True identifier, become a string if it is an array
20
+ * (no need to be reactive)
21
+ */
22
+ const identifier = Array.isArray(identifiers) ? identifiers.join(delimiter) : identifiers;
23
+ /**
24
+ * Dictionary of items (to be filled)
25
+ */
26
+ const itemDictionary = ref({});
27
+ /**
28
+ * List of items
29
+ */
30
+ const itemList = computed(() => Object.values(itemDictionary.value));
31
+ /**
32
+ * Set records directly to the dictionary
33
+ *
34
+ * @param items
35
+ */
36
+ const setRecords = (items) => itemDictionary.value = items;
37
+ /**
38
+ * Empty the items dictionary
39
+ */
40
+ const resetRecords = () => itemDictionary.value = {};
41
+ /**
42
+ * Get record from object dictionary using identifier
43
+ *
44
+ * @param _arguments
45
+ */
46
+ const getRecord = (..._arguments) => {
47
+ const id = _arguments.join(delimiter);
48
+ return Object.prototype.hasOwnProperty.call(itemDictionary.value, id) ? itemDictionary.value[id] : undefined;
49
+ };
50
+ /**
51
+ * Multiple getRecord
52
+ *
53
+ * @param idsArray
54
+ */
55
+ const getRecords = (idsArray = []) => idsArray
56
+ .map(id => Array.isArray(id) ? getRecord(...id) : getRecord(id))
57
+ .filter(Boolean);
58
+ /**
59
+ * Add item to the dictionary.
60
+ * If item already present, it will be overwritten
61
+ *
62
+ * @param itemData
63
+ */
64
+ const addRecord = (itemData) => itemDictionary.value[createIdentifier(itemData)] = itemData;
65
+ /**
66
+ * Add a list of items to the dictionary.
67
+ *
68
+ * @param itemsArray
69
+ */
70
+ const addRecords = (itemsArray) => {
71
+ for (let i = 0, len = itemsArray.length; i < len; i++) {
72
+ if (!itemsArray[i])
73
+ continue;
74
+ addRecord(itemsArray[i]);
75
+ }
76
+ };
77
+ /**
78
+ * Edit item,
79
+ * If item not present, it will be ignored
80
+ * If it is present, it will be merged with the new partial data
81
+ * WARNING: If identifier change, it does NOT automatically update the dictionary id.
82
+ *
83
+ * @param data
84
+ * @param id - WARNING: needed createIdentifier if identifiers is array
85
+ * @param create - if true it will be added if not present
86
+ */
87
+ const editRecord = (data = {}, id, create = true) => {
88
+ // If not specified, it will be inferred
89
+ const _inferredId = id ?? data[identifier];
90
+ // if multiple identifiers, then they need to be joined\translated
91
+ const _id = Array.isArray(_inferredId) ? _inferredId.join(delimiter) : _inferredId;
92
+ // if NOT forced to create and NOT found: error
93
+ if (!create &&
94
+ (!id || !Object.prototype.hasOwnProperty.call(itemDictionary.value, _id))) {
95
+ // eslint-disable-next-line no-console
96
+ console.error("storeDataStructure - data not found", data);
97
+ return;
98
+ }
99
+ // Replace data if already present
100
+ itemDictionary.value[_id] = {
101
+ ...itemDictionary.value[_id],
102
+ ...data
103
+ };
104
+ };
105
+ /**
106
+ * Same as addRecords but with editRecord
107
+ *
108
+ * @param itemsArray
109
+ */
110
+ const editRecords = (itemsArray) => {
111
+ for (let i = 0, len = itemsArray.length; i < len; i++) {
112
+ if (!itemsArray[i])
113
+ continue;
114
+ editRecord(itemsArray[i]);
115
+ }
116
+ };
117
+ /**
118
+ * Delete record
119
+ *
120
+ * @param id
121
+ */
122
+ const deleteRecord = (id) => getRecord(id) && delete itemDictionary.value[id];
123
+ /**
124
+ * Selected ID
125
+ */
126
+ const selectedIdentifier = ref();
127
+ /**
128
+ * Selected item (by @{selectedIdentifier})
129
+ * Can have 2 uses:
130
+ * - List mode: Show in modal or operations that require the details (example items in a table)
131
+ * - Target mode: a detail page or a form to edit the selected item (example item in a dedicated detail page)
132
+ */
133
+ const selectedRecord = computed(() => selectedIdentifier.value && itemDictionary.value[selectedIdentifier.value]);
134
+ /**
135
+ * ---------------------------------- OFFLINE PAGINATION ------------------------------------
136
+ */
137
+ /**
138
+ * Current selected page (start with 1)
139
+ */
140
+ const pageCurrent = ref(1);
141
+ /**
142
+ * How many items in page
143
+ */
144
+ const pageSize = ref(10);
145
+ /**
146
+ * How many pages exist
147
+ */
148
+ const pageTotal = computed(() => Math.ceil(itemList.value.length / pageSize.value));
149
+ /**
150
+ * First item of the current page
151
+ */
152
+ const pageOffset = computed(() => pageSize.value * (pageCurrent.value - 1));
153
+ /**
154
+ * Items shown in current page
155
+ */
156
+ const pageItemList = computed(() => itemList.value.slice(pageOffset.value, pageOffset.value + pageSize.value));
157
+ /**
158
+ * ----------------------------- hasMany & belongsTo relationships -----------------------------
159
+ */
160
+ /**
161
+ * If the item has a parent, here will be stored a "parent hasMany" relation
162
+ */
163
+ const parentHasMany = ref({});
164
+ /**
165
+ *
166
+ * @param parentId
167
+ * @param childId
168
+ */
169
+ const addToParent = (parentId, childId) => {
170
+ if (!parentHasMany.value[parentId])
171
+ parentHasMany.value[parentId] = [];
172
+ parentHasMany.value[parentId].push(childId);
173
+ };
174
+ /**
175
+ *
176
+ * @param parentId
177
+ * @param childId
178
+ */
179
+ const removeFromParent = (parentId, childId) => parentHasMany.value[parentId] =
180
+ parentHasMany.value[parentId]
181
+ .filter((id) => id !== childId);
182
+ /**
183
+ *
184
+ * @param parentId
185
+ */
186
+ const removeDuplicateChildren = (parentId) => parentHasMany.value[parentId] = [...new Set(parentHasMany.value[parentId])];
187
+ /**
188
+ * Get all records ID by parent and use them to retrieve the complete dictionary
189
+ * @param parentId
190
+ */
191
+ const getRecordsByParent = (parentId) => {
192
+ const result = {};
193
+ if (!parentId || !parentHasMany.value[parentId])
194
+ return result;
195
+ for (const key of parentHasMany.value[parentId]) {
196
+ const record = getRecord(key);
197
+ if (record)
198
+ result[key] = record;
199
+ }
200
+ return result;
201
+ };
202
+ /**
203
+ * Same as above but with array result
204
+ * @param parentId
205
+ */
206
+ const getListByParent = (parentId) => Object.values(getRecordsByParent(parentId));
207
+ return {
208
+ createIdentifier,
209
+ identifier,
210
+ itemDictionary,
211
+ itemList,
212
+ setRecords,
213
+ resetRecords,
214
+ getRecord,
215
+ getRecords,
216
+ addRecord,
217
+ addRecords,
218
+ editRecord,
219
+ editRecords,
220
+ deleteRecord,
221
+ selectedIdentifier,
222
+ selectedRecord,
223
+ // Pagination
224
+ pageCurrent,
225
+ pageSize,
226
+ pageTotal,
227
+ pageOffset,
228
+ pageItemList,
229
+ // belongsTo relationship
230
+ parentHasMany,
231
+ addToParent,
232
+ removeFromParent,
233
+ removeDuplicateChildren,
234
+ getRecordsByParent,
235
+ getListByParent,
236
+ };
237
+ };
238
+ //# sourceMappingURL=structureDataManagement.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"structureDataManagement.js","sourceRoot":"","sources":["../../src/composables/structureDataManagement.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,GAAG,EAAE,MAAM,KAAK,CAAA;AAGnC,MAAM,CAAC,MAAM,0BAA0B,GAAG;AAUxC,wEAAwE;AACxE,cAAiC,IAAI;AACrC,qCAAqC;AACrC,SAAS,GAAG,GAAG,EACf,EAAE;IAEF;;;;OAIG;IAEH,MAAM,gBAAgB,GAAG,CAAQ,QAAW,EAAE,iBAAqC,EAAK,EAAE;QACtF,MAAM,YAAY,GAAG,iBAAiB,IAAI,WAAW,CAAC;QACtD,IAAI,KAAK,CAAC,OAAO,CAAC,YAAY,CAAC;YAC7B,OAAO,YAAY,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,QAAQ,CAAC,GAAc,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAM,CAAA;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AG,IAAK,aAAa,CAAC,KAA8B,CAAC,QAAQ,CAAC,EAAE,CAAC;YAC1E,MAAM,MAAM,GAAG,SAAS,CAAC,GAAQ,CAAC,CAAA;YAClC,IAAI,MAAM;gBACR,MAAM,CAAC,GAAQ,CAAC,GAAG,MAAM,CAAA;QAC7B,CAAC;QACD,OAAO,MAAM,CAAA;IACf,CAAC,CAAA;IAED;;;OAGG;IACH,MAAM,eAAe,GAAG,CAAC,QAAY,EAAO,EAAE,CAAC,MAAM,CAAC,MAAM,CAAC,kBAAkB,CAAC,QAAQ,CAAC,CAAC,CAAC;IAE3F,OAAO;QACL,gBAAgB;QAChB,UAAU;QACV,cAAc;QACd,QAAQ;QACR,UAAU;QACV,YAAY;QACZ,SAAS;QACT,UAAU;QACV,SAAS;QACT,UAAU;QACV,UAAU;QACV,WAAW;QACX,YAAY;QACZ,kBAAkB;QAClB,cAAc;QAEd,aAAa;QACb,WAAW;QACX,QAAQ;QACR,SAAS;QACT,UAAU;QACV,YAAY;QAEZ,yBAAyB;QACzB,aAAa;QACb,WAAW;QACX,gBAAgB;QAChB,uBAAuB;QACvB,kBAAkB;QAClB,eAAe;KAChB,CAAA;AACH,CAAC,CAAC"}
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ // TODO with zod
3
+ //# sourceMappingURL=structureFormValidation.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"structureFormValidation.js","sourceRoot":"","sources":["../../src/composables/structureFormValidation.ts"],"names":[],"mappings":";AAAA,gBAAgB"}