mobiletto-orm-typedef-gen 2.3.41 → 2.8.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.
Files changed (108) hide show
  1. package/README.md +16 -0
  2. package/lib/esm/generate.d.ts +12 -2
  3. package/lib/esm/generate.js +213 -30
  4. package/lib/esm/generate.js.map +1 -0
  5. package/lib/esm/generators/h3.d.ts +7 -0
  6. package/lib/esm/generators/h3.js +84 -0
  7. package/lib/esm/generators/h3.js.map +1 -0
  8. package/lib/esm/generators/jsonSchema.d.ts +3 -0
  9. package/lib/esm/generators/jsonSchema.js +48 -0
  10. package/lib/esm/generators/jsonSchema.js.map +1 -0
  11. package/lib/esm/generators/server.d.ts +14 -9
  12. package/lib/esm/generators/server.js +79 -53
  13. package/lib/esm/generators/server.js.map +1 -0
  14. package/lib/esm/generators/tsType.d.ts +2 -2
  15. package/lib/esm/generators/tsType.js +2 -1
  16. package/lib/esm/generators/tsType.js.map +1 -0
  17. package/lib/esm/generators/vue/admin.d.ts +5 -0
  18. package/lib/esm/generators/vue/admin.js +83 -0
  19. package/lib/esm/generators/vue/admin.js.map +1 -0
  20. package/lib/esm/generators/vue/service.d.ts +4 -0
  21. package/lib/esm/generators/vue/service.js +47 -0
  22. package/lib/esm/generators/vue/service.js.map +1 -0
  23. package/lib/esm/generators/vue/store.d.ts +5 -0
  24. package/lib/esm/generators/vue/store.js +43 -0
  25. package/lib/esm/generators/vue/store.js.map +1 -0
  26. package/lib/esm/helpers.d.ts +4 -0
  27. package/lib/esm/helpers.js +160 -0
  28. package/lib/esm/helpers.js.map +1 -0
  29. package/lib/esm/index.d.ts +6 -2
  30. package/lib/esm/index.js +7 -2
  31. package/lib/esm/index.js.map +1 -0
  32. package/lib/esm/templates/admin/adminComponent.vue.hbs +662 -0
  33. package/lib/esm/templates/admin/adminHelper.ts.hbs +52 -0
  34. package/lib/esm/templates/admin/adminPage.vue.hbs +53 -0
  35. package/lib/esm/templates/api/h3.ts.hbs +13 -0
  36. package/lib/esm/templates/api/id.delete.ts.hbs +94 -32
  37. package/lib/esm/templates/api/id.get.ts.hbs +79 -18
  38. package/lib/esm/templates/api/id.patch.ts.hbs +76 -24
  39. package/lib/esm/templates/api/id.put.ts.hbs +65 -18
  40. package/lib/esm/templates/api/index.post.ts.hbs +111 -22
  41. package/lib/esm/templates/api/index.put.ts.hbs +75 -0
  42. package/lib/esm/templates/api/refs.ts.hbs +26 -0
  43. package/lib/esm/templates/filter/errorFilter.ts.hbs +10 -0
  44. package/lib/esm/templates/filter/sessionFilter.ts.hbs +125 -25
  45. package/lib/esm/templates/schema/jsonSchema.json.hbs +62 -0
  46. package/lib/esm/templates/service/service.ts.hbs +59 -29
  47. package/lib/esm/templates/service/serviceHelper.ts.hbs +38 -27
  48. package/lib/esm/templates/service/singletonService.ts.hbs +46 -24
  49. package/lib/esm/templates/store/singletonStore.ts.hbs +57 -28
  50. package/lib/esm/templates/store/store.ts.hbs +169 -39
  51. package/lib/esm/templates/store/storeHelper.ts.hbs +21 -9
  52. package/lib/esm/templates/type/tsType.ts.hbs +7 -3
  53. package/package.json +37 -22
  54. package/src/generate.ts +400 -0
  55. package/src/generators/h3.ts +125 -0
  56. package/src/generators/jsonSchema.ts +65 -0
  57. package/src/generators/server.ts +242 -0
  58. package/src/generators/tsType.ts +6 -0
  59. package/src/generators/vue/admin.ts +136 -0
  60. package/src/generators/vue/service.ts +76 -0
  61. package/src/generators/vue/store.ts +74 -0
  62. package/src/helpers.ts +195 -0
  63. package/src/index.ts +10 -0
  64. package/lib/cjs/generate.d.ts +0 -19
  65. package/lib/cjs/generate.js +0 -154
  66. package/lib/cjs/generators/client.d.ts +0 -8
  67. package/lib/cjs/generators/client.js +0 -75
  68. package/lib/cjs/generators/server.d.ts +0 -11
  69. package/lib/cjs/generators/server.js +0 -91
  70. package/lib/cjs/generators/tsType.d.ts +0 -3
  71. package/lib/cjs/generators/tsType.js +0 -4
  72. package/lib/cjs/generators/yup.d.ts +0 -3
  73. package/lib/cjs/generators/yup.js +0 -8
  74. package/lib/cjs/index.d.ts +0 -6
  75. package/lib/cjs/index.js +0 -6
  76. package/lib/cjs/package.json +0 -16
  77. package/lib/cjs/templates/api/id.delete.ts.hbs +0 -66
  78. package/lib/cjs/templates/api/id.get.ts.hbs +0 -38
  79. package/lib/cjs/templates/api/id.patch.ts.hbs +0 -51
  80. package/lib/cjs/templates/api/id.put.ts.hbs +0 -42
  81. package/lib/cjs/templates/api/index.post.ts.hbs +0 -39
  82. package/lib/cjs/templates/component/admin.vue.hbs +0 -493
  83. package/lib/cjs/templates/component/adminHelper.ts.hbs +0 -65
  84. package/lib/cjs/templates/component/componentHelper.d.ts +0 -3
  85. package/lib/cjs/templates/component/componentHelper.js +0 -53
  86. package/lib/cjs/templates/filter/errorFilter.ts.hbs +0 -51
  87. package/lib/cjs/templates/filter/sessionFilter.ts.hbs +0 -56
  88. package/lib/cjs/templates/service/service.ts.hbs +0 -58
  89. package/lib/cjs/templates/service/serviceHelper.ts.hbs +0 -97
  90. package/lib/cjs/templates/service/singletonService.ts.hbs +0 -49
  91. package/lib/cjs/templates/store/singletonStore.ts.hbs +0 -74
  92. package/lib/cjs/templates/store/store.ts.hbs +0 -94
  93. package/lib/cjs/templates/store/storeHelper.d.ts +0 -15
  94. package/lib/cjs/templates/store/storeHelper.js +0 -15
  95. package/lib/cjs/templates/store/storeHelper.ts.hbs +0 -34
  96. package/lib/cjs/templates/type/tsType.ts.hbs +0 -7
  97. package/lib/cjs/templates/validation/yup.ts.hbs +0 -39
  98. package/lib/esm/generators/client.d.ts +0 -8
  99. package/lib/esm/generators/client.js +0 -75
  100. package/lib/esm/generators/yup.d.ts +0 -3
  101. package/lib/esm/generators/yup.js +0 -8
  102. package/lib/esm/templates/component/admin.vue.hbs +0 -493
  103. package/lib/esm/templates/component/adminHelper.ts.hbs +0 -65
  104. package/lib/esm/templates/component/componentHelper.d.ts +0 -3
  105. package/lib/esm/templates/component/componentHelper.js +0 -53
  106. package/lib/esm/templates/store/storeHelper.d.ts +0 -15
  107. package/lib/esm/templates/store/storeHelper.js +0 -15
  108. package/lib/esm/templates/validation/yup.ts.hbs +0 -39
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,uBAAuB,CAAC;AACtC,cAAc,eAAe,CAAC;AAC9B,cAAc,cAAc,CAAC;AAC7B,cAAc,wBAAwB,CAAC;AACvC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,wBAAwB,CAAC;AACvC,cAAc,oBAAoB,CAAC;AACnC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,2BAA2B,CAAC;AAC1C,cAAc,2BAA2B,CAAC"}
@@ -0,0 +1,662 @@
1
+ {{#if disclaimer}}<!-- {{{ disclaimer }}}{{#if header}}
2
+ {{{header}}}{{/if}} -->{{/if}}
3
+
4
+ <template>
5
+ <v-container>
6
+ <v-row v-if="successSnackbar && successSnackbar.length > 0">
7
+ <KitSnackbarSuccess :text="successSnackbar" />
8
+ </v-row>
9
+ <v-row v-else-if="errorSnackbar && errorSnackbar.length > 0">
10
+ <KitSnackbarError :text="errorSnackbar" />
11
+ </v-row>
12
+ <v-row>
13
+ <v-col>
14
+ <h2>{{{curlyOpen}}}{{{curlyOpen}}} adminTitle() {{{curlyClose}}}{{{curlyClose}}}</h2>
15
+ <b v-if="messageExists('admin_title_{{name}}_administration_details', messages as HokeyLocaleMessages)">
16
+ {{{curlyOpen}}}{{{curlyOpen}}} messages['admin_title_{{name}}_administration_details'] {{{curlyClose}}}{{{curlyClose}}}
17
+ </b>
18
+ </v-col>
19
+ </v-row>
20
+ <v-row v-if="false"></v-row>{{#unless apiConfig.create.disabled}}
21
+ <v-row v-else-if="addingObject && {{name}}TypeDefFields">
22
+ <v-col>
23
+ <OrmForm
24
+ v-if="allRefsLoaded() && {{name}}TypeDef"
25
+ form-name="add_{{name}}_form"
26
+ :type-def="{{name}}TypeDef"
27
+ :type-name-message="typeNameMessage"
28
+ :thing="addObject"
29
+ :fields="{{name}}TypeDefFields"
30
+ :create="true"
31
+ :read-only-object="() => false"
32
+ :server-errors="create{{Name}}ServerErrors"
33
+ :label-prefixes="labelPfx"
34
+ :hint-suffixes="['_description']"
35
+ @submitted="onAddSubmitted"
36
+ @update="onAddUpdated"
37
+ @cancel="onAddCancel"
38
+ />
39
+ </v-col>
40
+ </v-row>{{/unless}}{{#unless apiConfig.update.disabled}}
41
+ <v-row v-else-if="Object.keys(editingObject).length > 0 && {{name}}TypeDefFields">
42
+ <v-col>
43
+ <OrmForm
44
+ v-if="allRefsLoaded() && {{name}}TypeDef"
45
+ form-name="edit_{{name}}_form"
46
+ :type-def="{{name}}TypeDef"
47
+ type-name-message="typeNameMessage"
48
+ :thing="editingObject"
49
+ :fields="{{name}}TypeDefFields"
50
+ :create="false"
51
+ :read-only-object="() => false"
52
+ :server-errors="edit{{Name}}ServerErrors"
53
+ :label-prefixes="labelPfx"
54
+ :hint-suffixes="['_description']"
55
+ @submitted="onEditSubmitted"
56
+ @update="onEditUpdated"
57
+ @cancel="onEditCancel"
58
+ />
59
+ </v-col>
60
+ </v-row>{{/unless}}{{#unless apiConfig.search.disabled}}{{#unless typeDef.singleton}}
61
+ <v-row v-else-if="allRefsLoaded() && {{name}}TypeDef">
62
+ <v-col>
63
+ <v-container>{{#if hasSearch}}
64
+ <v-form @submit.prevent="searchObjects">{{#if hasTextSearch}}
65
+ <v-row v-if="({{name}}Results?.objects?.length > 0) || searched">
66
+ <v-col>
67
+ <v-text-field
68
+ v-model="searchTerms"
69
+ :label="messages['label_search']"
70
+ :disabled="{{fullAdminName}}Store.{{name}}Busy"
71
+ type="text"
72
+ name="searchTerms"
73
+ class="form-control"
74
+ @keyup.enter="searchObjects"
75
+ />
76
+ </v-col>
77
+ </v-row>{{/if}}{{#if hasRefSearch}}
78
+ <v-row>
79
+ <v-col v-if="singleRefSearchField">
80
+ {{{curlyOpen}}}{{{curlyOpen}}} refSearchFields[0].title {{{curlyClose}}}{{{curlyClose}}}
81
+ </v-col>
82
+ <v-col v-else>
83
+ <v-select
84
+ v-model="selectedRef"
85
+ :v-bind="selectedRef"
86
+ :items="refSearchFields"
87
+ :full-width="false"
88
+ name="selectedRef"
89
+ class="form-control"
90
+ />
91
+ </v-col>{{#each refSearches}}
92
+ <v-col v-show="selectedRef === '{{ref}}'">{{#if text}}
93
+ <v-text-field
94
+ v-show="selectedRef === '{{ref}}'"
95
+ v-model="searchRef.{{ref}}"
96
+ :v-bind="searchRef.{{ref}}"
97
+ :label="fieldMessages.{{ref}}"
98
+ :disabled="{{fullAdminName}}Store.{{name}}Busy"
99
+ type="text"
100
+ name="searchRef_{{ref}}"
101
+ class="form-control"
102
+ @keyup.enter="searchObjects"
103
+ />{{/if}}{{#if select}}
104
+ <v-select
105
+ v-model="searchRef.{{ref}}"
106
+ :v-bind="searchRef.{{ref}}"
107
+ :label="fieldMessages.{{ref}}"
108
+ :items="ref{{RefType}}.items"
109
+ item-value="value"
110
+ item-title="label"
111
+ :full-width="false"
112
+ name="searchRef_{{ref}}"
113
+ class="form-control"
114
+ @update:model-value="searchObjects"
115
+ />
116
+ </v-col>{{/if}}{{/each}}
117
+ </v-row>{{/if}}
118
+ <v-row>
119
+ <v-col>
120
+ <v-btn class="btn btn-primary" :disabled="{{fullAdminName}}Store.{{name}}Busy" @click.stop="searchObjects">
121
+ <Icon name="material-symbols:search" />
122
+ </v-btn>
123
+ </v-col>
124
+ </v-row>
125
+ </v-form>{{/if}}
126
+ <v-row>
127
+ <v-col>
128
+ <table v-if="{{name}}Results?.objects?.length > 0">
129
+ <thead>
130
+ <tr>
131
+ <th v-for="(tableField, tableFieldIndex) in tableFields" :key="tableFieldIndex">
132
+ {{{curlyOpen}}}{{{curlyOpen}}} fieldMessages[tableField] {{{curlyClose}}}{{{curlyClose}}}
133
+ </th>{{#if apiConfig.paths}}
134
+ <th><Icon name="SitemapIcon" /></th>{{/if}}
135
+ <th v-if="Object.keys(actionConfigs).length > 0">
136
+ <Icon name="material-symbols:target" />
137
+ </th>{{#unless apiConfig.update.disabled}}
138
+ <th></th>{{/unless}}{{#unless apiConfig.delete.disabled}}
139
+ <th></th>{{/unless}}
140
+ </tr>
141
+ </thead>
142
+ <tbody>
143
+ <tr v-for="(obj, objIndex) in {{name}}Results.objects" :key="objIndex">
144
+ <td v-for="(fieldName, fieldIndex) in tableFields" :key="fieldIndex">
145
+ <OrmFieldDisplay v-if="{{name}}TypeDef?.fields[fieldName] || fieldName.startsWith('_meta')" :field="fieldName.startsWith('_meta') ? metaField(fieldName) : {{name}}TypeDef?.fields[fieldName]" :value="deepGet(obj, fieldName)" />
146
+ </td>{{#if apiConfig.paths}}
147
+ <td>{{#each apiConfig.paths}}
148
+ <NuxtLink
149
+ v-if="{{../typeDef.typeName}}TypeDef"
150
+ :to="{ path: nextAdminPath({{../typeDef.typeName}}TypeDef.id(obj), '{{typeDef.typeName}}') }"
151
+ >
152
+ <v-btn>{{typeDef.typeName}}</v-btn>
153
+ </NuxtLink>{{/each}}
154
+ </td>{{/if}}
155
+ <td v-if="Object.keys(actionConfigs).length > 0">
156
+ <div v-for="(action, actionIndex) in Object.keys(actionConfigs)" :key="actionIndex">
157
+ <div v-if="actionEnabled(obj, action) && actionConfig(action).path">
158
+ <NuxtLink
159
+ v-if="{{name}}TypeDef"
160
+ :to="{ path: `${actionConfig(action).path.replace(/\[id]/g, deepGet(obj, {{name}}TypeDef.idField(obj) || '?id?') as string)}` }"
161
+ >
162
+ <v-btn v-if="actionConfig(action).icon">
163
+ <Icon :name="actionConfig(action).icon" :tooltip="messages[actionConfig(action).message]" />
164
+ </v-btn>
165
+ <v-btn v-else>
166
+ {{{curlyOpen}}}{{{curlyOpen}}} messages[actionConfig(action).message] {{{curlyClose}}}{{{curlyClose}}}
167
+ </v-btn>
168
+ </NuxtLink>
169
+ </div>
170
+ <div v-else-if="actionEnabled(obj, action) && actionConfig(action).func">
171
+ <v-btn v-if="actionConfig(action).icon" @click.stop="invokeActionFunc(action, obj)">
172
+ <Icon :name="actionConfig(action).icon" :tooltip="messages[actionConfig(action).message]" />
173
+ </v-btn>
174
+ <v-btn v-else @click.stop="invokeActionFunc(action, obj)">
175
+ {{{curlyOpen}}}{{{curlyOpen}}} messages[actionConfig(action).message] {{{curlyClose}}}{{{curlyClose}}}
176
+ </v-btn>
177
+ </div>
178
+ </div>
179
+ </td>{{#unless apiConfig.update.disabled}}
180
+ <td>
181
+ <v-btn v-if="canEdit(obj, {{name}}Results.objects)" :disabled="{{fullAdminName}}Store.{{name}}Busy" @click.stop="showEditOrm(obj)">
182
+ <Icon name="material-symbols:edit" />
183
+ </v-btn>
184
+ </td>{{/unless}}{{#unless apiConfig.delete.disabled}}
185
+ <td>
186
+ <v-btn v-if="canDelete(obj, {{name}}Results.objects)" :disabled="{{fullAdminName}}Store.{{name}}Busy" @click.stop="delObject(obj)">
187
+ <Icon name="material-symbols:delete" />
188
+ </v-btn>
189
+ </td>{{/unless}}
190
+ </tr>
191
+ </tbody>
192
+ <tfoot v-if="{{fullAdminName}}Store.{{name}}Results?.next || {{fullAdminName}}Store.{{name}}Results?.previous">
193
+ <tr>
194
+ <td v-if="{{fullAdminName}}Store.{{name}}Results?.previous">
195
+ <v-btn class="btn btn-primary" :disabled="{{fullAdminName}}Store.searching" @click.stop="{{fullAdminName}}Store.previousPage">
196
+ <Icon name="material-symbols:chevron-left" />
197
+ </v-btn>
198
+ </td>
199
+ <td v-if="{{fullAdminName}}Store.{{name}}Results?.next">
200
+ <v-btn class="btn btn-primary" :disabled="{{fullAdminName}}Store.searching" @click.stop="{{fullAdminName}}Store.nextPage">
201
+ <Icon name="material-symbols:chevron-right" />
202
+ </v-btn>
203
+ </td>
204
+ </tr>
205
+ </tfoot>
206
+ </table>
207
+ </v-col>
208
+ </v-row>{{#unless apiConfig.create.disabled}}
209
+ <v-row v-if="canAdd()">
210
+ <v-col>
211
+ <v-btn class="btn btn-primary" :disabled="{{fullAdminName}}Store.{{name}}Busy" @click.stop="showAddOrm">
212
+ <Icon name="material-symbols:add" />
213
+ </v-btn>
214
+ </v-col>
215
+ </v-row>{{/unless}}
216
+ </v-container>
217
+ </v-col>
218
+ </v-row>{{/unless}}{{/unless}}
219
+ <v-row v-else>
220
+ <v-col>
221
+ <Icon name="LoadingIcon" />
222
+ </v-col>
223
+ </v-row>
224
+ </v-container>
225
+ </template>
226
+
227
+ <script setup lang="ts">
228
+ /* eslint-disable @typescript-eslint/no-unused-vars */
229
+ import { ref, watch, type Ref } from "vue";
230
+ import { storeToRefs } from "pinia";
231
+ import { deepGet, deepUpdate } from "zilla-util";
232
+ import {
233
+ type MobilettoOrmFieldDefConfig,{{#if typeDef.refTypes}}
234
+ type MobilettoOrmFieldDefConfigs,{{/if}}
235
+ type MobilettoOrmObject,
236
+ MobilettoOrmTypeDef,
237
+ type MobilettoOrmValidationErrors,
238
+ metaField
239
+ } from "mobiletto-orm-typedef";
240
+ import {
241
+ type {{Name}}Type,
242
+ {{Name}}TypeDef,{{#if typeDef.refTypes}}{{#each typeDef.refTypes}}{{#unless highCardinality}}
243
+ {{RefType}}TypeDef,{{/unless}}{{/each}}{{/if}}
244
+ } from "{{typeDefPackage}}";
245
+ import {
246
+ type HokeyLocaleMessages,
247
+ findMessage,
248
+ messageExists,
249
+ parseMessage{{#unless typeDef.singleton}},
250
+ normalizeMsg{{/unless}}
251
+ } from "hokey-runtime";
252
+ import { useSessionStore } from "{{sessionStoreImport}}";
253
+ import { use{{FullAdminName}}Store } from "~/stores/model/{{firstParentTypeDef.typeName}}/{{fullAdminName}}Store";{{#if typeDef.refTypes}}{{#each typeDef.refTypes}}{{#unless recursive}}{{#unless highCardinality}}
254
+ import { use{{RefType}}Store } from "~/stores/model/{{refType}}/{{refType}}Store";{{/unless}}{{/unless}}{{/each}}{{/if}}
255
+ import { type ActionConfig } from "{{adminHelperPath}}";{{#unless apiConfig.search.disabled}}
256
+ import { type MobilettoOrmFindApiOpts } from "{{utilsImportPath}}/storeHelper";{{/unless}}
257
+
258
+ const successSnackbar = ref("");
259
+ const errorSnackbar = ref("");
260
+ const props = withDefaults(
261
+ defineProps<{
262
+ labelPrefixes: string[];
263
+ typeNameMessage: string;
264
+ forceSearchField: string;
265
+ forceSearchValue: string;
266
+ msgAddSuccess: string;
267
+ msgAddError: string;
268
+ msgEditSuccess: string;
269
+ msgEditError: string;{{#if typeDef.singleton}}
270
+ msgFindSingletonError: string;{{else}}
271
+ msgDeleteSuccess: string;
272
+ msgDeleteError: string;
273
+ actionConfigs: Record<string, ActionConfig>;
274
+ canAdd: () => boolean;
275
+ newAddObject: () => MobilettoOrmObject,
276
+ canEdit: (obj: MobilettoOrmObject, objList: MobilettoOrmObject[]) => boolean;
277
+ canDelete: (obj: MobilettoOrmObject, objList: MobilettoOrmObject[]) => boolean;
278
+ deleteConfirmationMessage: string;
279
+ maxDeleteConfirmations: number;{{/if}}
280
+ }>(),{
281
+ labelPrefixes: () => ["label_", ""],
282
+ typeNameMessage: () => "typename_{{name}}",
283
+ forceSearchField: "",
284
+ forceSearchValue: "",
285
+ msgAddSuccess: () => "admin_info_added",
286
+ msgAddError: () => "admin_info_add_error",
287
+ msgEditSuccess: () => "admin_info_edited",
288
+ msgEditError: () => "admin_info_edit_error",{{#if typeDef.singleton}}
289
+ msgFindSingletonError: () => "admin_info_findSingleton_error",{{else}}
290
+ msgDeleteSuccess: () => "admin_info_deleted",
291
+ msgDeleteError: () => "admin_info_delete_error",
292
+ actionConfigs: () => ({}),
293
+ newAddObject: () => {{Name}}TypeDef.newBlankInstanceWithId(),
294
+ canAdd: () => {{#if apiConfig.create.disabled}}false{{else}}true{{/if}},
295
+ canEdit: () => {{#if apiConfig.update.disabled}}false{{else}}true{{/if}},
296
+ canDelete: () => {{#if apiConfig.delete.disabled}}false{{else}}true{{/if}},
297
+ deleteConfirmationMessage: () => "",
298
+ maxDeleteConfirmations: () => 3,{{/if}}
299
+ },
300
+ );
301
+
302
+ const emit = defineEmits<{{{curlyOpen}}}{{#unless apiConfig.create.disabled}}
303
+ empty: [];
304
+ refEmpty: [refType: string];
305
+ added: [obj: MobilettoOrmObject];
306
+ addCanceled: [];{{/unless}}{{#unless apiConfig.update.disabled}}
307
+ edited: [obj: MobilettoOrmObject];
308
+ editCanceled: [obj: MobilettoOrmObject];{{/unless}}{{#unless typeDef.singleton}}{{#unless apiConfig.delete.disabled}}
309
+ deleted: [obj: MobilettoOrmObject];{{/unless}}{{/unless}}
310
+ }>();
311
+
312
+ const labelPfx: Ref<string[]> = ref(["admin_label_{{name}}_", "label_", ""]);
313
+ if (props.labelPrefixes) {
314
+ props.labelPrefixes.forEach((p: string) => {
315
+ if (!labelPfx.value.includes(p)) labelPfx.value.unshift(p);
316
+ });
317
+ }
318
+
319
+ const sessionStore = useSessionStore();
320
+ const { {{localeMessages}} } = storeToRefs(sessionStore);
321
+ const messages = {{localeMessages}};
322
+
323
+ const adminTitle = () => messageExists("admin_title_{{name}}_administration", messages.value)
324
+ ? messages.value.admin_title_{{name}}_administration
325
+ : parseMessage("admin_title_site_administration", messages.value, { title: findMessage(props.typeNameMessage, messages.value, [""])});
326
+ {{#unless apiConfig.create.disabled}}
327
+ const addingObject = ref(false);
328
+ const addObject = ref(props.newAddObject() as {{Name}}Type);
329
+ const create{{Name}}ServerErrors = ref({} as MobilettoOrmValidationErrors);
330
+ {{/unless}}{{#unless apiConfig.update.disabled}}
331
+ const editingObject = ref({} as {{Name}}Type);
332
+ const edit{{Name}}ServerErrors = ref({} as MobilettoOrmValidationErrors);{{/unless}}{{#unless typeDef.singleton}}{{#unless apiConfig.delete.disabled}}
333
+ const delete{{Name}}ServerErrors = ref({} as MobilettoOrmValidationErrors);{{/unless}}
334
+
335
+ // const pageNumber = ref(1);
336
+ // const pageSize = ref(20);
337
+ const searchTerms = ref("");
338
+ const searched = ref(false);
339
+ const lastQuery = ref({});
340
+ {{#if parentPaths}}
341
+ const PARENT_REF_FIELDS = [{{#each parentPaths}}"{{refField}}"{{#unless @last}}, {{/unless}}{{/each}}];{{/if}}
342
+
343
+ const tableFields: Ref<string[]> = ref([]);
344
+ const fieldMessages: Ref<Record<string, string>> = ref({});
345
+ const initFieldMessages = (tableFields: string[]) => {
346
+ const defaultFieldMessages: Record<string, string> = {};
347
+ tableFields.forEach((f: string) => {
348
+ defaultFieldMessages[f] = findMessage(normalizeMsg(f), messages.value, props.labelPrefixes);
349
+ });{{#each fieldNamesArray}}
350
+ if (!tableFields.includes("{{this}}")) {
351
+ defaultFieldMessages["{{this}}"] = findMessage(normalizeMsg("{{this}}"), messages.value, props.labelPrefixes);
352
+ }
353
+ {{/each}}
354
+ fieldMessages.value = defaultFieldMessages;
355
+ };{{/unless}}
356
+
357
+ const {{fullAdminName}}Store = use{{FullAdminName}}Store();
358
+ {{#if typeDef.singleton}}const { found } = storeToRefs({{fullAdminName}}Store);
359
+ {{else}}const { {{name}}Results } = storeToRefs({{fullAdminName}}Store);{{/if}}
360
+ const {{name}}TypeDef: Ref<MobilettoOrmTypeDef | null> = ref(null);
361
+ const {{name}}TypeDefFields: Ref<MobilettoOrmFieldDefConfig[] | undefined> = ref(undefined);
362
+ {{#if typeDef.singleton}}const findSingleton{{Name}}ServerErrors = ref({} as MobilettoOrmValidationErrors);
363
+ {{else}}{{#unless apiConfig.search.disabled}}const searchQuery = () => {
364
+ const q: MobilettoOrmFindApiOpts = {};
365
+ {{#if hasTextSearch}}if (searchTerms.value && searchTerms.value.trim().length > 0) q.textSearch = searchTerms.value;{{/if}}
366
+ {{#if hasRefSearch}}const val = searchRef.value[selectedRef.value as string];
367
+ if (val && val.length > 0) {
368
+ q.field = selectedRef.value;
369
+ q.value = val;
370
+ }
371
+ {{/if}}
372
+ return Object.keys(q).length > 0 ? q : undefined;
373
+ };
374
+
375
+ const isForceSearchField = () => props.forceSearchField && props.forceSearchField.length > 0
376
+
377
+ const _search = (q?: MobilettoOrmFindApiOpts) => isForceSearchField()
378
+ ? {{fullAdminName}}Store.search({{#if parentPaths}}{{#each parentPaths}}route.params.{{shortId}}_id as string, {{/each}}{{/if}}Object.assign({}, q || {}, { field: props.forceSearchField, value: props.forceSearchValue }))
379
+ : {{fullAdminName}}Store.search({{#if parentPaths}}{{#each parentPaths}}route.params.{{shortId}}_id as string, {{/each}}{{/if}}q);
380
+
381
+ const searchObjects = () => {
382
+ const query = searchQuery();
383
+ if (!query) return; // something was wrong, don't send the query
384
+ if (lastQuery.value && JSON.stringify(lastQuery.value) === JSON.stringify(query)) {
385
+ // not sending duplicate search
386
+ } else {
387
+ searched.value = true;
388
+ lastQuery.value = Object.assign({}, query);
389
+ _search(query);
390
+ }
391
+ };{{/unless}}{{/if}}{{#unless typeDef.singleton}}
392
+
393
+ const navigating = ref(false);{{/unless}}
394
+ {{#if typeDef.refTypes}}
395
+ const initTypeDef = () => {
396
+ const refFields = {} as MobilettoOrmFieldDefConfigs;{{#each typeDef.refTypes}}{{#unless highCardinality}}{{#each fieldPaths}}
397
+ deepUpdate(refFields, "{{this}}".replace(".", ".fields."), {...ref{{../RefType}}.value});{{/each}}{{/unless}}{{/each}}
398
+ const typeDef = {{Name}}TypeDef.extend({
399
+ fields: refFields
400
+ });
401
+ {{name}}TypeDefFields.value = typeDef.tabIndexedFields();
402
+ {{name}}TypeDef.value = typeDef;{{#unless typeDef.singleton}}
403
+ tableFields.value = {{name}}TypeDef.value.tableFields && Array.isArray({{name}}TypeDef.value.tableFields)
404
+ ? {{name}}TypeDef.value.tableFields
405
+ : {{name}}TypeDef.value.primary
406
+ ? [{{name}}TypeDef.value.primary, "ctime", "mtime"]
407
+ : ["id", "ctime", "mtime"];
408
+ {{#if parentPaths}}// filter out parent fields
409
+ tableFields.value = tableFields.value.filter(f => !PARENT_REF_FIELDS.includes(f));{{/if}}
410
+ initFieldMessages(tableFields.value);{{/unless}}
411
+ }
412
+
413
+ // eslint-disable-next-line prefer-const
414
+ let refsToLoadCount = 0;
415
+ const allRefs: Ref<Boolean>[] = [];
416
+ const allRefsLoaded = () => allRefs.length === refsToLoadCount && allRefs.filter(r => r.value).length === refsToLoadCount;{{#if hasRefSearch}}const refSearchFields = ref([] as { value: string, title: string }[]);
417
+ const selectedRef = ref("{{firstRefSearch.ref}}");
418
+ const searchRef: Ref<Record<string, string>> = ref({});{{#each refSearches}}{{#unless highCardinality}}
419
+ searchRef.value.{{ref}} = "";
420
+ refSearchFields.value.push({ value: "{{ref}}", title: findMessage("{{refType}}", messages.value, ["typename_", ...props.labelPrefixes]) });
421
+ {{/unless}}{{/each}}
422
+ const singleRefSearchField = ref(refSearchFields.value.length === 1);{{/if}}{{#if typeDef.refTypes}}{{#each typeDef.refTypes}}{{#unless highCardinality}}
423
+ const ref{{RefType}} = ref({} as MobilettoOrmFieldDefConfig);
424
+ const ref{{RefType}}Loaded = ref(false);
425
+ refsToLoadCount++;
426
+ allRefs.push(ref{{RefType}}Loaded);{{#unless recursive}}
427
+ const {{refType}}Store = use{{RefType}}Store();
428
+ const { {{refType}}Results } = storeToRefs({{refType}}Store);{{/unless}}
429
+
430
+ watch({{refType}}Results, (newList) => {
431
+ if (newList && newList.length === 0) {
432
+ if (navigating.value) return;
433
+ navigating.value = true;
434
+ emit("refEmpty", "{{refType}}");
435
+ } else if (newList && newList.length > 0) {
436
+ {{#if valueAsObject}}
437
+ ref{{RefType}}.value.items = newList.map((s) => s);{{else}}
438
+ ref{{RefType}}.value.values = newList.map((s) => {{RefType}}TypeDef.id(s));
439
+ ref{{RefType}}.value.labels = newList.map((s) => s.name);
440
+ ref{{RefType}}.value.items = newList.map((s) => ({
441
+ label: s.name,
442
+ value: {{RefType}}TypeDef.id(s),
443
+ title: s.name,
444
+ rawLabel: true,
445
+ }));{{/if}}{{#each jsonFieldPaths}}
446
+ {{../../Name}}TypeDef.fields.{{this}}.items = ref{{../RefType}}.value.items;{{/each}}
447
+ ref{{RefType}}Loaded.value = true;
448
+ if (allRefsLoaded()) {
449
+ initTypeDef();
450
+ }
451
+ }
452
+ });
453
+ {{/unless}}{{/each}}
454
+ if (refsToLoadCount === 0) {
455
+ initTypeDef();
456
+ }
457
+ {{/if}}{{else}}
458
+ const allRefsLoaded = () => true;
459
+ {{name}}TypeDef.value = {{Name}}TypeDef;
460
+ {{name}}TypeDefFields.value = {{Name}}TypeDef.tabIndexedFields();{{#unless typeDef.singleton}}
461
+ tableFields.value = {{name}}TypeDef.value.tableFields && Array.isArray({{name}}TypeDef.value.tableFields)
462
+ ? {{name}}TypeDef.value.tableFields
463
+ : {{name}}TypeDef.value.primary
464
+ ? [{{name}}TypeDef.value.primary, "ctime", "mtime"]
465
+ : ["id", "ctime", "mtime"];
466
+ {{#if parentPaths}}// filter out parent fields
467
+ tableFields.value = tableFields.value.filter(f => !PARENT_REF_FIELDS.includes(f));{{/if}}
468
+ initFieldMessages(tableFields.value);{{/unless}}
469
+ {{/if}}
470
+ {{#unless apiConfig.create.disabled}}
471
+ const onAddUpdated = (update: { field: string; value: any }) => {
472
+ deepUpdate(addObject.value, update.field, update.value);
473
+ };
474
+ const onAddSubmitted = async (obj: MobilettoOrmObject) => {
475
+ await {{fullAdminName}}Store.create({{#if parentPaths}}{{#each parentPaths}}
476
+ route.params.{{shortId}}_id as string,{{/each}}{{/if}}
477
+ obj as {{Name}}Type,
478
+ create{{Name}}ServerErrors
479
+ );
480
+ addingObject.value = false;
481
+ successSnackbar.value = parseMessage(props.msgAddSuccess, messages.value, {
482
+ type: messages.value.typename_{{typeDef.typeName}},
483
+ id: {{Name}}TypeDef.id(obj),
484
+ });
485
+ emit("added", obj);{{#if typeDef.singleton}}
486
+ return await {{fullAdminName}}Store.lookup({{#if parentPaths}}{{#each parentPaths}}route.params.{{shortId}}_id as string, {{/each}}{{/if}}findSingleton{{Name}}ServerErrors);{{else}}
487
+ addObject.value = {} as {{Name}}Type;
488
+ return await _search();{{/if}}
489
+ }
490
+ watch(create{{Name}}ServerErrors, () => {
491
+ if (create{{Name}}ServerErrors.value && Object.keys(create{{Name}}ServerErrors.value).length > 0) {
492
+ errorSnackbar.value = parseMessage(props.msgAddError, messages.value, {
493
+ type: messages.value.typename_{{typeDef.typeName}},
494
+ error: JSON.stringify(create{{Name}}ServerErrors),
495
+ });
496
+ }
497
+ });
498
+
499
+ const showAddOrm = () => {
500
+ addingObject.value = true;
501
+ };
502
+ const onAddCancel = () => {
503
+ addingObject.value = false;
504
+ emit("addCanceled");
505
+ };
506
+ {{/unless}}{{#unless apiConfig.update.disabled}}
507
+ const onEditUpdated = (update: { field: string; value: any }) => {
508
+ if (editingObject.value) {
509
+ deepUpdate(editingObject.value, update.field, update.value);
510
+ }
511
+ };
512
+
513
+ const onEditSubmitted = async (obj: MobilettoOrmObject) => {
514
+ await {{fullAdminName}}Store.update({{#if parentPaths}}{{#each parentPaths}}
515
+ route.params.{{shortId}}_id as string,{{/each}}{{/if}}
516
+ obj as {{Name}}Type,
517
+ edit{{Name}}ServerErrors
518
+ );
519
+ {{#unless typeDef.singleton}}editingObject.value = {} as {{Name}}Type;{{/unless}}
520
+ successSnackbar.value = parseMessage(props.msgEditSuccess, messages.value, {
521
+ type: messages.value.typename_{{typeDef.typeName}},
522
+ id: {{Name}}TypeDef.id(obj),
523
+ });
524
+ emit("edited", obj);{{#if typeDef.singleton}}
525
+ return await {{fullAdminName}}Store.lookup({{#if parentPaths}}{{#each parentPaths}}route.params.{{shortId}}_id, {{/each}}{{/if}}findSingleton{{Name}}ServerErrors);{{else}}
526
+ return await _search();{{/if}}
527
+ };
528
+ watch(edit{{Name}}ServerErrors, () => {
529
+ if (edit{{Name}}ServerErrors.value && Object.keys(edit{{Name}}ServerErrors.value).length > 0) {
530
+ errorSnackbar.value = parseMessage(props.msgEditError, messages.value, {
531
+ type: messages.value.typename_{{typeDef.typeName}},
532
+ id: {{Name}}TypeDef.id(editingObject.value),
533
+ error: JSON.stringify(edit{{Name}}ServerErrors),
534
+ });
535
+ }
536
+ });
537
+
538
+ const showEditOrm = (obj: MobilettoOrmObject) => {
539
+ if (Object.keys(editingObject.value).length > 0) {
540
+ // already editing something else
541
+ return;
542
+ }
543
+ if (obj) {
544
+ const id = {{Name}}TypeDef.id(obj);
545
+ if (id && id.length > 0) {{{curlyOpen}}}{{#unless apiConfig.create.disabled}}
546
+ addingObject.value = false; // ensure add form is closed{{/unless}}
547
+ editingObject.value = JSON.parse(JSON.stringify(obj)) as {{Name}}Type;
548
+ }
549
+ }
550
+ };
551
+ const onEditCancel = () => {
552
+ emit("editCanceled", editingObject.value);
553
+ {{#if typeDef.singleton}}
554
+ editingObject.value = JSON.parse(JSON.stringify(found.value)) as {{Name}}Type;
555
+ {{else}}
556
+ editingObject.value = {} as {{Name}}Type;
557
+ {{/if}}
558
+ };{{/unless}}
559
+ {{#if typeDef.singleton}}
560
+ watch(found, () => {
561
+ if (found.value && {{Name}}TypeDef.id(found.value)) {
562
+ showEditOrm(JSON.parse(JSON.stringify(found.value)));{{#unless apiConfig.create.disabled}}
563
+ } else {
564
+ showAddOrm();{{/unless}}
565
+ }
566
+ });
567
+ watch(findSingleton{{Name}}ServerErrors, () => {
568
+ if (findSingleton{{Name}}ServerErrors.value && Object.keys(findSingleton{{Name}}ServerErrors.value).length > 0) {
569
+ errorSnackbar.value = parseMessage(props.msgFindSingletonError, messages.value, {
570
+ type: messages.value.typename_{{typeDef.typeName}},
571
+ error: JSON.stringify(findSingleton{{Name}}ServerErrors),
572
+ });
573
+ }
574
+ });{{else}}
575
+ {{#or apiConfig.paths typeDef.refTypes}}
576
+ const route = useRoute();{{/or}}{{#if apiConfig.paths}}
577
+ const nextAdminPath = (objId: string, refField: string): string => {
578
+ const basePath = route.fullPath.substring(0, route.fullPath.lastIndexOf("/"));
579
+ return `${basePath}/${objId}/${refField}/admin`;
580
+ };
581
+ {{/if}}
582
+ const actionConfig = (action: string): ActionConfig => {
583
+ return props.actionConfigs[action];
584
+ };
585
+
586
+ const actionEnabled = (obj: MobilettoOrmObject, action: string): boolean => {
587
+ const cfg = actionConfig(action);
588
+ if (!cfg) return false;
589
+ if (!cfg.when || typeof cfg.when !== "function") {
590
+ return true;
591
+ }
592
+ if (typeof cfg.when === "function") {
593
+ return cfg.when(obj);
594
+ }
595
+ return true;
596
+ };
597
+ const invokeActionFunc = (action: string, obj: MobilettoOrmObject): unknown => {
598
+ const cfg: ActionConfig = actionConfig(action);
599
+ return cfg && cfg.func ? cfg.func(obj, successSnackbar, errorSnackbar) : undefined;
600
+ };{{#unless apiConfig.delete.disabled}}
601
+ const delConfirmCount = ref(0);
602
+ const deletingObject: Ref<MobilettoOrmObject | null> = ref(null);
603
+ const delObject = (obj: MobilettoOrmObject) => {
604
+ if (props.deleteConfirmationMessage && props.deleteConfirmationMessage.length > 0 && delConfirmCount.value < props.maxDeleteConfirmations) {
605
+ const confirmationMessage = parseMessage(props.deleteConfirmationMessage, messages.value, {
606
+ id: {{Name}}TypeDef.id(obj)
607
+ });
608
+ if (confirm(confirmationMessage)) {
609
+ delConfirmCount.value = delConfirmCount.value + 1;
610
+ } else {
611
+ delConfirmCount.value = 0;
612
+ return;
613
+ }
614
+ }
615
+ deletingObject.value = obj;
616
+ const id = {{Name}}TypeDef.id(obj);
617
+ {{fullAdminName}}Store.delete({{#if parentPaths}}{{#each parentPaths}}route.params.{{shortId}}_id as string, {{/each}}{{/if}}id, delete{{Name}}ServerErrors)
618
+ .then(() => {
619
+ successSnackbar.value = parseMessage(props.msgDeleteSuccess, messages.value, {
620
+ type: messages.value.typename_{{typeDef.typeName}},
621
+ id: {{Name}}TypeDef.id(obj),
622
+ });
623
+ emit("deleted", obj);
624
+ return _search();
625
+ });
626
+ };
627
+ watch(delete{{Name}}ServerErrors, () => {
628
+ if (delete{{Name}}ServerErrors.value && Object.keys(delete{{Name}}ServerErrors.value).length > 0) {
629
+ errorSnackbar.value = parseMessage(props.msgDeleteError, messages.value, {
630
+ type: messages.value.typename_{{typeDef.typeName}},
631
+ id: deletingObject.value ? {{Name}}TypeDef.id(deletingObject.value) : "null",
632
+ error: JSON.stringify(delete{{Name}}ServerErrors),
633
+ });
634
+ }
635
+ });{{/unless}}
636
+ watch({{name}}Results, (newList) => {
637
+ if (newList && Array.isArray(newList) &&
638
+ (!newList.length || newList.length === 0) &&
639
+ (!searchTerms.value || !searchTerms.value.length || (searchTerms.value && searchTerms.value.length === 0))
640
+ ) {
641
+ if (navigating.value) return;
642
+ navigating.value = true;
643
+ emit("empty");
644
+ }
645
+ });{{/if}}
646
+
647
+ const initRefs = async () => {
648
+ {{#if typeDef.refTypes}}const refSearches: Promise<MobilettoOrmObject[]>[] = [];{{#each typeDef.refTypes}}{{#unless recursive}}{{#unless highCardinality}}
649
+ refSearches.push({{refType}}Store.search({{#if parentPaths}}{{#each parentPaths}}route.params.{{shortId}}_id, {{/each}}{{/if}}{}));{{/unless}}{{/unless}}{{/each}}
650
+ await Promise.all(refSearches);
651
+ {{/if}}{{#if typeDef.singleton}}return await {{fullAdminName}}Store.lookup({{#if parentPaths}}{{#each parentPaths}}route.params.{{shortId}}_id, {{/each}}{{/if}}findSingleton{{Name}}ServerErrors);{{/if}}
652
+ };{{#if typeDef.singleton}}
653
+ initRefs()
654
+ .then(obj => showEditOrm(obj)){{#unless apiConfig.create.disabled}}
655
+ .catch(_e => showAddOrm()){{/unless}};
656
+ {{else}}
657
+
658
+ _search(){{#if typeDef.refTypes}}.then(() => {
659
+ initRefs();
660
+ }){{/if}};
661
+ {{/if}}
662
+ </script>