captcha.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. "use strict";
  2. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  3. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  4. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  5. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  6. return c > 3 && r && Object.defineProperty(target, key, r), r;
  7. };
  8. var __metadata = (this && this.__metadata) || function (k, v) {
  9. if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
  10. };
  11. Object.defineProperty(exports, "__esModule", { value: true });
  12. var core_1 = require("@angular/core");
  13. var common_1 = require("@angular/common");
  14. var Captcha = /** @class */ (function () {
  15. function Captcha(el, _zone) {
  16. this.el = el;
  17. this._zone = _zone;
  18. this.siteKey = null;
  19. this.theme = 'light';
  20. this.type = 'image';
  21. this.size = 'normal';
  22. this.tabindex = 0;
  23. this.language = null;
  24. this.initCallback = "initRecaptcha";
  25. this.onResponse = new core_1.EventEmitter();
  26. this.onExpire = new core_1.EventEmitter();
  27. this._instance = null;
  28. }
  29. Captcha.prototype.ngAfterViewInit = function () {
  30. var _this = this;
  31. if (window.grecaptcha) {
  32. if (!window.grecaptcha.render) {
  33. setTimeout(function () {
  34. _this.init();
  35. }, 100);
  36. }
  37. else {
  38. this.init();
  39. }
  40. }
  41. else {
  42. window[this.initCallback] = function () {
  43. _this.init();
  44. };
  45. }
  46. };
  47. Captcha.prototype.init = function () {
  48. var _this = this;
  49. this._instance = window.grecaptcha.render(this.el.nativeElement.children[0], {
  50. 'sitekey': this.siteKey,
  51. 'theme': this.theme,
  52. 'type': this.type,
  53. 'size': this.size,
  54. 'tabindex': this.tabindex,
  55. 'hl': this.language,
  56. 'callback': function (response) { _this._zone.run(function () { return _this.recaptchaCallback(response); }); },
  57. 'expired-callback': function () { _this._zone.run(function () { return _this.recaptchaExpiredCallback(); }); }
  58. });
  59. };
  60. Captcha.prototype.reset = function () {
  61. if (this._instance === null)
  62. return;
  63. window.grecaptcha.reset(this._instance);
  64. };
  65. Captcha.prototype.getResponse = function () {
  66. if (this._instance === null)
  67. return null;
  68. return window.grecaptcha.getResponse(this._instance);
  69. };
  70. Captcha.prototype.recaptchaCallback = function (response) {
  71. this.onResponse.emit({
  72. response: response
  73. });
  74. };
  75. Captcha.prototype.recaptchaExpiredCallback = function () {
  76. this.onExpire.emit();
  77. };
  78. Captcha.prototype.ngOnDestroy = function () {
  79. if (this._instance != null) {
  80. window.grecaptcha.reset(this._instance);
  81. }
  82. };
  83. __decorate([
  84. core_1.Input(),
  85. __metadata("design:type", String)
  86. ], Captcha.prototype, "siteKey", void 0);
  87. __decorate([
  88. core_1.Input(),
  89. __metadata("design:type", Object)
  90. ], Captcha.prototype, "theme", void 0);
  91. __decorate([
  92. core_1.Input(),
  93. __metadata("design:type", Object)
  94. ], Captcha.prototype, "type", void 0);
  95. __decorate([
  96. core_1.Input(),
  97. __metadata("design:type", Object)
  98. ], Captcha.prototype, "size", void 0);
  99. __decorate([
  100. core_1.Input(),
  101. __metadata("design:type", Object)
  102. ], Captcha.prototype, "tabindex", void 0);
  103. __decorate([
  104. core_1.Input(),
  105. __metadata("design:type", String)
  106. ], Captcha.prototype, "language", void 0);
  107. __decorate([
  108. core_1.Input(),
  109. __metadata("design:type", Object)
  110. ], Captcha.prototype, "initCallback", void 0);
  111. __decorate([
  112. core_1.Output(),
  113. __metadata("design:type", core_1.EventEmitter)
  114. ], Captcha.prototype, "onResponse", void 0);
  115. __decorate([
  116. core_1.Output(),
  117. __metadata("design:type", core_1.EventEmitter)
  118. ], Captcha.prototype, "onExpire", void 0);
  119. Captcha = __decorate([
  120. core_1.Component({
  121. selector: 'p-captcha',
  122. template: "<div></div>"
  123. }),
  124. __metadata("design:paramtypes", [core_1.ElementRef, core_1.NgZone])
  125. ], Captcha);
  126. return Captcha;
  127. }());
  128. exports.Captcha = Captcha;
  129. var CaptchaModule = /** @class */ (function () {
  130. function CaptchaModule() {
  131. }
  132. CaptchaModule = __decorate([
  133. core_1.NgModule({
  134. imports: [common_1.CommonModule],
  135. exports: [Captcha],
  136. declarations: [Captcha]
  137. })
  138. ], CaptchaModule);
  139. return CaptchaModule;
  140. }());
  141. exports.CaptchaModule = CaptchaModule;
  142. //# sourceMappingURL=captcha.js.map