Skip to content
Permalink
Browse files

IMP newsletter snippet

[TO-DO] adapt changes for toast
  • Loading branch information...
kig-odoo committed Apr 17, 2019
1 parent bb67152 commit 137fd7722d8ab7bb5b210cc898262ba78c017d9b
@@ -8,7 +8,7 @@ class MassMailingList(models.Model):
_inherit = 'mail.mass_mailing.list'

def _default_toast_content(self):
return self.env['ir.ui.view'].render_template('website_mass_mailing.s_newsletter_toast_content')
return '<p>Thanks for subscribing!</p>'

website_popup_ids = fields.One2many('mail.mass_mailing.popup', 'mailing_list_id')
toast_content = fields.Html(string="Toast content", default=_default_toast_content, translate=True)
@@ -10,62 +10,58 @@ var qweb = core.qweb;
publicWidget.registry.subscribe = publicWidget.Widget.extend({
selector: ".js_subscribe",
disabledInEditableMode: false,
read_events: {
'click .js_subscribe_btn': '_onClickSubscribe',
},
xmlDependencies: ['/website_mass_mailing/static/src/xml/website_mass_mailing.xml'],

start: function () {
var self = this;
var defs = [this._super.apply(this, arguments)];

var always = function (data) {
self.$target.find('input.js_subscribe_email')
.val(data.email ? data.email : "")
.attr("disabled", data.is_subscriber && data.email.length ? "disabled" : false);
self.$target.attr("data-subscribe", data.is_subscriber ? 'on' : 'off');
self.$target.find('a.js_subscribe_btn')
.attr("disabled", data.is_subscriber && data.email.length ? "disabled" : false);
var isSubscriber = data.is_subscriber;
self.$target.find('.js_subscribe_btn')
.attr("disabled", isSubscriber ? "disabled" : false);
self.$('input.js_subscribe_email')
.val(data.email || "")
.attr("disabled", isSubscriber ? "disabled" : false);
self.$target.removeClass('d-none');
self.$target.find('.js_subscribe_btn').toggleClass('d-none', !!data.is_subscriber);
self.$target.find('.js_subscribed_btn').toggleClass('d-none', !data.is_subscriber);
self.$('.js_subscribe_btn').toggleClass('d-none', !!isSubscriber);
self.$('.js_subscribed_btn').toggleClass('d-none', !isSubscriber);
};

// set value and display button
self.$target.find("input").removeClass('d-none');
this._rpc({
defs.push(this._rpc({
route: '/website_mass_mailing/is_subscriber',
params: {
list_id: this.$target.data('list-id'),
},
}).then(always).guardedCatch(always);

// not if editable mode to allow designer to edit alert field
if (!this.editableMode) {
$('.js_subscribe > .alert').addClass('d-none');
$('.js_subscribe > .input-group-append.d-none').removeClass('d-none');
this.$target.find('.js_subscribe_btn').on('click', function (event) {
event.preventDefault();
self._onClick();
});
}
}).then(always).guardedCatch(always));
return Promise.all(defs);
},
_onClick: function () {
_onClickSubscribe: function () {
var self = this;
var $email = this.$target.find(".js_subscribe_email:visible");

if ($email.length && !$email.val().match(/.+@.+/)) {
this.$target.addClass('o_has_error').find('.form-control, .custom-select').addClass('is-invalid');
this.$target.addClass('o_has_error').find('.form-control').addClass('is-invalid');
return false;
}
this.$target.removeClass('o_has_error').find('.form-control, .custom-select').removeClass('is-invalid');
this.$target.removeClass('o_has_error').find('.form-control').removeClass('is-invalid');

this._rpc({
route: '/website_mass_mailing/subscribe',
params: {
'list_id': this.$target.data('list-id'),
'email': $email.length ? $email.val() : false,
},
}).then(function (subscribe) {
self.$target.find(".js_subscribe_email, .input-group-append").addClass('d-none');
self.$target.find(".alert").removeClass('d-none');
self.$target.find('input.js_subscribe_email').attr("disabled", subscribe ? "disabled" : false);
self.$target.attr("data-subscribe", subscribe ? 'on' : 'off');
}).then(function (result) {
self.$target.find(".js_subscribe_btn").addClass('d-none');
var $ele = $(qweb.render('website_mass_mailing.subscribe_success', {content: result.toast_content}));
$(document.body).append($ele);
$ele.find('.toast').toast('show');
self.$target.find(".js_subscribed_btn").removeClass('d-none');
self.$target.find('input.js_subscribe_email').attr("disabled", result ? "disabled" : false);
});
},
});
@@ -27,7 +27,6 @@
<a role="button" href="#" class="btn btn-primary js_subscribe_btn">Subscribe</a>
<a role="button" href="#" class="btn btn-success js_subscribed_btn d-none" disabled="disabled">Thanks</a>
</span>
<div class="alert alert-success d-none" role="status">Thanks for your subscription!</div>
</div>
</template>

@@ -154,8 +153,4 @@
</section>
</template>

<template id="s_newsletter_toast_content" name="Newsletter">
<p>Thanks for subscribing!</p>
</template>

</odoo>

0 comments on commit 137fd77

Please sign in to comment.
You can’t perform that action at this time.