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.

55 lines
2.1 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("website.content.snippets.animation");
  7. animation.registry.subscribe.include({
  8. start: function(editable_mode) {
  9. this.$email = this.$(".js_subscribe_email");
  10. this.$name = this.$(".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. _onClick: function() {
  19. // Upstream will not tell user what is wrong with the
  20. // email validation so this will report with a helping message
  21. var email_valid = this.$email[0].reportValidity(),
  22. name_valid = this.$name[0].reportValidity();
  23. if (!name_valid || !email_valid) {
  24. return false;
  25. }
  26. return this._super.apply(this, arguments);
  27. },
  28. on_ajax_send: function(event, jqXHR, ajaxOptions) {
  29. // Add handlers on correct requests
  30. if (ajaxOptions.url == "/website_mass_mailing/is_subscriber") {
  31. jqXHR.done($.proxy(this.on_start, this));
  32. } else if (ajaxOptions.url == "/website_mass_mailing/subscribe") {
  33. var data = JSON.parse(ajaxOptions.data);
  34. data.params.email = _.str.sprintf(
  35. "%s <%s>",
  36. this.$name.val(),
  37. data.params.email
  38. );
  39. ajaxOptions.data = JSON.stringify(data);
  40. }
  41. },
  42. on_start: function(data) {
  43. this.$name.val(data.result.name)
  44. .attr(
  45. "disabled",
  46. Boolean(data.result.is_subscriber && data.result.name.length)
  47. );
  48. },
  49. });
  50. });