You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
2.2 KiB

  1. /* © 2016 Jairo Llopis <jairo.llopis@tecnativa.com>
  2. * License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). */
  3. "use strict";
  4. (function ($) {
  5. openerp.website.snippet.animationRegistry.subscribe.include({
  6. start: function(editable_mode) {
  7. var self = this;
  8. self.$email = self.$target.find(".js_subscribe_email");
  9. self.$name = self.$target.find(".js_subscribe_name");
  10. // Thanks upstream for your @$&#?!! inheritance-ready code.
  11. // Injecting ajax events to modify behavior of snippet.
  12. if (self.$name) {
  13. $(document).ajaxSend(function(event, jqXHR, ajaxOptions) {
  14. return self.on_ajax_send(event, jqXHR, ajaxOptions);
  15. });
  16. }
  17. return self._super(editable_mode);
  18. },
  19. on_click: function() {
  20. var self = this,
  21. email_error = !self.$email.val().match(/.+@.+/),
  22. name_error = self.$name.length && !self.$name.val(),
  23. values = {
  24. "list_id": self.$target.data('list-id'),
  25. "email": self.$email.val(),
  26. };
  27. // Stop on error
  28. if (email_error || name_error) {
  29. self.$target.addClass("has-error")
  30. return false;
  31. }
  32. return self._super();
  33. },
  34. on_ajax_send: function(event, jqXHR, ajaxOptions) {
  35. var self = this;
  36. // Add handlers on correct requests
  37. if (ajaxOptions.url == "/website_mass_mailing/is_subscriber") {
  38. jqXHR.then(function(data) {
  39. return self.on_start(data);
  40. });
  41. } else if (ajaxOptions.url == "/website_mass_mailing/subscribe") {
  42. var data = JSON.parse(ajaxOptions.data);
  43. data.params.email =
  44. self.$name.val() + " <" + data.params.email + ">";
  45. ajaxOptions.data = JSON.stringify(data);
  46. }
  47. },
  48. on_start: function(data) {
  49. this.$name.val(data.result.name)
  50. .attr(
  51. "disabled",
  52. Boolean(data.result.is_subscriber && data.result.name.length)
  53. );
  54. },
  55. });
  56. })(jQuery);