Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: handle bad form element positions #619

Merged
merged 1 commit into from
Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@
limitations under the License.
*/

import { findIndices, removeAndReturnItemOnward, removeFromMatrix } from '../../../generate-form-definition/helpers';
import {
findIndices,
mapElementMatrix,
removeAndReturnItemOnward,
removeFromMatrix,
} from '../../../generate-form-definition/helpers';
import { getBasicFormDefinition } from '../../__utils__/basic-form-definition';

describe('findIndices', () => {
Expand Down Expand Up @@ -59,3 +64,62 @@ describe('removeAndReturnItemOnward', () => {
expect(formDefinition.elementMatrix).toStrictEqual([['one'], ['five'], ['six', 'seven', 'eight']]);
});
});

describe('mapElementMatrix', () => {
it('vertical - should throw if element is positioned relative to non-existing name', () => {
hein-j marked this conversation as resolved.
Show resolved Hide resolved
const elementQueue = [
{ name: 'a', position: { below: 'b' } },
{ name: 'b', position: { below: 'c' } },
{ name: 'c', position: { below: 'd' } },
];
expect(() => mapElementMatrix({ elementQueue, formDefinition: getBasicFormDefinition() })).toThrow();
});

it('horizontal - should throw if element is positioned relative to non-existing name', () => {
const elementQueue = [
{ name: 'a', position: { rightOf: 'b' } },
{ name: 'b', position: { rightOf: 'c' } },
{ name: 'c', position: { rightOf: 'd' } },
];
expect(() => mapElementMatrix({ elementQueue, formDefinition: getBasicFormDefinition() })).toThrow();
});

it('vertical - should throw if there is a circular dependency', () => {
const elementQueue = [
{ name: 'a', position: { below: 'b' } },
{ name: 'b', position: { below: 'c' } },
{ name: 'c', position: { below: 'a' } },
];
expect(() => mapElementMatrix({ elementQueue, formDefinition: getBasicFormDefinition() })).toThrow();
});

it('horizontal - should throw if there is a circular dependency', () => {
const elementQueue = [
{ name: 'a', position: { rightOf: 'b' } },
{ name: 'b', position: { rightOf: 'c' } },
{ name: 'c', position: { rightOf: 'a' } },
];
expect(() => mapElementMatrix({ elementQueue, formDefinition: getBasicFormDefinition() })).toThrow();
});

it('should map positions', () => {
const elementQueue = [
{ name: 'g', position: { rightOf: 'f' } },
{ name: 'f', position: { below: 'd' } },
{ name: 'e', position: { rightOf: 'd' } },
{ name: 'd', position: { below: 'a' } },
{ name: 'c', position: { rightOf: 'b' } },
{ name: 'b', position: { rightOf: 'a' } },
{ name: 'a' },
];
const formDefinition = getBasicFormDefinition();

mapElementMatrix({ elementQueue, formDefinition });

expect(formDefinition.elementMatrix).toStrictEqual([
['a', 'b', 'c'],
['d', 'e'],
['f', 'g'],
]);
});
});
111 changes: 65 additions & 46 deletions packages/codegen-ui/lib/generate-form-definition/helpers/position.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
import { InvalidInputError } from '../../errors';
import { FormDefinition, StudioFieldPosition } from '../../types';

/**
Expand All @@ -38,6 +39,13 @@ export function removeAndReturnItemOnward(indices: [number, number], formDefinit
return row.splice(indices[1], row.length - indices[1]);
}

// helper that throws if every element traversed but none mapped
function throwIfUnmappable(originalQueue: unknown[], requeued: unknown[]) {
if (originalQueue.length === requeued.length) {
throw new InvalidInputError('Unmappable element positions in form');
}
}

/* eslint-disable no-param-reassign */

/**
Expand All @@ -51,67 +59,78 @@ export function mapElementMatrix({
formDefinition: FormDefinition;
elementQueue: { name: string; position?: StudioFieldPosition; excluded?: boolean }[];
}): void {
const rightOfElementQueue: typeof elementQueue = [];
let belowElementQueue: typeof elementQueue = [...elementQueue];
let rightOfElementQueue: typeof elementQueue = [];

// map elements with no position; position below; position fixed to first
while (elementQueue.length) {
const element = elementQueue.shift();
if (!element) {
break;
}
if (element.excluded) {
const previousIndices = findIndices(element.name, formDefinition.elementMatrix);
while (belowElementQueue.length) {
const requeued: typeof elementQueue = [];
const tempRightOf: typeof elementQueue = [];

if (previousIndices) {
removeFromMatrix(previousIndices, formDefinition);
}
} else if (element.position && 'rightOf' in element.position && element.position.rightOf) {
rightOfElementQueue.push(element);
} else if (element.position && 'below' in element.position && element.position.below) {
const relationIndices = findIndices(element.position.below, formDefinition.elementMatrix);
if (!relationIndices) {
elementQueue.push(element);
} else {
belowElementQueue.forEach((element) => {
if (element.excluded) {
const previousIndices = findIndices(element.name, formDefinition.elementMatrix);

if (previousIndices) {
removeFromMatrix(previousIndices, formDefinition);
}
formDefinition.elementMatrix.splice(relationIndices[0] + 1, 0, [element.name]);
}
} else if (element.position && 'fixed' in element.position && element.position.fixed === 'first') {
const previousIndices = findIndices(element.name, formDefinition.elementMatrix);
if (previousIndices) {
removeFromMatrix(previousIndices, formDefinition);
}
formDefinition.elementMatrix.unshift([element.name]);
} else {
const previousIndices = findIndices(element.name, formDefinition.elementMatrix);
if (!previousIndices) {
formDefinition.elementMatrix.push([element.name]);
} else if (element.position && 'rightOf' in element.position && element.position.rightOf) {
tempRightOf.push(element);
} else if (element.position && 'below' in element.position && element.position.below) {
const relationIndices = findIndices(element.position.below, formDefinition.elementMatrix);
if (!relationIndices) {
requeued.push(element);
} else {
const previousIndices = findIndices(element.name, formDefinition.elementMatrix);
if (previousIndices) {
removeFromMatrix(previousIndices, formDefinition);
}
formDefinition.elementMatrix.splice(relationIndices[0] + 1, 0, [element.name]);
}
} else if (element.position && 'fixed' in element.position && element.position.fixed === 'first') {
const previousIndices = findIndices(element.name, formDefinition.elementMatrix);
if (previousIndices) {
removeFromMatrix(previousIndices, formDefinition);
}
formDefinition.elementMatrix.unshift([element.name]);
} else {
const previousIndices = findIndices(element.name, formDefinition.elementMatrix);
if (!previousIndices) {
formDefinition.elementMatrix.push([element.name]);
}
}
}
});

throwIfUnmappable(belowElementQueue, requeued);

belowElementQueue = requeued;
rightOfElementQueue.push(...tempRightOf);
}

// map elements with rightOf position
while (rightOfElementQueue.length) {
const element = rightOfElementQueue.shift();
if (!element) {
break;
}
if (element.position && 'rightOf' in element.position && element.position.rightOf) {
const relationIndices = findIndices(element.position.rightOf, formDefinition.elementMatrix);
if (!relationIndices) {
rightOfElementQueue.push(element);
} else {
const previousIndices = findIndices(element.name, formDefinition.elementMatrix);
if (previousIndices) {
const removedItems = removeAndReturnItemOnward(previousIndices, formDefinition);
formDefinition.elementMatrix[relationIndices[0]].splice(relationIndices[1] + 1, 0, ...removedItems);
const requeued: typeof elementQueue = [];

rightOfElementQueue.forEach((element) => {
if (element.position && 'rightOf' in element.position && element.position.rightOf) {
const relationIndices = findIndices(element.position.rightOf, formDefinition.elementMatrix);
if (!relationIndices) {
requeued.push(element);
} else {
formDefinition.elementMatrix[relationIndices[0]].splice(relationIndices[1] + 1, 0, element.name);
const previousIndices = findIndices(element.name, formDefinition.elementMatrix);
if (previousIndices) {
const removedItems = removeAndReturnItemOnward(previousIndices, formDefinition);
formDefinition.elementMatrix[relationIndices[0]].splice(relationIndices[1] + 1, 0, ...removedItems);
} else {
formDefinition.elementMatrix[relationIndices[0]].splice(relationIndices[1] + 1, 0, element.name);
}
}
}
}
});

throwIfUnmappable(rightOfElementQueue, requeued);

rightOfElementQueue = requeued;
}

// filter out empty rows
Expand Down