diff --git a/src/ngx-mailto/ngx-mailto.module.ts b/src/ngx-mailto/ngx-mailto.module.ts index d0d65b4..ef63fbf 100644 --- a/src/ngx-mailto/ngx-mailto.module.ts +++ b/src/ngx-mailto/ngx-mailto.module.ts @@ -1,12 +1,16 @@ -import { NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; - +import { NgModule } from '@angular/core'; +import { NgxMailtoPipe } from './ngx-mailto.pipe'; @NgModule({ - declarations: [], + declarations: [NgxMailtoPipe], + exports: [ + NgxMailtoPipe + ], imports: [ CommonModule ] }) -export class NgxMailtoModule { } +export class NgxMailtoModule { +} diff --git a/src/ngx-mailto/ngx-mailto.pipe.spec.ts b/src/ngx-mailto/ngx-mailto.pipe.spec.ts new file mode 100644 index 0000000..0d6611b --- /dev/null +++ b/src/ngx-mailto/ngx-mailto.pipe.spec.ts @@ -0,0 +1,8 @@ +import { NgxMailtoPipe } from './ngx-mailto.pipe'; + +describe('NgxMailtoPipe', () => { + it('create an instance', () => { + const pipe = new NgxMailtoPipe(); + expect(pipe).toBeTruthy(); + }); +}); diff --git a/src/ngx-mailto/ngx-mailto.pipe.ts b/src/ngx-mailto/ngx-mailto.pipe.ts new file mode 100644 index 0000000..615f409 --- /dev/null +++ b/src/ngx-mailto/ngx-mailto.pipe.ts @@ -0,0 +1,18 @@ +import { Pipe, PipeTransform } from '@angular/core'; +import { Mailto } from './mailto'; +import { NgxMailtoService } from './ngx-mailto.service'; + +@Pipe({ + name: 'mailto' +}) +export class NgxMailtoPipe implements PipeTransform { + + constructor(private ngxMailtoService: NgxMailtoService) { + } + + transform(value: Mailto): string | void { + console.log('transform', value); + return this.ngxMailtoService.compose(value); + } + +} diff --git a/src/ngx-mailto/ngx-mailto.service.ts b/src/ngx-mailto/ngx-mailto.service.ts index d0887c2..10a7a78 100644 --- a/src/ngx-mailto/ngx-mailto.service.ts +++ b/src/ngx-mailto/ngx-mailto.service.ts @@ -16,41 +16,47 @@ export class NgxMailtoService { } compose(value: Mailto): string | void { + console.log('compose -->', value); + let link = 'mailto:'; - let properties = []; + + if (!value) { + return link; + } + const properties = []; if (this.isClientSide()) { - if (value.receiver) { - if (typeof value.receiver === 'string') { - link += value.receiver; + if (value?.receiver) { + if (typeof value?.receiver === 'string') { + link += value?.receiver; } else { - link += value.receiver.join(); + link += value?.receiver.join(); } } - if (value.cc) { - if (typeof value.cc === 'string') { - // link += `?cc=${value.cc}`; - properties.push(`cc=${value.cc}`); + if (value?.cc) { + if (typeof value?.cc === 'string') { + // link += `?cc=${value?.cc}`; + properties.push(`cc=${value?.cc}`); } else { - // link += `?cc=${value.cc.join()}`; - properties.push(`cc=${value.cc.join()}`); + // link += `?cc=${value?.cc.join()}`; + properties.push(`cc=${value?.cc.join()}`); } } - if (value.bcc) { - if (typeof value.bcc === 'string') { - // link += `?bcc=${value.bcc}`; - properties.push(`bcc=${value.bcc}`); + if (value?.bcc) { + if (typeof value?.bcc === 'string') { + // link += `?bcc=${value?.bcc}`; + properties.push(`bcc=${value?.bcc}`); } else { - // link += `?bcc=${value.bcc.join()}`; - properties.push(`bcc=${value.bcc.join()}`); + // link += `?bcc=${value?.bcc.join()}`; + properties.push(`bcc=${value?.bcc.join()}`); } } - if (value.subject) { - // link += `?subject=${value.subject}`; - properties.push(`subject=${value.subject}`); + if (value?.subject) { + // link += `?subject=${value?.subject}`; + properties.push(`subject=${encodeURIComponent(value?.subject)}`); } - if (value.body) { - // link += `?body=${value.body}`; - properties.push(`body=${value.body}`); + if (value?.body) { + // link += `?body=${value?.body}`; + properties.push(`body=${encodeURIComponent(value?.body)}`); } if (properties.length > 0) { link += `?${properties.join('&')}`; @@ -58,4 +64,10 @@ export class NgxMailtoService { return link; } } + + open(mailto: Mailto): void { + if (this.isClientSide()) { + window.location.href = this.compose(mailto) as string; + } + } }