Skip to content

Commit e888b03

Browse files
authored
Merge pull request #9 from jornetsimon/module-config
Global module config
2 parents 771eb5f + f44675d commit e888b03

File tree

6 files changed

+105
-10
lines changed

6 files changed

+105
-10
lines changed

README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ Add the ngxVibration directive to an HTML element. The directive takes a vibrati
2727
```html
2828
<button [ngxVibration]="[200, 100, 200]">VIBRATE</button>
2929
```
30+
```html
31+
<button [ngxVibration]="500">VIBRATE</button>
32+
```
33+
Or with the [global config](#global-configuration) _defaultPattern_ set:
34+
```html
35+
<button ngxVibration>VIBRATE</button>
36+
```
3037

3138
### Vibration Service
3239

@@ -48,4 +55,19 @@ export class AppComponent implements OnInit {
4855
this.vibrationService.cancelVibration();
4956
}
5057
}
51-
```
58+
```
59+
60+
### Global configuration
61+
62+
You can provide a default configuration object when importing the module using the `forRoot()` method.
63+
64+
``` typescript
65+
NgxVibrationModule.forRoot({
66+
defaultPattern: [100, 0, 100]
67+
})
68+
```
69+
70+
| Property | Type | Description |
71+
| -------------- | ---------- | ------------------------------------------------------------ |
72+
| defaultPattern | `number[]` | The pattern to use by default when no other is specified on the directives |
73+

projects/ngx-vibration/README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ Add the ngxVibration directive to an HTML element. The directive takes a vibrati
2727
```html
2828
<button [ngxVibration]="[200, 100, 200]">VIBRATE</button>
2929
```
30+
```html
31+
<button [ngxVibration]="500">VIBRATE</button>
32+
```
33+
Or with the [global config](#global-configuration) _defaultPattern_ set:
34+
```html
35+
<button ngxVibration>VIBRATE</button>
36+
```
3037

3138
### Vibration Service
3239

@@ -48,4 +55,19 @@ export class AppComponent implements OnInit {
4855
this.vibrationService.cancelVibration();
4956
}
5057
}
51-
```
58+
```
59+
60+
### Global configuration
61+
62+
You can provide a default configuration object when importing the module using the `forRoot()` method.
63+
64+
``` typescript
65+
NgxVibrationModule.forRoot({
66+
defaultPattern: [100, 0, 100]
67+
})
68+
```
69+
70+
| Property | Type | Description |
71+
| -------------- | ---------- | ------------------------------------------------------------ |
72+
| defaultPattern | `number[]` | The pattern to use by default when no other is specified on the directives |
73+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { InjectionToken } from '@angular/core';
2+
3+
export interface NgxVibrationConfig {
4+
defaultPattern?: number[];
5+
}
6+
7+
export const GLOBAL_CONFIG_TOKEN = new InjectionToken<NgxVibrationConfig>(
8+
'GLOBAL_CONFIG'
9+
);
Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
1-
import { NgModule } from "@angular/core";
2-
import { VibrationDirective } from "./vibration.directive";
1+
import { ModuleWithProviders, NgModule } from '@angular/core';
2+
import { VibrationDirective } from './vibration.directive';
3+
import { GLOBAL_CONFIG_TOKEN, NgxVibrationConfig } from './ngx-vibration.config';
34

45
@NgModule({
56
declarations: [VibrationDirective],
67
imports: [],
78
exports: [VibrationDirective],
89
})
9-
export class NgxVibrationModule {}
10+
export class NgxVibrationModule {
11+
static forRoot(config: NgxVibrationConfig): ModuleWithProviders<NgxVibrationModule> {
12+
return {
13+
ngModule: NgxVibrationModule,
14+
providers: [{ provide: GLOBAL_CONFIG_TOKEN, useValue: config }],
15+
};
16+
}
17+
}
18+

projects/ngx-vibration/src/lib/ngx-vibration.service.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
import { Injectable } from '@angular/core';
1+
import { Inject, Injectable, Optional } from '@angular/core';
2+
import { GLOBAL_CONFIG_TOKEN, NgxVibrationConfig } from './ngx-vibration.config';
23

34
@Injectable({
45
providedIn: 'root'
56
})
67
export class NgxVibrationService {
8+
constructor(@Optional() @Inject(GLOBAL_CONFIG_TOKEN)
9+
public readonly config: NgxVibrationConfig | null) {
10+
}
711

812
/**
913
* Takes vibrationPattern as input and returns a boolean indicating success

projects/ngx-vibration/src/lib/vibration.directive.ts

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,41 @@
1-
import { Directive, Input, HostListener } from "@angular/core";
2-
import { NgxVibrationService } from "./ngx-vibration.service";
1+
import { Directive, HostListener, Input } from '@angular/core';
2+
import { NgxVibrationService } from './ngx-vibration.service';
33

44
@Directive({
55
selector: "[ngxVibration]",
66
})
77
export class VibrationDirective {
8-
@Input("ngxVibration")
9-
vibratePattern: number[];
8+
private vibratePattern: number[];
9+
10+
/**
11+
* Handle directive arguments
12+
* Accepts pattern or digits as string or number
13+
*/
14+
@Input('ngxVibration')
15+
set inputPattern(input: number[] | number | string) {
16+
if (typeof input === 'string') {
17+
if (input) {
18+
if (!input.match(/^\d+$/)) {
19+
// The input does not match a number
20+
throw new Error('At least one number is expected as vibration pattern');
21+
}
22+
// Building the pattern from the given number
23+
this.vibratePattern = [parseInt(input, 10)];
24+
} else {
25+
// No input was given, falling back to module config
26+
const defaultPattern = this.vibrationService.config?.defaultPattern;
27+
if (!defaultPattern) {
28+
throw new Error('No pattern provided in vibrate() call nor module configuration');
29+
}
30+
this.vibratePattern = defaultPattern;
31+
}
32+
} else if (typeof input === 'number') {
33+
// Building the pattern from the given number
34+
this.vibratePattern = [input];
35+
} else {
36+
this.vibratePattern = input;
37+
}
38+
}
1039

1140
constructor(private vibrationService: NgxVibrationService) {}
1241

0 commit comments

Comments
 (0)