Skip to content

Commit

Permalink
associations
Browse files Browse the repository at this point in the history
  • Loading branch information
Ицкович Алексей Анатольевич committed Feb 10, 2024
1 parent 83c8b68 commit 9a1df78
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 60 deletions.
30 changes: 23 additions & 7 deletions apps/configurator/src/app/components/footer/footer.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { SaveAsFileDirective } from '../../directives/save-as-file/save-as-file.
import { PinsStateService } from '../../services/store/pins-state.service';
import { combineLatest, map, Observable } from 'rxjs';
import { AsyncPipe, NgIf } from '@angular/common';
import { Action, Association, Rule } from '@configurator/shared';
import { Association, Rule } from '@configurator/shared';
import { UploaderModule } from '@configurator/uploader';


Expand Down Expand Up @@ -52,15 +52,14 @@ export class FooterComponent {
}

private rulesToCode(rules: Rule[], associations: Association[]): string {
console.log('==============================');
return rules.map(r => {
console.log(r, associations);

return r;
}).map(r => this.makeRule(r)).join('\n');
}).map(r => this.makeRule(r, associations)).join('\n');
}

private makeRule(rule: Rule): string {
private makeRule(rule: Rule, associations: Association[]): string {
let elseBlock = '';

if (rule.elseBlock.filter(Boolean).length) {
Expand All @@ -73,10 +72,27 @@ ${

if(rule.expression && rule.actions.filter(Boolean).length) {
return `
if (${// @ts-ignore
if (${
rule.expression[0]?.['parentId'] ?? rule.expression[0]} ${rule.expression[1]} ${rule.expression[2]?.['parentId'] ?? rule.expression[2]}) {
${// @ts-ignore
rule.actions.filter(Boolean).map(a => ` ${a.template.replace('{0}', a.parameters[0])}`).join('\n')}${elseBlock}
${
rule.actions
.filter(Boolean)
.map(a => {
const index = associations.findIndex(association => association.uuid === a.parentId);
const hasAssociation = a.template.includes('{1}')
// TODO добавить сообщение об ошибке в форме (отсутствует ассоциация)
if (hasAssociation && index < 0) {
return null;
}
return ` ${
a.template
.replace('{1}', (2 + index).toString())
.replace('{0}', a.parameters[0].toString())}`
})
.filter(Boolean)
.join('\n')}${elseBlock
}
}`
;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ export class AssociationsComponent {
}

public addItem(item: Association): void {
const uuid = crypto.randomUUID();
this.relationsStateService.update({ ...item, uuid });
const parentId = crypto.randomUUID();
const { actions, ...other} = item;
this.relationsStateService.update({ ...other, actions: actions.map(a => ({...a, parentId})), uuid: parentId });
this.changeDetectorRef.detectChanges();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Component, OnDestroy, OnInit } from '@angular/core';
import { FormControl, FormGroup, NG_VALUE_ACCESSOR, Validators } from '@angular/forms';
import { Action, Expression } from '@configurator/shared';
import { distinctUntilChanged, map, Observable, Subject, takeUntil } from 'rxjs';
import { Observable, Subject, takeUntil } from 'rxjs';
import { ExpressionForm } from '../rules.component';
import { PinsStateService } from '../../../../services/store/pins-state.service';

Expand All @@ -22,7 +22,10 @@ export class ExpressionComponent implements OnInit, OnDestroy {

public readonly expressionForm: FormGroup<ExpressionForm>
= new FormGroup<ExpressionForm>({
left: new FormControl<string | null | undefined| number>(null),
left: new FormControl<string>('', {
validators: [Validators.required],
nonNullable: true,
}),
operand: new FormControl<string>('', {
validators: [Validators.required],
nonNullable: true,
Expand Down Expand Up @@ -85,25 +88,13 @@ export class ExpressionComponent implements OnInit, OnDestroy {
public ngOnInit(): void {
this.expressionForm.valueChanges
.pipe(
// filter(() => this.expressionForm.valid),
takeUntil(this.destroy$),
)
.subscribe((data) => {
if (typeof data.right === 'string') {
const le: Action = {
parentId: 'custom id',
title: data.right,
parameters: [data.right],
template: '{0}',
};
}
if (data.right && data.operand) {
this.onChange([
data.left ? this.makeAction(data.left) : data.left, data.operand, this.makeAction(data.right)]);
}
this.onChange([this.makeAction(data.left), data.operand!, this.makeAction(data.right)]);
});

this.watchUnary();
// this.watchUnary();
}

public ngOnDestroy(): void {
Expand All @@ -128,42 +119,40 @@ export class ExpressionComponent implements OnInit, OnDestroy {
}

public writeValue(expression: Expression): void {
if (expression) {
// console.log('[Expression]: writeValue', expression);
if (expression[0] && expression[2]) {
this.expressionForm.controls.left.setValue(expression[0]);
this.expressionForm.controls.operand.setValue(expression[1]);
// @ts-ignore
this.expressionForm.controls.right.setValue(expression[2]);
}
this.expression = expression;
}

private watchUnary(): void {
const unary = this.operandList
.filter(({ unary }) => unary)
.map(({ value }) => value);

this.expressionForm.controls.operand.valueChanges
.pipe(
map((value) => unary.includes(value)),
distinctUntilChanged(),
takeUntil(this.destroy$),
)
.subscribe((disable) => {
const control = this.expressionForm.controls.left;

if (disable) {
control.setValue(null);
control.disable();
} else {
control.enable();
}

this.expressionForm.updateValueAndValidity();
});
}

private makeAction(value: Action | string | number): Action {
// private watchUnary(): void {
// const unary = this.operandList
// .filter(({ unary }) => unary)
// .map(({ value }) => value);
//
// this.expressionForm.controls.operand.valueChanges
// .pipe(
// map((value) => unary.includes(value)),
// distinctUntilChanged(),
// takeUntil(this.destroy$),
// )
// .subscribe((disable) => {
// const control = this.expressionForm.controls.left;
//
// // if (disable) {
// // control.setValue(null);
// // control.disable();
// // } else {
// // control.enable();
// // }
//
// this.expressionForm.updateValueAndValidity();
// });
// }

private makeAction(value: Action | string | number | undefined | null): Action | null {
if (typeof value === 'string' || typeof value === 'number') {
return {
parentId: value.toString(),
Expand All @@ -173,7 +162,7 @@ export class ExpressionComponent implements OnInit, OnDestroy {
}
}

return value;
return value ?? null;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ export class RulesComponent implements OnInit, OnDestroy {
this.form.valueChanges.subscribe(console.log);
}

public addRule({expression, elseBlock, actions}: Rule = {expression: [null, '', ''], elseBlock: [], actions: []}): void {
console.warn('aAAA');
public addRule({expression, elseBlock, actions}: Rule = {expression: [null, '', null], elseBlock: [], actions: []}): void {
const control = new FormGroup<RuleForm>({
expression: new FormControl<Expression | null >(expression),
actions: new FormArray<FormControl<Action>>([]),
Expand Down Expand Up @@ -93,7 +92,7 @@ export interface ActionForm {
}

export interface ExpressionForm {
left: FormControl<string | Action | null | undefined | number>;
left: FormControl<string | Action>;
operand: FormControl<string>;
right: FormControl<string | Action>;
}
Expand Down
1 change: 0 additions & 1 deletion libs/shared/src/lib/models/association.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export interface Action {
title: string;
template: string;
parameters: (ActionParameter | number | string)[];
uuid?: string;
}

export interface Association {
Expand Down
2 changes: 1 addition & 1 deletion libs/shared/src/lib/models/board-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ export interface BoardConfig {
}


export type Expression = [string | number | null | undefined | Action, string, string | number | Action]
export type Expression = [Action | null, string, Action | null]

0 comments on commit 9a1df78

Please sign in to comment.