This repository has been archived by the owner on Aug 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 260
/
directive.js
129 lines (106 loc) · 4.59 KB
/
directive.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*global angular, Recaptcha */
(function (ng) {
'use strict';
var app = ng.module('vcRecaptcha');
app.directive('vcRecaptcha', ['$document', '$timeout', 'vcRecaptchaService', function ($document, $timeout, vcRecaptcha) {
return {
restrict: 'A',
require: "?^^form",
scope: {
response: '=?ngModel',
key: '=?',
stoken: '=?',
theme: '=?',
size: '=?',
type: '=?',
lang: '=?',
badge: '=?',
tabindex: '=?',
required: '=?',
onCreate: '&',
onSuccess: '&',
onExpire: '&',
onError: '&'
},
link: function (scope, elm, attrs, ctrl) {
scope.widgetId = null;
if(ctrl && ng.isDefined(attrs.required)){
scope.$watch('required', validate);
}
var removeCreationListener = scope.$watch('key', function (key) {
var callback = function (gRecaptchaResponse) {
// Safe $apply
$timeout(function () {
scope.response = gRecaptchaResponse;
validate();
// Notify about the response availability
scope.onSuccess({response: gRecaptchaResponse, widgetId: scope.widgetId});
});
};
vcRecaptcha.create(elm[0], {
callback: callback,
key: key,
stoken: scope.stoken || attrs.stoken || null,
theme: scope.theme || attrs.theme || null,
type: scope.type || attrs.type || null,
lang: scope.lang || attrs.lang || null,
tabindex: scope.tabindex || attrs.tabindex || null,
size: scope.size || attrs.size || null,
badge: scope.badge || attrs.badge || null,
'expired-callback': expired,
'error-callback': attrs.onError ? error : undefined
}).then(function (widgetId) {
// The widget has been created
validate();
scope.widgetId = widgetId;
scope.onCreate({widgetId: widgetId});
scope.$on('$destroy', destroy);
scope.$on('reCaptchaReset', function(event, resetWidgetId){
if(ng.isUndefined(resetWidgetId) || widgetId === resetWidgetId){
scope.response = "";
validate();
}
})
});
// Remove this listener to avoid creating the widget more than once.
removeCreationListener();
});
function destroy() {
if (ctrl) {
// reset the validity of the form if we were removed
ctrl.$setValidity('recaptcha', null);
}
cleanup();
}
function expired(){
// Safe $apply
$timeout(function () {
scope.response = "";
validate();
// Notify about the response availability
scope.onExpire({ widgetId: scope.widgetId });
});
}
function error() {
var args = arguments;
$timeout(function () {
scope.response = "";
validate();
// Notify about the response availability
scope.onError({ widgetId: scope.widgetId, arguments: args });
});
}
function validate(){
if(ctrl){
ctrl.$setValidity('recaptcha', scope.required === false ? null : Boolean(scope.response));
}
}
function cleanup(){
vcRecaptcha.destroy(scope.widgetId);
// removes elements reCaptcha added.
ng.element($document[0].querySelectorAll('.pls-container')).parent().remove();
}
}
};
}]);
}(angular));