1 | import { EventEmitter, Component, ChangeDetectionStrategy, ViewEncapsulation, ElementRef, NgZone, ChangeDetectorRef, Input, Output, NgModule } from '@angular/core';
|
2 | import { CommonModule } from '@angular/common';
|
3 |
|
4 | class Captcha {
|
5 | constructor(el, _zone, cd) {
|
6 | this.el = el;
|
7 | this._zone = _zone;
|
8 | this.cd = cd;
|
9 | this.siteKey = null;
|
10 | this.theme = 'light';
|
11 | this.type = 'image';
|
12 | this.size = 'normal';
|
13 | this.tabindex = 0;
|
14 | this.language = null;
|
15 | this.initCallback = "initRecaptcha";
|
16 | this.onResponse = new EventEmitter();
|
17 | this.onExpire = new EventEmitter();
|
18 | this._instance = null;
|
19 | }
|
20 | ngAfterViewInit() {
|
21 | if (window.grecaptcha) {
|
22 | if (!window.grecaptcha.render) {
|
23 | setTimeout(() => {
|
24 | this.init();
|
25 | }, 100);
|
26 | }
|
27 | else {
|
28 | this.init();
|
29 | }
|
30 | }
|
31 | else {
|
32 | window[this.initCallback] = () => {
|
33 | this.init();
|
34 | };
|
35 | }
|
36 | }
|
37 | init() {
|
38 | this._instance = window.grecaptcha.render(this.el.nativeElement.children[0], {
|
39 | 'sitekey': this.siteKey,
|
40 | 'theme': this.theme,
|
41 | 'type': this.type,
|
42 | 'size': this.size,
|
43 | 'tabindex': this.tabindex,
|
44 | 'hl': this.language,
|
45 | 'callback': (response) => { this._zone.run(() => this.recaptchaCallback(response)); },
|
46 | 'expired-callback': () => { this._zone.run(() => this.recaptchaExpiredCallback()); }
|
47 | });
|
48 | }
|
49 | reset() {
|
50 | if (this._instance === null)
|
51 | return;
|
52 | window.grecaptcha.reset(this._instance);
|
53 | this.cd.markForCheck();
|
54 | }
|
55 | getResponse() {
|
56 | if (this._instance === null)
|
57 | return null;
|
58 | return window.grecaptcha.getResponse(this._instance);
|
59 | }
|
60 | recaptchaCallback(response) {
|
61 | this.onResponse.emit({
|
62 | response: response
|
63 | });
|
64 | }
|
65 | recaptchaExpiredCallback() {
|
66 | this.onExpire.emit();
|
67 | }
|
68 | ngOnDestroy() {
|
69 | if (this._instance != null) {
|
70 | window.grecaptcha.reset(this._instance);
|
71 | }
|
72 | }
|
73 | }
|
74 | Captcha.decorators = [
|
75 | { type: Component, args: [{
|
76 | selector: 'p-captcha',
|
77 | template: `<div></div>`,
|
78 | changeDetection: ChangeDetectionStrategy.OnPush,
|
79 | encapsulation: ViewEncapsulation.None
|
80 | },] }
|
81 | ];
|
82 | Captcha.ctorParameters = () => [
|
83 | { type: ElementRef },
|
84 | { type: NgZone },
|
85 | { type: ChangeDetectorRef }
|
86 | ];
|
87 | Captcha.propDecorators = {
|
88 | siteKey: [{ type: Input }],
|
89 | theme: [{ type: Input }],
|
90 | type: [{ type: Input }],
|
91 | size: [{ type: Input }],
|
92 | tabindex: [{ type: Input }],
|
93 | language: [{ type: Input }],
|
94 | initCallback: [{ type: Input }],
|
95 | onResponse: [{ type: Output }],
|
96 | onExpire: [{ type: Output }]
|
97 | };
|
98 | class CaptchaModule {
|
99 | }
|
100 | CaptchaModule.decorators = [
|
101 | { type: NgModule, args: [{
|
102 | imports: [CommonModule],
|
103 | exports: [Captcha],
|
104 | declarations: [Captcha]
|
105 | },] }
|
106 | ];
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 | export { Captcha, CaptchaModule };
|
113 |
|