waibu-db 1.1.11 → 1.1.12
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/lib/crud/list-handler.js
CHANGED
|
@@ -3,7 +3,7 @@ async function listHandler ({ req, reply, model, template, params = {}, addOnsHa
|
|
|
3
3
|
const { recordFind, getSchemaExt } = this.app.waibuDb
|
|
4
4
|
const { get, merge, isArray, upperFirst } = this.lib._
|
|
5
5
|
const qsKey = this.app.waibu.config.qsKey
|
|
6
|
-
const options = { count: true }
|
|
6
|
+
const options = { count: true, rels: '*' }
|
|
7
7
|
model = model ?? pascalCase(req.params.model)
|
|
8
8
|
const { schema } = await getSchemaExt(model, 'list', { params })
|
|
9
9
|
if (schema.disabled.includes('find')) return reply.view(templateDisabled, { action: 'list' })
|
package/package.json
CHANGED
|
@@ -2,8 +2,38 @@ import path from 'path'
|
|
|
2
2
|
|
|
3
3
|
const defReadonly = ['id', 'createdAt', 'updatedAt']
|
|
4
4
|
|
|
5
|
+
const defFormatter = {
|
|
6
|
+
lat: function (val, rec) {
|
|
7
|
+
const format = this.format ?? this.app.bajo.format
|
|
8
|
+
return format(val, 'double', { latitude: true })
|
|
9
|
+
},
|
|
10
|
+
lng: function (val, rec) {
|
|
11
|
+
const format = this.format ?? this.app.bajo.format
|
|
12
|
+
return format(val, 'double', { longitude: true })
|
|
13
|
+
},
|
|
14
|
+
speed: function (val, rec) {
|
|
15
|
+
const format = this.format ?? this.app.bajo.format
|
|
16
|
+
return format(val, 'float', { speed: true, withType: true })
|
|
17
|
+
},
|
|
18
|
+
course: function (val, rec) {
|
|
19
|
+
const format = this.format ?? this.app.bajo.format
|
|
20
|
+
return format(val, 'float', { degree: true })
|
|
21
|
+
},
|
|
22
|
+
heading: function (val, rec) {
|
|
23
|
+
const format = this.format ?? this.app.bajo.format
|
|
24
|
+
return format(val, 'float', { degree: true })
|
|
25
|
+
},
|
|
26
|
+
distance: function (val, rec) {
|
|
27
|
+
const format = this.format ?? this.app.bajo.format
|
|
28
|
+
return format(val, 'float', { distance: true })
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
5
32
|
function getCommons (action, schema, ext, opts = {}) {
|
|
6
|
-
const { map, get, set, without, uniq } = this.lib._
|
|
33
|
+
const { merge, map, get, set, without, uniq } = this.lib._
|
|
34
|
+
const calcFields = get(ext, `view.${action}.calcFields`, get(ext, 'common.calcFields', []))
|
|
35
|
+
const valueFormatter = get(ext, `view.${action}.valueFormatter`, get(ext, 'common.valueFormatter', {}))
|
|
36
|
+
const formatter = get(ext, `view.${action}.formatter`, get(ext, 'common.formatter', {}))
|
|
7
37
|
const label = get(ext, `view.${action}.label`, get(ext, 'common.label', {}))
|
|
8
38
|
const card = get(ext, `view.${action}.card`, get(ext, 'common.card', true))
|
|
9
39
|
const hidden = get(ext, `view.${action}.hidden`, get(ext, 'common.hidden', []))
|
|
@@ -13,15 +43,19 @@ function getCommons (action, schema, ext, opts = {}) {
|
|
|
13
43
|
hidden.push(...schema.hidden, ...(opts.hidden ?? []))
|
|
14
44
|
const allFields = without(map(schema.properties, 'name'), ...hidden)
|
|
15
45
|
const forFields = get(ext, `view.${action}.fields`, get(ext, 'common.fields', allFields))
|
|
46
|
+
set(schema, 'view.calcFields', calcFields)
|
|
47
|
+
set(schema, 'view.valueFormatter', valueFormatter)
|
|
48
|
+
set(schema, 'view.formatter', merge({}, defFormatter, formatter))
|
|
16
49
|
set(schema, 'view.stat.aggregate', aggregate)
|
|
17
50
|
set(schema, 'view.disabled', disabled)
|
|
18
51
|
set(schema, 'view.x', x)
|
|
19
52
|
if (schema.disabled.length > 0) schema.view.disabled.push(...schema.disabled)
|
|
20
53
|
let fields = []
|
|
21
54
|
for (const f of forFields) {
|
|
22
|
-
if (allFields.includes(f)) fields.push(f)
|
|
55
|
+
if (allFields.includes(f) || map(calcFields, 'name').includes(f)) fields.push(f)
|
|
23
56
|
}
|
|
24
57
|
fields = uniq(without(fields, ...hidden))
|
|
58
|
+
|
|
25
59
|
if (action !== 'add' && !fields.includes('id')) fields.unshift('id')
|
|
26
60
|
let noWrap = get(ext, `view.${action}.noWrap`, get(ext, 'common.noWrap', true))
|
|
27
61
|
if (noWrap === true) noWrap = fields
|
|
@@ -126,39 +126,38 @@ async function table () {
|
|
|
126
126
|
let prop = find(schema.properties, { name: f })
|
|
127
127
|
if (!prop) prop = find(schema.view.calcFields, { name: f })
|
|
128
128
|
if (!prop) continue
|
|
129
|
+
let dataValue = d[f] ?? ''
|
|
130
|
+
if (['datetime'].includes(prop.type)) dataValue = escape(dataValue.toISOString())
|
|
131
|
+
if (['string', 'text'].includes(prop.type)) dataValue = escape(dataValue)
|
|
132
|
+
if (['array', 'object'].includes(prop.type)) dataValue = escape(JSON.stringify(d[f]))
|
|
129
133
|
const opts = {}
|
|
130
|
-
if (f === 'lng') opts.longitude = true
|
|
131
|
-
else if (f === 'lat') opts.latitude = true
|
|
132
134
|
let value = req.format(d[f], prop.type, opts)
|
|
133
135
|
if (prop.type === 'boolean') {
|
|
134
136
|
value = (await this.component.buildTag({ tag: 'icon', attr: { name: `circle${d[f] ? 'Check' : ''}` } })) +
|
|
135
137
|
' ' + (req.t(d[f] ? 'Yes' : 'No'))
|
|
136
138
|
} else value = escape(value)
|
|
137
|
-
let dataValue = d[f] ?? ''
|
|
138
|
-
if (['datetime'].includes(prop.type)) dataValue = escape(dataValue.toISOString())
|
|
139
|
-
if (['string', 'text'].includes(prop.type)) dataValue = escape(dataValue)
|
|
140
|
-
if (['array', 'object'].includes(prop.type)) dataValue = escape(JSON.stringify(d[f]))
|
|
141
139
|
const vf = get(schema, `view.valueFormatter.${f}`)
|
|
142
140
|
if (vf) {
|
|
143
|
-
if (isFunction(vf)) dataValue = escape(await vf(d[f], d))
|
|
141
|
+
if (isFunction(vf)) dataValue = escape(await vf.call(req, d[f], d))
|
|
144
142
|
else dataValue = await callHandler(vf, req, d[f], d)
|
|
143
|
+
value = dataValue
|
|
145
144
|
}
|
|
146
145
|
const attr = { dataValue, dataKey: prop.name, dataType: prop.type }
|
|
147
146
|
if (!disableds.includes('get')) attr.style = { cursor: 'pointer' }
|
|
148
147
|
const cellFormatter = get(schema, `view.cellFormatter.${f}`)
|
|
149
|
-
if (cellFormatter) merge(attr, await cellFormatter(dataValue, d))
|
|
148
|
+
if (cellFormatter) merge(attr, await cellFormatter.call(req, dataValue, d))
|
|
150
149
|
const noWrap = this.isNoWrap(f, schema, group.body.nowrap) ? 'nowrap' : ''
|
|
151
150
|
if (this.isRightAligned(f, schema)) attr.text = `align:end ${noWrap}`
|
|
152
151
|
else attr.text = noWrap
|
|
153
152
|
const lookup = get(schema, `view.lookup.${f}`)
|
|
154
153
|
if (lookup) {
|
|
155
|
-
const item = find(lookup.values, set({}, lookup.id ?? 'id',
|
|
154
|
+
const item = find(lookup.values, set({}, lookup.id ?? 'id', d[f]))
|
|
156
155
|
if (item) value = req.t(item[lookup.field ?? 'name'])
|
|
157
156
|
}
|
|
158
157
|
const formatter = get(schema, `view.formatter.${f}`)
|
|
159
158
|
if (formatter) {
|
|
160
|
-
if (isFunction(formatter)) value = await formatter(
|
|
161
|
-
else value = await callHandler(formatter, req,
|
|
159
|
+
if (isFunction(formatter)) value = await formatter.call(req, d[f], d)
|
|
160
|
+
else value = await callHandler(formatter, req, d[f], d)
|
|
162
161
|
value = await this.component.buildSentence(value)
|
|
163
162
|
}
|
|
164
163
|
const line = await this.component.buildTag({ tag: 'td', attr, html: value })
|