Skip to content

Commit 1312531

Browse files
committed
feat(module): global module config - default pattern
1 parent 5e51d82 commit 1312531

File tree

4 files changed

+59
-8
lines changed

4 files changed

+59
-8
lines changed
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)