"use strict"; var IncitationNewslettersPopupController = function ($el) { this.$el = $el; this.listen(); }; IncitationNewslettersPopupController.prototype = { listen: function () { this.$el.on('click', '.close, .decline', $.proxy(this.declineClickEvent, this)); this.$el.on('click', '.optin', $.proxy(this.optinClickEvent, this)); $(document).on('mouseout', $.proxy(this.windowsOutEvent, this)); }, windowsOutEvent: function (e) { if (e.clientY < 0) { this.getIncitation(); } }, hasCookie: function () { //check cookie incitation var oRegex = new RegExp("(?:; )?" + "incitationNlPopup" + "=([^;]*);?"); var incitation; if (oRegex.test(document.cookie)) { incitation = decodeURIComponent(RegExp["$1"]); } if (incitation && incitation === 'true') { return true; } return false; }, createCookie: function () { var dateCookies = new Date(); //date du cookie sur 1 semaine. dateCookies.setTime(dateCookies.getTime() + (7 * 24 * 3600 * 1000)); document.cookie = 'incitationNlPopup=true;expires=' + dateCookies + '; path=/'; }, getIncitation: function () { var self = this; if (!this.hasCookie()) { $.ajax({ type: "GET", url: "/moncompte/Newsletter/GetIncitation", dataType: "json", success: function (data) { if (data === false) { self.$el.modal(); }; } }); } }, optinClickEvent: function (e) { var self = this; $.post('/moncompte/MyAccount/Newsletters', { Newsletter: true, NewsletterPromotions: true, NewsletterPartners: false }) .done(function () { self.displayConfirm(); }) .fail(function () { self.declineClickEvent(); }); }, declineClickEvent: function (evt) { evt.preventDefault(); this.closeModal(); }, displayConfirm: function() { this.$el.find('.intro, .actions, .legend').hide(); this.$el.find('.confirm-message').show(); }, closeModal: function () { this.createCookie(); this.$el.modal('hide'); } };