PageSourceSearch

https://sadekmusicshop.com/extension/popup_sales/catalog/view/java…/popupsales.js?version=1748302698

js sadekmusicshop.com collected 2026-09-24 13:04:17 UTC 17,036 bytes, 542 lines download raw bytes

1var show_popup = false;
2var visitor_click = 0;
3var is_showing = 0;
4
5$(document).ready(function () {
6    init_popupsales();
7
8    var midnight = "0:00:00";
9    var now = null;
10    now = moment().format("H:mm:ss");
11    if (now === midnight) {
12        eraseCookie('popup_showed');
13        eraseCookie('popup_id');
14        eraseCookie('popup_after_action');
15        eraseCookie('popup_visitor_click');
16        eraseCookie('popup_visited_time');
17    }
18
19    $('.wrapper-popup .btn-subscribe').on('click', (e) => {
20        e.preventDefault();
21        
22        var is_add_to_cart = false;
23        var product_id = 0;
24        var error = [];
25        var formData = new FormData();
26
27        if (!$('.wrapper-popup #popup_product').val()) {
28            $('.wrapper-popup').find('input').each((i, elem) => {
29                var required = $(elem).attr('required');
30        
31                if (required && !elem.value) {
32                    error.push({
33                        name: elem.id,
34                        message: elem.id + ' is required',
35                    });
36                }
37            });
38        }
39
40        if ($('.wrapper-popup #name') != 'undefined') {
41            formData.append('name', $('.wrapper-popup #name').val());
42        } else {
43            formData.append('name', null);
44        }
45        if ($('.wrapper-popup #phone') != 'undefined') {
46            formData.append('phone', $('.wrapper-popup #phone').val());
47        } else {
48            formData.append('phone', null);
49        }
50        if ($('.wrapper-popup #text') != 'undefined') {
51            formData.append('text', $('.wrapper-popup #text').val());
52        } else {
53            formData.append('text', null);
54        }
55
56        if ($('.wrapper-popup #popup_product') != 'undefined') {
57            formData.append('product_id', $('.wrapper-popup #popup_product').val());
58            is_add_to_cart = true;
59            product_id = $('.wrapper-popup #popup_product').val();
60            
61            $.ajax({
62                type: 'POST',
63                url: 'index.php?route=checkout/cart.add',
64                data: { product_id: product_id, quantity: 1 },
65                dataType: 'json',
66                success: function(response) {
67                    window.location.href = 'index.php?route=checkout/cart';
68                },
69                error: function(xhr, status, error) {
70                    console.error('Error adding product to cart:', error);
71                }
72            });
73        } else {
74            formData.append('product_id', null);
75        }
76
77        if ($('.wrapper-popup #email') != 'undefined' && !is_add_to_cart) {
78            formData.append('email', $('.wrapper-popup #email').val());
79
80            const validate = validateEmail($('.wrapper-popup #email').val());
81
82            if (!validate) {
83                error.push({
84                    name: 'email',
85                    message: 'Please insert a valid email address',
86                });
87            }
88        } else {
89            formData.append('email', null);
90        }
91
92        if (error.length) {
93            for (let a = 0; a < error.length; a++) {
94                var id = error[a].name;
95                $(`.wrapper-popup div[data-error="popup-${id}"]`).html(error[a].message);
96            }
97
98            console.log('error', error);
99            error = [];
100            return false;
101        }
102
103        var pathinfo = window.location;
104        var pathname = pathinfo.pathname;
105        var pathsearch = pathinfo.search;
106        if (pathsearch != "") {
107            pathname += pathsearch;
108        }
109
110        formData.append('popup_id', getCookie('popup_id'));
111        formData.append('pathname', pathname);
112        formData.append('is_mobile', isMobile());
113        
114        $.ajax({
115            type: "POST",
116            url: 'index.php?route=extension/popup_sales/module/popup_sales.subscribe',
117            data: formData,
118            cache: false,
119            contentType: false,
120            processData: false,
121            beforeSend: function () {
122                $('.wrapper-popup .btn-subscribe').prop('disabled', true);
123            },
124            success: function (res) {
125                $('.wrapper-popup .btn-subscribe').prop('disabled', false);
126                var after_action = JSON.parse(getCookie('popup_after_action'));
127
128                if (is_add_to_cart && product_id) {
129                    cart.add(product_id, 1);
130                }
131
132                closePopupsales();
133                if (after_action.selected == 2) {
134                    if (after_action.detail.open_in_new_tab) {
135                        window.open(
136                            after_action.detail.link,
137                            '_blank',
138                        );
139                    } else {
140                        window.location.href = after_action.detail.link;
141                    }
142                }
143            },
144            error: function (err) {
145                $('.wrapper-popup .btn-subscribe').prop('disabled', false);
146            }
147        })
148    });
149})
150
151function validateEmail(email) {
152    return String(email)
153      .toLowerCase()
154      .match(
155        /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|.(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
156      );
157}
158
159function init_visited_time() {
160    if (!getCookie('popup_visited_time')) {
161        var now = moment().format('YYYY-MM-DD HH:mm');
162    
163        setCookie('popup_visited_time', now, 1);
164    } else {
165        if (get_diff_visited_time()) {
166            eraseCookie('popup_visited_time');
167            init_visited_time();
168        }
169    }
170}
171
172function get_diff_visited_time() {
173    var current = moment(getCookie('popup_visited_time'));
174    var next = moment().format('YYYY-MM-DD HH:mm');
175    next = moment(next);
176
177    var diff = next.diff(current);
178
179    var diff_in_minutes = moment.duration(diff).hours();
180
181    if (diff_in_minutes > 2) {
182        return true;
183    } else {
184        return false;
185    }
186}
187
188function init_visitor_click() {
189    visitor_click = getCookie('popup_visitor_click');
190}
191
192function isMobile() {
193    if(/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)){
194        return true;
195    }else{
196        return false;
197    }
198}
199
200function getCookie(name) {
201    var nameEQ = name + "=";
202    var ca = document.cookie.split(';');
203    for(var i=0;i < ca.length;i++) {
204        var c = ca[i];
205        while (c.charAt(0)==' ') c = c.substring(1,c.length);
206        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
207    }
208    return null;
209}
210
211function setCookie(name,value,days) {
212    var expires = "";
213    if (days) {
214        var date = new Date();
215        date.setTime(date.getTime() + (days*24*60*60*1000));
216        expires = "; expires=" + date.toUTCString();
217    }
218    document.cookie = name + "=" + (value || "")  + expires + "; path=/";
219}
220
221function eraseCookie(name) {   
222    document.cookie = name +'=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';
223}
224
225
226function init_popupsales() {
227    $.ajax({
228        type: "POST",
229        url: "index.php?route=extension/popup_sales/module/popup_sales.init_popupsales",
230        dataType: 'json',
231        data: {
232            is_mobile: isMobile() ? 1 : 0,
233            pathname: window.location.href,
234        },
235        success: function (res) {
236            // console.log(res);
237            if (res.reset_popup) {
238                eraseCookie('popup_showed');
239                eraseCookie('popup_id');
240                eraseCookie('popup_after_action');
241                eraseCookie('popup_visited_time');
242            }
243            // resetVisitorClick();
244
245            if (res.popup) {
246                buildPopup(res.popup);
247                initRules(res.popup.rules);
248                setCookie('popup_sequence', res.popup.rules.display_frequency.sequence, 2);
249                setCookie('popup_id', res.popup.id);
250                setCookie('popup_after_action', JSON.stringify(res.popup.subscribe_button_action));
251            }
252        },
253        error: function (err) {
254            console.log('err', err);
255        }
256    })
257}
258
259function resetVisitorClick() {
260    eraseCookie('popup_visitor_click');
261    init_visitor_click();
262}
263
264function initRules(rules) {
265    initPageTargeting(rules.page_targeting);
266
267    initSequence();
268
269    // console.log('show_popup', show_popup);
270
271    if (!show_popup) {
272        return false;
273    }
274
275    if (rules.display_trigger.time_delay.selected) {
276        initTimeDelay(rules.display_trigger.time_delay.value);
277    }
278
279    if (rules.display_trigger.click_count.selected) {
280        initClickCount(rules.display_trigger.click_count.value);
281    }
282
283    if (rules.display_trigger.exit_intent == 1) {
284        initExitIntent();
285    }
286}
287
288function validateVisitedTime() {
289    if (getCookie('popup_visited_time')) {
290        var time = moment(getCookie('popup_visited_time'));
291        var now = moment().format('YYYY-MM-DD HH:mm');
292        now = moment(now);
293
294        var diff = now.diff(time);
295        var diff_in_hours = moment.duration(diff).hours();
296
297        if (diff_in_hours > 2) { // clear cookie
298            eraseCookie('popup_visited_time');
299        }
300    }
301}
302
303function initSequence() {
304    var popup_showed = getCookie('popup_showed');
305    var sequence = getCookie('popup_sequence');
306
307    // console.log('sequence', sequence);
308    // console.log('get_diff_visited_time', get_diff_visited_time());
309
310    // validate time visited cookie
311    validateVisitedTime();
312
313    if (sequence == 1) { // once a day
314        if ((popup_showed) && (popup_showed == 1)) {
315            show_popup = false;
316        }
317    } else {
318        /**
319         * Get different time with first visited time
320         * if the different is less than 2 hours, dont show the popup
321         * If the different is more than 2 ours, show the popup
322         */
323        if (getCookie('popup_visited_time')) {
324            if (get_diff_visited_time()) {
325                show_popup = true;
326            } else {
327                show_popup = false;
328            }
329        } else {
330            show_popup = true;
331        }
332    }
333}
334
335function initClickCount(click_count) {
336    if (show_popup) {
337        $(document).click((e) => {
338    
339            if (!$('.wrapper-popup').hasClass('showed')) {
340                visitor_click++;
341                setCookie('popup_visitor_click', visitor_click, 2);
342                // console.log('visitor_click', visitor_click);
343                // console.log('click_count', click_count);
344
345                if (visitor_click.toString() == click_count.toString()) {
346                    showPopup();
347                }
348            }
349        });
350    
351    }
352}
353
354function initTimeDelay(time_delay) {
355    var time = parseInt(time_delay) * 1000;
356    setTimeout(() => {
357        showPopup();
358    }, time);
359}
360
361function initPageTargeting(rules) {
362    var pathname = window.location.href;
363    for (let a = 0; a < rules.length; a++) {
364        if (
365            (rules[a].condition == 'contains' && pathname.search(rules[a].value)) ||
366            (rules[a].condition == 'not_contains' && pathname.search(rules[a].value) < 0)
367        ) {
368            show_popup = true;
369        }
370    }
371}
372
373function initExitIntent() {
374    const body = document.querySelector('body');
375    let mouseY;
376    
377    body.addEventListener('mouseleave', (event) => {
378        mouseY = event.clientY;
379        if (mouseY < 0) {
380            if (show_popup) {
381                showPopup();
382            }
383        }
384    });
385}
386
387function closePopupsales() {
388    $('.wrapper-popup').css({
389        display: 'none',
390    }).removeClass('showed');
391
392    $('.wrapper-popup').find('input').each((i, elem) => {
393        $(elem).val('');
394    });
395
396    // init_visited_time();
397
398    is_showing = 0;
399
400    $.ajax({
401        type: 'POST',
402        url: 'index.php?route=extension/popup_sales/module/popup_sales.identify_close_popup',
403        dataType: 'json',
404        success: function (res) {
405            initSequence();
406        },
407        error: function (err) {}
408    })
409}
410
411function showPopup() {
412    if (!is_showing) {
413        if (show_popup) {
414            $('.wrapper-popup').css({
415                display: 'flex',
416            }).addClass('showed');
417            
418            setCookie('popup_showed', 1, 2);
419        
420            initSequence();
421    
422            init_visited_time();
423    
424            is_showing = 1;
425    
426            $.ajax({
427                type: 'POST',
428                url: 'index.php?route=extension/popup_sales/module/popup_sales.identify_view',
429                dataType: 'json',
430                success: function (res) {
431    
432                },
433                error: function (err) {}
434            })
435        }
436    }
437}
438
439function buildPopup(popup) {
440    // console.log('popup', popup);
441    // title
442    $('.wrapper-popup .box-item-title').text(popup.popup_title);
443
444    // button
445    $('.wrapper-popup .btn-subscribe').text(popup.button_property.text);
446
447    // fields
448    var fields = '';
449
450    for (let a = 0; a < popup.forms.length; a++) {
451        var required = popup.forms[a].required ? 'required' : '';
452
453        var type = 'text';
454        if (popup.forms[a].type == 'email') {
455            type = 'email';
456        }
457
458        fields += `
459        <input class="form-control" type="${type}" ${required} name="${popup.forms[a].type}" id="${popup.forms[a].type}" placeholder="${popup.forms[a].value}" />
460        <div class="text-danger" data-error="popup-${popup.forms[a].type}" style="margin-bottom: 8px; text-align: left;"></div>
461        `;
462    }
463
464    $('.wrapper-popup #target-preview-form').html(fields);
465
466    // styling
467    $('.wrapper-popup .btn-subscribe').css({
468        width: popup.button_property.width,
469        background: popup.button_property.background_color,
470        borderRadius: popup.button_property.border_radius + 'px',
471        color: popup.button_property.text_color,
472    });
473
474    $('.preview-box.popup').css({
475        backgroundColor: popup.background_color,
476        backgroundImage: `url('${popup.background_image}')`,
477        backgroundRepeat: 'no-repeat',
478        backgroundSize: 'cover',
479    });
480
481    if (popup.template_type == 'sales') {
482        $('.preview-box.popup .box-item-subtitle').show();
483        $('.preview-box.popup #target-preview-form').hide();
484        $('.preview-box.popup .box-item').css({
485            padding: '0 20px',
486            gap: '12px',
487            alignItems: 'center',
488            display: 'flex',
489            justifyContent: 'center',
490        });
491        $('.preview-box.popup .product-preview').show();
492        $('.preview-box.popup .product-preview img').attr('src', popup.product.image);
493        $('.preview-box.popup .box-item-subtitle').show();
494        $('.preview-box.popup .box-item-subtitle').text(popup.subtitle);
495        $('.preview-box.popup #popup_product').val(popup.product.id);
496    } else {
497        $('.preview-box.popup .box-item-subtitle').hide();
498        $('.preview-box.popup #target-preview-form').show();
499        $('.preview-box.popup #popup_product').val('');
500    }
501    
502    sizePreviewChanges(popup);
503}
504
505function sizePreviewChanges(popup) {
506    var popup_width = popup.popup_property.width;
507    var popup_height = popup.popup_property.height;
508    var scale = 1;
509    var scale_width = 1;
510    var scale_height = 1;
511    var innerWidth = document.body.clientWidth;
512    var innerHeight = document.body.clientHeight;
513    scale_width = Math.min(innerWidth / popup_width, scale);
514    scale_height = Math.min(innerHeight / popup_height, scale);
515    var new_scale = Math.min(scale_width, scale_height);
516
517    var $width = popup_width / innerWidth;
518    var $height = popup_height / innerHeight;
519    $width = $width * 100;
520    $height = $height * 100;
521    var _top = (popup_height - new_scale * popup_height) / 2;
522    _top = _top < 100 ? _top - 20 : _top;
523    var top = _top == 0 ? '20px' : "-" + _top + "px";
524    var left = (popup_width - innerWidth) / 2 < 0 ? '0' : "-" + (popup_width - innerWidth) / 2 + "px";
525    var bottom = "";
526
527    if (popup.template_type == 'leads') {
528        $('.preview-box.popup').css({
529            transform: `scale(${new_scale})`,
530            height: `${popup_height}px`,
531            width: `${popup_width}px`,
532            top: top,
533            left: left,
534            bottom: bottom,
535        });
536    } else if (popup.template_type == 'sales') {
537        var styles = initPopupPosition(popup.popup_position);
538        styles.height = popup_height + 'px';
539        styles.width = popup_width + 'px';
540        $('.preview-box.popup').css(styles);
541    }
542}

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.