PageSourceSearch

https://www.editorialgeu.com/modules/psproductcountdownpro/views/js/front.js

js editorialgeu.com collected 2026-09-26 12:01:57 UTC 14,124 bytes, 347 lines download raw bytes

1/**
2 * NOTICE OF LICENSE
3 *
4 * This file is licenced under the Software License Agreement.
5 * With the purchase or the installation of the software in your application
6 * you accept the licence agreement.
7 *
8 * @author    Presta.Site
9 * @copyright 2019 Presta.Site
10 * @license   LICENSE.txt
11 */
12var pspc_init_working = false;
13
14if (typeof updateDisplay === 'function') {
15    var updateDisplay_pspc_original = updateDisplay;
16    updateDisplay = function () {
17        updateDisplay_pspc_original();
18        pspc_refreshProductTimers();
19    }
20}
21
22$(function () {
23    pspc_initCountdown();
24
25    $(document).on('click', '#grid', function(e){
26        e.preventDefault();
27        pspc_initCountdown('.psproductcountdown');
28    });
29
30    $(document).on('click', '#list', function(e){
31        e.preventDefault();
32        pspc_initCountdown('.psproductcountdown');
33    });
34
35    var $block_page16 = $('.pspc-block-page.pspc16');
36    if ($block_page16.length) {
37        $block_page16.on('change', '.selectProductSort', function (e) {
38            if (typeof request != 'undefined' && request)
39                var requestSortProducts = request;
40            var splitData = $(this).val().split(':');
41            var url = '';
42            if (typeof requestSortProducts != 'undefined' && requestSortProducts) {
43                url += requestSortProducts;
44                if (typeof splitData[0] !== 'undefined' && splitData[0]) {
45                    url += (requestSortProducts.indexOf('?') < 0 ? '?' : '&') + 'orderby=' + splitData[0];
46                    if (typeof splitData[1] !== 'undefined' && splitData[1])
47                        url += '&orderway=' + splitData[1];
48                }
49                document.location.href = url;
50            }
51        });
52
53        $block_page16.on('change', 'select[name="n"]', function () {
54            $(this.form).submit();
55        });
56    }
57});
58
59function pspc_initCountdown(selector) {
60    if (pspc_init_working) {
61        return true;
62    }
63
64    pspc_init_working = true;
65
66    // set timeout 0 for preventing conflicts with html updates by other modules
67    setTimeout(function () {
68        // if the plugin was unloaded by reloading Jquery (can happen at least with amazzingfilter)
69        if (typeof $.fn.pspccountdown === 'undefined') {
70            $('body').append('<script src="' + pspc_countdown_js + '"></script>');
71        }
72        selector = (selector ? selector : '.pspc-inactive');
73        $(selector).each(function(){
74            var $pspc = $(this);
75            var $pspc_container = $(this).parent('.pspc-wrp');
76            $pspc_container = ($pspc_container.length ? $pspc_container : $pspc);
77
78            // get "to" date
79            var to = $pspc.data('to');
80            to = (to ? to : $pspc.text());
81            // Check if "to" is a number or a string
82            to = (isNaN(to) ? dateStringToTimestamp(to) : parseInt(to));
83
84            var $pspc_main = $pspc.find('.pspc-main').clone();
85            $pspc.html('');
86            if ($pspc_main.length) {
87                $pspc_main.appendTo($pspc);
88            } else {
89                $pspc.append('<div class="pspc-main" />');
90            }
91            $pspc_main = $pspc.find('.pspc-main');
92
93            // calculate "to" if this countdown is personal
94            if ($pspc.data('personal')) {
95                var id_countdown = parseInt($pspc.data('id-countdown'));
96                var hours = $pspc.data('personal-hours');
97                var hours_restart = $pspc.data('personal-hours-restart');
98                var type = $pspc.data('countdown-type');
99                var cookie_name = 'pspc_to_' + type + id_countdown;
100
101                if (localStorage.getItem(cookie_name)) {
102                    to = new Date();
103                    to.setTime(localStorage.getItem(cookie_name));
104                    var personal_now = + new Date();
105                    if (to < personal_now) {
106                        if (hours_restart !== '') {
107                            var hours_restart_ms = hours_restart * 60000 * 60;
108                            if ((+to + hours_restart_ms) <= personal_now || hours_restart === 0) {
109                                var d1 = new Date();
110                                to = new Date(d1);
111                                var new_to = d1.getTime() + (hours*60*60 - 1)*1000;
112                                to.setTime( new_to );
113                                localStorage.setItem(cookie_name, new_to);
114                            }
115                        }
116                    }
117                } else {
118                    var d1 = new Date();
119                    to = new Date(d1);
120                    var new_to = d1.getTime() + (hours*60*60 - 1)*1000;
121                    to.setTime( new_to );
122                    localStorage.setItem(cookie_name, new_to);
123                }
124            }
125
126            var pspc_long = $pspc.hasClass('pspc-long');
127
128            if (!pspc_long) {
129                var now = +new Date();
130                if (!to || (to < now && (pspc_hide_expired || pspc_hide_after_end))) {
131                    $pspc.hide();
132                    pspc_init_working = false;
133                    return true;
134                }
135            }
136
137            // adjust countdown position at the page
138            if ($pspc.hasClass('pspc-custom')) {
139                $pspc.wrap('<div class="pspc-wrp"></div>');
140                if (typeof pspc_show_colon !== 'undefined' && pspc_show_colon 
140=== 1) {
141                    $pspc.addClass('pspc-show-colon');
142                }
143            } else if (pspc_adjust_positions) {
144                var $parent = $pspc_container.closest('.product-prices');
145                if (pspc_position_product === 'displayProductPriceBlock' && $parent.length) {
146                    $pspc_container.detach().appendTo($parent);
147                } else if ($pspc_container.hasClass('pspc-wrp-over-img')) {
148                    var $img_container = $pspc_container.closest('.product-miniature');
149                    $img_container = ($img_container.length ? $img_container : $pspc_container.closest('.ajax_block_product'));
150                    if ($img_container) {
151                        var $thumb = $img_container.find('.thumbnail');
152                        var $img = ($thumb.length ? $thumb.find('img:first') : $img_container.find('img:first'));
153                        if ($img && $img.length) {
154                            $pspc_container.detach();
155                            $img.after($pspc_container);
156                            $pspc_container.parent().addClass('pspc-parent');
157                        }
158                    }
159                }
160            }
161
162            setTimeout(function () {
163                $pspc.addClass('psproductcountdown').removeClass('pspc-inactive');
164            }, 0);
165
166            if (pspc_long) {
167                if (!$pspc.hasClass('pspc-cw-hide')) {
168                    $pspc.show();
169                }
170
171                pspc_init_working = false;
172                return true;
173            }
174
175            if (typeof pspc_callbackBeforeDisplay === 'function') {
176                pspc_callbackBeforeDisplay($pspc_container, $pspc);
177            }
178
179            // minimal themes:
180            if (pspc_theme.indexOf('minimal') !== -1) {
181                $pspc.pspccountdown(to, function(event) {
182                    var now = + new Date();
183                    var from = $pspc.data('from');
184
185                    if (to < now && pspc_hide_after_end) {
186                        $pspc.hide(400);
187                    } else if (from) {
188                        from = dateStringToTimestamp(from);
189                        if (now < from) {
190                            $pspc.hide();
191                        } else {
192                            $pspc.show(300);
193                        }
194                    }
195
196                    var days = parseInt(event.strftime('%D'));
197                    days = (days === 1 ? pspc_labels_lang_1['days'] : pspc_labels_lang['days']);
198                    var name = $pspc.data('name');
199                    name = (name ? name : pspc_offer_txt);
200                    var name_html = (name ? '<span class="pspc_h">' + name + '</span><br/>' : '');
201                    var name_left = (pspc_promo_side === 'left' ? name_html : '');
202                    var name_right = (pspc_promo_side === 'right' ? ' ' + name_html : '');
203                    var html = name_left + '<div class="pspc-countdown-wrp"><span class="days-%D">%D ' + days + '</span> %H:%M:%S </div>' + name_right;
204                    if (!$pspc_main.parent().length) {
205                        $pspc_main = $pspc.find('.pspc-main');
206                    }
207                    $pspc_main.html(event.strftime(html));
208                });
209            // regular themes:
210            } else {
211                var tpl = pspc_countdown_tpl;
212                var labels = pspc_labels,
213                    template = _.template(tpl);
214                var currDate = '00:00:00:00';
215                var nextDate = '00:00:00:00';
216
217                // Build the layout
218                var initData = pspc_strfobj(currDate);
219                $pspc_main.find('.pspc-time').remove();
220                labels.forEach(function(label, i) {
221                    $pspc_main.append(template({
222                        curr: initData[label],
223                        next: initData[label],
224                        label: label,
225                        label_lang: pspc_labels_lang[label]
226                    }));
227                });
228                // Start the countdown
229                $pspc_main.pspccountdown(to, function(event) {
230                    var now = + new Date();
231                    var from = $pspc.data('from');
232
233                    if (to < now && pspc_hide_after_end) {
234                        $pspc.hide(400);
235                    } else if (from) {
236                        from = dateStringToTimestamp(from);
237                        if (now < from) {
238                            $pspc.hide();
239                        } else {
240                            $pspc.show(300);
241                        }
242                    }
243
244                    var data;
245                    var newDate = event.strftime('%D:%H:%M:%S');
246
247                    if (newDate !== nextDate) {
248                        currDate = nextDate;
249                        nextDate = newDate;
250                        // Setup the data
251                        data = {
252                            'curr': pspc_strfobj(currDate),
253                            'next': pspc_strfobj(nextDate)
254                        };
255                        // Apply the new values to each node that changed
256                        pspc_diff(data.curr, data.next).forEach(function(label) {
257                            var selector = '.%s'.replace(/%s/, label),
258                                $node = $pspc_main.find(selector);
259                            // Update the node
260                            $node.removeClass('flip hidden');
261                            $node.find('.pspc-curr').text(data.curr[label]);
262                            $node.find('.pspc-next').text(data.next[label]);
263                            // Wait for a repaint to then flip
264                            _.delay(function($node) {
265                                $node.addClass('flip');
266                            }, 50, $node);
267                        });
268                    }
269                });
270            }
271        });
272
273        pspc_init_working = false;
274    }, 0);
275}
276if (typeof initCountdown === 'undefined') {
277    var initCountdown = pspc_initCountdown;
278}
279
280// Parse countdown string to an object
281function pspc_strfobj(str) {
282    var pieces = str.split(':');
283    var obj = {};
284    pspc_labels.forEach(function(label, i) {
285        obj[label] = pieces[i]
286    });
287    return obj;
288}
289
290// Return the time components that diffs
291function pspc_diff(obj1, obj2) {
292    var diff = [];
293    pspc_labels.forEach(function(key) {
294        if (obj1[key] !== obj2[key]) {
295            diff.push(key);
296        }
297    });
298    return diff;
299}
300
301function dateStringToTimestamp(dateString) {
302    var dateTimeParts = dateString.split(' '),
303        timeParts = dateTimeParts[1].split(':'),
304        dateParts = dateTimeParts[0].split('-'),
305        date;
306
307    date = new Date(dateParts[0], parseInt(dateParts[1], 10) - 1, dateParts[2], timeParts[0], timeParts[1]);
308
309    return date.getTime();
310}
311
312function pspc_refreshProductTimers() {
313    try {
314        var id_pa = $('#idCombination').val();
315        var $combi_wrp = $('.pspc-combi-wrp');
316        $combi_wrp.hide().addClass('pspc-cw-hide');
317        if (id_pa) {
318            $('.pspc-cw-' + id_pa).removeClass('pspc-cw-hide').fadeIn(100);
319        } else {
320            $('.pspc-combi-wrp:first').removeClass('pspc-cw-hide').fadeIn(100);
321        }
322
323        var $pspc_main1 = $('.pspc-cw-hide').find('.pspc-main');
324        $pspc_main1.each(function () {
325           if ($(this).data("pspccountdown-instance")) {
326               $(this).pspccountdown('stop');
327           }
328        });
329        var $pspc_main2 = $combi_wrp.not('.pspc-cw-hide').find('.pspc-main');
330        $pspc_main2.each(function () {
331            if ($(this).data("pspccountdown-instance")) {
332                $(this).pspccountdown('start');
333            }
334        });
335    } catch(e) {
336        console.log(e);
337    }
338}
339
340var pspc_countdown_tpl = '' +
341    '<div class="pspc-time <%= label %> <%= label == pspc_highlight ? \'pspc-highlight\' : \'\' %>">' +
342    '<span class="pspc-count pspc-curr pspc-top"><%= curr %></span>' +
343    '<span class="pspc-count pspc-next pspc-top"><%= next %></span>' +
344    '<span class="pspc-count pspc-next pspc-bottom"><%= next %></span>' +
345    '<span class="pspc-count pspc-curr pspc-bottom"><%= curr %></span>' +
346    '<span class="pspc-label"><%= label_lang %></span>' +
347    '</div>';

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.