Skip to content
This repository was archived by the owner on Aug 17, 2021. It is now read-only.

Commit d001b38

Browse files
committed
Make possible passing a promise as a site key.
1 parent a187773 commit d001b38

File tree

1 file changed

+42
-37
lines changed

1 file changed

+42
-37
lines changed

src/directive.js

Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
var app = ng.module('vcRecaptcha');
66

7-
app.directive('vcRecaptcha', ['$document', '$timeout', 'vcRecaptchaService', function ($document, $timeout, vcRecaptcha) {
7+
app.directive('vcRecaptcha', ['$document', '$timeout', 'vcRecaptchaService', '$q', function ($document, $timeout, vcRecaptcha, $q) {
88

99
return {
1010
restrict: 'A',
11-
require: "?^^form",
11+
require: '?^^form',
1212
scope: {
1313
response: '=?ngModel',
1414
key: '=?',
@@ -27,11 +27,16 @@
2727
link: function (scope, elm, attrs, ctrl) {
2828
scope.widgetId = null;
2929

30-
if(ctrl && ng.isDefined(attrs.required)){
30+
if (ctrl && ng.isDefined(attrs.required)) {
3131
scope.$watch('required', validate);
3232
}
3333

3434
var removeCreationListener = scope.$watch('key', function (key) {
35+
// Abort if undefined or null
36+
if (ng.isUndefined(key) || key === null) {
37+
return;
38+
}
39+
3540
var callback = function (gRecaptchaResponse) {
3641
// Safe $apply
3742
$timeout(function () {
@@ -43,19 +48,20 @@
4348
});
4449
};
4550

46-
vcRecaptcha.create(elm[0], {
47-
48-
callback: callback,
49-
key: key,
50-
stoken: scope.stoken || attrs.stoken || null,
51-
theme: scope.theme || attrs.theme || null,
52-
type: scope.type || attrs.type || null,
53-
lang: scope.lang || attrs.lang || null,
54-
tabindex: scope.tabindex || attrs.tabindex || null,
55-
size: scope.size || attrs.size || null,
56-
badge: scope.badge || attrs.badge || null,
57-
'expired-callback': expired
58-
51+
// Accept a promise, or resolve immediately if the value is a bare string
52+
$q.resolve(key).then(function (resolved) {
53+
return vcRecaptcha.create(elm[0], {
54+
callback: callback,
55+
key: resolved,
56+
stoken: scope.stoken || attrs.stoken || null,
57+
theme: scope.theme || attrs.theme || null,
58+
type: scope.type || attrs.type || null,
59+
lang: scope.lang || attrs.lang || null,
60+
tabindex: scope.tabindex || attrs.tabindex || null,
61+
size: scope.size || attrs.size || null,
62+
badge: scope.badge || attrs.badge || null,
63+
'expired-callback': expired
64+
});
5965
}).then(function (widgetId) {
6066
// The widget has been created
6167
validate();
@@ -64,50 +70,49 @@
6470

6571
scope.$on('$destroy', destroy);
6672

67-
scope.$on('reCaptchaReset', function(event, resetWidgetId){
68-
if(ng.isUndefined(resetWidgetId) || widgetId === resetWidgetId){
69-
scope.response = "";
70-
validate();
71-
}
72-
})
73-
73+
scope.$on('reCaptchaReset', function (event, resetWidgetId) {
74+
if (ng.isUndefined(resetWidgetId) || widgetId === resetWidgetId) {
75+
scope.response = '';
76+
validate();
77+
}
78+
});
7479
});
7580

7681
// Remove this listener to avoid creating the widget more than once.
7782
removeCreationListener();
7883
});
7984

8085
function destroy() {
81-
if (ctrl) {
82-
// reset the validity of the form if we were removed
83-
ctrl.$setValidity('recaptcha', null);
84-
}
86+
if (ctrl) {
87+
// reset the validity of the form if we were removed
88+
ctrl.$setValidity('recaptcha', null);
89+
}
8590

86-
cleanup();
91+
cleanup();
8792
}
8893

89-
function expired(){
94+
function expired() {
9095
// Safe $apply
9196
$timeout(function () {
92-
scope.response = "";
97+
scope.response = '';
9398
validate();
9499

95100
// Notify about the response availability
96-
scope.onExpire({ widgetId: scope.widgetId });
101+
scope.onExpire({widgetId: scope.widgetId});
97102
});
98103
}
99104

100-
function validate(){
101-
if(ctrl){
105+
function validate() {
106+
if (ctrl) {
102107
ctrl.$setValidity('recaptcha', scope.required === false ? null : Boolean(scope.response));
103108
}
104109
}
105110

106-
function cleanup(){
107-
vcRecaptcha.destroy(scope.widgetId);
111+
function cleanup() {
112+
vcRecaptcha.destroy(scope.widgetId);
108113

109-
// removes elements reCaptcha added.
110-
ng.element($document[0].querySelectorAll('.pls-container')).parent().remove();
114+
// removes elements reCaptcha added.
115+
ng.element($document[0].querySelectorAll('.pls-container')).parent().remove();
111116
}
112117
}
113118
};

0 commit comments

Comments
 (0)