onshore-forms 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/karma.conf.js +44 -0
- package/ng-package.json +11 -0
- package/package.json +39 -0
- package/src/lib/components/form-autocomplete-item/form-autocomplete-item.component.html +46 -0
- package/src/lib/components/form-autocomplete-item/form-autocomplete-item.component.scss +0 -0
- package/src/lib/components/form-autocomplete-item/form-autocomplete-item.component.ts +133 -0
- package/src/lib/components/form-checkbox-item/form-checkbox-item.component.html +23 -0
- package/src/lib/components/form-checkbox-item/form-checkbox-item.component.scss +0 -0
- package/src/lib/components/form-checkbox-item/form-checkbox-item.component.ts +65 -0
- package/src/lib/components/form-colorpicker-item/form-colorpicker-item.component.html +30 -0
- package/src/lib/components/form-colorpicker-item/form-colorpicker-item.component.scss +0 -0
- package/src/lib/components/form-colorpicker-item/form-colorpicker-item.component.ts +71 -0
- package/src/lib/components/form-dropdown-item/form-dropdown-item.component.html +44 -0
- package/src/lib/components/form-dropdown-item/form-dropdown-item.component.scss +0 -0
- package/src/lib/components/form-dropdown-item/form-dropdown-item.component.ts +68 -0
- package/src/lib/components/form-image-item/form-image-item.component.html +79 -0
- package/src/lib/components/form-image-item/form-image-item.component.scss +3 -0
- package/src/lib/components/form-image-item/form-image-item.component.ts +155 -0
- package/src/lib/components/form-input-item/form-input-item.component.html +81 -0
- package/src/lib/components/form-input-item/form-input-item.component.scss +3 -0
- package/src/lib/components/form-input-item/form-input-item.component.ts +66 -0
- package/src/lib/components/form-switch-item/form-switch-item.component.html +8 -0
- package/src/lib/components/form-switch-item/form-switch-item.component.scss +0 -0
- package/src/lib/components/form-switch-item/form-switch-item.component.ts +72 -0
- package/src/lib/components/form-textarea-item/form-textarea-item.component.html +39 -0
- package/src/lib/components/form-textarea-item/form-textarea-item.component.scss +0 -0
- package/src/lib/components/form-textarea-item/form-textarea-item.component.ts +64 -0
- package/src/lib/components/form-validation-output/form-validation-output.component.html +2 -0
- package/src/lib/components/form-validation-output/form-validation-output.component.scss +1 -0
- package/src/lib/components/form-validation-output/form-validation-output.component.ts +43 -0
- package/src/lib/enums/form.enums.ts +24 -0
- package/src/lib/models/form.models.ts +70 -0
- package/src/lib/onshore-forms.component.ts +20 -0
- package/src/lib/onshore-forms.module.ts +78 -0
- package/src/lib/onshore-forms.service.ts +172 -0
- package/src/public-api.ts +18 -0
- package/src/test.ts +27 -0
- package/styles/css/main.css +1 -0
- package/styles/css/theme1.css +1 -0
- package/styles/scss/main.scss +4 -0
- package/styles/scss/theme1.scss +8 -0
- package/tsconfig.lib.json +16 -0
- package/tsconfig.lib.prod.json +12 -0
- package/tsconfig.spec.json +17 -0
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { NgModule } from '@angular/core';
|
|
2
|
+
import { OnshoreFormsComponent } from './onshore-forms.component';
|
|
3
|
+
import { OnshoreFormsService } from './onshore-forms.service';
|
|
4
|
+
import { OnshoreFormInputItemComponent } from './components/form-input-item/form-input-item.component';
|
|
5
|
+
import { OnshoreFormSwitchItemComponent } from './components/form-switch-item/form-switch-item.component';
|
|
6
|
+
import { OnshoreFormTextareaItemComponent } from './components/form-textarea-item/form-textarea-item.component';
|
|
7
|
+
import { OnshoreFormValidationOutputComponent } from './components/form-validation-output/form-validation-output.component';
|
|
8
|
+
import { OnshoreFormImageItemComponent } from './components/form-image-item/form-image-item.component';
|
|
9
|
+
import { OnshoreFormDropdownItemComponent } from './components/form-dropdown-item/form-dropdown-item.component';
|
|
10
|
+
import { OnshoreFormColorpickerItemComponent } from './components/form-colorpicker-item/form-colorpicker-item.component';
|
|
11
|
+
import { OnshoreFormCheckboxItemComponent } from './components/form-checkbox-item/form-checkbox-item.component';
|
|
12
|
+
import { OnshoreFormAutocompleteItemComponent } from './components/form-autocomplete-item/form-autocomplete-item.component';
|
|
13
|
+
import { AutoCompleteModule } from 'primeng/autocomplete';
|
|
14
|
+
import { CheckboxModule } from 'primeng/checkbox';
|
|
15
|
+
import { ColorPickerModule } from 'primeng/colorpicker';
|
|
16
|
+
import { DropdownModule } from 'primeng/dropdown';
|
|
17
|
+
import { MultiSelectModule } from 'primeng/multiselect';
|
|
18
|
+
import { TooltipModule } from 'primeng/tooltip';
|
|
19
|
+
import { InputNumberModule } from 'primeng/inputnumber';
|
|
20
|
+
import { InputSwitchModule } from 'primeng/inputswitch';
|
|
21
|
+
import { InputTextModule } from 'primeng/inputtext';
|
|
22
|
+
import { InputTextareaModule } from 'primeng/inputtextarea';
|
|
23
|
+
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
|
|
24
|
+
import { ButtonModule } from 'primeng/button';
|
|
25
|
+
import { RippleModule } from 'primeng/ripple';
|
|
26
|
+
import { DialogModule } from 'primeng/dialog';
|
|
27
|
+
import { CommonModule } from '@angular/common';
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
@NgModule({
|
|
32
|
+
declarations: [
|
|
33
|
+
OnshoreFormsComponent,
|
|
34
|
+
OnshoreFormInputItemComponent,
|
|
35
|
+
OnshoreFormSwitchItemComponent,
|
|
36
|
+
OnshoreFormTextareaItemComponent,
|
|
37
|
+
OnshoreFormValidationOutputComponent,
|
|
38
|
+
OnshoreFormImageItemComponent,
|
|
39
|
+
OnshoreFormDropdownItemComponent,
|
|
40
|
+
OnshoreFormColorpickerItemComponent,
|
|
41
|
+
OnshoreFormCheckboxItemComponent,
|
|
42
|
+
OnshoreFormAutocompleteItemComponent
|
|
43
|
+
],
|
|
44
|
+
imports: [
|
|
45
|
+
CommonModule,
|
|
46
|
+
FormsModule,
|
|
47
|
+
ReactiveFormsModule,
|
|
48
|
+
ButtonModule,
|
|
49
|
+
RippleModule,
|
|
50
|
+
AutoCompleteModule,
|
|
51
|
+
CheckboxModule,
|
|
52
|
+
ColorPickerModule,
|
|
53
|
+
DropdownModule,
|
|
54
|
+
MultiSelectModule,
|
|
55
|
+
TooltipModule,
|
|
56
|
+
InputNumberModule,
|
|
57
|
+
InputSwitchModule,
|
|
58
|
+
InputTextModule,
|
|
59
|
+
InputTextareaModule,
|
|
60
|
+
DialogModule
|
|
61
|
+
],
|
|
62
|
+
exports: [
|
|
63
|
+
OnshoreFormsComponent,
|
|
64
|
+
OnshoreFormInputItemComponent,
|
|
65
|
+
OnshoreFormSwitchItemComponent,
|
|
66
|
+
OnshoreFormTextareaItemComponent,
|
|
67
|
+
OnshoreFormValidationOutputComponent,
|
|
68
|
+
OnshoreFormImageItemComponent,
|
|
69
|
+
OnshoreFormDropdownItemComponent,
|
|
70
|
+
OnshoreFormColorpickerItemComponent,
|
|
71
|
+
OnshoreFormCheckboxItemComponent,
|
|
72
|
+
OnshoreFormAutocompleteItemComponent
|
|
73
|
+
],
|
|
74
|
+
providers: [
|
|
75
|
+
OnshoreFormsService
|
|
76
|
+
]
|
|
77
|
+
})
|
|
78
|
+
export class OnshoreFormsModule { }
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { Injectable } from '@angular/core';
|
|
2
|
+
import { FormArray, FormControl, FormGroup } from '@angular/forms';
|
|
3
|
+
import { Subject, Subscription } from 'rxjs';
|
|
4
|
+
import { OnshoreFormTemplateType } from './enums/form.enums';
|
|
5
|
+
import { OnshoreFormTemplateItem, OnshoreFormValidationItem } from './models/form.models';
|
|
6
|
+
@Injectable({
|
|
7
|
+
providedIn: 'root'
|
|
8
|
+
})
|
|
9
|
+
export class OnshoreFormsService {
|
|
10
|
+
|
|
11
|
+
_form: FormGroup;
|
|
12
|
+
_formTemplate: OnshoreFormTemplateItem[];
|
|
13
|
+
_formValidations: OnshoreFormValidationItem[] = [];
|
|
14
|
+
|
|
15
|
+
subscriptions: Array<Subscription> = [];
|
|
16
|
+
|
|
17
|
+
public isLoading$: Subject<boolean> = new Subject<boolean>();
|
|
18
|
+
|
|
19
|
+
constructor() {
|
|
20
|
+
this._form = new FormGroup({}, {updateOn: 'change'});
|
|
21
|
+
this._formTemplate = [];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
public get form(): FormGroup {
|
|
25
|
+
return this._form;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
public get formValues() {
|
|
29
|
+
return this._form?.value;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
public patchForm(value: any) {
|
|
33
|
+
this._form?.patchValue(value);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
public resetForm() {
|
|
37
|
+
this._form?.reset();
|
|
38
|
+
this._form?.updateValueAndValidity({ onlySelf: false, emitEvent: true });
|
|
39
|
+
this.subscriptions.forEach((subscription: Subscription) => {
|
|
40
|
+
subscription.unsubscribe();
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
this._form = null!;
|
|
44
|
+
this._formTemplate = null!;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
public generateForm(formTemplate: OnshoreFormTemplateItem[]): FormGroup | null {
|
|
48
|
+
this._formTemplate = formTemplate;
|
|
49
|
+
this._form = new FormGroup({}, {updateOn: 'change'});
|
|
50
|
+
this._form = this.iterateNestedForm(this._form, formTemplate);
|
|
51
|
+
return this._form;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
public formTemplate(name: string, formTemplate: OnshoreFormTemplateItem[] = this._formTemplate): OnshoreFormTemplateItem {
|
|
55
|
+
let template: OnshoreFormTemplateItem;
|
|
56
|
+
|
|
57
|
+
if(formTemplate) {
|
|
58
|
+
for(let i = 0; i < formTemplate?.length; i++) {
|
|
59
|
+
const item = formTemplate[i];
|
|
60
|
+
if(item.name === name) {
|
|
61
|
+
template = item;
|
|
62
|
+
i = formTemplate.length;
|
|
63
|
+
}else if(item.subformArray) {
|
|
64
|
+
const arrayTemplate = this.formTemplate(name, item.subformArray);
|
|
65
|
+
if(arrayTemplate) template = arrayTemplate;
|
|
66
|
+
}else if(item.subformObject) {
|
|
67
|
+
const objectTemplate = this.formTemplate(name, item.subformObject);
|
|
68
|
+
if(objectTemplate) template = objectTemplate;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return template!;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
public formControl(name: string, formGroup: FormGroup | null = this._form): FormControl {
|
|
77
|
+
return formGroup?.get(name) as FormControl;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
public formArray(name: string): FormArray {
|
|
81
|
+
return this._form?.get(name) as FormArray;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
public formGroup(form: FormGroup, name: string): FormGroup {
|
|
85
|
+
return form.get(name) as FormGroup;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
public addFormArrayItem(name: string, item: OnshoreFormTemplateItem) {
|
|
89
|
+
let formControls = <any>{};
|
|
90
|
+
|
|
91
|
+
if(item) {
|
|
92
|
+
item.subformArray?.forEach((subitem: OnshoreFormTemplateItem) => {
|
|
93
|
+
formControls[subitem.name] = new FormControl(subitem.default !== undefined ? subitem.default : '', {
|
|
94
|
+
validators: subitem.validators || [],
|
|
95
|
+
updateOn: 'change'
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const formGroup = new FormGroup(formControls);
|
|
101
|
+
formGroup.setParent(this.formArray(name));
|
|
102
|
+
|
|
103
|
+
this.formArray(name).push(formGroup);
|
|
104
|
+
|
|
105
|
+
this.subscribeNestedFormGroup(formGroup);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
public removeFormArrayItem(form: FormArray, index: number) {
|
|
109
|
+
form.removeAt(index);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
public setFormValidations(validations: OnshoreFormValidationItem[] = []) {
|
|
113
|
+
this._formValidations = validations;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
public enable() {
|
|
117
|
+
setTimeout(() => {
|
|
118
|
+
this._form?.enable({onlySelf: true, emitEvent: false});
|
|
119
|
+
}, 100);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
public disable() {
|
|
123
|
+
setTimeout(() => {
|
|
124
|
+
this._form?.disable({onlySelf: true, emitEvent: false});
|
|
125
|
+
}, 100);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
public setError(control: string, error: Object) {
|
|
129
|
+
this.formControl(control).setErrors(error);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
public setLoader(loaders$: Subject<boolean>[]) {
|
|
133
|
+
loaders$.forEach($loader => {
|
|
134
|
+
this.subscriptions.push($loader.subscribe(isLoading => {
|
|
135
|
+
isLoading ? this.disable() : this.enable();
|
|
136
|
+
this.isLoading$.next(isLoading);
|
|
137
|
+
}));
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
iterateNestedForm(form: FormGroup, formTemplate: OnshoreFormTemplateItem[]): any {
|
|
142
|
+
formTemplate.forEach((item: OnshoreFormTemplateItem) => {
|
|
143
|
+
if(item.type === OnshoreFormTemplateType.subform) {
|
|
144
|
+
if(item.subformArray) {
|
|
145
|
+
form.controls[item.name] = new FormArray([]);
|
|
146
|
+
form.controls[item.name].setParent(form);
|
|
147
|
+
}
|
|
148
|
+
if(item.subformObject) {
|
|
149
|
+
form.controls[item.name] = new FormGroup({});
|
|
150
|
+
form.controls[item.name] = this.iterateNestedForm(form.controls[item.name] as FormGroup, item.subformObject) as FormGroup;
|
|
151
|
+
this.subscribeNestedFormGroup(form.controls[item.name] as FormGroup);
|
|
152
|
+
}
|
|
153
|
+
} else {
|
|
154
|
+
form.addControl(item.name, new FormControl(item.default !== undefined ? item.default : '', {
|
|
155
|
+
validators: item.validators || [],
|
|
156
|
+
updateOn: 'change'
|
|
157
|
+
}));
|
|
158
|
+
}
|
|
159
|
+
});
|
|
160
|
+
return form;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
subscribeNestedFormGroup(nestedFormItem: FormGroup) {
|
|
164
|
+
this.subscriptions.push(nestedFormItem.valueChanges.subscribe(() => {
|
|
165
|
+
this.form?.updateValueAndValidity({ onlySelf: true, emitEvent: false });
|
|
166
|
+
}));
|
|
167
|
+
this.subscriptions.push(nestedFormItem.statusChanges.subscribe(() => {
|
|
168
|
+
this.form?.updateValueAndValidity({ onlySelf: true, emitEvent: false });
|
|
169
|
+
}));
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Public API Surface of onshore-forms
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
export * from './lib/onshore-forms.service';
|
|
6
|
+
export * from './lib/onshore-forms.component';
|
|
7
|
+
export * from './lib/onshore-forms.module';
|
|
8
|
+
export * from './lib/enums/form.enums';
|
|
9
|
+
export * from './lib/models/form.models';
|
|
10
|
+
export * from './lib/components/form-autocomplete-item/form-autocomplete-item.component';
|
|
11
|
+
export * from './lib/components/form-checkbox-item/form-checkbox-item.component';
|
|
12
|
+
export * from './lib/components/form-colorpicker-item/form-colorpicker-item.component';
|
|
13
|
+
export * from './lib/components/form-dropdown-item/form-dropdown-item.component';
|
|
14
|
+
export * from './lib/components/form-image-item/form-image-item.component';
|
|
15
|
+
export * from './lib/components/form-input-item/form-input-item.component';
|
|
16
|
+
export * from './lib/components/form-switch-item/form-switch-item.component';
|
|
17
|
+
export * from './lib/components/form-textarea-item/form-textarea-item.component';
|
|
18
|
+
export * from './lib/components/form-validation-output/form-validation-output.component';
|
package/src/test.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// This file is required by karma.conf.js and loads recursively all the .spec and framework files
|
|
2
|
+
|
|
3
|
+
import 'zone.js';
|
|
4
|
+
import 'zone.js/testing';
|
|
5
|
+
import { getTestBed } from '@angular/core/testing';
|
|
6
|
+
import {
|
|
7
|
+
BrowserDynamicTestingModule,
|
|
8
|
+
platformBrowserDynamicTesting
|
|
9
|
+
} from '@angular/platform-browser-dynamic/testing';
|
|
10
|
+
|
|
11
|
+
declare const require: {
|
|
12
|
+
context(path: string, deep?: boolean, filter?: RegExp): {
|
|
13
|
+
<T>(id: string): T;
|
|
14
|
+
keys(): string[];
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
// First, initialize the Angular testing environment.
|
|
19
|
+
getTestBed().initTestEnvironment(
|
|
20
|
+
BrowserDynamicTestingModule,
|
|
21
|
+
platformBrowserDynamicTesting(),
|
|
22
|
+
);
|
|
23
|
+
|
|
24
|
+
// Then we find all the tests.
|
|
25
|
+
const context = require.context('./', true, /\.spec\.ts$/);
|
|
26
|
+
// And load the modules.
|
|
27
|
+
context.keys().forEach(context);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
@import"~primeng/resources/themes/saga-blue/theme.css";@import"~primeng/resources/primeng.min.css";@import"~primeflex/primeflex.min.css";@import"~primeicons/primeicons.css"
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
@import"~primeng/resources/themes/saga-blue/theme.css";@import"~primeng/resources/primeng.min.css";@import"~primeflex/primeflex.min.css";@import"~primeicons/primeicons.css";:root{--color-light: #ffffff;--color-dark: #000000;--color-primary: #24C875;--color-secondary: #20A6FC}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/* To learn more about this file see: https://angular.io/config/tsconfig. */
|
|
2
|
+
{
|
|
3
|
+
"extends": "../../tsconfig.json",
|
|
4
|
+
"compilerOptions": {
|
|
5
|
+
"outDir": "../../out-tsc/lib",
|
|
6
|
+
"declaration": true,
|
|
7
|
+
"declarationMap": true,
|
|
8
|
+
"inlineSources": true,
|
|
9
|
+
"strictNullChecks": false,
|
|
10
|
+
"types": []
|
|
11
|
+
},
|
|
12
|
+
"exclude": [
|
|
13
|
+
"src/test.ts",
|
|
14
|
+
"**/*.spec.ts"
|
|
15
|
+
]
|
|
16
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/* To learn more about this file see: https://angular.io/config/tsconfig. */
|
|
2
|
+
{
|
|
3
|
+
"extends": "./tsconfig.lib.json",
|
|
4
|
+
"compilerOptions": {
|
|
5
|
+
"declarationMap": false,
|
|
6
|
+
"strictNullChecks": false
|
|
7
|
+
},
|
|
8
|
+
"angularCompilerOptions": {
|
|
9
|
+
"compilationMode": "partial",
|
|
10
|
+
"strictTemplates": false
|
|
11
|
+
}
|
|
12
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/* To learn more about this file see: https://angular.io/config/tsconfig. */
|
|
2
|
+
{
|
|
3
|
+
"extends": "../../tsconfig.json",
|
|
4
|
+
"compilerOptions": {
|
|
5
|
+
"outDir": "../../out-tsc/spec",
|
|
6
|
+
"types": [
|
|
7
|
+
"jasmine"
|
|
8
|
+
]
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"src/test.ts"
|
|
12
|
+
],
|
|
13
|
+
"include": [
|
|
14
|
+
"**/*.spec.ts",
|
|
15
|
+
"**/*.d.ts"
|
|
16
|
+
]
|
|
17
|
+
}
|