-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathcaptcha.component.ts
121 lines (107 loc) · 3.64 KB
/
captcha.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import {
Component,
OnInit,
Input,
Output,
EventEmitter,
NgZone,
ViewChild, ElementRef, forwardRef
} from '@angular/core';
import { NG_VALUE_ACCESSOR, ControlValueAccessor } from '@angular/forms';
import { ReCaptchaService } from './captcha.service';
@Component({
selector: 're-captcha',
template: '<div #target></div>',
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => ReCaptchaComponent),
multi: true
}
]
})
export class ReCaptchaComponent implements OnInit, ControlValueAccessor {
@Input() site_key: string = null;
@Input() theme = 'light';
@Input() type = 'image';
@Input() size = 'normal';
@Input() tabindex = 0;
@Input() badge = 'bottomright';
/* Available languages: https://developers.google.com/recaptcha/docs/language */
@Input() language: string = null;
@Input() global: boolean = false;
@Input() scriptUrl: string = 'www.google.com'
@Output() captchaResponse = new EventEmitter<string>();
@Output() captchaExpired = new EventEmitter();
@Output() loaded = new EventEmitter<boolean>();
@ViewChild('target') targetRef: ElementRef;
widgetId: any = null;
onChange: Function = () => {};
onTouched: Function = () => {};
constructor(
private _zone: NgZone,
private _captchaService: ReCaptchaService
) {
}
ngOnInit() {
this._captchaService.getReady(this.language, this.global, this.scriptUrl)
.subscribe((ready) => {
if (!ready)
return;
// noinspection TypeScriptUnresolvedVariable,TypeScriptUnresolvedFunction
this.widgetId = (<any>window).grecaptcha.render(this.targetRef.nativeElement, {
'sitekey': this.site_key,
'badge': this.badge,
'theme': this.theme,
'type': this.type,
'size': this.size,
'tabindex': this.tabindex,
'callback': <any>((response: any) => this._zone.run(this.recaptchaCallback.bind(this, response))),
'expired-callback': <any>(() => this._zone.run(this.recaptchaExpiredCallback.bind(this)))
});
setTimeout(() => {
this.loaded.emit(true);
}, 0);
});
}
// noinspection JSUnusedGlobalSymbols
public reset() {
if (this.widgetId === null)
return;
// noinspection TypeScriptUnresolvedVariable
this._zone.runOutsideAngular((<any>window).grecaptcha.reset.bind(this.widgetId));
this.onChange(null);
}
// noinspection JSUnusedGlobalSymbols
public execute() {
if (this.widgetId === null)
return;
// noinspection TypeScriptUnresolvedVariable
(<any>window).grecaptcha.execute(this.widgetId);
}
public getResponse(): string {
if (this.widgetId === null)
return null;
// noinspection TypeScriptUnresolvedVariable
return (<any>window).grecaptcha.getResponse(this.widgetId);
}
writeValue(newValue: any): void {
/* ignore it */
}
registerOnChange(fn: any): void {
this.onChange = fn;
}
registerOnTouched(fn: any): void {
this.onTouched = fn;
}
private recaptchaCallback(response: string) {
this.onChange(response);
this.onTouched();
this.captchaResponse.emit(response);
}
private recaptchaExpiredCallback() {
this.onChange(null);
this.onTouched();
this.captchaExpired.emit();
}
}