@ssv/ngx.command 0.0.0-PLACEHOLDER
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 +243 -0
- package/eslint.config.js +43 -0
- package/jest.config.ts +21 -0
- package/ng-package.json +7 -0
- package/package.json +30 -0
- package/project.json +36 -0
- package/src/command-ref.directive.ts +57 -0
- package/src/command.directive.ts +191 -0
- package/src/command.directive.xspec.ts +105 -0
- package/src/command.model.ts +36 -0
- package/src/command.options.ts +45 -0
- package/src/command.spec.ts +215 -0
- package/src/command.ts +170 -0
- package/src/command.util.ts +50 -0
- package/src/index.ts +8 -0
- package/src/module.ts +17 -0
- package/src/test-setup.ts +8 -0
- package/src/version.ts +1 -0
- package/tsconfig.json +28 -0
- package/tsconfig.lib.json +17 -0
- package/tsconfig.lib.prod.json +9 -0
- package/tsconfig.spec.json +16 -0
package/README.md
ADDED
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
[projectUri]: https://github.com/sketch7/ngx.command
|
|
2
|
+
[changeLog]: ./CHANGELOG.md
|
|
3
|
+
[releaseWorkflowWiki]: ./docs/RELEASE-WORKFLOW.md
|
|
4
|
+
|
|
5
|
+
[npm]: https://www.npmjs.com
|
|
6
|
+
[commandpatternwiki]: https://en.wikipedia.org/wiki/Command_pattern
|
|
7
|
+
|
|
8
|
+
# @ssv/ngx.command
|
|
9
|
+
[](https://github.com/sketch7/ngx.command/actions/workflows/ci.yml)
|
|
10
|
+
[](https://badge.fury.io/js/%40ssv%2Fngx.command)
|
|
11
|
+
|
|
12
|
+
[Command pattern][commandpatternwiki] implementation for angular. Command's are used to encapsulate information which is needed to perform an action.
|
|
13
|
+
|
|
14
|
+
Primary usage is to disable a button when an action is executing, or not in a valid state (e.g. busy, invalid), and also to show an activity progress while executing.
|
|
15
|
+
|
|
16
|
+
**Quick links**
|
|
17
|
+
|
|
18
|
+
[Change logs][changeLog] | [Project Repository][projectUri]
|
|
19
|
+
|
|
20
|
+
## Installation
|
|
21
|
+
|
|
22
|
+
Get library via [npm]
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
npm install @ssv/ngx.command
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Choose the version corresponding to your Angular version:
|
|
29
|
+
|
|
30
|
+
| Angular | library |
|
|
31
|
+
| ------- | ------- |
|
|
32
|
+
| 10+ | 2.x+ |
|
|
33
|
+
| 4 to 9 | 1.x+ |
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
# Usage
|
|
37
|
+
|
|
38
|
+
## Register module
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
import { SsvCommandModule } from "@ssv/ngx.command";
|
|
42
|
+
|
|
43
|
+
@NgModule({
|
|
44
|
+
imports: [
|
|
45
|
+
SsvCommandModule
|
|
46
|
+
]
|
|
47
|
+
}
|
|
48
|
+
export class AppModule {
|
|
49
|
+
}
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Command
|
|
53
|
+
In order to start working with Command, you need to create a new instance of it.
|
|
54
|
+
|
|
55
|
+
```ts
|
|
56
|
+
import { CommandDirective, Command, CommandAsync, ICommand } from "@ssv/ngx.command";
|
|
57
|
+
|
|
58
|
+
isValid$ = new BehaviorSubject(false);
|
|
59
|
+
|
|
60
|
+
// use `CommandAsync` when execute function returns an observable/promise OR else 3rd argument must be true.
|
|
61
|
+
saveCmd = new Command(() => this.save()), this.isValid$);
|
|
62
|
+
|
|
63
|
+
// using CommandAsync
|
|
64
|
+
saveCmd = new CommandAsync(() => Observable.timer(2000), this.isValid$);
|
|
65
|
+
|
|
66
|
+
// using ICommand interface
|
|
67
|
+
saveCmd: ICommand = new CommandAsync(() => Observable.timer(2000), this.isValid$);
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## Command Attribute (Directive)
|
|
71
|
+
Handles the command `canExecute$`, `isExecuting` and `execute` functions of the `Command`, in order to
|
|
72
|
+
enable/disable, add/remove a cssClass while executing in order alter styling during execution (if desired)
|
|
73
|
+
and execute when its enabled and clicked.
|
|
74
|
+
|
|
75
|
+
Generally used on a `<button>` as below.
|
|
76
|
+
|
|
77
|
+
### Usage
|
|
78
|
+
|
|
79
|
+
```html
|
|
80
|
+
<!-- simple usage -->
|
|
81
|
+
<button [ssvCommand]="saveCmd">Save</button>
|
|
82
|
+
|
|
83
|
+
<!-- using isExecuting + showing spinner -->
|
|
84
|
+
<button [ssvCommand]="saveCmd">
|
|
85
|
+
<i *ngIf="saveCmd.isExecuting" class="ai-circled ai-indicator ai-dark-spin small"></i>
|
|
86
|
+
Save
|
|
87
|
+
</button>
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
#### Usage with params
|
|
91
|
+
This is useful for collections (loops) or using multiple actions with different args.
|
|
92
|
+
*NOTE: This will share the `isExecuting` when used with multiple controls.*
|
|
93
|
+
|
|
94
|
+
```html
|
|
95
|
+
<!-- with single param -->
|
|
96
|
+
<button [ssvCommand]="saveCmd" [ssvCommandParams]="{id: 1}">Save</button>
|
|
97
|
+
<!--
|
|
98
|
+
NOTE: if you have only 1 argument as an array, it should be enclosed within an array e.g. [['apple', 'banana']],
|
|
99
|
+
else it will spread and you will arg1: "apple", arg2: "banana"
|
|
100
|
+
-->
|
|
101
|
+
|
|
102
|
+
<!-- with multi params -->
|
|
103
|
+
<button [ssvCommand]="saveCmd" [ssvCommandParams]="[{id: 1}, 'hello', hero]">Save</button>
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
#### Usage with command creator
|
|
107
|
+
This is useful for collections (loops) or using multiple actions with different args, whilst not sharing `isExecuting`.
|
|
108
|
+
|
|
109
|
+
```html
|
|
110
|
+
<button [ssvCommand]="{host: this, execute: removeHero$, canExecute: isValid$, params: [hero, 1337, 'xx']}">Remove</button>
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
##### canExecute with params
|
|
114
|
+
```html
|
|
115
|
+
<button [ssvCommand]="{host: this, execute: removeHero$, canExecute: canRemoveHero$, params: [hero, 1337, 'xx']}">Remove</button>
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
```ts
|
|
119
|
+
canRemoveHero$(hero: Hero, id: number, param2): Observable<boolean> {
|
|
120
|
+
return of(id).pipe(
|
|
121
|
+
map(x => x === "invulnerable")
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
## Usage without Attribute
|
|
127
|
+
It can also be used as below without the command attribute.
|
|
128
|
+
|
|
129
|
+
```html
|
|
130
|
+
<button
|
|
131
|
+
[disabled]="!saveCmd.canExecute"
|
|
132
|
+
(click)="saveCmd.execute()">
|
|
133
|
+
Save
|
|
134
|
+
</button>
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
## CommandRef Attribute (directive)
|
|
138
|
+
Command creator ref, directive which allows creating Command in the template and associate it to a command (in order to share executions).
|
|
139
|
+
|
|
140
|
+
```html
|
|
141
|
+
<div *ngFor="let hero of heroes">
|
|
142
|
+
<div #actionCmd="ssvCommandRef" [ssvCommandRef]="{host: this, execute: removeHero$, canExecute: isValid$}" class="button-group">
|
|
143
|
+
<button [ssvCommand]="actionCmd.command" [ssvCommandParams]="hero">
|
|
144
|
+
Remove
|
|
145
|
+
</button>
|
|
146
|
+
<button [ssvCommand]="actionCmd.command" [ssvCommandParams]="hero">
|
|
147
|
+
Remove
|
|
148
|
+
</button>
|
|
149
|
+
</div>
|
|
150
|
+
</div>
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
## Utils
|
|
154
|
+
|
|
155
|
+
### canExecuteFromNgForm
|
|
156
|
+
In order to use with `NgForm` easily, you can use the following utility method.
|
|
157
|
+
This will make canExecute respond to `form.valid` and for `form.dirty` - also can optionally disable validity or dirty.
|
|
158
|
+
|
|
159
|
+
```ts
|
|
160
|
+
import { CommandAsync, canExecuteFromNgForm } from "@ssv/ngx.command";
|
|
161
|
+
|
|
162
|
+
loginCmd = new CommandAsync(this.login.bind(this), canExecuteFromNgForm(this.form));
|
|
163
|
+
|
|
164
|
+
// options - disable dirty check
|
|
165
|
+
loginCmd = new CommandAsync(this.login.bind(this), canExecuteFromNgForm(this.form, {
|
|
166
|
+
dirty: false
|
|
167
|
+
}));
|
|
168
|
+
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
## Configure
|
|
173
|
+
In order to configure globally, you can do so as following:
|
|
174
|
+
|
|
175
|
+
```ts
|
|
176
|
+
import { SsvCommandModule } from "@ssv/ngx.command";
|
|
177
|
+
|
|
178
|
+
imports: [
|
|
179
|
+
SsvCommandModule.forRoot({ executingCssClass: "is-busy" })
|
|
180
|
+
],
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
## Getting Started
|
|
185
|
+
|
|
186
|
+
### Setup Machine for Development
|
|
187
|
+
Install/setup the following:
|
|
188
|
+
|
|
189
|
+
- NodeJS v18.16.0+
|
|
190
|
+
- Visual Studio Code or similar code editor
|
|
191
|
+
- TypeScript 5.0+
|
|
192
|
+
- Git + SourceTree, SmartGit or similar (optional)
|
|
193
|
+
- Ensure to install **global NPM modules** using the following:
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
```bash
|
|
197
|
+
npm install -g git gulp devtool
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
### Project Setup
|
|
202
|
+
The following process need to be executed in order to get started.
|
|
203
|
+
|
|
204
|
+
```bash
|
|
205
|
+
npm install
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
### Building the code
|
|
210
|
+
|
|
211
|
+
```bash
|
|
212
|
+
npm run build
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
### Running the tests
|
|
216
|
+
|
|
217
|
+
```bash
|
|
218
|
+
npm test
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
#### Watch
|
|
222
|
+
Handles compiling of changes.
|
|
223
|
+
|
|
224
|
+
```bash
|
|
225
|
+
npm start
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
#### Running Continuous Tests
|
|
230
|
+
Spawns test runner and keep watching for changes.
|
|
231
|
+
|
|
232
|
+
```bash
|
|
233
|
+
npm run tdd
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
### Preparation for Release
|
|
238
|
+
|
|
239
|
+
- Update changelogs
|
|
240
|
+
- bump version
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
Check out the [release workflow guide][releaseWorkflowWiki] in order to guide you creating a release and publishing it.
|
package/eslint.config.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
const nx = require('@nx/eslint-plugin');
|
|
2
|
+
const baseConfig = require('../../eslint.config.js');
|
|
3
|
+
|
|
4
|
+
module.exports = [
|
|
5
|
+
...baseConfig,
|
|
6
|
+
{
|
|
7
|
+
files: ['**/*.json'],
|
|
8
|
+
rules: {
|
|
9
|
+
'@nx/dependency-checks': ['error', { ignoredFiles: ['{projectRoot}/eslint.config.{js,cjs,mjs}'] }],
|
|
10
|
+
},
|
|
11
|
+
languageOptions: { parser: require('jsonc-eslint-parser') },
|
|
12
|
+
},
|
|
13
|
+
...nx.configs['flat/angular'],
|
|
14
|
+
...nx.configs['flat/angular-template'],
|
|
15
|
+
{
|
|
16
|
+
files: ['**/*.ts'],
|
|
17
|
+
rules: {
|
|
18
|
+
'@angular-eslint/directive-selector': [
|
|
19
|
+
'error',
|
|
20
|
+
{
|
|
21
|
+
type: 'attribute',
|
|
22
|
+
prefix: 'ssv',
|
|
23
|
+
style: 'camelCase',
|
|
24
|
+
},
|
|
25
|
+
],
|
|
26
|
+
'@angular-eslint/component-selector': [
|
|
27
|
+
'error',
|
|
28
|
+
{
|
|
29
|
+
type: 'element',
|
|
30
|
+
prefix: 'ssv',
|
|
31
|
+
style: 'kebab-case',
|
|
32
|
+
},
|
|
33
|
+
],
|
|
34
|
+
'@angular-eslint/directive-selector': 'off',
|
|
35
|
+
'@angular-eslint/no-input-rename': 'off',
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
files: ['**/*.html'],
|
|
40
|
+
// Override or add rules here
|
|
41
|
+
rules: {},
|
|
42
|
+
},
|
|
43
|
+
];
|
package/jest.config.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export default {
|
|
2
|
+
displayName: '@ssv/ngx.command',
|
|
3
|
+
preset: '../../jest.preset.js',
|
|
4
|
+
setupFilesAfterEnv: ['<rootDir>/src/test-setup.ts'],
|
|
5
|
+
coverageDirectory: '../../coverage/libs/ngx.command',
|
|
6
|
+
transform: {
|
|
7
|
+
'^.+\\.(ts|mjs|js|html)$': [
|
|
8
|
+
'jest-preset-angular',
|
|
9
|
+
{
|
|
10
|
+
tsconfig: '<rootDir>/tsconfig.spec.json',
|
|
11
|
+
stringifyContentPathRegex: '\\.(html|svg)$',
|
|
12
|
+
},
|
|
13
|
+
],
|
|
14
|
+
},
|
|
15
|
+
transformIgnorePatterns: ['node_modules/(?!.*\\.mjs$)'],
|
|
16
|
+
snapshotSerializers: [
|
|
17
|
+
'jest-preset-angular/build/serializers/no-ng-attributes',
|
|
18
|
+
'jest-preset-angular/build/serializers/ng-snapshot',
|
|
19
|
+
'jest-preset-angular/build/serializers/html-comment',
|
|
20
|
+
],
|
|
21
|
+
};
|
package/ng-package.json
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ssv/ngx.command",
|
|
3
|
+
"version": "0.0.0-PLACEHOLDER",
|
|
4
|
+
"versionSuffix": "",
|
|
5
|
+
"description": "Command pattern implementation for angular. Command used to encapsulate information which is needed to perform an action.",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"sketch7",
|
|
8
|
+
"ngx",
|
|
9
|
+
"angular17",
|
|
10
|
+
"angular",
|
|
11
|
+
"ssv",
|
|
12
|
+
"command",
|
|
13
|
+
"pattern",
|
|
14
|
+
"ui"
|
|
15
|
+
],
|
|
16
|
+
"author": "Stephen Lautier <stephen.lautier@outlook.com>",
|
|
17
|
+
"license": "MIT",
|
|
18
|
+
"private": false,
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "https://github.com/sketch7/ngx.command.git"
|
|
22
|
+
},
|
|
23
|
+
"dependencies": {},
|
|
24
|
+
"peerDependencies": {
|
|
25
|
+
"@angular/core": ">=17.0.0",
|
|
26
|
+
"@angular/forms": ">=17.0.0",
|
|
27
|
+
"rxjs": ">=6.0.0"
|
|
28
|
+
},
|
|
29
|
+
"sideEffects": false
|
|
30
|
+
}
|
package/project.json
ADDED
|
@@ -0,0 +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
|
+
}
|
|
@@ -0,0 +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
|
+
}
|
|
@@ -0,0 +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
|
+
@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
|
+
|