ngx-form-rules 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 ADDED
@@ -0,0 +1,180 @@
1
+ # ngx-form-rules
2
+
3
+ Lightweight Angular helper to enable/disable form controls based on rules.
4
+
5
+ This README explains how to build, test, and consume the library both locally (via the `dist/` folder) and after publishing to npm. It also documents the public API and provides concrete examples that match the rules used by the host app at `src/app/checking-rules.config.ts`.
6
+
7
+ ## Public API
8
+
9
+ The library exposes the following primary symbols from its public API:
10
+
11
+ - `FormRule` (interface)
12
+ - `NgxFormRulesService` (service)
13
+
14
+ ### FormRule
15
+
16
+ ```ts
17
+ export interface FormRule {
18
+ trigger: string; // control name to watch
19
+ onValue: any; // value that triggers the rule (true/false or any value)
20
+ disable: string[]; // control names to disable when rule matches
21
+ enable: string[]; // control names to enable when rule matches
22
+ }
23
+ ```
24
+
25
+ ### NgxFormRulesService
26
+
27
+ Provided in root. Key methods:
28
+
29
+ - `applyRules(form: FormGroup, rules: FormRule[])`
30
+ - Subscribes to `valueChanges` of each trigger and applies enable/disable actions when values change.
31
+
32
+ - `syncRulesWithCurrentValues(form: FormGroup, rules: FormRule[])`
33
+ - Runs rules once against current values (useful on initial load to set correct enabled/disabled states).
34
+
35
+ Usage: inject the service and call `applyRules` and `syncRulesWithCurrentValues` with a `FormGroup` and an array of `FormRule`.
36
+
37
+ ## Example — rules configuration
38
+
39
+ Place your rules in a standalone file such as `src/app/checking-rules.config.ts`. Example matching this repo:
40
+
41
+ ```ts
42
+ import { FormRule } from 'ngx-form-rules';
43
+
44
+ export const RULES: FormRule[] = [
45
+ { trigger: 'isLandCategory', onValue: true, disable: ['isVerifyQualityComments'], enable: ['isLandAccurateData'] },
46
+ { trigger: 'isLandCategory', onValue: false, disable: ['isLandAccurateData'], enable: ['isCommitteeComments'] },
47
+ { trigger: 'landAcqCostBasis', onValue: false, disable: ['isLandCostBasisData'], enable: ['isVeriCostEstiComments'] },
48
+ { trigger: 'landAcqCostBasis', onValue: true, disable: ['isVeriCostEstiComments'], enable: ['isLandCostBasisData'] },
49
+ { trigger: 'rehabComply', onValue: false, disable: ['rehabDataComply'], enable: ['verJustLandComments'] },
50
+ { trigger: 'rehabComply', onValue: true, disable: ['verJustLandComments'], enable: ['rehabDataComply'] }
51
+ ];
52
+ ```
53
+
54
+ > Tip: Import `FormRule` from the package name (`'ngx-form-rules'`) — this works both when consuming from `dist/` (with the workspace `tsconfig` path mapping) and after publishing to npm.
55
+
56
+ ## Example — component wiring
57
+
58
+ Minimal example showing form setup and service usage in `src/app/app.component.ts`:
59
+
60
+ ```ts
61
+ import { Component, OnInit } from '@angular/core';
62
+ import { FormBuilder, FormGroup } from '@angular/forms';
63
+ import { NgxFormRulesService } from 'ngx-form-rules';
64
+ import { RULES } from './checking-rules.config';
65
+
66
+ @Component({ selector: 'app-root', templateUrl: './app.component.html' })
67
+ export class AppComponent implements OnInit {
68
+ form: FormGroup;
69
+
70
+ constructor(private fb: FormBuilder, private rulesService: NgxFormRulesService) {}
71
+
72
+ ngOnInit(): void {
73
+ // create all controls referenced in RULES
74
+ this.form = this.fb.group({
75
+ isLandCategory: [null],
76
+ isVerifyQualityComments: [null],
77
+ isLandAccurateData: [null],
78
+ isCommitteeComments: [null],
79
+ landAcqCostBasis: [null],
80
+ isLandCostBasisData: [null],
81
+ isVeriCostEstiComments: [null],
82
+ rehabComply: [null],
83
+ rehabDataComply: [null],
84
+ verJustLandComments: [null]
85
+ });
86
+
87
+ this.rulesService.applyRules(this.form, RULES);
88
+ this.rulesService.syncRulesWithCurrentValues(this.form, RULES);
89
+ }
90
+ }
91
+ ```
92
+
93
+ And a minimal `app.component.html` showing a couple of controls:
94
+
95
+ ```html
96
+ <form [formGroup]="form">
97
+ <label>
98
+ <input type="checkbox" formControlName="isLandCategory" /> Is Land Category
99
+ </label>
100
+
101
+ <label>
102
+ <input type="checkbox" formControlName="rehabComply" /> Rehab Comply
103
+ </label>
104
+
105
+ <div>
106
+ <label>Land Accurate Data</label>
107
+ <input type="text" formControlName="isLandAccurateData" />
108
+ </div>
109
+
110
+ <div>
111
+ <label>Rehab Data Comply</label>
112
+ <input type="text" formControlName="rehabDataComply" />
113
+ </div>
114
+ </form>
115
+ ```
116
+
117
+ ## Build & local consumption (recommended workflow)
118
+
119
+ You can test the library locally without publishing to npm by building it and using the package name import. The workspace `tsconfig.json` is already configured with a path mapping so `import { ... } from 'ngx-form-rules'` resolves to `dist/ngx-form-rules` after the build.
120
+
121
+ 1. Build the library:
122
+
123
+ ```bash
124
+ ng build ngx-form-rules
125
+ ```
126
+
127
+ 2a. (Preferred for workspace) Restart your dev server or IDE typecheck so imports resolve to `dist/ngx-form-rules`.
128
+
129
+ 2b. (Alternative) Create a tarball and install locally in your app:
130
+
131
+ ```bash
132
+ cd dist/ngx-form-rules
133
+ npm pack
134
+ cd ../../
135
+ npm install ./dist/ngx-form-rules/ngx-form-rules-0.0.1.tgz
136
+ ```
137
+
138
+ ## Publish to npm
139
+
140
+ From the built artifact directory:
141
+
142
+ ```bash
143
+ cd dist/ngx-form-rules
144
+ npm publish --access public
145
+ ```
146
+
147
+ Then in your consumer app (after publishing):
148
+
149
+ ```bash
150
+ npm install ngx-form-rules
151
+ ```
152
+
153
+ and import as:
154
+
155
+ ```ts
156
+ import { NgxFormRulesService, FormRule } from 'ngx-form-rules';
157
+ ```
158
+
159
+ ## Notes & best practices
160
+
161
+ - Ensure every name referenced by `trigger`, `disable`, and `enable` appears as a control in the consuming `FormGroup`.
162
+ - The service marks disabled controls with `emitEvent: false` and will restore values from an internal snapshot where available when re-enabling.
163
+ - Avoid `npm link` for Angular libraries unless you understand peer dependency handling — prefer `npm pack` or workspace path mapping.
164
+
165
+ ## Troubleshooting
166
+
167
+ - If `import 'ngx-form-rules'` is not resolved locally after `ng build ngx-form-rules`, make sure:
168
+ - `tsconfig.json` contains the `paths` entry mapping `ngx-form-rules` to `dist/ngx-form-rules`.
169
+ - You restarted the dev server / TypeScript language service.
170
+
171
+ - If you see runtime errors complaining about multiple Angular instances after `npm link`, remove the link and use `npm pack` / local install instead.
172
+
173
+ ---
174
+
175
+ If you'd like, I can also:
176
+
177
+ - Add a short demo component in `src/app/` wired to the rules and add a unit test.
178
+ - Add a small `USAGE.md` in the root with copy-paste snippets for consuming apps.
179
+
180
+ Happy to add those next.
@@ -0,0 +1,116 @@
1
+ (function (global, factory) {
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/core')) :
3
+ typeof define === 'function' && define.amd ? define('ngx-form-rules', ['exports', '@angular/core'], factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global['ngx-form-rules'] = {}, global.ng.core));
5
+ }(this, (function (exports, i0) { 'use strict';
6
+
7
+ var NgxFormRulesService = /** @class */ (function () {
8
+ function NgxFormRulesService() {
9
+ this.dbSnapshot = {};
10
+ }
11
+ NgxFormRulesService.prototype.applyRules = function (form, rules) {
12
+ var _this = this;
13
+ rules.forEach(function (rule) {
14
+ var _a;
15
+ (_a = form.get(rule.trigger)) === null || _a === void 0 ? void 0 : _a.valueChanges.subscribe(function (value) {
16
+ _this.executeRule(form, rule, value, false);
17
+ });
18
+ });
19
+ };
20
+ NgxFormRulesService.prototype.syncRulesWithCurrentValues = function (form, rules) {
21
+ var _this = this;
22
+ this.dbSnapshot = Object.assign({}, form.getRawValue());
23
+ rules.forEach(function (rule) {
24
+ var _a;
25
+ var currentValue = (_a = form.get(rule.trigger)) === null || _a === void 0 ? void 0 : _a.value;
26
+ _this.executeRule(form, rule, currentValue, true);
27
+ });
28
+ };
29
+ NgxFormRulesService.prototype.executeRule = function (form, rule, currentValue, isInitialLoad) {
30
+ var _this = this;
31
+ if (currentValue === null || currentValue === undefined)
32
+ return;
33
+ var val = currentValue === 'true' || currentValue === true ? true :
34
+ currentValue === 'false' || currentValue === false ? false :
35
+ currentValue;
36
+ if (val === rule.onValue) {
37
+ rule.disable.forEach(function (key) {
38
+ var target = form.get(key);
39
+ if (target) {
40
+ target.disable({ emitEvent: false });
41
+ if (!isInitialLoad) {
42
+ target.setValue(null, { emitEvent: false });
43
+ }
44
+ }
45
+ });
46
+ rule.enable.forEach(function (key) { var _a; return (_a = form.get(key)) === null || _a === void 0 ? void 0 : _a.enable({ emitEvent: false }); });
47
+ }
48
+ else {
49
+ rule.disable.forEach(function (key) {
50
+ var target = form.get(key);
51
+ if (target) {
52
+ target.enable({ emitEvent: false });
53
+ if (!isInitialLoad && _this.dbSnapshot[key] !== undefined) {
54
+ target.setValue(_this.dbSnapshot[key], { emitEvent: false });
55
+ }
56
+ }
57
+ });
58
+ }
59
+ };
60
+ return NgxFormRulesService;
61
+ }());
62
+ NgxFormRulesService.ɵprov = i0.ɵɵdefineInjectable({ factory: function NgxFormRulesService_Factory() { return new NgxFormRulesService(); }, token: NgxFormRulesService, providedIn: "root" });
63
+ NgxFormRulesService.decorators = [
64
+ { type: i0.Injectable, args: [{
65
+ providedIn: 'root'
66
+ },] }
67
+ ];
68
+
69
+ var NgxFormRulesComponent = /** @class */ (function () {
70
+ function NgxFormRulesComponent() {
71
+ }
72
+ NgxFormRulesComponent.prototype.ngOnInit = function () {
73
+ };
74
+ return NgxFormRulesComponent;
75
+ }());
76
+ NgxFormRulesComponent.decorators = [
77
+ { type: i0.Component, args: [{
78
+ selector: 'lib-ngx-form-rules',
79
+ template: "\n <p>\n ngx-form-rules works! xxxxx\n </p>\n "
80
+ },] }
81
+ ];
82
+ NgxFormRulesComponent.ctorParameters = function () { return []; };
83
+
84
+ var NgxFormRulesModule = /** @class */ (function () {
85
+ function NgxFormRulesModule() {
86
+ }
87
+ return NgxFormRulesModule;
88
+ }());
89
+ NgxFormRulesModule.decorators = [
90
+ { type: i0.NgModule, args: [{
91
+ declarations: [
92
+ NgxFormRulesComponent
93
+ ],
94
+ imports: [],
95
+ exports: [
96
+ NgxFormRulesComponent
97
+ ]
98
+ },] }
99
+ ];
100
+
101
+ /*
102
+ * Public API Surface of ngx-form-rules
103
+ */
104
+
105
+ /**
106
+ * Generated bundle index. Do not edit.
107
+ */
108
+
109
+ exports.NgxFormRulesComponent = NgxFormRulesComponent;
110
+ exports.NgxFormRulesModule = NgxFormRulesModule;
111
+ exports.NgxFormRulesService = NgxFormRulesService;
112
+
113
+ Object.defineProperty(exports, '__esModule', { value: true });
114
+
115
+ })));
116
+ //# sourceMappingURL=ngx-form-rules.umd.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ngx-form-rules.umd.js","sources":["../../../projects/ngx-form-rules/src/lib/ngx-form-rules.service.ts","../../../projects/ngx-form-rules/src/lib/ngx-form-rules.component.ts","../../../projects/ngx-form-rules/src/lib/ngx-form-rules.module.ts","../../../projects/ngx-form-rules/src/public-api.ts","../../../projects/ngx-form-rules/src/ngx-form-rules.ts"],"sourcesContent":["import { Injectable } from '@angular/core';\nimport { FormGroup } from '@angular/forms';\n\nexport interface FormRule {\n trigger: string;\n onValue: any;\n disable: string[];\n enable: string[];\n}\n\n@Injectable({\n providedIn: 'root'\n})\nexport class NgxFormRulesService {\n private dbSnapshot: any = {};\n\n applyRules(form: FormGroup, rules: FormRule[]) {\n rules.forEach(rule => {\n form.get(rule.trigger)?.valueChanges.subscribe(value => {\n this.executeRule(form, rule, value, false);\n });\n });\n }\n\n syncRulesWithCurrentValues(form: FormGroup, rules: FormRule[]) {\n this.dbSnapshot = { ...form.getRawValue() };\n\n rules.forEach(rule => {\n const currentValue = form.get(rule.trigger)?.value;\n this.executeRule(form, rule, currentValue, true);\n });\n }\n\n private executeRule(\n form: FormGroup,\n rule: FormRule,\n currentValue: any,\n isInitialLoad: boolean\n ) {\n if (currentValue === null || currentValue === undefined) return;\n\n const val =\n currentValue === 'true' || currentValue === true ? true :\n currentValue === 'false' || currentValue === false ? false :\n currentValue;\n\n if (val === rule.onValue) {\n rule.disable.forEach(key => {\n const target = form.get(key);\n if (target) {\n target.disable({ emitEvent: false });\n if (!isInitialLoad) {\n target.setValue(null, { emitEvent: false });\n }\n }\n });\n\n rule.enable.forEach(key =>\n form.get(key)?.enable({ emitEvent: false })\n );\n\n } else {\n rule.disable.forEach(key => {\n const target = form.get(key);\n if (target) {\n target.enable({ emitEvent: false });\n\n if (!isInitialLoad && this.dbSnapshot[key] !== undefined) {\n target.setValue(this.dbSnapshot[key], { emitEvent: false });\n }\n }\n });\n }\n }\n}\n","import { Component, OnInit } from '@angular/core';\n\n@Component({\n selector: 'lib-ngx-form-rules',\n template: `\n <p>\n ngx-form-rules works! xxxxx\n </p>\n `,\n styles: [\n ]\n})\nexport class NgxFormRulesComponent implements OnInit {\n\n constructor() { }\n\n ngOnInit(): void {\n }\n\n}\n","import { NgModule } from '@angular/core';\nimport { NgxFormRulesComponent } from './ngx-form-rules.component';\n\n\n\n@NgModule({\n declarations: [\n NgxFormRulesComponent\n ],\n imports: [\n ],\n exports: [\n NgxFormRulesComponent\n ]\n})\nexport class NgxFormRulesModule { }\n","/*\n * Public API Surface of ngx-form-rules\n */\n\nexport * from './lib/ngx-form-rules.service';\nexport * from './lib/ngx-form-rules.component';\nexport * from './lib/ngx-form-rules.module';\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './public-api';\n"],"names":["Injectable","Component","NgModule"],"mappings":";;;;;;;QAUA;YAIU,eAAU,GAAQ,EAAE,CAAC;SA4D9B;QA1DC,wCAAU,GAAV,UAAW,IAAe,EAAE,KAAiB;YAA7C,iBAMC;YALC,KAAK,CAAC,OAAO,CAAC,UAAA,IAAI;;gBAChB,MAAA,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,OAAO,CAAC,0CAAE,YAAY,CAAC,SAAS,CAAC,UAAA,KAAK;oBAClD,KAAI,CAAC,WAAW,CAAC,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,CAAC,CAAC;iBAC5C,EAAE;aACJ,CAAC,CAAC;SACJ;QAED,wDAA0B,GAA1B,UAA2B,IAAe,EAAE,KAAiB;YAA7D,iBAOC;YANC,IAAI,CAAC,UAAU,qBAA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,CAAC,UAAA,GAAG;oBACtB,IAAM,MAAM,GAAG,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;oBAC7B,IAAI,MAAM,EAAE;wBACV,MAAM,CAAC,MAAM,CAAC,EAAE,SAAS,EAAE,KAAK,EAAE,CAAC,CAAC;wBAEpC,IAAI,CAAC,aAAa,IAAI,KAAI,CAAC,UAAU,CAAC,GAAG,CAAC,KAAK,SAAS,EAAE;4BACxD,MAAM,CAAC,QAAQ,CAAC,KAAI,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE,EAAE,SAAS,EAAE,KAAK,EAAE,CAAC,CAAC;yBAC7D;qBACF;iBACF,CAAC,CAAC;aACJ;SACF;;;;;gBA/DFA,aAAU,SAAC;oBACV,UAAU,EAAE,MAAM;iBACnB;;;;QCEC;SAAiB;QAEjB,wCAAQ,GAAR;SACC;;;;gBAfFC,YAAS,SAAC;oBACT,QAAQ,EAAE,oBAAoB;oBAC9B,QAAQ,EAAE,4DAIT;iBAGF;;;;;QCID;;;;;gBAVCC,WAAQ,SAAC;oBACR,YAAY,EAAE;wBACZ,qBAAqB;qBACtB;oBACD,OAAO,EAAE,EACR;oBACD,OAAO,EAAE;wBACP,qBAAqB;qBACtB;iBACF;;;ICdD;;;;ICAA;;;;;;;;;;;;;;"}
@@ -0,0 +1,2 @@
1
+ !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@angular/core")):"function"==typeof define&&define.amd?define("ngx-form-rules",["exports","@angular/core"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self)["ngx-form-rules"]={},e.ng.core)}(this,(function(e,t){"use strict";var n=function(){function e(){this.dbSnapshot={}}return e.prototype.applyRules=function(e,t){var n=this;t.forEach((function(t){var o;null===(o=e.get(t.trigger))||void 0===o||o.valueChanges.subscribe((function(o){n.executeRule(e,t,o,!1)}))}))},e.prototype.syncRulesWithCurrentValues=function(e,t){var n=this;this.dbSnapshot=Object.assign({},e.getRawValue()),t.forEach((function(t){var o,r=null===(o=e.get(t.trigger))||void 0===o?void 0:o.value;n.executeRule(e,t,r,!0)}))},e.prototype.executeRule=function(e,t,n,o){var r=this;null!=n&&(("true"===n||!0===n||"false"!==n&&!1!==n&&n)===t.onValue?(t.disable.forEach((function(t){var n=e.get(t);n&&(n.disable({emitEvent:!1}),o||n.setValue(null,{emitEvent:!1}))})),t.enable.forEach((function(t){var n;return null===(n=e.get(t))||void 0===n?void 0:n.enable({emitEvent:!1})}))):t.disable.forEach((function(t){var n=e.get(t);n&&(n.enable({emitEvent:!1}),o||void 0===r.dbSnapshot[t]||n.setValue(r.dbSnapshot[t],{emitEvent:!1}))})))},e}();n.ɵprov=t.ɵɵdefineInjectable({factory:function(){return new n},token:n,providedIn:"root"}),n.decorators=[{type:t.Injectable,args:[{providedIn:"root"}]}];var o=function(){function e(){}return e.prototype.ngOnInit=function(){},e}();o.decorators=[{type:t.Component,args:[{selector:"lib-ngx-form-rules",template:"\n <p>\n ngx-form-rules works! xxxxx\n </p>\n "}]}],o.ctorParameters=function(){return[]};var r=function(){};r.decorators=[{type:t.NgModule,args:[{declarations:[o],imports:[],exports:[o]}]}],e.NgxFormRulesComponent=o,e.NgxFormRulesModule=r,e.NgxFormRulesService=n,Object.defineProperty(e,"__esModule",{value:!0})}));
2
+ //# sourceMappingURL=ngx-form-rules.umd.min.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../projects/ngx-form-rules/src/lib/ngx-form-rules.service.ts","../../../projects/ngx-form-rules/src/lib/ngx-form-rules.component.ts","../../../projects/ngx-form-rules/src/lib/ngx-form-rules.module.ts"],"names":["NgxFormRulesService","this","dbSnapshot","prototype","applyRules","form","rules","_this","forEach","rule","_a","get","trigger","valueChanges","subscribe","value","executeRule","syncRulesWithCurrentValues","Object","assign","getRawValue","currentValue","isInitialLoad","onValue","disable","key","target","emitEvent","setValue","enable","undefined","Injectable","args","providedIn","NgxFormRulesComponent","ngOnInit","Component","selector","template","NgModule","declarations","imports","exports"],"mappings":"gV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W,8HA1D9DI,EAAAA,WAAUC,KAAA,CAAC,CACVC,WAAY,4BCGZ,SAAAC,YAEAA,EAAA/B,UAAAgC,SAAA,sCAdDC,EAAAA,UAASJ,KAAA,CAAC,CACTK,SAAU,qBACVC,SAAU,6GCWZ,iCAVCC,EAAAA,SAAQP,KAAA,CAAC,CACRQ,aAAc,CACZN,GAEFO,QAAS,GAETC,QAAS,CACPR","sourcesContent":["import { Injectable } from '@angular/core';\nimport { FormGroup } from '@angular/forms';\n\nexport interface FormRule {\n trigger: string;\n onValue: any;\n disable: string[];\n enable: string[];\n}\n\n@Injectable({\n providedIn: 'root'\n})\nexport class NgxFormRulesService {\n private dbSnapshot: any = {};\n\n applyRules(form: FormGroup, rules: FormRule[]) {\n rules.forEach(rule => {\n form.get(rule.trigger)?.valueChanges.subscribe(value => {\n this.executeRule(form, rule, value, false);\n });\n });\n }\n\n syncRulesWithCurrentValues(form: FormGroup, rules: FormRule[]) {\n this.dbSnapshot = { ...form.getRawValue() };\n\n rules.forEach(rule => {\n const currentValue = form.get(rule.trigger)?.value;\n this.executeRule(form, rule, currentValue, true);\n });\n }\n\n private executeRule(\n form: FormGroup,\n rule: FormRule,\n currentValue: any,\n isInitialLoad: boolean\n ) {\n if (currentValue === null || currentValue === undefined) return;\n\n const val =\n currentValue === 'true' || currentValue === true ? true :\n currentValue === 'false' || currentValue === false ? false :\n currentValue;\n\n if (val === rule.onValue) {\n rule.disable.forEach(key => {\n const target = form.get(key);\n if (target) {\n target.disable({ emitEvent: false });\n if (!isInitialLoad) {\n target.setValue(null, { emitEvent: false });\n }\n }\n });\n\n rule.enable.forEach(key =>\n form.get(key)?.enable({ emitEvent: false })\n );\n\n } else {\n rule.disable.forEach(key => {\n const target = form.get(key);\n if (target) {\n target.enable({ emitEvent: false });\n\n if (!isInitialLoad && this.dbSnapshot[key] !== undefined) {\n target.setValue(this.dbSnapshot[key], { emitEvent: false });\n }\n }\n });\n }\n }\n}\n","import { Component, OnInit } from '@angular/core';\n\n@Component({\n selector: 'lib-ngx-form-rules',\n template: `\n <p>\n ngx-form-rules works! xxxxx\n </p>\n `,\n styles: [\n ]\n})\nexport class NgxFormRulesComponent implements OnInit {\n\n constructor() { }\n\n ngOnInit(): void {\n }\n\n}\n","import { NgModule } from '@angular/core';\nimport { NgxFormRulesComponent } from './ngx-form-rules.component';\n\n\n\n@NgModule({\n declarations: [\n NgxFormRulesComponent\n ],\n imports: [\n ],\n exports: [\n NgxFormRulesComponent\n ]\n})\nexport class NgxFormRulesModule { }\n"]}
@@ -0,0 +1,18 @@
1
+ import { Component } from '@angular/core';
2
+ export class NgxFormRulesComponent {
3
+ constructor() { }
4
+ ngOnInit() {
5
+ }
6
+ }
7
+ NgxFormRulesComponent.decorators = [
8
+ { type: Component, args: [{
9
+ selector: 'lib-ngx-form-rules',
10
+ template: `
11
+ <p>
12
+ ngx-form-rules works! xxxxx
13
+ </p>
14
+ `
15
+ },] }
16
+ ];
17
+ NgxFormRulesComponent.ctorParameters = () => [];
18
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibmd4LWZvcm0tcnVsZXMuY29tcG9uZW50LmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vLi4vLi4vcHJvamVjdHMvbmd4LWZvcm0tcnVsZXMvc3JjL2xpYi9uZ3gtZm9ybS1ydWxlcy5jb21wb25lbnQudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxFQUFFLFNBQVMsRUFBVSxNQUFNLGVBQWUsQ0FBQztBQVlsRCxNQUFNLE9BQU8scUJBQXFCO0lBRWhDLGdCQUFnQixDQUFDO0lBRWpCLFFBQVE7SUFDUixDQUFDOzs7WUFmRixTQUFTLFNBQUM7Z0JBQ1QsUUFBUSxFQUFFLG9CQUFvQjtnQkFDOUIsUUFBUSxFQUFFOzs7O0dBSVQ7YUFHRiIsInNvdXJjZXNDb250ZW50IjpbImltcG9ydCB7IENvbXBvbmVudCwgT25Jbml0IH0gZnJvbSAnQGFuZ3VsYXIvY29yZSc7XG5cbkBDb21wb25lbnQoe1xuICBzZWxlY3RvcjogJ2xpYi1uZ3gtZm9ybS1ydWxlcycsXG4gIHRlbXBsYXRlOiBgXG4gICAgPHA+XG4gICAgICBuZ3gtZm9ybS1ydWxlcyB3b3JrcyEgeHh4eHhcbiAgICA8L3A+XG4gIGAsXG4gIHN0eWxlczogW1xuICBdXG59KVxuZXhwb3J0IGNsYXNzIE5neEZvcm1SdWxlc0NvbXBvbmVudCBpbXBsZW1lbnRzIE9uSW5pdCB7XG5cbiAgY29uc3RydWN0b3IoKSB7IH1cblxuICBuZ09uSW5pdCgpOiB2b2lkIHtcbiAgfVxuXG59XG4iXX0=
@@ -0,0 +1,16 @@
1
+ import { NgModule } from '@angular/core';
2
+ import { NgxFormRulesComponent } from './ngx-form-rules.component';
3
+ export class NgxFormRulesModule {
4
+ }
5
+ NgxFormRulesModule.decorators = [
6
+ { type: NgModule, args: [{
7
+ declarations: [
8
+ NgxFormRulesComponent
9
+ ],
10
+ imports: [],
11
+ exports: [
12
+ NgxFormRulesComponent
13
+ ]
14
+ },] }
15
+ ];
16
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibmd4LWZvcm0tcnVsZXMubW9kdWxlLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vLi4vLi4vcHJvamVjdHMvbmd4LWZvcm0tcnVsZXMvc3JjL2xpYi9uZ3gtZm9ybS1ydWxlcy5tb2R1bGUudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxFQUFFLFFBQVEsRUFBRSxNQUFNLGVBQWUsQ0FBQztBQUN6QyxPQUFPLEVBQUUscUJBQXFCLEVBQUUsTUFBTSw0QkFBNEIsQ0FBQztBQWNuRSxNQUFNLE9BQU8sa0JBQWtCOzs7WUFWOUIsUUFBUSxTQUFDO2dCQUNSLFlBQVksRUFBRTtvQkFDWixxQkFBcUI7aUJBQ3RCO2dCQUNELE9BQU8sRUFBRSxFQUNSO2dCQUNELE9BQU8sRUFBRTtvQkFDUCxxQkFBcUI7aUJBQ3RCO2FBQ0YiLCJzb3VyY2VzQ29udGVudCI6WyJpbXBvcnQgeyBOZ01vZHVsZSB9IGZyb20gJ0Bhbmd1bGFyL2NvcmUnO1xuaW1wb3J0IHsgTmd4Rm9ybVJ1bGVzQ29tcG9uZW50IH0gZnJvbSAnLi9uZ3gtZm9ybS1ydWxlcy5jb21wb25lbnQnO1xuXG5cblxuQE5nTW9kdWxlKHtcbiAgZGVjbGFyYXRpb25zOiBbXG4gICAgTmd4Rm9ybVJ1bGVzQ29tcG9uZW50XG4gIF0sXG4gIGltcG9ydHM6IFtcbiAgXSxcbiAgZXhwb3J0czogW1xuICAgIE5neEZvcm1SdWxlc0NvbXBvbmVudFxuICBdXG59KVxuZXhwb3J0IGNsYXNzIE5neEZvcm1SdWxlc01vZHVsZSB7IH1cbiJdfQ==
@@ -0,0 +1,60 @@
1
+ import { Injectable } from '@angular/core';
2
+ import * as i0 from "@angular/core";
3
+ export class NgxFormRulesService {
4
+ constructor() {
5
+ this.dbSnapshot = {};
6
+ }
7
+ applyRules(form, rules) {
8
+ rules.forEach(rule => {
9
+ var _a;
10
+ (_a = form.get(rule.trigger)) === null || _a === void 0 ? void 0 : _a.valueChanges.subscribe(value => {
11
+ this.executeRule(form, rule, value, false);
12
+ });
13
+ });
14
+ }
15
+ syncRulesWithCurrentValues(form, rules) {
16
+ this.dbSnapshot = Object.assign({}, form.getRawValue());
17
+ rules.forEach(rule => {
18
+ var _a;
19
+ const currentValue = (_a = form.get(rule.trigger)) === null || _a === void 0 ? void 0 : _a.value;
20
+ this.executeRule(form, rule, currentValue, true);
21
+ });
22
+ }
23
+ executeRule(form, rule, currentValue, isInitialLoad) {
24
+ if (currentValue === null || currentValue === undefined)
25
+ return;
26
+ const val = currentValue === 'true' || currentValue === true ? true :
27
+ currentValue === 'false' || currentValue === false ? false :
28
+ currentValue;
29
+ if (val === rule.onValue) {
30
+ rule.disable.forEach(key => {
31
+ const target = form.get(key);
32
+ if (target) {
33
+ target.disable({ emitEvent: false });
34
+ if (!isInitialLoad) {
35
+ target.setValue(null, { emitEvent: false });
36
+ }
37
+ }
38
+ });
39
+ rule.enable.forEach(key => { var _a; return (_a = form.get(key)) === null || _a === void 0 ? void 0 : _a.enable({ emitEvent: false }); });
40
+ }
41
+ else {
42
+ rule.disable.forEach(key => {
43
+ const target = form.get(key);
44
+ if (target) {
45
+ target.enable({ emitEvent: false });
46
+ if (!isInitialLoad && this.dbSnapshot[key] !== undefined) {
47
+ target.setValue(this.dbSnapshot[key], { emitEvent: false });
48
+ }
49
+ }
50
+ });
51
+ }
52
+ }
53
+ }
54
+ NgxFormRulesService.ɵprov = i0.ɵɵdefineInjectable({ factory: function NgxFormRulesService_Factory() { return new NgxFormRulesService(); }, token: NgxFormRulesService, providedIn: "root" });
55
+ NgxFormRulesService.decorators = [
56
+ { type: Injectable, args: [{
57
+ providedIn: 'root'
58
+ },] }
59
+ ];
60
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibmd4LWZvcm0tcnVsZXMuc2VydmljZS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uLy4uL3Byb2plY3RzL25neC1mb3JtLXJ1bGVzL3NyYy9saWIvbmd4LWZvcm0tcnVsZXMuc2VydmljZS50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxPQUFPLEVBQUUsVUFBVSxFQUFFLE1BQU0sZUFBZSxDQUFDOztBQWEzQyxNQUFNLE9BQU8sbUJBQW1CO0lBSGhDO1FBSVUsZUFBVSxHQUFRLEVBQUUsQ0FBQztLQTREOUI7SUExREMsVUFBVSxDQUFDLElBQWUsRUFBRSxLQUFpQjtRQUMzQyxLQUFLLENBQUMsT0FBTyxDQUFDL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0pO1xuICAgICAgICAgIGlmICghaXNJbml0aWFsTG9hZCkge1xuICAgICAgICAgICAgdGFyZ2V0LnNldFZhbHVlKG51bGwsIHsgZW1pdEV2ZW50OiBmYWxzZSB9KTtcbiAgICAgICAgICB9XG4gICAgICAgIH1cbiAgICAgIH0pO1xuXG4gICAgICBydWxlLmVuYWJsZS5mb3JFYWNoKGtleSA9PlxuICAgICAgICBmb3JtLmdldChrZXkpPy5lbmFibGUoeyBlbWl0RXZlbnQ6IGZhbHNlIH0pXG4gICAgICApO1xuXG4gICAgfSBlbHNlIHtcbiAgICAgIHJ1bGUuZGlzYWJsZS5mb3JFYWNoKGtleSA9PiB7XG4gICAgICAgIGNvbnN0IHRhcmdldCA9IGZvcm0uZ2V0KGtleSk7XG4gICAgICAgIGlmICh0YXJnZXQpIHtcbiAgICAgICAgICB0YXJnZXQuZW5hYmxlKHsgZW1pdEV2ZW50OiBmYWxzZSB9KTtcblxuICAgICAgICAgIGlmICghaXNJbml0aWFsTG9hZCAmJiB0aGlzLmRiU25hcHNob3Rba2V5XSAhPT0gdW5kZWZpbmVkKSB7XG4gICAgICAgICAgICB0YXJnZXQuc2V0VmFsdWUodGhpcy5kYlNuYXBzaG90W2tleV0sIHsgZW1pdEV2ZW50OiBmYWxzZSB9KTtcbiAgICAgICAgICB9XG4gICAgICAgIH1cbiAgICAgIH0pO1xuICAgIH1cbiAgfVxufVxuIl19
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Generated bundle index. Do not edit.
3
+ */
4
+ export * from './public-api';
5
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibmd4LWZvcm0tcnVsZXMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi9wcm9qZWN0cy9uZ3gtZm9ybS1ydWxlcy9zcmMvbmd4LWZvcm0tcnVsZXMudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUE7O0dBRUc7QUFFSCxjQUFjLGNBQWMsQ0FBQyIsInNvdXJjZXNDb250ZW50IjpbIi8qKlxuICogR2VuZXJhdGVkIGJ1bmRsZSBpbmRleC4gRG8gbm90IGVkaXQuXG4gKi9cblxuZXhwb3J0ICogZnJvbSAnLi9wdWJsaWMtYXBpJztcbiJdfQ==
@@ -0,0 +1,7 @@
1
+ /*
2
+ * Public API Surface of ngx-form-rules
3
+ */
4
+ export * from './lib/ngx-form-rules.service';
5
+ export * from './lib/ngx-form-rules.component';
6
+ export * from './lib/ngx-form-rules.module';
7
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicHVibGljLWFwaS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uL3Byb2plY3RzL25neC1mb3JtLXJ1bGVzL3NyYy9wdWJsaWMtYXBpLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBOztHQUVHO0FBRUgsY0FBYyw4QkFBOEIsQ0FBQztBQUM3QyxjQUFjLGdDQUFnQyxDQUFDO0FBQy9DLGNBQWMsNkJBQTZCLENBQUMiLCJzb3VyY2VzQ29udGVudCI6WyIvKlxuICogUHVibGljIEFQSSBTdXJmYWNlIG9mIG5neC1mb3JtLXJ1bGVzXG4gKi9cblxuZXhwb3J0ICogZnJvbSAnLi9saWIvbmd4LWZvcm0tcnVsZXMuc2VydmljZSc7XG5leHBvcnQgKiBmcm9tICcuL2xpYi9uZ3gtZm9ybS1ydWxlcy5jb21wb25lbnQnO1xuZXhwb3J0ICogZnJvbSAnLi9saWIvbmd4LWZvcm0tcnVsZXMubW9kdWxlJztcbiJdfQ==
@@ -0,0 +1,101 @@
1
+ import { ɵɵdefineInjectable, Injectable, Component, NgModule } from '@angular/core';
2
+
3
+ class NgxFormRulesService {
4
+ constructor() {
5
+ this.dbSnapshot = {};
6
+ }
7
+ applyRules(form, rules) {
8
+ rules.forEach(rule => {
9
+ var _a;
10
+ (_a = form.get(rule.trigger)) === null || _a === void 0 ? void 0 : _a.valueChanges.subscribe(value => {
11
+ this.executeRule(form, rule, value, false);
12
+ });
13
+ });
14
+ }
15
+ syncRulesWithCurrentValues(form, rules) {
16
+ this.dbSnapshot = Object.assign({}, form.getRawValue());
17
+ rules.forEach(rule => {
18
+ var _a;
19
+ const currentValue = (_a = form.get(rule.trigger)) === null || _a === void 0 ? void 0 : _a.value;
20
+ this.executeRule(form, rule, currentValue, true);
21
+ });
22
+ }
23
+ executeRule(form, rule, currentValue, isInitialLoad) {
24
+ if (currentValue === null || currentValue === undefined)
25
+ return;
26
+ const val = currentValue === 'true' || currentValue === true ? true :
27
+ currentValue === 'false' || currentValue === false ? false :
28
+ currentValue;
29
+ if (val === rule.onValue) {
30
+ rule.disable.forEach(key => {
31
+ const target = form.get(key);
32
+ if (target) {
33
+ target.disable({ emitEvent: false });
34
+ if (!isInitialLoad) {
35
+ target.setValue(null, { emitEvent: false });
36
+ }
37
+ }
38
+ });
39
+ rule.enable.forEach(key => { var _a; return (_a = form.get(key)) === null || _a === void 0 ? void 0 : _a.enable({ emitEvent: false }); });
40
+ }
41
+ else {
42
+ rule.disable.forEach(key => {
43
+ const target = form.get(key);
44
+ if (target) {
45
+ target.enable({ emitEvent: false });
46
+ if (!isInitialLoad && this.dbSnapshot[key] !== undefined) {
47
+ target.setValue(this.dbSnapshot[key], { emitEvent: false });
48
+ }
49
+ }
50
+ });
51
+ }
52
+ }
53
+ }
54
+ NgxFormRulesService.ɵprov = ɵɵdefineInjectable({ factory: function NgxFormRulesService_Factory() { return new NgxFormRulesService(); }, token: NgxFormRulesService, providedIn: "root" });
55
+ NgxFormRulesService.decorators = [
56
+ { type: Injectable, args: [{
57
+ providedIn: 'root'
58
+ },] }
59
+ ];
60
+
61
+ class NgxFormRulesComponent {
62
+ constructor() { }
63
+ ngOnInit() {
64
+ }
65
+ }
66
+ NgxFormRulesComponent.decorators = [
67
+ { type: Component, args: [{
68
+ selector: 'lib-ngx-form-rules',
69
+ template: `
70
+ <p>
71
+ ngx-form-rules works! xxxxx
72
+ </p>
73
+ `
74
+ },] }
75
+ ];
76
+ NgxFormRulesComponent.ctorParameters = () => [];
77
+
78
+ class NgxFormRulesModule {
79
+ }
80
+ NgxFormRulesModule.decorators = [
81
+ { type: NgModule, args: [{
82
+ declarations: [
83
+ NgxFormRulesComponent
84
+ ],
85
+ imports: [],
86
+ exports: [
87
+ NgxFormRulesComponent
88
+ ]
89
+ },] }
90
+ ];
91
+
92
+ /*
93
+ * Public API Surface of ngx-form-rules
94
+ */
95
+
96
+ /**
97
+ * Generated bundle index. Do not edit.
98
+ */
99
+
100
+ export { NgxFormRulesComponent, NgxFormRulesModule, NgxFormRulesService };
101
+ //# sourceMappingURL=ngx-form-rules.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ngx-form-rules.js","sources":["../../../projects/ngx-form-rules/src/lib/ngx-form-rules.service.ts","../../../projects/ngx-form-rules/src/lib/ngx-form-rules.component.ts","../../../projects/ngx-form-rules/src/lib/ngx-form-rules.module.ts","../../../projects/ngx-form-rules/src/public-api.ts","../../../projects/ngx-form-rules/src/ngx-form-rules.ts"],"sourcesContent":["import { Injectable } from '@angular/core';\nimport { FormGroup } from '@angular/forms';\n\nexport interface FormRule {\n trigger: string;\n onValue: any;\n disable: string[];\n enable: string[];\n}\n\n@Injectable({\n providedIn: 'root'\n})\nexport class NgxFormRulesService {\n private dbSnapshot: any = {};\n\n applyRules(form: FormGroup, rules: FormRule[]) {\n rules.forEach(rule => {\n form.get(rule.trigger)?.valueChanges.subscribe(value => {\n this.executeRule(form, rule, value, false);\n });\n });\n }\n\n syncRulesWithCurrentValues(form: FormGroup, rules: FormRule[]) {\n this.dbSnapshot = { ...form.getRawValue() };\n\n rules.forEach(rule => {\n const currentValue = form.get(rule.trigger)?.value;\n this.executeRule(form, rule, currentValue, true);\n });\n }\n\n private executeRule(\n form: FormGroup,\n rule: FormRule,\n currentValue: any,\n isInitialLoad: boolean\n ) {\n if (currentValue === null || currentValue === undefined) return;\n\n const val =\n currentValue === 'true' || currentValue === true ? true :\n currentValue === 'false' || currentValue === false ? false :\n currentValue;\n\n if (val === rule.onValue) {\n rule.disable.forEach(key => {\n const target = form.get(key);\n if (target) {\n target.disable({ emitEvent: false });\n if (!isInitialLoad) {\n target.setValue(null, { emitEvent: false });\n }\n }\n });\n\n rule.enable.forEach(key =>\n form.get(key)?.enable({ emitEvent: false })\n );\n\n } else {\n rule.disable.forEach(key => {\n const target = form.get(key);\n if (target) {\n target.enable({ emitEvent: false });\n\n if (!isInitialLoad && this.dbSnapshot[key] !== undefined) {\n target.setValue(this.dbSnapshot[key], { emitEvent: false });\n }\n }\n });\n }\n }\n}\n","import { Component, OnInit } from '@angular/core';\n\n@Component({\n selector: 'lib-ngx-form-rules',\n template: `\n <p>\n ngx-form-rules works! xxxxx\n </p>\n `,\n styles: [\n ]\n})\nexport class NgxFormRulesComponent implements OnInit {\n\n constructor() { }\n\n ngOnInit(): void {\n }\n\n}\n","import { NgModule } from '@angular/core';\nimport { NgxFormRulesComponent } from './ngx-form-rules.component';\n\n\n\n@NgModule({\n declarations: [\n NgxFormRulesComponent\n ],\n imports: [\n ],\n exports: [\n NgxFormRulesComponent\n ]\n})\nexport class NgxFormRulesModule { }\n","/*\n * Public API Surface of ngx-form-rules\n */\n\nexport * from './lib/ngx-form-rules.service';\nexport * from './lib/ngx-form-rules.component';\nexport * from './lib/ngx-form-rules.module';\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './public-api';\n"],"names":[],"mappings":";;MAaa,mBAAmB;IAHhC;QAIU,eAAU,GAAQ,EAAE,CAAC;KA4D9B;IA1DC,UAAU,CAAC,IAAe,EAAE,KAAiB;QAC3C,KAAK,CAAC,OAAO,CAAC,IAAI;;YAChB,MAAA,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,OAAO,CAAC,0CAAE,YAAY,CAAC,SAAS,CAAC,KAAK;gBAClD,IAAI,CAAC,WAAW,CAAC,IAAI,EAA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AAC,EAAE,SAAS,EAAE,KAAK,EAAE,IAAC,CAC5C,CAAC;SAEH;aAAM;YACL,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,GAAG;gBACtB,MAAM,MAAM,GAAG,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;gBAC7B,IAAI,MAAM,EAAE;oBACV,MAAM,CAAC,MAAM,CAAC,EAAE,SAAS,EAAE,KAAK,EAAE,CAAC,CAAC;oBAEpC,IAAI,CAAC,aAAa,IAAI,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,KAAK,SAAS,EAAE;wBACxD,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE,EAAE,SAAS,EAAE,KAAK,EAAE,CAAC,CAAC;qBAC7D;iBACF;aACF,CAAC,CAAC;SACJ;KACF;;;;YA/DF,UAAU,SAAC;gBACV,UAAU,EAAE,MAAM;aACnB;;;MCAY,qBAAqB;IAEhC,iBAAiB;IAEjB,QAAQ;KACP;;;YAfF,SAAS,SAAC;gBACT,QAAQ,EAAE,oBAAoB;gBAC9B,QAAQ,EAAE;;;;GAIT;aAGF;;;;MCIY,kBAAkB;;;YAV9B,QAAQ,SAAC;gBACR,YAAY,EAAE;oBACZ,qBAAqB;iBACtB;gBACD,OAAO,EAAE,EACR;gBACD,OAAO,EAAE;oBACP,qBAAqB;iBACtB;aACF;;;ACdD;;;;ACAA;;;;;;"}
@@ -0,0 +1,6 @@
1
+ import { OnInit } from '@angular/core';
2
+ export declare class NgxFormRulesComponent implements OnInit {
3
+ constructor();
4
+ ngOnInit(): void;
5
+ }
6
+ //# sourceMappingURL=ngx-form-rules.component.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ngx-form-rules.component.d.ts","sourceRoot":"","sources":["../../../projects/ngx-form-rules/src/lib/ngx-form-rules.component.ts"],"names":[],"mappings":"AAAA,OAAO,EAAa,MAAM,EAAE,MAAM,eAAe,CAAC;AAElD,qBAUa,qBAAsB,YAAW,MAAM;;IAIlD,QAAQ,IAAI,IAAI;CAGjB"}
@@ -0,0 +1,3 @@
1
+ export declare class NgxFormRulesModule {
2
+ }
3
+ //# sourceMappingURL=ngx-form-rules.module.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ngx-form-rules.module.d.ts","sourceRoot":"","sources":["../../../projects/ngx-form-rules/src/lib/ngx-form-rules.module.ts"],"names":[],"mappings":"AAKA,qBAUa,kBAAkB;CAAI"}
@@ -0,0 +1,14 @@
1
+ import { FormGroup } from '@angular/forms';
2
+ export interface FormRule {
3
+ trigger: string;
4
+ onValue: any;
5
+ disable: string[];
6
+ enable: string[];
7
+ }
8
+ export declare class NgxFormRulesService {
9
+ private dbSnapshot;
10
+ applyRules(form: FormGroup, rules: FormRule[]): void;
11
+ syncRulesWithCurrentValues(form: FormGroup, rules: FormRule[]): void;
12
+ private executeRule;
13
+ }
14
+ //# sourceMappingURL=ngx-form-rules.service.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ngx-form-rules.service.d.ts","sourceRoot":"","sources":["../../../projects/ngx-form-rules/src/lib/ngx-form-rules.service.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAE3C,MAAM,WAAW,QAAQ;IACvB,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE,GAAG,CAAC;IACb,OAAO,EAAE,MAAM,EAAE,CAAC;IAClB,MAAM,EAAE,MAAM,EAAE,CAAC;CAClB;AAED,qBAGa,mBAAmB;IAC9B,OAAO,CAAC,UAAU,CAAW;IAE7B,UAAU,CAAC,IAAI,EAAE,SAAS,EAAE,KAAK,EAAE,QAAQ,EAAE;IAQ7C,0BAA0B,CAAC,IAAI,EAAE,SAAS,EAAE,KAAK,EAAE,QAAQ,EAAE;IAS7D,OAAO,CAAC,WAAW;CAyCpB"}
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Generated bundle index. Do not edit.
3
+ */
4
+ export * from './public-api';
5
+ //# sourceMappingURL=ngx-form-rules.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ngx-form-rules.d.ts","sourceRoot":"","sources":["../../projects/ngx-form-rules/src/ngx-form-rules.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,cAAc,cAAc,CAAC"}
@@ -0,0 +1 @@
1
+ {"__symbolic":"module","version":4,"metadata":{"FormRule":{"__symbolic":"interface"},"NgxFormRulesService":{"__symbolic":"class","decorators":[{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"Injectable","line":10,"character":1},"arguments":[{"providedIn":"root"}]}],"members":{"applyRules":[{"__symbolic":"method"}],"syncRulesWithCurrentValues":[{"__symbolic":"method"}],"executeRule":[{"__symbolic":"method"}]},"statics":{"ɵprov":{}}},"NgxFormRulesComponent":{"__symbolic":"class","decorators":[{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"Component","line":2,"character":1},"arguments":[{"selector":"lib-ngx-form-rules","template":"\n <p>\n ngx-form-rules works! xxxxx\n </p>\n ","styles":[]}]}],"members":{"__ctor__":[{"__symbolic":"constructor"}],"ngOnInit":[{"__symbolic":"method"}]}},"NgxFormRulesModule":{"__symbolic":"class","decorators":[{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"NgModule","line":5,"character":1},"arguments":[{"declarations":[{"__symbolic":"reference","name":"NgxFormRulesComponent"}],"imports":[],"exports":[{"__symbolic":"reference","name":"NgxFormRulesComponent"}]}]}],"members":{}}},"origins":{"FormRule":"./lib/ngx-form-rules.service","NgxFormRulesService":"./lib/ngx-form-rules.service","NgxFormRulesComponent":"./lib/ngx-form-rules.component","NgxFormRulesModule":"./lib/ngx-form-rules.module"},"importAs":"ngx-form-rules"}
package/package.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "name": "ngx-form-rules",
3
+ "version": "0.0.1",
4
+ "repository": {
5
+ "type": "git",
6
+ "url": "https://github.com/bulbul5391/ngx-form-rules.git"
7
+ },
8
+ "peerDependencies": {
9
+ "@angular/common": "^11.2.14",
10
+ "@angular/core": "^11.2.14"
11
+ },
12
+ "dependencies": {
13
+ "tslib": "^2.0.0"
14
+ },
15
+ "main": "bundles/ngx-form-rules.umd.js",
16
+ "module": "fesm2015/ngx-form-rules.js",
17
+ "es2015": "fesm2015/ngx-form-rules.js",
18
+ "esm2015": "esm2015/ngx-form-rules.js",
19
+ "fesm2015": "fesm2015/ngx-form-rules.js",
20
+ "typings": "ngx-form-rules.d.ts",
21
+ "metadata": "ngx-form-rules.metadata.json",
22
+ "sideEffects": false
23
+ }
@@ -0,0 +1,4 @@
1
+ export * from './lib/ngx-form-rules.service';
2
+ export * from './lib/ngx-form-rules.component';
3
+ export * from './lib/ngx-form-rules.module';
4
+ //# sourceMappingURL=public-api.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"public-api.d.ts","sourceRoot":"","sources":["../../projects/ngx-form-rules/src/public-api.ts"],"names":[],"mappings":"AAIA,cAAc,8BAA8B,CAAC;AAC7C,cAAc,gCAAgC,CAAC;AAC/C,cAAc,6BAA6B,CAAC"}