@ssv/ngx.command 3.0.0-dev.1 → 3.0.0-dev.1-dev.10

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/project.json CHANGED
@@ -1,36 +1,36 @@
1
- {
2
- "name": "@ssv/ngx.command",
3
- "$schema": "../../node_modules/nx/schemas/project-schema.json",
4
- "sourceRoot": "libs/ngx.command/src",
5
- "prefix": "ssv",
6
- "projectType": "library",
7
- "tags": [],
8
- "targets": {
9
- "build": {
10
- "executor": "@nx/angular:package",
11
- "outputs": ["{workspaceRoot}/dist/{projectRoot}"],
12
- "options": {
13
- "project": "libs/ngx.command/ng-package.json"
14
- },
15
- "configurations": {
16
- "production": {
17
- "tsConfig": "libs/ngx.command/tsconfig.lib.prod.json"
18
- },
19
- "development": {
20
- "tsConfig": "libs/ngx.command/tsconfig.lib.json"
21
- }
22
- },
23
- "defaultConfiguration": "production"
24
- },
25
- "test": {
26
- "executor": "@nx/jest:jest",
27
- "outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
28
- "options": {
29
- "jestConfig": "libs/ngx.command/jest.config.ts"
30
- }
31
- },
32
- "lint": {
33
- "executor": "@nx/eslint:lint"
34
- }
35
- }
36
- }
1
+ {
2
+ "name": "@ssv/ngx.command",
3
+ "$schema": "../../node_modules/nx/schemas/project-schema.json",
4
+ "sourceRoot": "libs/ngx.command/src",
5
+ "prefix": "ssv",
6
+ "projectType": "library",
7
+ "tags": [],
8
+ "targets": {
9
+ "build": {
10
+ "executor": "@nx/angular:package",
11
+ "outputs": ["{workspaceRoot}/dist/{projectRoot}"],
12
+ "options": {
13
+ "project": "libs/ngx.command/ng-package.json"
14
+ },
15
+ "configurations": {
16
+ "production": {
17
+ "tsConfig": "libs/ngx.command/tsconfig.lib.prod.json"
18
+ },
19
+ "development": {
20
+ "tsConfig": "libs/ngx.command/tsconfig.lib.json"
21
+ }
22
+ },
23
+ "defaultConfiguration": "production"
24
+ },
25
+ "test": {
26
+ "executor": "@nx/jest:jest",
27
+ "outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
28
+ "options": {
29
+ "jestConfig": "libs/ngx.command/jest.config.ts"
30
+ }
31
+ },
32
+ "lint": {
33
+ "executor": "@nx/eslint:lint"
34
+ }
35
+ }
36
+ }
@@ -1,57 +1,57 @@
1
- import { Observable } from "rxjs";
2
- import { Directive, OnInit, OnDestroy, Input } from "@angular/core";
3
-
4
- import { ICommand, CommandCreator } from "./command.model";
5
- import { isCommandCreator } from "./command.util";
6
- import { Command } from "./command";
7
-
8
- const NAME_CAMEL = "ssvCommandRef";
9
-
10
- /**
11
- * Command creator ref, directive which allows creating Command in the template
12
- * and associate it to a command (in order to share executions).
13
- * @example
14
- * ### Most common usage
15
- * ```html
16
- * <div #actionCmd="ssvCommandRef" [ssvCommandRef]="{host: this, execute: removeHero$, canExecute: isValid$}">
17
- * <button [ssvCommand]="actionCmd.command" [ssvCommandParams]="hero">
18
- * Remove
19
- * </button>
20
- * <button [ssvCommand]="actionCmd.command" [ssvCommandParams]="hero">
21
- * Remove
22
- * </button>
23
- * </div>
24
- * ```
25
- *
26
- */
27
- @Directive({
28
- selector: `[${NAME_CAMEL}]`,
29
- exportAs: NAME_CAMEL,
30
- standalone: true,
31
- })
32
- export class CommandRefDirective implements OnInit, OnDestroy {
33
-
34
- @Input(NAME_CAMEL) commandCreator: CommandCreator | undefined;
35
-
36
- get command(): ICommand { return this._command; }
37
- private _command!: ICommand;
38
-
39
- ngOnInit(): void {
40
- if (isCommandCreator(this.commandCreator)) {
41
- const isAsync = this.commandCreator.isAsync || this.commandCreator.isAsync === undefined;
42
-
43
- const execFn = this.commandCreator.execute.bind(this.commandCreator.host);
44
- this._command = new Command(execFn, this.commandCreator.canExecute as Observable<boolean> | undefined, isAsync);
45
- } else {
46
- throw new Error(`${NAME_CAMEL}: [${NAME_CAMEL}] is not defined properly!`);
47
- }
48
- }
49
-
50
- ngOnDestroy(): void {
51
- // console.log("[commandRef::destroy]");
52
- if (this._command) {
53
- this._command.destroy();
54
- }
55
- }
56
-
57
- }
1
+ import { Observable } from "rxjs";
2
+ import { Directive, OnInit, OnDestroy, Input } from "@angular/core";
3
+
4
+ import { ICommand, CommandCreator } from "./command.model";
5
+ import { isCommandCreator } from "./command.util";
6
+ import { Command } from "./command";
7
+
8
+ const NAME_CAMEL = "ssvCommandRef";
9
+
10
+ /**
11
+ * Command creator ref, directive which allows creating Command in the template
12
+ * and associate it to a command (in order to share executions).
13
+ * @example
14
+ * ### Most common usage
15
+ * ```html
16
+ * <div #actionCmd="ssvCommandRef" [ssvCommandRef]="{host: this, execute: removeHero$, canExecute: isValid$}">
17
+ * <button [ssvCommand]="actionCmd.command" [ssvCommandParams]="hero">
18
+ * Remove
19
+ * </button>
20
+ * <button [ssvCommand]="actionCmd.command" [ssvCommandParams]="hero">
21
+ * Remove
22
+ * </button>
23
+ * </div>
24
+ * ```
25
+ *
26
+ */
27
+ @Directive({
28
+ selector: `[${NAME_CAMEL}]`,
29
+ exportAs: NAME_CAMEL,
30
+ standalone: true,
31
+ })
32
+ export class CommandRefDirective implements OnInit, OnDestroy {
33
+
34
+ @Input(NAME_CAMEL) commandCreator: CommandCreator | undefined;
35
+
36
+ get command(): ICommand { return this._command; }
37
+ private _command!: ICommand;
38
+
39
+ ngOnInit(): void {
40
+ if (isCommandCreator(this.commandCreator)) {
41
+ const isAsync = this.commandCreator.isAsync || this.commandCreator.isAsync === undefined;
42
+
43
+ const execFn = this.commandCreator.execute.bind(this.commandCreator.host);
44
+ this._command = new Command(execFn, this.commandCreator.canExecute as Observable<boolean> | undefined, isAsync);
45
+ } else {
46
+ throw new Error(`${NAME_CAMEL}: [${NAME_CAMEL}] is not defined properly!`);
47
+ }
48
+ }
49
+
50
+ ngOnDestroy(): void {
51
+ // console.log("[commandRef::destroy]");
52
+ if (this._command) {
53
+ this._command.destroy();
54
+ }
55
+ }
56
+
57
+ }
@@ -1,192 +1,191 @@
1
- import {
2
- Directive,
3
- OnInit,
4
- OnDestroy,
5
- Input,
6
- HostListener,
7
- ElementRef,
8
- Renderer2,
9
- ChangeDetectorRef,
10
- inject,
11
- } from "@angular/core";
12
- import { Subject } from "rxjs";
13
- import { tap, delay, takeUntil } from "rxjs/operators";
14
-
15
- import { CommandOptions, COMMAND_OPTIONS } from "./command.options";
16
- import { Command } from "./command";
17
- import { isCommand, isCommandCreator } from "./command.util";
18
- import { CommandCreator, ICommand } from "./command.model";
19
-
20
- /**
21
- * Controls the state of a component in sync with `Command`.
22
- *
23
- * @example
24
- * ### Most common usage
25
- * ```html
26
- * <button [ssvCommand]="saveCmd">Save</button>
27
- * ```
28
- *
29
- *
30
- * ### Usage with options
31
- * ```html
32
- * <button [ssvCommand]="saveCmd" [ssvCommandOptions]="{executingCssClass: 'in-progress'}">Save</button>
33
- * ```
34
- *
35
- *
36
- * ### Usage with params
37
- * This is useful for collections (loops) or using multiple actions with different args.
38
- * *NOTE: This will share the `isExecuting` when used with multiple controls.*
39
- *
40
- * #### With single param
41
- *
42
- * ```html
43
- * <button [ssvCommand]="saveCmd" [ssvCommandParams]="{id: 1}">Save</button>
44
- * ```
45
- * *NOTE: if you have only 1 argument as an array, it should be enclosed within an array e.g. `[['apple', 'banana']]`,
46
- * else it will spread and you will `arg1: "apple", arg2: "banana"`*
47
- *
48
- * #### With multi params
49
- * ```html
50
- * <button [ssvCommand]="saveCmd" [ssvCommandParams]="[{id: 1}, 'hello', hero]">Save</button>
51
- * ```
52
- *
53
- * ### Usage with Command Creator
54
- * This is useful for collections (loops) or using multiple actions with different args, whilst not sharing `isExecuting`.
55
- *
56
- *
57
- * ```html
58
- * <button [ssvCommand]="{host: this, execute: removeHero$, canExecute: isValid$, params: [hero, 1337, 'xx']}">Save</button>
59
- * ```
60
- *
61
- */
62
-
63
- const NAME_CAMEL = "ssvCommand";
64
-
65
- // let nextUniqueId = 0;
66
-
67
- @Directive({
68
- selector: `[${NAME_CAMEL}]`,
69
- exportAs: NAME_CAMEL,
70
- standalone: true,
71
- })
72
- export class CommandDirective implements OnInit, OnDestroy {
73
-
74
- // readonly id = `${NAME_CAMEL}-${nextUniqueId++}`;
75
- private readonly globalOptions = inject(COMMAND_OPTIONS);
76
- private readonly renderer = inject(Renderer2);
77
- private readonly element = inject(ElementRef);
78
- private readonly cdr = inject(ChangeDetectorRef);
79
-
80
-
81
- @Input(NAME_CAMEL) commandOrCreator: ICommand | CommandCreator | undefined;
82
-
83
- @Input(`${NAME_CAMEL}Options`)
84
- get commandOptions(): CommandOptions { return this._commandOptions; }
85
- set commandOptions(value: Partial<CommandOptions>) {
86
- if (value === this._commandOptions) {
87
- return;
88
- }
89
- this._commandOptions = {
90
- ...this.globalOptions,
91
- ...value,
92
- };
93
- }
94
-
95
- @Input(`${NAME_CAMEL}Params`) commandParams: unknown | unknown[];
96
-
97
- get command(): ICommand { return this._command; }
98
-
99
- private _command!: ICommand;
100
- private _commandOptions: CommandOptions = this.globalOptions;
101
- private _destroy$ = new Subject<void>();
102
-
103
- ngOnInit(): void {
104
- // console.log("[ssvCommand::init]", this.config);
105
- if (!this.commandOrCreator) {
106
- throw new Error(`${NAME_CAMEL}: [${NAME_CAMEL}] should be defined!`);
107
- } else if (isCommand(this.commandOrCreator)) {
108
- this._command = this.commandOrCreator;
109
- } else if (isCommandCreator(this.commandOrCreator)) {
110
- const isAsync = this.commandOrCreator.isAsync || this.commandOrCreator.isAsync === undefined;
111
-
112
- // todo: find something like this for ivy (or angular10+)
113
- // const hostComponent = (this.viewContainer as any)._view.component;
114
-
115
- const execFn = this.commandOrCreator.execute.bind(this.commandOrCreator.host);
116
- this.commandParams = this.commandParams || this.commandOrCreator.params;
117
-
118
- const canExec = this.commandOrCreator.canExecute instanceof Function
119
- ? this.commandOrCreator.canExecute.bind(this.commandOrCreator.host, this.commandParams)()
120
- : this.commandOrCreator.canExecute;
121
-
122
- // console.log("[ssvCommand::init] command creator", {
123
- // firstParam: this.commandParams ? this.commandParams[0] : null,
124
- // params: this.commandParams
125
- // });
126
- this._command = new Command(execFn, canExec, isAsync);
127
- } else {
128
- throw new Error(`${NAME_CAMEL}: [${NAME_CAMEL}] is not defined properly!`);
129
- }
130
-
131
- this._command.subscribe();
132
- this._command.canExecute$.pipe(
133
- this.commandOptions.hasDisabledDelay
134
- ? delay(1)
135
- : tap(() => { /* stub */ }),
136
- tap(x => {
137
- this.trySetDisabled(!x);
138
- // console.log("[ssvCommand::canExecute$]", { canExecute: x });
139
- this.cdr.markForCheck();
140
- }),
141
- takeUntil(this._destroy$),
142
- ).subscribe();
143
-
144
- if (this._command.isExecuting$) {
145
- this._command.isExecuting$.pipe(
146
- tap(x => {
147
- // console.log("[ssvCommand::isExecuting$]", x, this.commandOptions);
148
- if (x) {
149
- this.renderer.addClass(
150
- this.element.nativeElement,
151
- this.commandOptions.executingCssClass
152
- );
153
- } else {
154
- this.renderer.removeClass(
155
- this.element.nativeElement,
156
- this.commandOptions.executingCssClass
157
- );
158
- }
159
- }),
160
- takeUntil(this._destroy$),
161
- ).subscribe();
162
- }
163
- }
164
-
165
- @HostListener("click")
166
- onClick(): void {
167
- // console.log("[ssvCommand::onClick]", this.commandParams);
168
- if (Array.isArray(this.commandParams)) {
169
- this._command.execute(...this.commandParams);
170
- } else {
171
- this._command.execute(this.commandParams);
172
- }
173
- }
174
-
175
- ngOnDestroy(): void {
176
- // console.log("[ssvCommand::destroy]");
177
- this._destroy$.next();
178
- this._destroy$.complete();
179
- if (this._command) {
180
- this._command.unsubscribe();
181
- }
182
- }
183
-
184
- private trySetDisabled(disabled: boolean) {
185
- if (this.commandOptions.handleDisabled) {
186
- // console.warn(">>>> disabled", { id: this.id, disabled });
187
- this.renderer.setProperty(this.element.nativeElement, "disabled", disabled);
188
- }
189
- }
190
-
191
- }
192
-
1
+ import {
2
+ Directive,
3
+ OnInit,
4
+ OnDestroy,
5
+ Input,
6
+ HostListener,
7
+ ElementRef,
8
+ Renderer2,
9
+ ChangeDetectorRef,
10
+ inject,
11
+ } from "@angular/core";
12
+ import { Subject } from "rxjs";
13
+ import { tap, delay, takeUntil } from "rxjs/operators";
14
+
15
+ import { CommandOptions, COMMAND_OPTIONS } from "./command.options";
16
+ import { Command } from "./command";
17
+ import { isCommand, isCommandCreator } from "./command.util";
18
+ import { CommandCreator, ICommand } from "./command.model";
19
+
20
+ /**
21
+ * Controls the state of a component in sync with `Command`.
22
+ *
23
+ * @example
24
+ * ### Most common usage
25
+ * ```html
26
+ * <button [ssvCommand]="saveCmd">Save</button>
27
+ * ```
28
+ *
29
+ *
30
+ * ### Usage with options
31
+ * ```html
32
+ * <button [ssvCommand]="saveCmd" [ssvCommandOptions]="{executingCssClass: 'in-progress'}">Save</button>
33
+ * ```
34
+ *
35
+ *
36
+ * ### Usage with params
37
+ * This is useful for collections (loops) or using multiple actions with different args.
38
+ * *NOTE: This will share the `isExecuting` when used with multiple controls.*
39
+ *
40
+ * #### With single param
41
+ *
42
+ * ```html
43
+ * <button [ssvCommand]="saveCmd" [ssvCommandParams]="{id: 1}">Save</button>
44
+ * ```
45
+ * *NOTE: if you have only 1 argument as an array, it should be enclosed within an array e.g. `[['apple', 'banana']]`,
46
+ * else it will spread and you will `arg1: "apple", arg2: "banana"`*
47
+ *
48
+ * #### With multi params
49
+ * ```html
50
+ * <button [ssvCommand]="saveCmd" [ssvCommandParams]="[{id: 1}, 'hello', hero]">Save</button>
51
+ * ```
52
+ *
53
+ * ### Usage with Command Creator
54
+ * This is useful for collections (loops) or using multiple actions with different args, whilst not sharing `isExecuting`.
55
+ *
56
+ *
57
+ * ```html
58
+ * <button [ssvCommand]="{host: this, execute: removeHero$, canExecute: isValid$, params: [hero, 1337, 'xx']}">Save</button>
59
+ * ```
60
+ *
61
+ */
62
+
63
+ const NAME_CAMEL = "ssvCommand";
64
+
65
+ // let nextUniqueId = 0;
66
+
67
+ @Directive({
68
+ selector: `[${NAME_CAMEL}]`,
69
+ exportAs: NAME_CAMEL,
70
+ standalone: true,
71
+ })
72
+ export class CommandDirective implements OnInit, OnDestroy {
73
+
74
+ // readonly id = `${NAME_CAMEL}-${nextUniqueId++}`;
75
+ private readonly globalOptions = inject(COMMAND_OPTIONS);
76
+ private readonly renderer = inject(Renderer2);
77
+ private readonly element = inject(ElementRef);
78
+ private readonly cdr = inject(ChangeDetectorRef);
79
+
80
+ @Input(NAME_CAMEL) commandOrCreator: ICommand | CommandCreator | undefined;
81
+
82
+ @Input(`${NAME_CAMEL}Options`)
83
+ get commandOptions(): CommandOptions { return this._commandOptions; }
84
+ set commandOptions(value: Partial<CommandOptions>) {
85
+ if (value === this._commandOptions) {
86
+ return;
87
+ }
88
+ this._commandOptions = {
89
+ ...this.globalOptions,
90
+ ...value,
91
+ };
92
+ }
93
+
94
+ @Input(`${NAME_CAMEL}Params`) commandParams: unknown | unknown[];
95
+
96
+ get command(): ICommand { return this._command; }
97
+
98
+ private _command!: ICommand;
99
+ private _commandOptions: CommandOptions = this.globalOptions;
100
+ private _destroy$ = new Subject<void>();
101
+
102
+ ngOnInit(): void {
103
+ // console.log("[ssvCommand::init]", this.config);
104
+ if (!this.commandOrCreator) {
105
+ throw new Error(`${NAME_CAMEL}: [${NAME_CAMEL}] should be defined!`);
106
+ } else if (isCommand(this.commandOrCreator)) {
107
+ this._command = this.commandOrCreator;
108
+ } else if (isCommandCreator(this.commandOrCreator)) {
109
+ const isAsync = this.commandOrCreator.isAsync || this.commandOrCreator.isAsync === undefined;
110
+
111
+ // todo: find something like this for ivy (or angular10+)
112
+ // const hostComponent = (this.viewContainer as any)._view.component;
113
+
114
+ const execFn = this.commandOrCreator.execute.bind(this.commandOrCreator.host);
115
+ this.commandParams = this.commandParams || this.commandOrCreator.params;
116
+
117
+ const canExec = this.commandOrCreator.canExecute instanceof Function
118
+ ? this.commandOrCreator.canExecute.bind(this.commandOrCreator.host, this.commandParams)()
119
+ : this.commandOrCreator.canExecute;
120
+
121
+ // console.log("[ssvCommand::init] command creator", {
122
+ // firstParam: this.commandParams ? this.commandParams[0] : null,
123
+ // params: this.commandParams
124
+ // });
125
+ this._command = new Command(execFn, canExec, isAsync);
126
+ } else {
127
+ throw new Error(`${NAME_CAMEL}: [${NAME_CAMEL}] is not defined properly!`);
128
+ }
129
+
130
+ this._command.subscribe();
131
+ this._command.canExecute$.pipe(
132
+ this.commandOptions.hasDisabledDelay
133
+ ? delay(1)
134
+ : tap(() => { /* stub */ }),
135
+ tap(x => {
136
+ this.trySetDisabled(!x);
137
+ // console.log("[ssvCommand::canExecute$]", { canExecute: x });
138
+ this.cdr.markForCheck();
139
+ }),
140
+ takeUntil(this._destroy$),
141
+ ).subscribe();
142
+
143
+ if (this._command.isExecuting$) {
144
+ this._command.isExecuting$.pipe(
145
+ tap(x => {
146
+ // console.log("[ssvCommand::isExecuting$]", x, this.commandOptions);
147
+ if (x) {
148
+ this.renderer.addClass(
149
+ this.element.nativeElement,
150
+ this.commandOptions.executingCssClass
151
+ );
152
+ } else {
153
+ this.renderer.removeClass(
154
+ this.element.nativeElement,
155
+ this.commandOptions.executingCssClass
156
+ );
157
+ }
158
+ }),
159
+ takeUntil(this._destroy$),
160
+ ).subscribe();
161
+ }
162
+ }
163
+
164
+ @HostListener("click")
165
+ onClick(): void {
166
+ // console.log("[ssvCommand::onClick]", this.commandParams);
167
+ if (Array.isArray(this.commandParams)) {
168
+ this._command.execute(...this.commandParams);
169
+ } else {
170
+ this._command.execute(this.commandParams);
171
+ }
172
+ }
173
+
174
+ ngOnDestroy(): void {
175
+ // console.log("[ssvCommand::destroy]");
176
+ this._destroy$.next();
177
+ this._destroy$.complete();
178
+ if (this._command) {
179
+ this._command.unsubscribe();
180
+ }
181
+ }
182
+
183
+ private trySetDisabled(disabled: boolean) {
184
+ if (this.commandOptions.handleDisabled) {
185
+ // console.warn(">>>> disabled", { id: this.id, disabled });
186
+ this.renderer.setProperty(this.element.nativeElement, "disabled", disabled);
187
+ }
188
+ }
189
+
190
+ }
191
+