73 行
2.1KB

  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 on 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. return this.each(function () {
  56. $(window).unbind('.yiiCaptcha');
  57. $(this).removeData('yiiCaptcha');
  58. });
  59. },
  60. data: function () {
  61. return this.data('yiiCaptcha');
  62. }
  63. };
  64. })(window.jQuery);