-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8b8f132
commit 51053a6
Showing
12 changed files
with
226 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/** | ||
* @author Anthony Nahas | ||
* @since 25.11.2020 | ||
* @version 1.0 | ||
* | ||
* All properties are optional | ||
*/ | ||
export interface Mailto { | ||
receiver?: string | string[]; | ||
subject?: string; | ||
cc?: string | string[]; | ||
bcc?: string | string[]; | ||
body?: string; | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,16 @@ | ||
import { CommonModule } from '@angular/common'; | ||
import { NgModule } from '@angular/core'; | ||
import { NgxMailtoComponent } from './ngx-mailto.component'; | ||
|
||
import { NgxMailtoPipe } from './ngx-mailto.pipe'; | ||
|
||
|
||
@NgModule({ | ||
declarations: [NgxMailtoComponent], | ||
imports: [ | ||
declarations: [NgxMailtoPipe], | ||
exports: [ | ||
NgxMailtoPipe | ||
], | ||
exports: [NgxMailtoComponent] | ||
imports: [ | ||
CommonModule | ||
] | ||
}) | ||
export class NgxMailtoModule { } | ||
export class NgxMailtoModule { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { NgxMailtoPipe } from './ngx-mailto.pipe'; | ||
|
||
describe('NgxMailtoPipe', () => { | ||
it('create an instance', () => { | ||
const pipe = new NgxMailtoPipe(); | ||
expect(pipe).toBeTruthy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,73 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { isPlatformBrowser } from '@angular/common'; | ||
import { Inject, Injectable, PLATFORM_ID } from '@angular/core'; | ||
import { Mailto } from './mailto'; | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class NgxMailtoService { | ||
|
||
constructor() { } | ||
// tslint:disable-next-line:ban-types | ||
constructor(@Inject(PLATFORM_ID) private platformId: Object) { | ||
} | ||
|
||
isClientSide(): boolean { | ||
return isPlatformBrowser(this.platformId); | ||
} | ||
|
||
compose(value: Mailto): string | void { | ||
console.log('compose -->', value); | ||
|
||
let link = 'mailto:'; | ||
|
||
if (!value) { | ||
return link; | ||
} | ||
const properties = []; | ||
if (this.isClientSide()) { | ||
if (value?.receiver) { | ||
if (typeof value?.receiver === 'string') { | ||
link += value?.receiver; | ||
} else { | ||
link += value?.receiver.join(); | ||
} | ||
} | ||
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()}`); | ||
} | ||
} | ||
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()}`); | ||
} | ||
} | ||
if (value?.subject) { | ||
// link += `?subject=${value?.subject}`; | ||
properties.push(`subject=${encodeURIComponent(value?.subject)}`); | ||
} | ||
if (value?.body) { | ||
// link += `?body=${value?.body}`; | ||
properties.push(`body=${encodeURIComponent(value?.body)}`); | ||
} | ||
if (properties.length > 0) { | ||
link += `?${properties.join('&')}`; | ||
} | ||
return link; | ||
} | ||
} | ||
|
||
open(mailto: Mailto): void { | ||
if (this.isClientSide()) { | ||
window.location.href = this.compose(mailto) as string; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
```html | ||
<button class="try-me" mat-raised-button color="primary" (click)="open()">Try Me!</button> | ||
``` | ||
|
||
```typescript | ||
|
||
emails: string[] = ['[email protected]']; | ||
cc: string[] = []; | ||
bcc: string[] = []; | ||
subject!: string; | ||
body!: string; | ||
|
||
mailto: Mailto = { | ||
receiver: this.emails, | ||
cc: this.cc, | ||
bcc: this.bcc, | ||
subject: undefined, | ||
body: undefined | ||
}; | ||
|
||
constructor(private mailtoService: NgxMailtoService) { | ||
} | ||
|
||
|
||
open(): void { | ||
this.mailtoService.open(this.mailto); | ||
} | ||
|
||
``` |