captcha.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /**
  2. * Copyright © Magento, Inc. All rights reserved.
  3. * See COPYING.txt for license details.
  4. */
  5. define([
  6. 'jquery',
  7. 'jquery/ui'
  8. ], function ($) {
  9. 'use strict';
  10. /**
  11. * @api
  12. */
  13. $.widget('mage.captcha', {
  14. options: {
  15. refreshClass: 'refreshing',
  16. reloadSelector: '.captcha-reload',
  17. imageSelector: '.captcha-img',
  18. imageLoader: ''
  19. },
  20. /**
  21. * Method binds click event to reload image
  22. * @private
  23. */
  24. _create: function () {
  25. this.element.on('click', this.options.reloadSelector, $.proxy(this.refresh, this));
  26. },
  27. /**
  28. * Method triggers an AJAX request to refresh the CAPTCHA image
  29. */
  30. refresh: function () {
  31. var imageLoader = this.options.imageLoader;
  32. if (imageLoader) {
  33. this.element.find(this.options.imageSelector).attr('src', imageLoader);
  34. }
  35. this.element.addClass(this.options.refreshClass);
  36. $.ajax({
  37. url: this.options.url,
  38. type: 'post',
  39. async: false,
  40. dataType: 'json',
  41. context: this,
  42. data: {
  43. 'formId': this.options.type
  44. },
  45. /**
  46. * @param {Object} response
  47. */
  48. success: function (response) {
  49. if (response.imgSrc) {
  50. this.element.find(this.options.imageSelector).attr('src', response.imgSrc);
  51. }
  52. },
  53. /** Complete callback. */
  54. complete: function () {
  55. this.element.removeClass(this.options.refreshClass);
  56. }
  57. });
  58. }
  59. });
  60. return $.mage.captcha;
  61. });