|
2 | 2 | (function (ng) {
|
3 | 3 | 'use strict';
|
4 | 4 |
|
| 5 | + function throwNoKeyException() { |
| 6 | + throw new Error('You need to set the "key" attribute to your public reCaptcha key. If you don\'t have a key, please get one from https://www.google.com/recaptcha/admin/create'); |
| 7 | + } |
| 8 | + |
5 | 9 | var app = ng.module('vcRecaptcha');
|
6 | 10 |
|
7 | 11 | /**
|
8 | 12 | * An angular service to wrap the reCaptcha API
|
9 | 13 | */
|
10 |
| - app.service('vcRecaptchaService', ['$window', '$q', function ($window, $q) { |
11 |
| - var deferred = $q.defer(), promise = deferred.promise, recaptcha; |
| 14 | + app.provider('vcRecaptchaService', function(){ |
| 15 | + var provider = this; |
| 16 | + var config = {}; |
| 17 | + |
| 18 | + /** |
| 19 | + * Sets the reCaptcha configuration values which will be used by default is not specified in a specific directive instance. |
| 20 | + * |
| 21 | + * @since 2.5.0 |
| 22 | + * @param defaults object which overrides the current defaults object. |
| 23 | + */ |
| 24 | + provider.setDefaults = function(defaults){ |
| 25 | + angular.copy(config, defaults); |
| 26 | + }; |
12 | 27 |
|
13 |
| - $window.vcRecaptchaApiLoadedCallback = $window.vcRecaptchaApiLoadedCallback || []; |
| 28 | + /** |
| 29 | + * Sets the reCaptcha key which will be used by default is not specified in a specific directive instance. |
| 30 | + * |
| 31 | + * @since 2.5.0 |
| 32 | + * @param siteKey the reCaptcha public key (refer to the README file if you don't know what this is). |
| 33 | + */ |
| 34 | + provider.setSiteKey = function(siteKey){ |
| 35 | + config.key = siteKey; |
| 36 | + }; |
14 | 37 |
|
15 |
| - var callback = function () { |
16 |
| - recaptcha = $window.grecaptcha; |
| 38 | + /** |
| 39 | + * Sets the reCaptcha theme which will be used by default is not specified in a specific directive instance. |
| 40 | + * |
| 41 | + * @since 2.5.0 |
| 42 | + * @param theme The reCaptcha theme. |
| 43 | + */ |
| 44 | + provider.setTheme = function(theme){ |
| 45 | + config.theme = theme; |
| 46 | + }; |
17 | 47 |
|
18 |
| - deferred.resolve(recaptcha); |
| 48 | + /** |
| 49 | + * Sets the reCaptcha stoken which will be used by default is not specified in a specific directive instance. |
| 50 | + * |
| 51 | + * @since 2.5.0 |
| 52 | + * @param stoken The reCaptcha stoken. |
| 53 | + */ |
| 54 | + provider.setStoken = function(stoken){ |
| 55 | + config.stoken = stoken; |
19 | 56 | };
|
20 | 57 |
|
21 |
| - $window.vcRecaptchaApiLoadedCallback.push(callback); |
| 58 | + /** |
| 59 | + * Sets the reCaptcha size which will be used by default is not specified in a specific directive instance. |
| 60 | + * |
| 61 | + * @since 2.5.0 |
| 62 | + * @param size The reCaptcha size. |
| 63 | + */ |
| 64 | + provider.setSize = function(size){ |
| 65 | + config.size = size; |
| 66 | + }; |
22 | 67 |
|
23 |
| - $window.vcRecaptchaApiLoaded = function () { |
24 |
| - $window.vcRecaptchaApiLoadedCallback.forEach(function(callback) { |
25 |
| - callback(); |
26 |
| - }); |
| 68 | + /** |
| 69 | + * Sets the reCaptcha type which will be used by default is not specified in a specific directive instance. |
| 70 | + * |
| 71 | + * @since 2.5.0 |
| 72 | + * @param type The reCaptcha type. |
| 73 | + */ |
| 74 | + provider.setType = function(type){ |
| 75 | + config.type = type; |
27 | 76 | };
|
28 | 77 |
|
| 78 | + provider.$get = ['$window', '$q', function ($window, $q) { |
| 79 | + var deferred = $q.defer(), promise = deferred.promise, recaptcha; |
29 | 80 |
|
30 |
| - function getRecaptcha() { |
31 |
| - if (!!recaptcha) { |
32 |
| - return $q.when(recaptcha); |
33 |
| - } |
| 81 | + $window.vcRecaptchaApiLoadedCallback = $window.vcRecaptchaApiLoadedCallback || []; |
34 | 82 |
|
35 |
| - return promise; |
36 |
| - } |
| 83 | + var callback = function () { |
| 84 | + recaptcha = $window.grecaptcha; |
37 | 85 |
|
38 |
| - function validateRecaptchaInstance() { |
39 |
| - if (!recaptcha) { |
40 |
| - throw new Error('reCaptcha has not been loaded yet.'); |
41 |
| - } |
42 |
| - } |
43 |
| - |
44 |
| - |
45 |
| - // Check if grecaptcha is not defined already. |
46 |
| - if (ng.isDefined($window.grecaptcha)) { |
47 |
| - callback(); |
48 |
| - } |
49 |
| - |
50 |
| - return { |
51 |
| - |
52 |
| - /** |
53 |
| - * Creates a new reCaptcha object |
54 |
| - * |
55 |
| - * @param elm the DOM element where to put the captcha |
56 |
| - * @param key the recaptcha public key (refer to the README file if you don't know what this is) |
57 |
| - * @param fn a callback function to call when the captcha is resolved |
58 |
| - * @param conf the captcha object configuration |
59 |
| - */ |
60 |
| - create: function (elm, key, fn, conf) { |
61 |
| - conf.callback = fn; |
62 |
| - conf.sitekey = key; |
63 |
| - |
64 |
| - return getRecaptcha().then(function (recaptcha) { |
65 |
| - return recaptcha.render(elm, conf); |
| 86 | + deferred.resolve(recaptcha); |
| 87 | + }; |
| 88 | + |
| 89 | + $window.vcRecaptchaApiLoadedCallback.push(callback); |
| 90 | + |
| 91 | + $window.vcRecaptchaApiLoaded = function () { |
| 92 | + $window.vcRecaptchaApiLoadedCallback.forEach(function(callback) { |
| 93 | + callback(); |
66 | 94 | });
|
67 |
| - }, |
68 |
| - |
69 |
| - /** |
70 |
| - * Reloads the reCaptcha |
71 |
| - */ |
72 |
| - reload: function (widgetId) { |
73 |
| - validateRecaptchaInstance(); |
74 |
| - |
75 |
| - // $log.info('Reloading captcha'); |
76 |
| - recaptcha.reset(widgetId); |
77 |
| - |
78 |
| - // reCaptcha will call the same callback provided to the |
79 |
| - // create function once this new captcha is resolved. |
80 |
| - }, |
81 |
| - |
82 |
| - /** |
83 |
| - * Gets the response from the reCaptcha widget. |
84 |
| - * |
85 |
| - * @see https://developers.google.com/recaptcha/docs/display#js_api |
86 |
| - * |
87 |
| - * @returns {String} |
88 |
| - */ |
89 |
| - getResponse: function (widgetId) { |
90 |
| - validateRecaptchaInstance(); |
91 |
| - |
92 |
| - return recaptcha.getResponse(widgetId); |
| 95 | + }; |
| 96 | + |
| 97 | + |
| 98 | + function getRecaptcha() { |
| 99 | + if (!!recaptcha) { |
| 100 | + return $q.when(recaptcha); |
| 101 | + } |
| 102 | + |
| 103 | + return promise; |
| 104 | + } |
| 105 | + |
| 106 | + function validateRecaptchaInstance() { |
| 107 | + if (!recaptcha) { |
| 108 | + throw new Error('reCaptcha has not been loaded yet.'); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + |
| 113 | + // Check if grecaptcha is not defined already. |
| 114 | + if (ng.isDefined($window.grecaptcha)) { |
| 115 | + callback(); |
93 | 116 | }
|
94 |
| - }; |
95 | 117 |
|
96 |
| - }]); |
| 118 | + return { |
| 119 | + |
| 120 | + /** |
| 121 | + * Creates a new reCaptcha object |
| 122 | + * |
| 123 | + * @param elm the DOM element where to put the captcha |
| 124 | + * @param conf the captcha object configuration |
| 125 | + * @throws NoKeyException if no key is provided in the provider config or the directive instance (via attribute) |
| 126 | + */ |
| 127 | + create: function (elm, conf) { |
| 128 | + |
| 129 | + conf.key = conf.key || config.key; |
| 130 | + conf.theme = conf.theme || config.theme; |
| 131 | + conf.stoken = conf.stoken || config.stoken; |
| 132 | + conf.size = conf.size || config.size; |
| 133 | + conf.type = conf.type || config.type; |
| 134 | + |
| 135 | + if (!conf.key || conf.key.length !== 40) { |
| 136 | + throwNoKeyException(); |
| 137 | + } |
| 138 | + return getRecaptcha().then(function (recaptcha) { |
| 139 | + return recaptcha.render(elm, conf); |
| 140 | + }); |
| 141 | + }, |
| 142 | + |
| 143 | + /** |
| 144 | + * Reloads the reCaptcha |
| 145 | + */ |
| 146 | + reload: function (widgetId) { |
| 147 | + validateRecaptchaInstance(); |
| 148 | + |
| 149 | + // $log.info('Reloading captcha'); |
| 150 | + recaptcha.reset(widgetId); |
| 151 | + |
| 152 | + // reCaptcha will call the same callback provided to the |
| 153 | + // create function once this new captcha is resolved. |
| 154 | + }, |
| 155 | + |
| 156 | + /** |
| 157 | + * Gets the response from the reCaptcha widget. |
| 158 | + * |
| 159 | + * @see https://developers.google.com/recaptcha/docs/display#js_api |
| 160 | + * |
| 161 | + * @returns {String} |
| 162 | + */ |
| 163 | + getResponse: function (widgetId) { |
| 164 | + validateRecaptchaInstance(); |
| 165 | + |
| 166 | + return recaptcha.getResponse(widgetId); |
| 167 | + } |
| 168 | + }; |
| 169 | + |
| 170 | + }]; |
| 171 | + }); |
97 | 172 |
|
98 | 173 | }(angular));
|
0 commit comments