yii.captcha.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /**
  2. * Yii Captcha widget.
  3. *
  4. * This is the JavaScript widget used by the yii\captcha\Captcha widget.
  5. *
  6. * @link http://www.yiiframework.com/
  7. * @copyright Copyright (c) 2008 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. * @author Qiang Xue <qiang.xue@gmail.com>
  10. * @since 2.0
  11. */
  12. (function ($) {
  13. $.fn.yiiCaptcha = function (method) {
  14. if (methods[method]) {
  15. return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
  16. } else if (typeof method === 'object' || !method) {
  17. return methods.init.apply(this, arguments);
  18. } else {
  19. $.error('Method ' + method + ' does not exist in jQuery.yiiCaptcha');
  20. return false;
  21. }
  22. };
  23. var defaults = {
  24. refreshUrl: undefined,
  25. hashKey: undefined
  26. };
  27. var methods = {
  28. init: function (options) {
  29. return this.each(function () {
  30. var $e = $(this);
  31. var settings = $.extend({}, defaults, options || {});
  32. $e.data('yiiCaptcha', {
  33. settings: settings
  34. });
  35. $e.on('click.yiiCaptcha', function () {
  36. methods.refresh.apply($e);
  37. return false;
  38. });
  39. });
  40. },
  41. refresh: function () {
  42. var $e = this,
  43. settings = this.data('yiiCaptcha').settings;
  44. $.ajax({
  45. url: $e.data('yiiCaptcha').settings.refreshUrl,
  46. dataType: 'json',
  47. cache: false,
  48. success: function (data) {
  49. $e.attr('src', data.url);
  50. $('body').data(settings.hashKey, [data.hash1, data.hash2]);
  51. }
  52. });
  53. },
  54. destroy: function () {
  55. this.off('.yiiCaptcha');
  56. this.removeData('yiiCaptcha');
  57. return this;
  58. },
  59. data: function () {
  60. return this.data('yiiCaptcha');
  61. }
  62. };
  63. })(window.jQuery);