schema extensions

This commit is contained in:
2022-01-14 11:51:38 +01:00
parent 0f3fe4362a
commit 110d4e6a3d
16 changed files with 200 additions and 138 deletions
@@ -1,134 +1,151 @@
<template> <template>
<div class="ui-input-list" :class="{'is-disabled': disabled }"> <div class="ui-check-list" :class="{'is-disabled': disabled, 'is-inline': inline }">
<div v-for="item in items" class="ui-input-list-item"> <label v-for="item in list" class="ui-native-check ui-check-list-item">
<input v-model="item.value" type="text" class="ui-input" :maxlength="maxLength" :readonly="disabled" @input="onChange" size="5" /> <input type="checkbox" :checked="isChecked(item)" @input="onChange(item)" :disabled="disabled" />
<ui-icon-button type="light" icon="fth-x" @click="removeItem(item)" v-if="!disabled" /> <span class="ui-native-check-toggle"></span>
</div> <span v-localize="item[labelKey]"></span>
<ui-button v-if="!disabled && !plusButton" type="light" :label="addLabel" @click="addItem" /> <span class="-desc" v-if="item[descriptionKey]" v-localize="item[descriptionKey]"></span>
<ui-select-button v-if="!disabled && plusButton" icon="fth-plus" :label="items.length > 0 ? null : addLabel" @click="addItem" /> </label>
</div> </div>
</template> </template>
<script> <script>
export default { export default {
name: 'uiInputList', name: 'uiCheckList',
props: { props: {
addLabel: {
type: String,
default: '@ui.add'
},
value: { value: {
type: Array, type: Array,
default: () => [] default: () => []
}, },
items: {
type: [Array, Function, Promise],
required: true
},
disabled: { disabled: {
type: Boolean, type: Boolean,
default: false default: false
}, },
maxItems: { inline: {
type: Number,
default: 100
},
maxLength: {
type: Number,
default: 200
},
plusButton: {
type: Boolean, type: Boolean,
default: false default: false
}, },
limit: {
type: Number,
default: 100
},
reverse: Boolean,
labelKey: {
type: String,
default: 'value'
},
descriptionKey: {
type: String,
default: 'description'
},
idKey: {
type: String,
default: 'key'
}
}, },
data: () => ({ data: () => ({
items: [] list: []
}), }),
watch: { watch: {
value(value) items()
{ {
this.reload(); this.init();
} }
}, },
mounted() mounted()
{ {
this.reload(); this.init();
}, },
methods: { methods: {
init()
reload()
{ {
this.items = this.value.map(item => ({ value: item })); if (typeof this.items === 'function')
},
onChange()
{
this.$emit('input', this.items.map(item => item.value));
this.$emit('change', this.items.map(item => item.value));
},
addItem()
{
this.items.push({
value: null
});
this.onChange();
this.$nextTick(() =>
{ {
this.$el.querySelector('.ui-input-list-item:last-of-type input').focus(); this.items().then(res =>
}); {
this.list = res.data;
});
}
else
{
this.list = JSON.parse(JSON.stringify(this.items));
}
}, },
removeItem(item) isChecked(item)
{ {
const index = this.items.indexOf(item); let index = this.value.indexOf(item[this.idKey]);
this.items.splice(index, 1); return (!this.reverse && index > -1) || (this.reverse && index < 0);
this.onChange(); },
onChange(item)
{
let index = this.value.indexOf(item[this.idKey]);
let value = JSON.parse(JSON.stringify(this.value));
if (index < 0)
{
value.push(item[this.idKey]);
}
else
{
value.splice(index, 1);
}
this.$emit('input', value);
} }
} }
} }
</script> </script>
<style lang="scss"> <style lang="scss">
.ui-input-list .ui-check-list-item
{ {
display: block;
} }
.ui-input-list-item .ui-check-list .ui-check-list-item + .ui-check-list-item
{ {
display: grid; margin-top: 8px;
grid-template-columns: 1fr auto auto; }
background: var(--color-input);
border-radius: var(--radius);
margin-bottom: 6px;
.ui-input-list.is-disabled & .ui-alias + .ui-check-list-item
{ {
background: transparent; margin-top: 14px;
} }
.ui-input .ui-check-list.is-inline .ui-check-list-item
{ {
border-radius: var(--radius) 0 0 var(--radius); display: inline-block;
} }
.ui-icon-button .ui-check-list.is-inline .ui-check-list-item + .ui-check-list-item
{ {
border-radius: 0 var(--radius) var(--radius) 0; margin-top: 0;
height: 48px; margin-left: 30px;
width: 48px; }
border-left: none;
background: transparent !important;
box-shadow: none;
}
.ui-icon-button + .ui-icon-button .ui-check-list.is-inline .ui-check-list-item .ui-native-check-toggle
{ {
margin-left: 0; margin-right: 6px;
} }
.ui-check-list-item .-desc
{
display: inline-block;
color: var(--color-text-dim);
font-size: var(--font-size-s);
margin-left: 0.5em;
&:before { content: '('; }
&:after { content: ')'; }
} }
</style> </style>
@@ -143,8 +143,6 @@
this.setState('loading'); this.setState('loading');
this.clearErrors(); this.clearErrors();
console.info(response);
if (!response.success) if (!response.success)
{ {
this.setState('error'); this.setState('error');
@@ -426,7 +426,7 @@
if (this.configuration.paging && typeof items === 'object' && !Array.isArray(items)) if (this.configuration.paging && typeof items === 'object' && !Array.isArray(items))
{ {
// TODO we need to store metadata to allow pagination // TODO we need to store metadata to allow pagination
items = items.items; items = items.data;
} }
if (needsFilter) if (needsFilter)
+6 -1
View File
@@ -1,6 +1,6 @@
import { Emitter, EventType } from "mitt"; import { Emitter, EventType } from "mitt";
import { Component } from "vue"; import { Component } from "vue";
import { ZeroSchema } from "zero/schemas"; import { ZeroSchema, ZeroSchemaExtension } from "zero/schemas";
export interface Zero export interface Zero
{ {
@@ -27,6 +27,11 @@ export interface Zero
**/ **/
getSchema(alias: string): Promise<ZeroSchema | null>; getSchema(alias: string): Promise<ZeroSchema | null>;
/**
* get all defined extensions for a schema
**/
getSchemaExtensions(alias: string): ZeroSchemaExtension[];
/** /**
* get all defined link areas * get all defined link areas
**/ **/
@@ -3,6 +3,7 @@ import { RouteRecordRaw } from 'vue-router';
import { App, Component } from 'vue'; import { App, Component } from 'vue';
import { ZeroSchemaProp } from '../zero'; import { ZeroSchemaProp } from '../zero';
import { ZeroLinkArea } from './zero'; import { ZeroLinkArea } from './zero';
import { ZeroSchema, ZeroSchemaExtension } from 'zero/schemas';
export interface ZeroPluginOptions export interface ZeroPluginOptions
{ {
@@ -11,6 +12,8 @@ export interface ZeroPluginOptions
route: (route: RouteRecordRaw) => void; route: (route: RouteRecordRaw) => void;
schemas: Record<string, ZeroSchemaProp>; schemas: Record<string, ZeroSchemaProp>;
schema: (alias: string, schema: ZeroSchemaProp) => void; schema: (alias: string, schema: ZeroSchemaProp) => void;
schemaExtensions: ZeroSchemaExtension[];
extendSchema: (alias: string, extension: ((schema: ZeroSchema) => void)) => void;
fieldTypes: Record<string, Component>; fieldTypes: Record<string, Component>;
fieldType: (alias: string, component: Component) => void; fieldType: (alias: string, component: Component) => void;
linkAreas: Record<string, ZeroLinkArea>; linkAreas: Record<string, ZeroLinkArea>;
+20 -3
View File
@@ -25,7 +25,7 @@ import
pageModulePlugin pageModulePlugin
} from '../modules'; } from '../modules';
import editorPlugin from '../editor/plugin'; import editorPlugin from '../editor/plugin';
import { ZeroSchema } from 'zero/schemas'; import { ZeroSchema, ZeroSchemaExtension } from 'zero/schemas';
import { ZeroSchemaProp } from './zero'; import { ZeroSchemaProp } from './zero';
import * as zeroOptions from '../options'; import * as zeroOptions from '../options';
import plugins from '../plugins.generated'; import plugins from '../plugins.generated';
@@ -45,6 +45,7 @@ export class ZeroRuntime implements Zero
_installOptions: ZeroInstallOptions; _installOptions: ZeroInstallOptions;
_routerConfig: RouterOptions; _routerConfig: RouterOptions;
_schemas: Record<string, ZeroSchemaProp> = {}; _schemas: Record<string, ZeroSchemaProp> = {};
_schemaExtensions: ZeroSchemaExtension[] = [];
_fieldTypes: Record<string, Component> = {}; _fieldTypes: Record<string, Component> = {};
_linkAreas: Record<string, ZeroLinkArea> = {}; _linkAreas: Record<string, ZeroLinkArea> = {};
@@ -105,6 +106,7 @@ export class ZeroRuntime implements Zero
vue: this._app, vue: this._app,
routes: this._routerConfig.routes, routes: this._routerConfig.routes,
schemas: this._schemas, schemas: this._schemas,
schemaExtensions: this._schemaExtensions,
fieldTypes: this._fieldTypes, fieldTypes: this._fieldTypes,
linkAreas: this._linkAreas, linkAreas: this._linkAreas,
route(route: RouteRecordRaw) route(route: RouteRecordRaw)
@@ -115,6 +117,10 @@ export class ZeroRuntime implements Zero
{ {
this.schemas[alias] = schema; this.schemas[alias] = schema;
}, },
extendSchema(alias: string, extension: ((schema: ZeroSchema) => void))
{
this.schemaExtensions.push({ alias, extension });
},
fieldType(alias: string, component: Component) // TODO v3 allow default field options fieldType(alias: string, component: Component) // TODO v3 allow default field options
{ {
this.fieldTypes[alias] = component; this.fieldTypes[alias] = component;
@@ -171,7 +177,7 @@ export class ZeroRuntime implements Zero
**/ **/
async getSchema(alias: string): Promise<ZeroSchema | null> async getSchema(alias: string): Promise<ZeroSchema | null>
{ {
const schema: ZeroSchemaProp = this._schemas[alias]; let schema: ZeroSchemaProp = this._schemas[alias];
if (!schema) if (!schema)
{ {
@@ -181,13 +187,24 @@ export class ZeroRuntime implements Zero
if (typeof schema === 'function') if (typeof schema === 'function')
{ {
const res = await schema(); const res = await schema();
return res.default; schema = res.default as ZeroSchema;
} }
schema.alias = alias;
return Promise.resolve(schema); return Promise.resolve(schema);
} }
/**
* get all defined extensions for a schema
**/
getSchemaExtensions(alias: string): ZeroSchemaExtension[]
{
return this._schemaExtensions.filter(x => x.alias == alias);
}
/** /**
* get all defined link areas * get all defined link areas
**/ **/
@@ -1,24 +0,0 @@
<template>
<ui-pagepicker :config="config" :value="value" @input="$emit('input', $event)" :disabled="disabled" :limit="1" />
</template>
<script>
export default {
props: {
value: {
type: [String, Array],
default: null
},
disabled: {
type: Boolean,
default: false
},
limit: {
type: Number,
default: 1
},
config: Object
},
}
</script>
+14
View File
@@ -8,6 +8,8 @@ import { ZeroEditorFieldConfiguration, ZeroEditorFieldFilterPreview } from "./ed
import { localize } from '../services/localization'; import { localize } from '../services/localization';
let appliedExtensionsTo: string[] = [];
export interface ZeroCompiledEditor export interface ZeroCompiledEditor
{ {
alias: string; alias: string;
@@ -162,6 +164,18 @@ export function compileEditor(zero: Zero, editor: ZeroEditor): ZeroCompiledEdito
return null; return null;
} }
if (appliedExtensionsTo.indexOf(editor.alias) < 0)
{
appliedExtensionsTo.push(editor.alias);
let extensions = zero.getSchemaExtensions(editor.alias);
extensions.forEach(extension =>
{
extension.extension(editor);
});
}
let model = { let model = {
alias: editor.alias, alias: editor.alias,
blueprint: null, blueprint: null,
@@ -21,6 +21,11 @@ export class ZeroEditorCanvasBase
this.fields.push(proxy); this.fields.push(proxy);
return proxy; return proxy;
} }
getField(path: string): ZeroEditorField | undefined
{
return this.fields.find(x => x.path == path);
}
} }
@@ -55,6 +60,11 @@ export class ZeroEditorCanvas extends ZeroEditorCanvasBaseWithFieldset
this.tabs.push(tab); this.tabs.push(tab);
return tab; return tab;
} }
getTab(alias: string): ZeroEditorTab | undefined
{
return this.tabs.find(x => x.alias == alias);
}
} }
@@ -6,6 +6,7 @@ export default function createFields(app: ZeroPluginOptions): void
{ {
app.fieldType('text', defineAsyncComponent(() => import('./text.vue'))); app.fieldType('text', defineAsyncComponent(() => import('./text.vue')));
app.fieldType('number', defineAsyncComponent(() => import('./number.vue'))); app.fieldType('number', defineAsyncComponent(() => import('./number.vue')));
app.fieldType('password', defineAsyncComponent(() => import('./password.vue')));
app.fieldType('toggle', defineAsyncComponent(() => import('./toggle.vue'))); app.fieldType('toggle', defineAsyncComponent(() => import('./toggle.vue')));
app.fieldType('rte', defineAsyncComponent(() => import('./rte.vue'))); app.fieldType('rte', defineAsyncComponent(() => import('./rte.vue')));
app.fieldType('output', defineAsyncComponent(() => import('./output.vue'))); app.fieldType('output', defineAsyncComponent(() => import('./output.vue')));
@@ -22,6 +22,12 @@ declare module 'zero/schemas'
*/ */
number(options?: NumberFieldOptions): ZeroEditorField; number(options?: NumberFieldOptions): ZeroEditorField;
/**
* Render a password input
* @param {TextFieldOptions} [options] - Custom options
*/
password(options?: TextFieldOptions): ZeroEditorField;
/** /**
* Render a toggle * Render a toggle
* @param {ToggleFieldOptions} [options] - Custom options * @param {ToggleFieldOptions} [options] - Custom options
@@ -132,7 +138,7 @@ declare module 'zero/schemas'
export interface SelectFieldOptions export interface SelectFieldOptions
{ {
items: SelectFieldItem[]; items: SelectFieldItem[] | ((model: any) => SelectFieldItem[]);
emptyOption?: boolean | null; emptyOption?: boolean | null;
} }
@@ -156,6 +162,7 @@ declare module 'zero/schemas'
limit?: number; limit?: number;
reverse?: boolean | null; reverse?: boolean | null;
labelKey?: string; labelKey?: string;
descriptionKey?: string;
idKey?: string; idKey?: string;
} }
@@ -127,8 +127,6 @@
width: this.width, width: this.width,
}); });
console.info('nested result: ', result);
if (result.eventType == 'confirm') if (result.eventType == 'confirm')
{ {
if (isAdd) if (isAdd)
@@ -141,6 +139,8 @@
this.removeItem(index); this.removeItem(index);
this.items.splice(index, 0, result.value); this.items.splice(index, 0, result.value);
} }
this.onChange();
} }
}, },
@@ -154,7 +154,9 @@
onChange() onChange()
{ {
this.$emit('input', this.multiple ? this.items : (this.items.length > 0 ? this.items[0] : null)); let value = this.multiple ? this.items : (this.items.length > 0 ? this.items[0] : null)
this.$emit('input', value);
this.$emit('update:value', value);
}, },
@@ -1,28 +1,22 @@
<template> <template>
<input :value="value" @input="$emit('input', $event.target.value)" type="password" class="ui-input" v-placeholder="{ placeholder, model: entity }" :maxlength="maxLength" :disabled="disabled" /> <input :value="value" @input="$emit('input', $event.target.value)" type="password" class="ui-input" v-placeholder="{ placeholder, model: config.model }" :maxlength="maxLength" :disabled="config.disabled" />
</template> </template>
<script> <script>
export default { export default {
props: { props: {
value: { value: String,
type: String, config: Object,
default: null
},
maxLength: { maxLength: {
type: Number, type: Number,
default: null default: null
}, },
disabled: {
type: Boolean,
default: false
},
placeholder: { placeholder: {
type: String, type: [String, Function],
default: null default: null
}, }
entity: Object
} }
} }
</script> </script>
+22 -10
View File
@@ -32,13 +32,15 @@
watch: { watch: {
items: { items: {
deep: true, deep: true,
handler() handler(val)
{ {
this.rebuild(); if (typeof val !== 'function')
this.onChange({ target: { value: this.value } }); {
this.rebuild();
}
} }
}, },
'config.model.addresses': { 'config.model': {
deep: true, deep: true,
handler() handler()
{ {
@@ -52,20 +54,24 @@
rebuild() rebuild()
{ {
let items = []; let items = [];
let options = [];
if (!this.config.model || !this.items) if (!this.config.model || !this.items)
{ {
this.options = items; options = items;
return;
} }
else if (typeof this.items === 'function')
if (typeof this.items === 'function')
{ {
this.options = this.items(this.config.model); options = this.items(this.config.model);
} }
else else
{ {
this.options = [...this.items]; options = [...this.items];
}
if (JSON.stringify(options) !== JSON.stringify(this.options))
{
this.options = options;
} }
}, },
@@ -78,7 +84,13 @@
value = null; value = null;
} }
if (this.value === value)
{
return;
}
this.$emit('input', value); this.$emit('input', value);
this.$emit('update:value', value);
} }
} }
} }
+6
View File
@@ -5,4 +5,10 @@ declare module 'zero/schemas'
{ {
alias: string; alias: string;
} }
export interface ZeroSchemaExtension
{
alias: string;
extension: (schema: ZeroSchema) => void;
}
} }
+2 -2
View File
@@ -9,8 +9,8 @@ public class CountryStore : EntityStore<Country>, ICountryStore
/// <inheritdoc /> /// <inheritdoc />
protected override void ValidationRules(ZeroValidator<Country> validator) protected override void ValidationRules(ZeroValidator<Country> validator)
{ {
validator.RuleFor(x => x.Code).Length(2).Unique(Session); validator.RuleFor(x => x.Code).NotEmpty().Length(2).Unique(Session);
validator.RuleFor(x => x.Name).Length(2, 120); validator.RuleFor(x => x.Name).NotEmpty().Length(2, 120);
} }
} }