|
1 | 1 | /**
|
2 |
| - * angular-recaptcha build:2016-07-19 |
3 |
| - * https://github.com/vividcortex/angular-recaptcha |
4 |
| - * Copyright (c) 2016 VividCortex |
| 2 | + * @license angular-recaptcha build:2016-12-07 |
| 3 | + * https://github.com/vividcortex/angular-recaptcha |
| 4 | + * Copyright (c) 2016 VividCortex |
5 | 5 | **/
|
6 | 6 |
|
7 | 7 | /*global angular, Recaptcha */
|
|
90 | 90 | config.type = type;
|
91 | 91 | };
|
92 | 92 |
|
| 93 | + /** |
| 94 | + * Sets the reCaptcha language which will be used by default is not specified in a specific directive instance. |
| 95 | + * |
| 96 | + * @param lang The reCaptcha language. |
| 97 | + */ |
| 98 | + provider.setLang = function(lang){ |
| 99 | + config.lang = lang; |
| 100 | + }; |
| 101 | + |
93 | 102 | /**
|
94 | 103 | * Sets the reCaptcha configuration values which will be used by default is not specified in a specific directive instance.
|
95 | 104 | *
|
|
101 | 110 | };
|
102 | 111 |
|
103 | 112 | provider.$get = ['$rootScope','$window', '$q', function ($rootScope, $window, $q) {
|
104 |
| - var deferred = $q.defer(), promise = deferred.promise, recaptcha; |
| 113 | + var deferred = $q.defer(), promise = deferred.promise, instances = {}, recaptcha; |
105 | 114 |
|
106 | 115 | $window.vcRecaptchaApiLoadedCallback = $window.vcRecaptchaApiLoadedCallback || [];
|
107 | 116 |
|
|
156 | 165 | conf.stoken = conf.stoken || config.stoken;
|
157 | 166 | conf.size = conf.size || config.size;
|
158 | 167 | conf.type = conf.type || config.type;
|
| 168 | + conf.hl = conf.lang || config.lang; |
159 | 169 |
|
160 | 170 | if (!conf.sitekey || conf.sitekey.length !== 40) {
|
161 | 171 | throwNoKeyException();
|
162 | 172 | }
|
163 | 173 | return getRecaptcha().then(function (recaptcha) {
|
164 |
| - return recaptcha.render(elm, conf); |
| 174 | + var widgetId = recaptcha.render(elm, conf); |
| 175 | + instances[widgetId] = elm; |
| 176 | + return widgetId; |
165 | 177 | });
|
166 | 178 | },
|
167 | 179 |
|
|
171 | 183 | reload: function (widgetId) {
|
172 | 184 | validateRecaptchaInstance();
|
173 | 185 |
|
174 |
| - // $log.info('Reloading captcha'); |
175 | 186 | recaptcha.reset(widgetId);
|
176 | 187 |
|
177 | 188 | // Let everyone know this widget has been reset.
|
178 | 189 | $rootScope.$broadcast('reCaptchaReset', widgetId);
|
179 | 190 | },
|
180 | 191 |
|
| 192 | + /** |
| 193 | + * Get/Set reCaptcha language |
| 194 | + */ |
| 195 | + useLang: function (widgetId, lang) { |
| 196 | + var instance = instances[widgetId]; |
| 197 | + |
| 198 | + if (instance) { |
| 199 | + var iframe = instance.querySelector('iframe'); |
| 200 | + if (lang) { |
| 201 | + // Setter |
| 202 | + if (iframe && iframe.src) { |
| 203 | + var s = iframe.src; |
| 204 | + if (/[?&]hl=/.test(s)) { |
| 205 | + s = s.replace(/([?&]hl=)\w+/, '$1' + lang); |
| 206 | + } else { |
| 207 | + s += ((s.indexOf('?') === -1) ? '?' : '&') + 'hl=' + lang; |
| 208 | + } |
| 209 | + |
| 210 | + iframe.src = s; |
| 211 | + } |
| 212 | + } else { |
| 213 | + // Getter |
| 214 | + if (iframe && iframe.src && /[?&]hl=\w+/.test(iframe.src)) { |
| 215 | + return iframe.src.replace(/.+[?&]hl=(\w+)([^\w].+)?/, '$1'); |
| 216 | + } else { |
| 217 | + return null; |
| 218 | + } |
| 219 | + } |
| 220 | + } else { |
| 221 | + throw new Error('reCaptcha Widget ID not exists', widgetId); |
| 222 | + } |
| 223 | + }, |
| 224 | + |
181 | 225 | /**
|
182 | 226 | * Gets the response from the reCaptcha widget.
|
183 | 227 | *
|
|
189 | 233 | validateRecaptchaInstance();
|
190 | 234 |
|
191 | 235 | return recaptcha.getResponse(widgetId);
|
| 236 | + }, |
| 237 | + |
| 238 | + /** |
| 239 | + * Gets reCaptcha instance and configuration |
| 240 | + */ |
| 241 | + getInstance: function (widgetId) { |
| 242 | + return instances[widgetId]; |
| 243 | + }, |
| 244 | + |
| 245 | + /** |
| 246 | + * Destroy reCaptcha instance. |
| 247 | + */ |
| 248 | + destroy: function (widgetId) { |
| 249 | + delete instances[widgetId]; |
192 | 250 | }
|
193 | 251 | };
|
194 | 252 |
|
|
215 | 273 | theme: '=?',
|
216 | 274 | size: '=?',
|
217 | 275 | type: '=?',
|
| 276 | + lang: '=?', |
218 | 277 | tabindex: '=?',
|
219 | 278 | required: '=?',
|
220 | 279 | onCreate: '&',
|
|
247 | 306 | stoken: scope.stoken || attrs.stoken || null,
|
248 | 307 | theme: scope.theme || attrs.theme || null,
|
249 | 308 | type: scope.type || attrs.type || null,
|
| 309 | + lang: scope.lang || attrs.lang || null, |
250 | 310 | tabindex: scope.tabindex || attrs.tabindex || null,
|
251 | 311 | size: scope.size || attrs.size || null,
|
252 | 312 | 'expired-callback': expired
|
|
299 | 359 | }
|
300 | 360 |
|
301 | 361 | function cleanup(){
|
| 362 | + vcRecaptcha.destroy(scope.widgetId); |
| 363 | + |
302 | 364 | // removes elements reCaptcha added.
|
303 | 365 | ng.element($document[0].querySelectorAll('.pls-container')).parent().remove();
|
304 | 366 | }
|
|
0 commit comments