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.

59 lines
2.3 KiB

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