ng-recaptchav3
Version:
Angular8 library to provide easy extraction for google recaptcha v3, invisible recaptcha and recaptcha v2
440 lines (431 loc) • 12.6 kB
JavaScript
import { InjectionToken, Injectable, Inject, PLATFORM_ID, Optional, EventEmitter, Component, ElementRef, NgZone, Input, HostBinding, Output, NgModule } from '@angular/core';
import { isPlatformBrowser } from '@angular/common';
import { of, BehaviorSubject } from 'rxjs';
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/** @type {?} */
var RECAPTCHA_LANGUAGE = new InjectionToken('recaptcha-language');
var RecaptchaLoaderService = /** @class */ (function () {
function RecaptchaLoaderService(platformId, language) {
this.platformId = platformId;
this.language = language;
this.init();
this.ready = isPlatformBrowser(this.platformId) ? RecaptchaLoaderService.ready.asObservable() : of();
}
/** @internal */
/**
* \@internal
* @private
* @return {?}
*/
RecaptchaLoaderService.prototype.init = /**
* \@internal
* @private
* @return {?}
*/
function () {
if (RecaptchaLoaderService.ready || !isPlatformBrowser(this.platformId)) {
return;
}
window.ngRecaptchaLoaded = (/**
* @return {?}
*/
function () {
RecaptchaLoaderService.ready.next(grecaptcha);
});
RecaptchaLoaderService.ready = new BehaviorSubject(null);
/** @type {?} */
var script = (/** @type {?} */ (document.createElement('script')));
script.innerHTML = '';
/** @type {?} */
var langParam = this.language ? "&hl=" + this.language : '';
script.src = "https://www.google.com/recaptcha/api.js?render=explicit&onload=ngRecaptchaLoaded" + langParam;
script.async = true;
script.defer = true;
script.setAttribute('nonce', '');
document.head.appendChild(script);
};
/**
* \@internal
* @nocollapse
*/
RecaptchaLoaderService.ready = null;
RecaptchaLoaderService.decorators = [
{ type: Injectable }
];
/** @nocollapse */
RecaptchaLoaderService.ctorParameters = function () { return [
{ type: undefined, decorators: [{ type: Inject, args: [PLATFORM_ID,] }] },
{ type: String, decorators: [{ type: Optional }, { type: Inject, args: [RECAPTCHA_LANGUAGE,] }] }
]; };
return RecaptchaLoaderService;
}());
if (false) {
/**
* \@internal
* @nocollapse
* @type {?}
* @private
*/
RecaptchaLoaderService.ready;
/** @type {?} */
RecaptchaLoaderService.prototype.ready;
/**
* \@internal
* @type {?}
* @private
*/
RecaptchaLoaderService.prototype.language;
/**
* @type {?}
* @private
*/
RecaptchaLoaderService.prototype.platformId;
}
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/** @type {?} */
var RECAPTCHA_SETTINGS = new InjectionToken('recaptcha-settings');
/**
* @record
*/
function RecaptchaSettings() { }
if (false) {
/** @type {?|undefined} */
RecaptchaSettings.prototype.siteKey;
/** @type {?|undefined} */
RecaptchaSettings.prototype.theme;
/** @type {?|undefined} */
RecaptchaSettings.prototype.type;
/** @type {?|undefined} */
RecaptchaSettings.prototype.size;
/** @type {?|undefined} */
RecaptchaSettings.prototype.badge;
}
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/** @type {?} */
var nextId = 0;
var RecaptchaComponent = /** @class */ (function () {
function RecaptchaComponent(elementRef, loader, zone, settings) {
this.elementRef = elementRef;
this.loader = loader;
this.zone = zone;
this.id = "ngrecaptcha-" + nextId++;
this.resolved = new EventEmitter();
if (!settings) {
return;
}
this.siteKey = settings.siteKey;
this.theme = settings.theme;
this.type = settings.type;
this.size = settings.size;
this.badge = settings.badge;
}
/**
* @return {?}
*/
RecaptchaComponent.prototype.ngAfterViewInit = /**
* @return {?}
*/
function () {
var _this = this;
this.subscription = this.loader.ready.subscribe((/**
* @param {?} grecaptcha
* @return {?}
*/
function (grecaptcha) {
if (grecaptcha != null) {
_this.grecaptcha = grecaptcha;
_this.renderRecaptcha();
}
}));
};
/**
* @return {?}
*/
RecaptchaComponent.prototype.ngOnDestroy = /**
* @return {?}
*/
function () {
// reset the captcha to ensure it does not leave anything behind
// after the component is no longer needed
this.grecaptchaReset();
if (this.subscription) {
this.subscription.unsubscribe();
}
};
/**
* Executes the invisible recaptcha.
* Does nothing if component's size is not set to "invisible".
*/
/**
* Executes the invisible recaptcha.
* Does nothing if component's size is not set to "invisible".
* @return {?}
*/
RecaptchaComponent.prototype.execute = /**
* Executes the invisible recaptcha.
* Does nothing if component's size is not set to "invisible".
* @return {?}
*/
function () {
if (this.size !== 'invisible' || this.widget === null) {
return;
}
if (this.actionName) {
this.grecaptcha.execute(this.widget, { action: this.actionName });
}
else {
this.grecaptcha.execute(this.widget);
}
};
/**
* @return {?}
*/
RecaptchaComponent.prototype.reset = /**
* @return {?}
*/
function () {
if (this.widget === null) {
return;
}
if (this.grecaptcha.getResponse(this.widget)) {
// Only emit an event in case if something would actually change.
// That way we do not trigger "touching" of the control if someone does a "reset"
// on a non-resolved captcha.
this.resolved.emit(null);
}
this.grecaptchaReset();
};
/** @internal */
/**
* \@internal
* @private
* @return {?}
*/
RecaptchaComponent.prototype.expired = /**
* \@internal
* @private
* @return {?}
*/
function () {
this.resolved.emit(null);
};
/** @internal */
/**
* \@internal
* @private
* @param {?} response
* @return {?}
*/
RecaptchaComponent.prototype.captchaReponseCallback = /**
* \@internal
* @private
* @param {?} response
* @return {?}
*/
function (response) {
this.resolved.emit(response);
};
/** @internal */
/**
* \@internal
* @private
* @return {?}
*/
RecaptchaComponent.prototype.grecaptchaReset = /**
* \@internal
* @private
* @return {?}
*/
function () {
var _this = this;
if (this.widget != null) {
this.zone.runOutsideAngular((/**
* @return {?}
*/
function () { return _this.grecaptcha.reset(_this.widget); }));
}
};
/** @internal */
/**
* \@internal
* @private
* @return {?}
*/
RecaptchaComponent.prototype.renderRecaptcha = /**
* \@internal
* @private
* @return {?}
*/
function () {
var _this = this;
this.widget = this.grecaptcha.render(this.elementRef.nativeElement, {
badge: this.badge,
callback: (/**
* @param {?} response
* @return {?}
*/
function (response) {
_this.zone.run((/**
* @return {?}
*/
function () { return _this.captchaReponseCallback(response); }));
}),
'expired-callback': (/**
* @return {?}
*/
function () {
_this.zone.run((/**
* @return {?}
*/
function () { return _this.expired(); }));
}),
sitekey: this.siteKey,
size: this.size,
tabindex: this.tabIndex,
theme: this.theme,
type: this.type
});
};
RecaptchaComponent.decorators = [
{ type: Component, args: [{
exportAs: 'reCaptcha',
selector: 're-captcha',
template: ''
}] }
];
/** @nocollapse */
RecaptchaComponent.ctorParameters = function () { return [
{ type: ElementRef },
{ type: RecaptchaLoaderService },
{ type: NgZone },
{ type: undefined, decorators: [{ type: Optional }, { type: Inject, args: [RECAPTCHA_SETTINGS,] }] }
]; };
RecaptchaComponent.propDecorators = {
id: [{ type: Input }, { type: HostBinding, args: ['attr.id',] }],
siteKey: [{ type: Input }],
theme: [{ type: Input }],
type: [{ type: Input }],
size: [{ type: Input }],
tabIndex: [{ type: Input }],
badge: [{ type: Input }],
actionName: [{ type: Input }],
resolved: [{ type: Output }]
};
return RecaptchaComponent;
}());
if (false) {
/** @type {?} */
RecaptchaComponent.prototype.id;
/** @type {?} */
RecaptchaComponent.prototype.siteKey;
/** @type {?} */
RecaptchaComponent.prototype.theme;
/** @type {?} */
RecaptchaComponent.prototype.type;
/** @type {?} */
RecaptchaComponent.prototype.size;
/** @type {?} */
RecaptchaComponent.prototype.tabIndex;
/** @type {?} */
RecaptchaComponent.prototype.badge;
/** @type {?} */
RecaptchaComponent.prototype.actionName;
/** @type {?} */
RecaptchaComponent.prototype.resolved;
/**
* \@internal
* @type {?}
* @private
*/
RecaptchaComponent.prototype.subscription;
/**
* \@internal
* @type {?}
* @private
*/
RecaptchaComponent.prototype.widget;
/**
* \@internal
* @type {?}
* @private
*/
RecaptchaComponent.prototype.grecaptcha;
/**
* @type {?}
* @private
*/
RecaptchaComponent.prototype.elementRef;
/**
* @type {?}
* @private
*/
RecaptchaComponent.prototype.loader;
/**
* @type {?}
* @private
*/
RecaptchaComponent.prototype.zone;
}
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
var RecaptchaCommonModule = /** @class */ (function () {
function RecaptchaCommonModule() {
}
RecaptchaCommonModule.decorators = [
{ type: NgModule, args: [{
declarations: [RecaptchaComponent],
exports: [RecaptchaComponent],
},] }
];
return RecaptchaCommonModule;
}());
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
var RecaptchaModule = /** @class */ (function () {
function RecaptchaModule() {
}
/**
* @return {?}
*/
RecaptchaModule.forRoot = /**
* @return {?}
*/
function () {
return {
ngModule: RecaptchaModule,
providers: [
RecaptchaLoaderService
]
};
};
RecaptchaModule.decorators = [
{ type: NgModule, args: [{
exports: [RecaptchaComponent],
imports: [RecaptchaCommonModule]
},] }
];
return RecaptchaModule;
}());
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
export { RECAPTCHA_LANGUAGE, RECAPTCHA_SETTINGS, RecaptchaComponent, RecaptchaLoaderService, RecaptchaModule, RecaptchaCommonModule as ɵa };
//# sourceMappingURL=ng-recaptchav3.js.map