|
4 | 4 |
|
5 | 5 | var app = ng.module('vcRecaptcha'); |
6 | 6 |
|
7 | | - app.directive('vcRecaptcha', ['$document', '$timeout', 'vcRecaptchaService', function ($document, $timeout, vcRecaptcha) { |
| 7 | + app.directive('vcRecaptcha', ['$document', '$timeout', 'vcRecaptchaService', '$q', function ($document, $timeout, vcRecaptcha, $q) { |
8 | 8 |
|
9 | 9 | return { |
10 | 10 | restrict: 'A', |
11 | | - require: "?^^form", |
| 11 | + require: '?^^form', |
12 | 12 | scope: { |
13 | 13 | response: '=?ngModel', |
14 | 14 | key: '=?', |
|
27 | 27 | link: function (scope, elm, attrs, ctrl) { |
28 | 28 | scope.widgetId = null; |
29 | 29 |
|
30 | | - if(ctrl && ng.isDefined(attrs.required)){ |
| 30 | + if (ctrl && ng.isDefined(attrs.required)) { |
31 | 31 | scope.$watch('required', validate); |
32 | 32 | } |
33 | 33 |
|
34 | 34 | var removeCreationListener = scope.$watch('key', function (key) { |
| 35 | + // Abort if undefined or null |
| 36 | + if (ng.isUndefined(key) || key === null) { |
| 37 | + return; |
| 38 | + } |
| 39 | + |
35 | 40 | var callback = function (gRecaptchaResponse) { |
36 | 41 | // Safe $apply |
37 | 42 | $timeout(function () { |
|
43 | 48 | }); |
44 | 49 | }; |
45 | 50 |
|
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 | + }); |
59 | 65 | }).then(function (widgetId) { |
60 | 66 | // The widget has been created |
61 | 67 | validate(); |
|
64 | 70 |
|
65 | 71 | scope.$on('$destroy', destroy); |
66 | 72 |
|
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 | + }); |
74 | 79 | }); |
75 | 80 |
|
76 | 81 | // Remove this listener to avoid creating the widget more than once. |
77 | 82 | removeCreationListener(); |
78 | 83 | }); |
79 | 84 |
|
80 | 85 | 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 | + } |
85 | 90 |
|
86 | | - cleanup(); |
| 91 | + cleanup(); |
87 | 92 | } |
88 | 93 |
|
89 | | - function expired(){ |
| 94 | + function expired() { |
90 | 95 | // Safe $apply |
91 | 96 | $timeout(function () { |
92 | | - scope.response = ""; |
| 97 | + scope.response = ''; |
93 | 98 | validate(); |
94 | 99 |
|
95 | 100 | // Notify about the response availability |
96 | | - scope.onExpire({ widgetId: scope.widgetId }); |
| 101 | + scope.onExpire({widgetId: scope.widgetId}); |
97 | 102 | }); |
98 | 103 | } |
99 | 104 |
|
100 | | - function validate(){ |
101 | | - if(ctrl){ |
| 105 | + function validate() { |
| 106 | + if (ctrl) { |
102 | 107 | ctrl.$setValidity('recaptcha', scope.required === false ? null : Boolean(scope.response)); |
103 | 108 | } |
104 | 109 | } |
105 | 110 |
|
106 | | - function cleanup(){ |
107 | | - vcRecaptcha.destroy(scope.widgetId); |
| 111 | + function cleanup() { |
| 112 | + vcRecaptcha.destroy(scope.widgetId); |
108 | 113 |
|
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(); |
111 | 116 | } |
112 | 117 | } |
113 | 118 | }; |
|
0 commit comments