mirror of
https://github.com/lowdefy/lowdefy.git
synced 2025-02-11 14:20:07 +08:00
feat(engine): Add skeleton and loading eval.
This commit is contained in:
parent
31b8190b8d
commit
c3c35d1642
@ -19,7 +19,6 @@
|
||||
import { applyArrayIndices, get, serializer, swap, type } from '@lowdefy/helpers';
|
||||
|
||||
import Events from './Events.js';
|
||||
import getFieldValues from './getFieldValues.js';
|
||||
|
||||
class Blocks {
|
||||
constructor({ arrayIndices, areas, context }) {
|
||||
@ -73,18 +72,22 @@ class Blocks {
|
||||
? applyArrayIndices(this.arrayIndices, block.fieldPattern)
|
||||
: block.blockId;
|
||||
this.context._internal.RootBlocks.map[block.id] = block;
|
||||
block.visible = type.isNone(block.visible) ? true : block.visible;
|
||||
block.required = type.isNone(block.required) ? false : block.required;
|
||||
block.validate = type.isArray(block.validate) ? block.validate : [];
|
||||
block.properties = type.isNone(block.properties) ? {} : block.properties;
|
||||
block.style = type.isNone(block.style) ? {} : block.style;
|
||||
block.layout = type.isNone(block.layout) ? {} : block.layout;
|
||||
block.events = type.isNone(block.events) ? {} : block.events;
|
||||
block.layout = type.isNone(block.layout) ? {} : block.layout;
|
||||
block.loading = type.isNone(block.loading) ? false : block.loading;
|
||||
block.properties = type.isNone(block.properties) ? {} : block.properties;
|
||||
block.required = type.isNone(block.required) ? false : block.required;
|
||||
block.skeleton = type.isNone(block.skeleton) ? null : block.skeleton;
|
||||
block.style = type.isNone(block.style) ? {} : block.style;
|
||||
block.validate = type.isArray(block.validate) ? block.validate : [];
|
||||
block.visible = type.isNone(block.visible) ? true : block.visible;
|
||||
|
||||
block.areasLayoutEval = {};
|
||||
block.layoutEval = {};
|
||||
block.loadingEval = {};
|
||||
block.propertiesEval = {};
|
||||
block.requiredEval = {};
|
||||
block.skeletonEval = {};
|
||||
block.styleEval = {};
|
||||
block.validationEval = {};
|
||||
block.visibleEval = {};
|
||||
@ -102,14 +105,6 @@ class Blocks {
|
||||
block.areasLayout = {};
|
||||
}
|
||||
|
||||
block.requestKeys = getFieldValues(
|
||||
'_request',
|
||||
block.style,
|
||||
block.properties,
|
||||
block.validate,
|
||||
block.visible,
|
||||
block.required
|
||||
);
|
||||
block.methods = {};
|
||||
block.registerMethod = (methodName, method) => {
|
||||
block.methods[methodName] = method;
|
||||
@ -281,8 +276,10 @@ class Blocks {
|
||||
return serializer.serializeToString({
|
||||
areasLayoutEval: block.areasLayoutEval,
|
||||
layoutEval: block.layoutEval,
|
||||
loadingEval: block.loadingEval,
|
||||
propertiesEval: block.propertiesEval,
|
||||
requiredEval: block.requiredEval,
|
||||
skeletonEval: block.skeletonEval,
|
||||
styleEval: block.styleEval,
|
||||
validationEval: block.validationEval,
|
||||
value: block.value,
|
||||
@ -389,6 +386,16 @@ class Blocks {
|
||||
location: block.blockId,
|
||||
arrayIndices: this.arrayIndices,
|
||||
});
|
||||
block.loadingEval = this.context._internal.parser.parse({
|
||||
input: block.loading,
|
||||
location: block.blockId,
|
||||
arrayIndices: this.arrayIndices,
|
||||
});
|
||||
block.skeletonEval = this.context._internal.parser.parse({
|
||||
input: block.skeleton,
|
||||
location: block.blockId,
|
||||
arrayIndices: this.arrayIndices,
|
||||
});
|
||||
block.areasLayoutEval = this.context._internal.parser.parse({
|
||||
input: block.areasLayout,
|
||||
location: block.blockId,
|
||||
@ -573,6 +580,8 @@ class Blocks {
|
||||
areas: block.areasLayoutEval.output,
|
||||
events: type.isNone(block.Events.events) ? null : block.Events.events,
|
||||
properties: block.propertiesEval.output,
|
||||
loading: block.loadingEval.output,
|
||||
skeleton: block.skeletonEval.output,
|
||||
required: block.requiredEval.output,
|
||||
layout: block.layoutEval.output,
|
||||
style: block.styleEval.output,
|
||||
|
@ -1,33 +0,0 @@
|
||||
/*
|
||||
Copyright 2020-2021 Lowdefy, Inc
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
import { serializer } from '@lowdefy/helpers';
|
||||
|
||||
function getFieldValues(operatorName, ...args) {
|
||||
const result = new Set();
|
||||
function reviver(key, value) {
|
||||
if (key === operatorName) {
|
||||
result.add(value);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
[...args].forEach((element) => {
|
||||
serializer.deserializeFromString(serializer.serializeToString(element), { reviver });
|
||||
});
|
||||
return [...result];
|
||||
}
|
||||
|
||||
export default getFieldValues;
|
@ -1,17 +1,17 @@
|
||||
/*
|
||||
Copyright 2020-2021 Lowdefy, Inc
|
||||
Copyright 2020-2021 Lowdefy, Inc
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
import testContext from '../testContext.js';
|
||||
|
@ -453,3 +453,129 @@ test('default value for areas', async () => {
|
||||
const { textInput } = context.RootBlocks.map;
|
||||
expect(textInput.eval.areas).toEqual({});
|
||||
});
|
||||
|
||||
test('parse block loading', async () => {
|
||||
const rootBlock = {
|
||||
blockId: 'root',
|
||||
meta: {
|
||||
category: 'container',
|
||||
},
|
||||
areas: {
|
||||
content: {
|
||||
blocks: [
|
||||
{
|
||||
type: 'TextInput',
|
||||
blockId: 'textInput',
|
||||
meta: {
|
||||
category: 'input',
|
||||
valueType: 'string',
|
||||
},
|
||||
loading: { _state: 'key' },
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
};
|
||||
const context = await testContext({
|
||||
lowdefy,
|
||||
rootBlock,
|
||||
initState: { key: false },
|
||||
});
|
||||
const { textInput } = context.RootBlocks.map;
|
||||
expect(textInput.loading).toEqual({ _state: true });
|
||||
expect(textInput.eval.loading).toEqual(true);
|
||||
});
|
||||
|
||||
test('default value for loading', async () => {
|
||||
const rootBlock = {
|
||||
blockId: 'root',
|
||||
meta: {
|
||||
category: 'container',
|
||||
},
|
||||
areas: {
|
||||
content: {
|
||||
blocks: [
|
||||
{
|
||||
type: 'TextInput',
|
||||
blockId: 'textInput',
|
||||
meta: {
|
||||
category: 'input',
|
||||
valueType: 'string',
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
};
|
||||
const context = await testContext({
|
||||
lowdefy,
|
||||
rootBlock,
|
||||
initState: { key: 'value' },
|
||||
});
|
||||
const { textInput } = context.RootBlocks.map;
|
||||
expect(textInput.loading).toEqual({});
|
||||
expect(textInput.eval.loading).toEqual(false);
|
||||
});
|
||||
|
||||
test('parse block skeleton', async () => {
|
||||
const rootBlock = {
|
||||
blockId: 'root',
|
||||
meta: {
|
||||
category: 'container',
|
||||
},
|
||||
areas: {
|
||||
content: {
|
||||
blocks: [
|
||||
{
|
||||
type: 'TextInput',
|
||||
blockId: 'textInput',
|
||||
meta: {
|
||||
category: 'input',
|
||||
valueType: 'string',
|
||||
},
|
||||
skeleton: { _state: 'key' },
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
};
|
||||
const context = await testContext({
|
||||
lowdefy,
|
||||
rootBlock,
|
||||
initState: { key: false },
|
||||
});
|
||||
const { textInput } = context.RootBlocks.map;
|
||||
expect(textInput.skeleton).toEqual({ _state: 'key' });
|
||||
expect(textInput.eval.skeleton).toEqual(false);
|
||||
});
|
||||
|
||||
test('default value for skeleton', async () => {
|
||||
const rootBlock = {
|
||||
blockId: 'root',
|
||||
meta: {
|
||||
category: 'container',
|
||||
},
|
||||
areas: {
|
||||
content: {
|
||||
blocks: [
|
||||
{
|
||||
type: 'TextInput',
|
||||
blockId: 'textInput',
|
||||
meta: {
|
||||
category: 'input',
|
||||
valueType: 'string',
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
};
|
||||
const context = await testContext({
|
||||
lowdefy,
|
||||
rootBlock,
|
||||
initState: { key: 'value' },
|
||||
});
|
||||
const { textInput } = context.RootBlocks.map;
|
||||
expect(textInput.skeleton).toEqual({});
|
||||
expect(textInput.eval.skeleton).toEqual(null);
|
||||
});
|
||||
|
@ -1,47 +0,0 @@
|
||||
/*
|
||||
Copyright 2020-2021 Lowdefy, Inc
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
import getFieldValues from '../src/getFieldValues.js';
|
||||
|
||||
test('single object', () => {
|
||||
expect(getFieldValues('_req', { _req: 1 })).toEqual([1]);
|
||||
});
|
||||
|
||||
test('multiple objects', () => {
|
||||
expect(getFieldValues('_req', { _req: 1 }, { _req: 2 }, { _req: 1 }, { _req: 4 })).toEqual([
|
||||
1, 2, 4,
|
||||
]);
|
||||
});
|
||||
|
||||
test('multiple arrays', () => {
|
||||
expect(
|
||||
getFieldValues('_req', [{ _req: 1 }], [{ _req: 2 }], [{ _req: 1 }], [{ _req: 4 }])
|
||||
).toEqual([1, 2, 4]);
|
||||
});
|
||||
|
||||
test('multiple mixed', () => {
|
||||
expect(getFieldValues('_req', [{ _req: 1 }], { _req: 2 }, { _req: 1 }, [{ _req: 4 }])).toEqual([
|
||||
1, 2, 4,
|
||||
]);
|
||||
});
|
||||
|
||||
test('get on object of operator', () => {
|
||||
const data = {
|
||||
a: '1',
|
||||
defaultValue: { _request: 'a' },
|
||||
};
|
||||
expect(getFieldValues('defaultValue', data)).toEqual([{ _request: 'a' }]);
|
||||
});
|
Loading…
Reference in New Issue
Block a user