recaptcha.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /* global grecaptcha */
  2. import Ember from 'ember';
  3. import NoCaptcha from 'misago/services/nocaptcha';
  4. export default NoCaptcha.extend({
  5. baseUrl: 'https://www.google.com/recaptcha/api.js?render=explicit',
  6. field: 'recaptcha-field',
  7. includedJs: false,
  8. loadedJs: false,
  9. load: function() {
  10. // Load reCaptcha library from Google
  11. if (!this.get('includedJs')) {
  12. this._includeJs();
  13. }
  14. if (!this.get('loadedJs')) {
  15. return this._loadingPromise();
  16. } else {
  17. return this._loadedPromise();
  18. }
  19. },
  20. _includeJs: function() {
  21. Ember.$.getScript(this.get('url'));
  22. this.set('includedJs', true);
  23. },
  24. _loadingPromise: function() {
  25. var self = this;
  26. return new Ember.RSVP.Promise(function(resolve) {
  27. var wait = function() {
  28. if (typeof grecaptcha === "undefined") {
  29. Ember.run.later(function () {
  30. wait();
  31. }, 200);
  32. } else {
  33. self.set('loadedJs', true);
  34. Ember.run(null, resolve);
  35. }
  36. };
  37. wait();
  38. });
  39. },
  40. _loadedPromise: function() {
  41. // we have already loaded zxcvbn.js, resolve away!
  42. return new Ember.RSVP.Promise(function(resolve) {
  43. Ember.run(null, resolve);
  44. });
  45. },
  46. url: function() {
  47. return this.get('baseUrl') + '&hl=' + Ember.$('html').attr('lang');
  48. }.property('baseUrl'),
  49. refresh: function() {
  50. grecaptcha.reset();
  51. },
  52. value: function() {
  53. return grecaptcha.getResponse();
  54. }.property().volatile()
  55. });