39
39
<span v-else-if =" api.completed" >No Results</span >
40
40
</div >
41
41
</div >
42
+ <div v-if =" apis.Create && canCreate" class =" pl-2 mt-1" >
43
+ <button type =" button" @click =" onShowNewItem()" title =" modelTitle" :class =" grid.toolbarButtonClass" >
44
+ <svg class =" w-5 h-5 mr-1 text-gray-500 dark:text-gray-400 hover:text-gray-900 dark:hover:text-gray-50" xmlns =" http://www.w3.org/2000/svg" viewBox =" 0 0 24 24" ><path d =" M19 13h-6v6h-2v-6H5v-2h6V5h2v6h6v2z" fill =" currentColor" ></path ></svg >
45
+ <span class =" whitespace-nowrap" >{{ newButtonLabel }}</span >
46
+ </button >
47
+ <AutoCreateForm v-if =" create" ref =" createForm" :type =" apis.Create.request.name" :configure =" configureField" @done =" createDone" @save =" createSave" >
48
+ <template #header >
49
+ <slot name =" formheader" form =" create" :formInstance =" createForm" :apis =" apis" :type =" dataModelName" :updateModel =" setCreate" ></slot >
50
+ </template >
51
+ <template #footer >
52
+ <slot name =" formfooter" form =" create" :formInstance =" createForm" :apis =" apis" :type =" dataModelName" :updateModel =" setCreate" ></slot >
53
+ </template >
54
+ </AutoCreateForm >
55
+ </div >
42
56
43
57
<div v-if =" hasPrefs && showResetPreferences" class =" pl-2" >
44
58
<button type =" button" @click =" resetPreferences" title =" Reset Preferences & Filters" :class =" toolbarButtonClass" >
111
125
</template >
112
126
113
127
<script setup lang="ts">
114
- import type { ApiPrefs , ApiResponse , Column , ColumnSettings , MetadataPropertyType , RefInfo } from ' @/types'
115
- import { computed , inject , nextTick , onMounted , ref , useSlots } from ' vue'
128
+ import type { ApiPrefs , ApiResponse , Column , ColumnSettings , InputProp , MetadataPropertyType , RefInfo } from ' @/types'
129
+ import { computed , getCurrentInstance , inject , nextTick , onMounted , ref , useSlots } from ' vue'
116
130
import { ApiResult , delaySet , humanize , JsonServiceClient , mapGet } from ' @servicestack/client'
117
- import { parseJson } from ' @/use/utils'
131
+ import { parseJson , getTypeName } from ' @/use/utils'
118
132
import { useConfig } from ' @/use/config'
119
- import { createDto , Crud , getPrimaryKey , typeOf , typeProperties , useMetadata } from ' @/use/metadata'
133
+ import { Apis , createDto , Crud , getPrimaryKey , typeOf , typeProperties , useMetadata } from ' @/use/metadata'
120
134
import { grid } from ' ./css'
135
+ import { canAccess } from ' @/use/auth'
121
136
122
137
import FilterColumn from ' ./grids/FilterColumn.vue'
123
138
import FilterViews from ' ./grids/FilterViews.vue'
@@ -139,6 +154,11 @@ const props = withDefaults(defineProps<{
139
154
toolbarButtonClass? : string
140
155
141
156
canFilter? : (column : string ) => boolean
157
+
158
+ type? : string | InstanceType <any >| Function
159
+ modelTitle? : string
160
+ newButtonLabel? : string
161
+ configureField? : (field : InputProp ) => void
142
162
}>(), {
143
163
id: ' ModalLookup' ,
144
164
skip: 0 ,
@@ -224,6 +244,39 @@ const modalDialog = ref()
224
244
const showQueryPrefs = ref (false )
225
245
const showFilters = ref <{ column: Column , topLeft: {x: number ,y: number }}| null >()
226
246
247
+ const typeName = computed (() => getTypeName (props .refInfo .model ))
248
+ const apis = computed (() => Apis .forType (typeName .value , metadataApi .value ))
249
+ const dataModelName = computed (() => typeName .value || queryOp .value ?.dataModel .name )
250
+ const modelTitle = computed (() => props .modelTitle || dataModelName .value )
251
+ const newButtonLabel = computed (() => props .newButtonLabel || ` New ${modelTitle .value } ` )
252
+ const canCreate = computed (() => canAccess (apis .value .Create ))
253
+
254
+ const createForm = ref ()
255
+ const create = ref (false )
256
+ function onShowNewItem() {
257
+ create .value = true
258
+ }
259
+
260
+ function createDone() {
261
+ create .value = false
262
+ }
263
+ async function createSave(result : any ) {
264
+ createDone ()
265
+ emit (' done' , result )
266
+ }
267
+ function setCreate(props : any ) {
268
+ if (! createForm .value ) return
269
+ Object .assign (createForm .value ?.model , props )
270
+ console .log (' setCreate' , JSON .stringify (props , null , 2 ))
271
+ forceUpdate ()
272
+ }
273
+ function forceUpdate() {
274
+ createForm .value ?.forceUpdate ()
275
+ const instance = getCurrentInstance ()
276
+ instance ?.proxy ?.$forceUpdate ()
277
+ }
278
+
279
+
227
280
const prefsCacheKey = () => ` ${props .id }/ApiPrefs/${props .refInfo .model } `
228
281
const columnCacheKey = (name : string ) => ` Column/${props .id }:${props .refInfo .model }.${name } `
229
282
@@ -267,7 +320,6 @@ function onHeaderSelected(name:string, e:MouseEvent) {
267
320
}
268
321
}
269
322
270
-
271
323
function onFilterDone() {
272
324
showFilters .value = null
273
325
}
@@ -354,7 +406,6 @@ async function resetPreferences() {
354
406
await update ()
355
407
}
356
408
357
-
358
409
onMounted (async () => {
359
410
// console.debug('ModalLookup.onMounted', props.id, props.refInfo?.model, props.refInfo)
360
411
const prefs = props .prefs || parseJson (storage .getItem (prefsCacheKey ()))
0 commit comments