/** * Created by magenest on 12/01/2019. */ define([ 'jquery', 'Magento_Customer/js/customer-data', 'Magento_Ui/js/modal/modal', 'mage/url', 'Magento_Checkout/js/model/full-screen-loader', 'mage/storage', 'jquery/ui', 'bioEp', 'newCarousel' ], function ($, customerData, modal, urlBuilder, fullScreenLoader, storage) { 'use strict'; $.widget('magenest.popup', { options: { dataPopup: {} }, carousel: null, _create: function () { this._showPopup(); }, _showPopup: function () { var self = this, popupElem = $('#magenest-popup').length, html_content = this.options.dataPopup.html_content, popup_id = this.options.dataPopup.popup_id, cookie_lifetime = this.options.dataPopup.cookie_lifetime, enable_cookie = this.options.dataPopup.enable_cookie_lifetime, cookies = self._getCookie('magenest_cookie_popup'), values = {}, view_page = 1, css_style = this.options.dataPopup.css_style; if (cookies != null && cookies != "") { var cookieArr = JSON.parse(cookies); values = cookieArr; if (typeof cookieArr.view_page !== 'undefined' || cookieArr.view_page != null) { view_page += cookieArr.view_page; cookieArr.view_page = view_page; values = cookieArr; } else { values['view_page'] = view_page; } } else { values['view_page'] = view_page; } self._eraseCookie('magenest_cookie_popup'); self._createCookie('magenest_cookie_popup', JSON.stringify(values), cookie_lifetime); if (popupElem <= 1) { var popup_trigger = this.options.dataPopup.popup_trigger, number_x = this.options.dataPopup.number_x, display_popup = this.options.dataPopup.floating_button_display_popup; if (this.options.dataPopup.preview == 1 && this.options.dataPopup.preview) { bioEp.init({ html: html_content, css: this.options.dataPopup.css_style, popup_id: popup_id, showOnDelay: true, delay: 0, onPopup: function () { self._createCarousel().slick("setPosition") } }); } else if (popup_trigger === 1) { var id = 'magenest-popup popup-bio_ep'; $('#magenest-popup').attr('id', id); bioEp.init({ html: html_content, showOnDelay: true, delay: number_x, css: css_style, popup_id: popup_id, cookie_lifetime: cookie_lifetime, display_popup: display_popup, onPopup: function () { self._createCarousel().slick("setPosition") } }); } else if (popup_trigger === 2) { var id = 'magenest-popup popup-bio_ep'; $('#magenest-popup').attr('id', id); self._scrollToShow(); } else if (popup_trigger === 3 && enable_cookie === 1) { var id = 'magenest-popup popup-bio_ep'; $('#magenest-popup').attr('id', id); if (view_page === number_x) { bioEp.init({ html: html_content, css: this.options.dataPopup.css_style, popup_id: popup_id, cookie_lifetime: cookie_lifetime, showOnDelay: true, delay: 0, onPopup: function () { self._createCarousel().slick("setPosition") } }); } } else if (popup_trigger === 4) { var id = 'magenest-popup popup-bio_ep'; $('#magenest-popup').attr('id', id); bioEp.init({ html: html_content, css: this.options.dataPopup.css_style, popup_id: popup_id, cookie_lifetime: cookie_lifetime, onPopup: function () { self._createCarousel().slick("setPosition") } }); $(document).off('mouseleave'); } } self._clickSuccess(); self._addClassDefault(); self._addPositionInPage(); self._addAnimation(); self._closeVideo(); }, // Create a cookie _createCookie: function (name, value, days, sessionOnly) { var expires = ""; if (sessionOnly) { expires = "; expires=0" } else if (days) { var date = new Date(); date.setTime(date.getTime() + (days * 1000)); expires = "; expires=" + date.toGMTString(); } document.cookie = name + "=" + value + expires + "; path=/"; }, // Get the value of a cookie _getCookie: function (name) { var nameEQ = name + "="; var ca = document.cookie.split(";"); for (var i = 0; i < ca.length; i++) { var c = ca[i]; while (c.charAt(0) == " ") c = c.substring(1, c.length); if (c.indexOf(nameEQ) === 0) return c.substring(nameEQ.length, c.length); } return null; }, // Delete a cookie _eraseCookie: function (name) { this._createCookie(name, "", -1); }, /** * Scroll to show popup * @private */ _scrollToShow: function () { var self = this; $(window).scroll(function () { var scrollTop = $(window).scrollTop(), docHeight = $(document).height(), winHeight = $(window).height(), scrollPercent = (scrollTop) / (docHeight - winHeight), optionScroll = (self.options.dataPopup.number_x * 1) / 100, popup_id = self.options.dataPopup.popup_id, html_content = self.options.dataPopup.html_content, cookie_lifetime = self.options.dataPopup.cookie_lifetime; if (scrollPercent >= optionScroll) { bioEp.init({ html: html_content, showOnDelay: true, delay: 0, css: self.options.dataPopup.css_style, popup_id: popup_id, cookie_lifetime: cookie_lifetime, onPopup: function () { self._createCarousel().slick("setPosition") } }); $(window).off('scroll'); self._addClassDefault(); self._addPositionInPage(); self._addAnimation(); self._clickSuccess(); self._closeVideo(); } }); }, /** * Event click submit button * @private */ _clickSuccess: function () { var self = this, coupon = self.options.dataPopup.coupon_code, thankyou = self.options.dataPopup.thankyou_message, form = $('#form-newsletter'), popup_type = self.options.dataPopup.popup_type, popup_id = self.options.dataPopup.popup_id; $('#popup-submit-button').click(function () { var newsletter = $('#popup-newsletter').val(); if (newsletter == '') { } else { $('#popup-submit-button strong').hide(); $('#popup-submit-button i').hide(); $('#popup-submit-button').append(''); var url = urlBuilder.build('magenest_popup/popup/subscriber'); var data = new Array(), payload, popupObj = new Object(); popupObj.name = popup_id; popupObj.value = popup_type; data.push(popupObj); $('.popup-newsletter').each(function (i) { var obj = new Object(); obj.name = $(this).attr('name'); obj.value = $(this).val(); data.push(obj); }); $('.bld-table-cont input').each(function (i) { var obj = new Object(); obj.name = $(this).attr('name'); obj.value = $(this).val(); data.push(obj); }); payload = { dataPopup: data }; storage.post( url, JSON.stringify(payload) ).done( function (response) { $('.popup-step-1').hide(); $('.popup-step-1').after(''); $('.popup-step-2').prepend(''); var obj_response = JSON.parse(response); if (obj_response.status == 0) { $('.popup-step-2 .popup-content').prepend(function () { return ''; }); } else { if (coupon) { $('.popup-step-2 .popup-content').prepend(function () { return ''; }); } if (thankyou) { $('.popup-step-2 .popup-content').append(function () { return ''; }); } else { $('.popup-step-2 .popup-content').append(function () { return ''; }); } } } ).fail( function (response) { console.log(response); } ); } }) }, /** * Event add class template default * @private */ _addClassDefault: function () { var self = this, popup_template_id = self.options.dataPopup.popup_template_id, template_id = self.options.dataPopup.template_id, class_default = self.options.dataPopup.class; $('.cursor-pointer').click(function () { $('.popup-box-1').hide(); $('.popup-box-2').show(); }); if (popup_template_id || typeof template_id != 'undefined') { $('#bio_ep').addClass(class_default); if (class_default == 'popup-default-3') { $('#bio_ep_bg').addClass('popup-bg-3'); } else { } } else { $('#bio_ep').addClass('popup-default-1'); } }, /** * Event add postion in page for popup * @private */ _addPositionInPage: function () { var self = this, positioninpage = self.options.dataPopup.popup_positioninpage; //== [ general add class ] $('#bio_ep_bg').addClass('popup-bg-none'); if (positioninpage == 1) { $('#bio_ep').addClass('popup-top-left'); } else if (positioninpage == 2) { $('#bio_ep').addClass('popup-top-right'); } else if (positioninpage == 3) { $('#bio_ep').addClass('popup-bottom-left'); } else if (positioninpage == 4) { $('#bio_ep').addClass('popup-bottom-right'); } else if (positioninpage == 5) { $('#bio_ep').addClass('popup-middle-left'); } else if (positioninpage == 6) { $('#bio_ep').addClass('popup-middle-right'); } else if (positioninpage == 7) { $('#bio_ep').addClass('popup-top-center'); } else if (positioninpage == 8) { $('#bio_ep').addClass('popup-bottom-center'); } else if (positioninpage == 0) { $('#bio_ep').addClass('popup-center-center'); } }, /** * Event add animation for popup * @private */ _addAnimation: function () { var self = this, animation = self.options.dataPopup.popup_animation, positioninpage = self.options.dataPopup.popup_positioninpage; if (animation == 1) { $('#bio_ep .magenest-popup-inner').addClass('animate__zoomIn'); } else if (animation == 2) { //== [ Fix animation transform ] $('#bio_ep .magenest-popup-inner').addClass('animate__zoomOut'); } else if (animation == 3) { $('#bio_ep .magenest-popup-inner').addClass('animate__bounceInLeft'); } else if (animation == 4) { $('#bio_ep .magenest-popup-inner').addClass('animate__bounceInRight'); } else if (animation == 5) { $('#bio_ep .magenest-popup-inner').addClass('animate__bounceInDown'); } else if (animation == 6) { $('#bio_ep .magenest-popup-inner').addClass('animate__bounceInUp'); } }, /** * Turn off the video if the popup's has it * @private */ _closeVideo: function () { $('#bio_ep_close').click(function (e) { e.preventDefault(); $('.popup-video').children('iframe').attr('src', ''); }); }, _createCarousel: function () { return $('.popup-content-wrapper .popup-product-hotdeals .widget-product-grid').slick({ slidesToShow: 4, slidesToScroll: 1, arrows: true, dots: false, responsive: [ { breakpoint: 575, settings: { arrows: false, slidesToShow: 1 } }, { breakpoint: 767, settings: { arrows: false, slidesToShow: 3 } }, { breakpoint: 1199, settings: { slidesToShow: 4 } } ] }); } }); return $.magenest.popup; });