Files
alfresco-content-app/projects/aca-folder-rules/src/lib/rule-details/conditions/rule-composite-condition.ui-component.ts
swapnil-verma-gl d125fe5ff9 [ACS-5088] Replaced function calls in templates with variable references (#3227)
* [ACS-5088] Replaced method calls in templates with variables

* [ACS-5088] Replaced method calls in templates with variables

* [ACS-5088] Replaced method calls for *ngIf and *ngFor with variables - Batch 1 (WIP)

* [ACS-5088] Replaced method calls for *ngIf and *ngFor with variables - Batch 2 (WIP)

* [ACS-5088] Replaced instances of $any with cast pipe. Replaced other instances of method calls in templates with variables

* [ACS-5088] Resolved test cases

* [ACS-5088] Resolved test cases in aca-content library

* [ACS-5088] Resolved test cases in aca-shared library

* [ACS-5088] Resolved test cases in aca-folder-rules library

* [ACS-5088] Reverted usage of cast pipe to $any()

* [ACS-5088] Fixed incorrect revert

* [ACS-5088] Resolved code review findings - shortened expressions and made onDestroy subjects use void instead of boolean

* [ACS-5088] Resolved code review findings - changed parameter name in sort function

* [ACS-5088] Resolved code review findings - added 'void' type to onDestroy subjects

* [ACS-5088] Upgraded eslint version to 8.41.0. Added "@angular-eslint/template/no-call-expression" rule to prevent function calls in templates unless needed (reports warnings)

* [ACS-5088] Resolved typo in ToggleFavoriteComponent
2023-06-06 04:32:19 -04:00

155 lines
5.3 KiB
TypeScript

/*!
* Copyright © 2005-2023 Hyland Software, Inc. and its affiliates. All rights reserved.
*
* Alfresco Example Content Application
*
* This file is part of the Alfresco Example Content Application.
* If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms:
*
* The Alfresco Example Content Application is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The Alfresco Example Content Application is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* from Hyland Software. If not, see <http://www.gnu.org/licenses/>.
*/
import { Component, forwardRef, HostBinding, Input, OnChanges, OnDestroy, SimpleChanges, ViewEncapsulation } from '@angular/core';
import { RuleCompositeCondition } from '../../model/rule-composite-condition.model';
import { ControlValueAccessor, FormArray, FormControl, FormGroup, NG_VALUE_ACCESSOR } from '@angular/forms';
import { RuleSimpleCondition } from '../../model/rule-simple-condition.model';
@Component({
selector: 'aca-rule-composite-condition',
templateUrl: './rule-composite-condition.ui-component.html',
styleUrls: ['./rule-composite-condition.ui-component.scss'],
encapsulation: ViewEncapsulation.None,
host: { class: 'aca-rule-composite-condition' },
providers: [
{
provide: NG_VALUE_ACCESSOR,
multi: true,
useExisting: forwardRef(() => RuleCompositeConditionUiComponent)
}
]
})
export class RuleCompositeConditionUiComponent implements ControlValueAccessor, OnDestroy, OnChanges {
@HostBinding('class.secondaryBackground')
@Input()
secondaryBackground = false;
@HostBinding('class.childCompositeCondition')
@Input()
childCondition = false;
form = new FormGroup({
inverted: new FormControl(),
booleanMode: new FormControl(),
compositeConditions: new FormArray([]),
simpleConditions: new FormArray([])
});
readonly isOrImplemented = false;
@Input()
public readOnly = false;
private formSubscription = this.form.valueChanges.subscribe((value: any) => {
this.onChange(value);
this.onTouch();
});
public invertedControl = this.form.get('inverted') as FormControl;
public booleanModeControl = this.form.get('booleanMode') as FormControl;
get compositeConditionsFormArray(): FormArray {
return this.form.get('compositeConditions') as FormArray;
}
get simpleConditionsFormArray(): FormArray {
return this.form.get('simpleConditions') as FormArray;
}
get conditionFormControls(): FormControl[] {
return [...(this.compositeConditionsFormArray.controls as FormControl[]), ...(this.simpleConditionsFormArray.controls as FormControl[])];
}
get hasNoConditions(): boolean {
return this.conditionFormControls.length === 0;
}
onChange: (condition: RuleCompositeCondition) => void = () => undefined;
onTouch: () => void = () => undefined;
writeValue(value: RuleCompositeCondition) {
this.form.get('inverted').setValue(value.inverted);
this.form.get('booleanMode').setValue(value.booleanMode);
this.form.setControl('compositeConditions', new FormArray(value.compositeConditions.map((condition) => new FormControl(condition))));
this.form.setControl('simpleConditions', new FormArray(value.simpleConditions.map((condition) => new FormControl(condition))));
}
registerOnChange(fn: () => void) {
this.onChange = fn;
}
registerOnTouched(fn: () => void) {
this.onTouch = fn;
}
setDisabledState(isDisabled: boolean) {
if (isDisabled) {
this.readOnly = true;
this.form.disable();
} else {
this.readOnly = false;
this.form.enable();
}
}
isFormControlSimpleCondition(control: FormControl): boolean {
// eslint-disable-next-line no-prototype-builtins
return control.value.hasOwnProperty('field');
}
removeCondition(control: FormControl) {
const formArray = this.isFormControlSimpleCondition(control) ? this.simpleConditionsFormArray : this.compositeConditionsFormArray;
const index = (formArray.value as FormControl[]).indexOf(control.value);
formArray.removeAt(index);
}
addSimpleCondition() {
const newCondition: RuleSimpleCondition = {
field: 'cm:name',
comparator: 'equals',
parameter: ''
};
this.simpleConditionsFormArray.push(new FormControl(newCondition));
}
addCompositeCondition() {
const newCondition: RuleCompositeCondition = {
inverted: false,
booleanMode: 'and',
compositeConditions: [],
simpleConditions: []
};
this.compositeConditionsFormArray.push(new FormControl(newCondition));
}
ngOnDestroy() {
this.formSubscription.unsubscribe();
}
ngOnChanges(changes: SimpleChanges): void {
const readOnly = changes['readOnly'].currentValue;
if (readOnly !== undefined && readOnly !== null) {
this.readOnly = readOnly;
this.setDisabledState(readOnly);
}
}
}