|
1 | 1 | /** |
2 | | - * @license angular-recaptcha build:2017-11-22 |
| 2 | + * @license angular-recaptcha build:2018-01-03 |
3 | 3 | * https://github.com/vividcortex/angular-recaptcha |
4 | | - * Copyright (c) 2017 VividCortex |
| 4 | + * Copyright (c) 2018 VividCortex |
5 | 5 | **/ |
6 | 6 |
|
7 | 7 | /*global angular, Recaptcha */ |
|
295 | 295 |
|
296 | 296 | var app = ng.module('vcRecaptcha'); |
297 | 297 |
|
298 | | - app.directive('vcRecaptcha', ['$document', '$timeout', 'vcRecaptchaService', function ($document, $timeout, vcRecaptcha) { |
| 298 | + app.directive('vcRecaptcha', ['$document', '$timeout', 'vcRecaptchaService', '$q', function ($document, $timeout, vcRecaptcha, $q) { |
299 | 299 |
|
300 | 300 | return { |
301 | 301 | restrict: 'A', |
302 | | - require: "?^^form", |
| 302 | + require: '?^^form', |
303 | 303 | scope: { |
304 | 304 | response: '=?ngModel', |
305 | 305 | key: '=?', |
|
318 | 318 | link: function (scope, elm, attrs, ctrl) { |
319 | 319 | scope.widgetId = null; |
320 | 320 |
|
321 | | - if(ctrl && ng.isDefined(attrs.required)){ |
| 321 | + if (ctrl && ng.isDefined(attrs.required)) { |
322 | 322 | scope.$watch('required', validate); |
323 | 323 | } |
324 | 324 |
|
325 | 325 | var removeCreationListener = scope.$watch('key', function (key) { |
| 326 | + // Abort if undefined or null |
| 327 | + if (ng.isUndefined(key) || key === null) { |
| 328 | + return; |
| 329 | + } |
| 330 | + |
326 | 331 | var callback = function (gRecaptchaResponse) { |
327 | 332 | // Safe $apply |
328 | 333 | $timeout(function () { |
|
334 | 339 | }); |
335 | 340 | }; |
336 | 341 |
|
337 | | - vcRecaptcha.create(elm[0], { |
338 | | - |
339 | | - callback: callback, |
340 | | - key: key, |
341 | | - stoken: scope.stoken || attrs.stoken || null, |
342 | | - theme: scope.theme || attrs.theme || null, |
343 | | - type: scope.type || attrs.type || null, |
344 | | - lang: scope.lang || attrs.lang || null, |
345 | | - tabindex: scope.tabindex || attrs.tabindex || null, |
346 | | - size: scope.size || attrs.size || null, |
347 | | - badge: scope.badge || attrs.badge || null, |
348 | | - 'expired-callback': expired |
349 | | - |
| 342 | + // Accept a promise, or resolve immediately if the value is a bare string |
| 343 | + $q.resolve(key).then(function (resolved) { |
| 344 | + return vcRecaptcha.create(elm[0], { |
| 345 | + callback: callback, |
| 346 | + key: resolved, |
| 347 | + stoken: scope.stoken || attrs.stoken || null, |
| 348 | + theme: scope.theme || attrs.theme || null, |
| 349 | + type: scope.type || attrs.type || null, |
| 350 | + lang: scope.lang || attrs.lang || null, |
| 351 | + tabindex: scope.tabindex || attrs.tabindex || null, |
| 352 | + size: scope.size || attrs.size || null, |
| 353 | + badge: scope.badge || attrs.badge || null, |
| 354 | + 'expired-callback': expired |
| 355 | + }); |
350 | 356 | }).then(function (widgetId) { |
351 | 357 | // The widget has been created |
352 | 358 | validate(); |
|
355 | 361 |
|
356 | 362 | scope.$on('$destroy', destroy); |
357 | 363 |
|
358 | | - scope.$on('reCaptchaReset', function(event, resetWidgetId){ |
359 | | - if(ng.isUndefined(resetWidgetId) || widgetId === resetWidgetId){ |
360 | | - scope.response = ""; |
361 | | - validate(); |
362 | | - } |
363 | | - }) |
364 | | - |
| 364 | + scope.$on('reCaptchaReset', function (event, resetWidgetId) { |
| 365 | + if (ng.isUndefined(resetWidgetId) || widgetId === resetWidgetId) { |
| 366 | + scope.response = ''; |
| 367 | + validate(); |
| 368 | + } |
| 369 | + }); |
365 | 370 | }); |
366 | 371 |
|
367 | 372 | // Remove this listener to avoid creating the widget more than once. |
368 | 373 | removeCreationListener(); |
369 | 374 | }); |
370 | 375 |
|
371 | 376 | function destroy() { |
372 | | - if (ctrl) { |
373 | | - // reset the validity of the form if we were removed |
374 | | - ctrl.$setValidity('recaptcha', null); |
375 | | - } |
| 377 | + if (ctrl) { |
| 378 | + // reset the validity of the form if we were removed |
| 379 | + ctrl.$setValidity('recaptcha', null); |
| 380 | + } |
376 | 381 |
|
377 | | - cleanup(); |
| 382 | + cleanup(); |
378 | 383 | } |
379 | 384 |
|
380 | | - function expired(){ |
| 385 | + function expired() { |
381 | 386 | // Safe $apply |
382 | 387 | $timeout(function () { |
383 | | - scope.response = ""; |
| 388 | + scope.response = ''; |
384 | 389 | validate(); |
385 | 390 |
|
386 | 391 | // Notify about the response availability |
387 | | - scope.onExpire({ widgetId: scope.widgetId }); |
| 392 | + scope.onExpire({widgetId: scope.widgetId}); |
388 | 393 | }); |
389 | 394 | } |
390 | 395 |
|
391 | | - function validate(){ |
392 | | - if(ctrl){ |
| 396 | + function validate() { |
| 397 | + if (ctrl) { |
393 | 398 | ctrl.$setValidity('recaptcha', scope.required === false ? null : Boolean(scope.response)); |
394 | 399 | } |
395 | 400 | } |
396 | 401 |
|
397 | | - function cleanup(){ |
398 | | - vcRecaptcha.destroy(scope.widgetId); |
| 402 | + function cleanup() { |
| 403 | + vcRecaptcha.destroy(scope.widgetId); |
399 | 404 |
|
400 | | - // removes elements reCaptcha added. |
401 | | - ng.element($document[0].querySelectorAll('.pls-container')).parent().remove(); |
| 405 | + // removes elements reCaptcha added. |
| 406 | + ng.element($document[0].querySelectorAll('.pls-container')).parent().remove(); |
402 | 407 | } |
403 | 408 | } |
404 | 409 | }; |
|
0 commit comments