jquery.ajaxchimp.min.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. (function ($) {
  2. 'use strict';
  3. $.ajaxChimp = {
  4. responses: {
  5. 'We have sent you a confirmation email' : 0,
  6. 'Please enter a valid email' : 1,
  7. 'An email address must contain a single @' : 2,
  8. 'The domain portion of the email address is invalid (the portion after the @: )' : 3,
  9. 'The username portion of the email address is invalid (the portion before the @: )' : 4,
  10. 'This email address looks fake or invalid. Please enter a real email address' : 5
  11. },
  12. translations: {
  13. 'en': null
  14. },
  15. init: function (selector, options) {
  16. $(selector).ajaxChimp(options);
  17. }
  18. };
  19. $.fn.ajaxChimp = function (options) {
  20. $(this).each(function(i, elem) {
  21. var form = $(elem);
  22. var email = form.find('input[type=email]');
  23. var label = form.find('.info');
  24. var settings = $.extend({
  25. 'url': form.attr('action'),
  26. 'language': 'en'
  27. }, options);
  28. var url = settings.url.replace('/post?', '/post-json?').concat('&c=?');
  29. form.attr('novalidate', 'true');
  30. email.attr('name', 'EMAIL');
  31. form.submit(function () {
  32. var msg;
  33. function successCallback(resp) {
  34. if (resp.result === 'success') {
  35. msg = 'We have sent you a confirmation email';
  36. label.removeClass('error').addClass('valid');
  37. email.removeClass('error').addClass('valid');
  38. } else {
  39. email.removeClass('valid').addClass('error');
  40. label.removeClass('valid').addClass('error');
  41. var index = -1;
  42. try {
  43. var parts = resp.msg.split(' - ', 2);
  44. if (parts[1] === undefined) {
  45. msg = resp.msg;
  46. } else {
  47. var i = parseInt(parts[0], 10);
  48. if (i.toString() === parts[0]) {
  49. index = parts[0];
  50. msg = parts[1];
  51. } else {
  52. index = -1;
  53. msg = resp.msg;
  54. }
  55. }
  56. }
  57. catch (e) {
  58. index = -1;
  59. msg = resp.msg;
  60. }
  61. }
  62. // Translate and display message
  63. if (
  64. settings.language !== 'en'
  65. && $.ajaxChimp.responses[msg] !== undefined
  66. && $.ajaxChimp.translations
  67. && $.ajaxChimp.translations[settings.language]
  68. && $.ajaxChimp.translations[settings.language][$.ajaxChimp.responses[msg]]
  69. ) {
  70. msg = $.ajaxChimp.translations[settings.language][$.ajaxChimp.responses[msg]];
  71. }
  72. label.html(msg);
  73. label.show(2000);
  74. if (settings.callback) {
  75. settings.callback(resp);
  76. }
  77. }
  78. var data = {};
  79. var dataArray = form.serializeArray();
  80. $.each(dataArray, function (index, item) {
  81. data[item.name] = item.value;
  82. });
  83. $.ajax({
  84. url: url,
  85. data: data,
  86. success: successCallback,
  87. dataType: 'jsonp',
  88. error: function (resp, text) {
  89. console.log('mailchimp ajax submit error: ' + text);
  90. }
  91. });
  92. // Translate and display submit message
  93. var submitMsg = 'Submitting...';
  94. if(
  95. settings.language !== 'en'
  96. && $.ajaxChimp.translations
  97. && $.ajaxChimp.translations[settings.language]
  98. && $.ajaxChimp.translations[settings.language]['submit']
  99. ) {
  100. submitMsg = $.ajaxChimp.translations[settings.language]['submit'];
  101. }
  102. label.html(submitMsg).show(2000);
  103. return false;
  104. });
  105. });
  106. return this;
  107. };
  108. })(jQuery);