Skip to content

Commit

Permalink
upgrade dist folder
Browse files Browse the repository at this point in the history
  • Loading branch information
petrhavel2 committed Oct 17, 2023
1 parent ff1d1af commit 9cf11d0
Show file tree
Hide file tree
Showing 45 changed files with 1,737 additions and 0 deletions.
192 changes: 192 additions & 0 deletions dist/common.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dist/common.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions dist/common/generate.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dist/common/generate.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions dist/common/templates/formArrayExtended.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {AbstractControl, FormArray} from '@angular/forms';
import {ControlFactory} from './utils';

/** Extends FormArray so it contains definition of items for further creation */
export class FormArrayExtended extends FormArray {
constructor(public createControl: ControlFactory, controls: AbstractControl[], ...rest: any[]) {
super(controls, ...rest);
}

override setValue(value: any[], options: {onlySelf?: boolean, emitEvent?: boolean} = {}): void {
this.setSize(value.length);
super.setValue(value, options);
}

/**
* Sets specified number of controls in the array
* @param size of the array
*/
setSize(size: number) {
while (size < this.controls.length) this.removeAt(0);
while (size > this.controls.length) this.push(this.createControl());
}
}
28 changes: 28 additions & 0 deletions dist/common/templates/formMap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {AbstractControl, FormGroup} from '@angular/forms';
import {ControlFactory} from './utils';

/** Extends FormGroup so it contains definition of map items for further creation */
export class FormMap extends FormGroup {
constructor(public createControl: ControlFactory, controls: Record<string, AbstractControl>, ...rest: any[]) {
super(controls, ...rest);
}

override setValue(value: Record<string, any>, options: {onlySelf?: boolean, emitEvent?: boolean} = {}): void {
this.setShape(Object.keys(value));
super.setValue(value, options);
}

/**
* Sets child controls for a specified list of keys
* @param keys list of keys new form group should contain
*/
setShape(keys: string[]) {
const allKeys = new Set([...keys, ...Object.keys(this.controls)]);
allKeys.forEach(key => {
// add control for a new one
if (!(key in this.controls)) this.addControl(key, this.createControl());
// remove control if missing
else if (!keys.includes(key)) this.removeControl(key);
});
}
}
52 changes: 52 additions & 0 deletions dist/common/templates/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import {AbstractControl, FormArray, FormControl, FormGroup} from '@angular/forms';

import {FormArrayExtended} from './formArrayExtended';
import {FormMap} from './formMap';

export type ControlFactory = () => AbstractControl;

/**
* Recursively sets values of nested controls so nested object === null/undefined
* does not cause failure as in case of AbstractControl.patchValue
* @param control target FormControl, FormArray or FormGroup
* @param value source data
*/
export function safeSetValue(control: AbstractControl, value: any) {
if (control instanceof FormControl) {
control.setValue(value, {emitEvent: false});
return;
}

if (control instanceof FormArrayExtended) {
if (nullOrUndef(value)) value = [];
if (!Array.isArray(value)) throw new TypeError(`Cannot set value '${value}' on FormArrayExtended`);

control.setSize(value.length);
control.controls.forEach((c, idx) => safeSetValue(c, value[idx]));
} else if (control instanceof FormMap) {
if (nullOrUndef(value)) value = {};
if (typeof value !== 'object' || Array.isArray(value)) {
throw new TypeError(`Cannot set value '${value}' on FormMap`);
}

control.setShape(Object.keys(value));
Object.entries(control.controls).forEach(([name, c]) => safeSetValue(c, value[name]));
} else if (control instanceof FormArray) {
control.controls.forEach((child, idx) =>
safeSetValue(child, getValue(value, idx)));
} else if (control instanceof FormGroup) {
Object.keys(control.controls).forEach(name => {
safeSetValue(control.controls[name], getValue(value, name));
});
}
}

function nullOrUndef(input: any) {
return input === undefined || input === null;
}

function getValue(input: any, attribute: string | number) {
return nullOrUndef(input) || typeof input !== 'object' ?
undefined :
input[attribute];
}
Loading

0 comments on commit 9cf11d0

Please sign in to comment.