PageSourceSearch

https://www.verticcio.sk/modules/packetery/views/js/front.js?v=3.3.0

js verticcio.sk collected 2026-10-06 20:04:27 UTC 22,653 bytes, 597 lines download raw bytes

1PacketaModule = window.PacketaModule || {};
2
3PacketaModule.tools = {
4    isPS16: function () {
5        return PacketaModule.config.prestashopVersion.indexOf('1.6') === 0;
6    },
7    isPS8: function () {
8        return PacketaModule.config.prestashopMajorVersion === '8';
9    },
10    isPS9: function () {
11        return PacketaModule.config.prestashopMajorVersion === '9';
12    }
13};
14
15PacketaModule.runner = {
16    /**
17     * Supposed to be called only once
18     */
19    onThisScriptLoad: function () {
20        // non-blocking AJAX loading, speeds up page load
21        var dependencies = [];
22        dependencies.push($.getScript('https://widget.packeta.com/v6/www/js/library.js'));
23
24        $.when.apply(null, dependencies).done(function () {
25            PacketaModule.runner.onWidgetLoad();
26        });
27    },
28
29    /**
30     * Supposed to be called only once
31     */
32    onWidgetLoad: function () {
33        // register on document load callback after widget is loaded
34        $(PacketaModule.runner.onDocumentLoad);
35    },
36
37    /**
38     * Supposed to be called only once
39     */
40    onDocumentLoad: function () {
41        if (typeof PacketaModule.config === 'undefined') {
42            return; // this script is not loaded on a page with a selection of shipping methods
43        }
44
45        PacketaModule.runner.onShippingLoad();
46    },
47
48    /**
49     * May be called more than once in a lifetime of this script, when shipping methods are updated via AJAX
50     */
51    onShippingLoad: function () {
52        if (PacketaModule.tools.isPS16()) {
53            PacketaModule.ui.addAllExtraContents(PacketaModule.runner.onExtraContentLoad);
54        } else {
55            PacketaModule.runner.onExtraContentLoad();
56        }
57    },
58
59    onExtraContentLoad: function () {
60        var module = packeteryModulesManager.detectModule();
61        if (module === null) {
62            return;
63        }
64
65        PacketaModule.ui.initializeWidget();
66        PacketaModule.ui.autoOpenWidget();
67
68        PacketaModule.ui.toggleSubmit();
69        PacketaModule.ui.toggleExtraContent();
70
71        var $deliveryInputs = module.findDeliveryOptions();
72        $deliveryInputs.off('change.packetery').on('change.packetery', function () {
73            PacketaModule.runner.onShippingChange($(this));
74        });
75    },
76
77    onShippingChange: function ($selectedInput) {
78        var module = packeteryModulesManager.detectModule();
79        if (module === null) {
80            return;
81        }
82
83        // just in case this script dies, or an AJAX round trip delay would allow customer to continue without selecting a branch first
84        module.disableSubmitButton();
85
86        if (PacketaModule.config.toggleExtraContentOnShippingChange) {
87            PacketaModule.ui.autoOpenWidget();
88            PacketaModule.ui.toggleExtraContent();
89        }
90
91        var $widgetParent = packeteryModulesManager.getWidgetParent($selectedInput);
92
93        // if selected carrier is not Packeta then enable Continue button and we're done here
94        if (!$widgetParent.length) {
95            module.enableSubmitButton();
96            return;
97        }
98
99        var prestashopCarrierId = packeteryModulesManager.getCarrierId($selectedInput);
100        var validatedAddress = PacketaModule.storage.getAddress(prestashopCarrierId);
101        if (PacketaModule.ui.isPickupPointValid($widgetParent)) {
102            var branchId = $widgetParent.find(".packeta-branch-id").val();
103            var branchName = $widgetParent.find(".packeta-branch-name").val();
104            var branchCurrency = $widgetParent.find(".packeta-branch-currency").val();
105            var pickupPointType = $widgetParent.find(".packeta-pickup-point-type").val();
106            var widgetCarrierId = $widgetParent.find(".packeta-carrier-id").val();
107            var carrierPickupPointId = $widgetParent.find(".packeta-carrier-pickup-point-id").val();
108            if (branchId) {
109                PacketaModule.ajax.savePickupPointInCart(
110                    prestashopCarrierId,
111                    branchId,
112                    branchName,
113                    pickupPointType,
114                    widgetCarrierId,
115                    carrierPickupPointId,
116                    branchCurrency,
117                    PacketaModule.ui.toggleSubmit
118                );
119            } else {
120                PacketaModule.ui.toggleSubmit();
121            }
122        } else if (PacketaModule.ui.isAddressValidationSatisfied($widgetParent, $selectedInput) && validatedAddress) {
123            PacketaModule.ajax.saveAddressInCart(validatedAddress, PacketaModule.ui.toggleSubmit)
124        } else {
125            PacketaModule.ui.toggleSubmit();
126        }
127    },
128
129    /**
130     * Called in two scenarios:
131     * - during initial load - at this point, delivery methods have not been downloaded yet and module detection probably fails
132     * - on AJAX update of delivery methods
133     */
134    onBeforeCarrierLoad: function () {
135        var module = packeteryModulesManager.detectModule();
136        if (module === null) {
137            return;
138        }
139
140        if (module.findDeliveryOptions().length !== 0) {
141            PacketaModule.runner.onShippingLoad();
142        }
143    }
144}
145
146PacketaModule.ui = {
147    toggleExtraContent: function () {
148        var module = packeteryModulesManager.detectModule();
149        if (module === null) {
150            return;
151        }
152
153        // if template doesn't handle showing carrier-extra-content then we have to
154        var toggleExtraContent = PacketaModule.config.toggleExtraContent;
155        if (typeof module.toggleExtraContent !== 'undefined') {
156            toggleExtraContent = module.toggleExtraContent;
157        }
158        if (!toggleExtraContent && !PacketaModule.tools.isPS16()) {
159            return;
160        }
161
162        // hide it for all carriers - if they happen to have it
163        $(module.getExtraContentSelector()).hide();
164
165        // show it only for Packeta carriers (easier to do because we have id="packetery-carrier-{$carrier_id}")
166        var $selectedInput = module.getSelectedInput();
167        if ($selectedInput.length === 0) {
168            return;
169        }
170        packeteryModulesManager.getWidgetParent($selectedInput)
171            .closest(module.getExtraContentSelector())
172            .show();
173    },
174
175    extraContentCache: {},
176
177    /**
178     * May be called multiple times, even in a very short time, especially in Supercheckout PS 1.6
179     * @see display-before-carrier.tpl
180     */
181    addAllExtraContents: function (onExtraContentLoad) {
182        var module = packeteryModulesManager.detectModule();
183        if (module === null) {
184            return;
185        }
186
187        var $deliveryOptions = module.findDeliveryOptions();
188        var ajaxCalls = [],
189            loadedFromCache = false;
190
191        $deliveryOptions.each(function (i, e) {
192            var $deliveryInput = $(e);
193            var carrierId = packeteryModulesManager.getCarrierId($deliveryInput);
194
195            // TODO: fix address validation in PS 1.6
196            var isCarrierWithDeliveryPoints = PacketaModule.config.deliveryPointCarrierIds.indexOf(carrierId) >= 0;
197            if (!isCarrierWithDeliveryPoints) {
198                return;
199            }
200
201            if (typeof PacketaModule.ui.extraContentCache[carrierId] !== 'undefined') {
202                if (PacketaModule.ui.extraContentCache[carrierId] !== 'pending') {
203                    PacketaModule.ui.addOneExtraContent($deliveryInput, PacketaModule.ui.extraContentCache[carrierId]);
204                    loadedFromCache = true;
205                }
206                return;
207            }
208
209            PacketaModule.ui.extraContentCache[carrierId] = 'pending';
210
211            var ajaxCall = PacketaModule.ajax.fetchExtraContent(carrierId).done(function (result) {
212                PacketaModule.ui.addOneExtraContent($deliveryInput, result);
213                PacketaModule.ui.extraContentCache[carrierId] = result;
214            });
215            ajaxCalls.push(ajaxCall);
216        });
217
218        if (ajaxCalls.length > 0) {
219            $.when.apply(null, ajaxCalls).then(onExtraContentLoad);
220        }
221
222        if (loadedFromCache) {
223            onExtraContentLoad();
224        }
225    },
226
227    addOneExtraContent: function ($deliveryInput, html) {
228        var isAlreadyThere = packeteryModulesManager.getWidgetParent($deliveryInput).length > 0;
229        if (isAlreadyThere) {
230            return;
231        }
232
233        var module = packeteryModulesManager.detectModule();
234        if (module === null) {
235            return;
236        }
237
238        module.getExtraContentContainer($deliveryInput).append(html);
239    },
240
241    toggleSubmit: function () {
242        var module = packeteryModulesManager.detectModule();
243        if (module === null) {
244            return;
245        }
246
247        var $selectedInput = module.getSelectedInput();
248        if ($selectedInput.length === 0) {
249            return;
250        }
251        var $widgetParent = packeteryModulesManager.getWidgetParent($selectedInput);
252        if (
253            PacketaModule.ui.isPickupPointInvalid($widgetParent) ||
254            PacketaModule.ui.isAddressValidationUnsatisfied($widgetParent, $selectedInput)
255        ) {
256            module.disableSubmitButton();
257        } else {
258            module.enableSubmitButton();
259            module.hideValidationErrors();
260        }
261    },
262
263    initializeWidget: function () {
264        if (PacketaModule.config.apiKey === '') {
265            return;
266        }
267
268        var module = packeteryModulesManager.detectModule();
269        if (module === null) {
270            return;
271        }
272
273        var country = 'cz,sk';
274        if (PacketaModule.config.customerCountry !== '') {
275            country = PacketaModule.config.customerCountry;
276        }
277
278        var language = 'en';
279        if (PacketaModule.config.shopLanguage !== '') {
280            language = PacketaModule.config.shopLanguage;
281        }
282
283        $('.open-packeta-widget').click(function (e) {
284            e.preventDefault();
285            var widgetOptions = {
286                appIdentity: PacketaModule.config.appIdentity,
287                language: language,
288            };
289            var $selectedInput = module.getSelectedInput();
290            if ($selectedInput.length === 0) {
291                // in supercheckout after switching country and no delivery is selected
292                return;
293            }
294            var $widgetParent = packeteryModulesManager.getWidgetParent($selectedInput);
295            var widgetVendors = $widgetParent.find('#widget_vendors').val();
296            if (widgetVendors !== '' && widgetVendors !== '[]') {
297                widgetOptions.vendors = JSON.parse(widgetVendors);
298            } else {
299                widgetOptions.country = country;
300            }
301
302            if (PacketaModule.config.isAgeVerificationRequired) {
303                widgetOptions.livePickupPoint = true;
304            }
305
306            Packeta.Widget.pick(PacketaModule.config.apiKey, function (pickupPoint) {
307                if (pickupPoint == null) {
308                    return;
309                }
310
311                $widgetParent.find('.packeta-branch-id').val(pickupPoint.id);
312                $widgetParent.find('.packeta-branch-name').val(pickupPoint.name);
313                $widgetParent.find('.packeta-branch-currency').val(pickupPoint.currency);
314                $widgetParent.find('.packeta-pickup-point-type').val(pickupPoint.pickupPointType);
315                $widgetParent.find('.packeta-carrier-id').val(pickupPoint.carrierId);
316                $widgetParent.find('.packeta-carrier-pickup-point-id').val(pickupPoint.carrierPickupPointId);
317
318                // let the customer know which branch he picked
319                $widgetParent.find('.picked-delivery-place').html(pickupPoint.name);
320
321                var prestashopCarrierId = packeteryModulesManager.getCarrierId($selectedInput);
322
323                /* Save packetery order without order ID - just cart id so we can access carrier data later */
324                PacketaModule.ajax.savePickupPointInCart(
325                    prestashopCarrierId,
326                    pickupPoint.id,
327                    pickupPoint.name,
328                    pickupPoint.pickupPointType,
329                    pickupPoint.carrierId,
330                    pickupPoint.carrierPickupPointId,
331                    pickupPoint.currency,
332                    function (jsonResponse) {
333                        if (jsonResponse.result === true) {
334                            PacketaModule.ui.toggleSubmit();
335                        } else {
336                            console.error(jsonResponse.message);
337                        }
338                    }
339                );
340            }, widgetOptions);
341        });
342
343        $('.open-packeta-widget-hd').click(function (e) {
344            e.preventDefault();
345            var $selectedInput = module.getSelectedInput();
346            if ($selectedInput.length === 0) {
347                return;
348            }
349            var $widgetParent = packeteryModulesManager.getWidgetParent($selectedInput);
350            var widgetCarriers = $widgetParent.find('#widget_carriers').val();
351            var customerStreet = $widgetParent.find('#customerStreet').val();
352            var customerHouseNumber = $widgetParent.find('#customerHouseNumber').val();
353            var customerCity = $widgetParent.find('#customerCity').val();
354            var customerZip = $widgetParent.find('#customerZip').val();
355            var widgetOptions = {
356                layout: 'hd',
357                language: language,
358                country: country,
359                // in this case, there will always be one carrier
360                carrierId: widgetCarriers,
361                appIdentity: PacketaModule.config.appIdentity
362            };
363            if (customerStreet) {
364                widgetOptions.street = customerStreet;
365            }
366            if (customerHouseNumber) {
367                widgetOptions.houseNumber = customerHouseNumber;
368            }
369            if (customerCity) {
370                widgetOptions.city = customerCity;
371            }
372            if (customerZip) {
373                widgetOptions.postCode = customerZip;
374            }
375            Packeta.Widget.pick(PacketaModule.config.apiKey, function (result) {
376                if (result != null && result.address != null) {
377                    // there is also property packetaWidgetMessage which is true
378                    var address = result.address;
379                    var $addressValidationResult = $widgetParent.find('.address-validation-result');
380                    if (address.country === country) {
381                        PacketaModule.ajax.saveAddressInCart(address);
382                        var prestashopCarrierId = packeteryModulesManager.getCarrierId($selectedInput);
383                        PacketaModule.storage.setAddress(address, prestashopCarrierId)
384                        $widgetParent.find('#addressValidated').val(true);
385                        $addressValidationResult.addClass('address-validated');
386                        $addressValidationResult.text(PacketaModule.config.addressValidatedMessage);
387                        $widgetParent.find('.picked-delivery-place').html(
388                            address.street + ' ' + address.houseNumber + ', ' + address.city + ', ' + address.postcode
389                        );
390                        PacketaModule.ui.toggleSubmit();
391                    } else {
392                        $widgetParent.find('#addressValidated').val(false);
393                        $addressValidationResult.removeClass('address-validated');
394                        $addressValidationResult.text(PacketaModule.config.countryDiffersMessage);
395                        PacketaModule.ui.toggleSubmit();
396                    }
397                }
398            }, widgetOptions);
399        });
400    },
401
402    autoOpenWidget: function () {
403        if (!PacketaModule.config.widgetAutoOpen) {
404            return;
405        }
406
407        var module = packeteryModulesManager.detectModule();
408        if (module === null) {
409            return;
410        }
411
412        var $selectedDeliveryOption = module.getSelectedInput();
413        if ($selectedDeliveryOption.length !== 1) {
414            return;
415        }
416
417        var $widgetParent = packeteryModulesManager.getWidgetParent($selectedDeliveryOption);
418        var $widgetButton = $widgetParent.find('.open-packeta-widget');
419        if (
420            $widgetButton.length === 1 &&
421            $widgetParent.find('.packeta-branch-id').val() === ''
422            // todo PePa: how could we reach this point with widget already open?
423            // &&
424            // $('iframe #packeta-widget').length === 0
425        ) {
426            $widgetButton.click();
427        }
428    },
429
430    isHdCarrier: function ($widgetParent) {
431        return !!$widgetParent.find('#open-packeta-widget-hd').length;
432    },
433
434    isPpCarrier: function ($widgetParent) {
435        return !!$widgetParent.find('#open-packeta-widget').length;
436    },
437
438    isPickupPointValid: function ($widgetParent) {
439        var selectedBranchId = $widgetParent.find('.packeta-branch-id').val();
440        return this.isPpCarrier($widgetParent) && selectedBranchId;
441    },
442
443    isPickupPointInvalid: function ($widgetParent) {
444        var selectedBranchId = $widgetParent.find('.packeta-branch-id').val();
445        return this.isPpCarrier($widgetParent) && !selectedBranchId;
446    },
447
448    isAddressValidationSatisfied: function ($widgetParent, $selectedInput) {
449        var prestashopCarrierId = packeteryModulesManager.getCarrierId($selectedInput);
450        var addressValidationSetting = PacketaModule.config.addressValidationLevels[prestashopCarrierId];
451        var addressValidated = PacketaModule.ui.makeBoolean($widgetParent.find('#addressValidated').val());
452        return (addressValidationSetting === 'required' || addressValidationSetting === 'optional') && addressValidated;
453    },
454
455    isAddressValidationUnsatisfied: function ($widgetParent, $selectedInput) {
456        var prestashopCarrierId = packeteryModulesManager.getCarrierId($selectedInput);
457        var addressValidationSetting = PacketaModule.config.addressValidationLevels[prestashopCarrierId];
458        var addressValidated = PacketaModule.ui.makeBoolean($widgetParent.find('#addressValidated').val());
459        return (this.isHdCarrier($widgetParent) && addressValidationSetting === 'required' && !addressValidated);
460    },
461
462    makeBoolean: function (value) {
463        if (value === 'false') {
464            return false;
465        }
466        return !!value;
467    }
468};
469
470PacketaModule.ajax = {
471    post: function (action, data, onSuccess) {
472        var url =
473            PacketaModule.config.baseUri +
474            '/module/packetery/checkout?action=' + action +
475            '&token=' + PacketaModule.config.frontAjaxToken;
476
477        return $.ajax({
478            type: 'POST',
479            url: url,
480            data: data,
481            beforeSend: function () {
482                // todo: To which checkout module does this css class belong? Not Supercheckout PS 1.7, not PS 1.7, not PS 1.6 5-step nor OPC
483                $("body").toggleClass("wait");
484            },
485            success: function (response) {
486                if (typeof onSuccess !== 'undefined') {
487                    onSuccess(response);
488                }
489            },
490            complete: function () {
491                $("body").toggleClass("wait");
492            },
493        });
494    },
495
496    savePickupPointInCart: function (prestashopCarrierId, branchId, branchName, pickupPointType, widgetCarrierId, carrierPickupPointId, branchCurrency, onSuccess) {
497        return PacketaModule.ajax.post('savePickupPointInCart', {
498            'prestashop_carrier_id': prestashopCarrierId,
499            'id_branch': branchId,
500            'name_branch': branchName,
501            'currency_branch': branchCurrency,
502            'pickup_point_type': pickupPointType,
503            'widget_carrier_id': widgetCarrierId,
504            'carrier_pickup_point_id': carrierPickupPointId
505        }, onSuccess);
506    },
507
508    saveAddressInCart: function (address, onSuccess) {
509        return PacketaModule.ajax.post('saveAddressInCart', {'address': address}, onSuccess);
510    },
511
512    fetchExtraContent: function (prestashopCarrierId) {
513        return PacketaModule.ajax.post('fetchExtraContent', {
514            'prestashop_carrier_id': prestashopCarrierId,
515        });
516    }
517};
518
519PacketaModule.storage = {
520    setAddress: function (address, carrierId) {
521        this.addresses[carrierId] = address;
522    },
523
524    getAddress: function (carrierId) {
525        if (typeof this.addresses[carrierId] === 'undefined') {
526            return null;
527        }
528        return this.addresses[carrierId];
529    },
530
531    addresses: {}
532};
533
534function PacketeryCheckoutModulesManager()
535{
536    // ids correspond to parts of class names in checkout-module/*.js - first letter in upper case
537    // order is important because of false positives (Unknown has to be last)
538    this.supportedModules = ['Ps16', 'Ps17', 'Supercheckout', 'OpcZelarg', 'Hummingbird', 'Unknown'];
539    this.loadedModules = [];
540    this.detectedModule = null;
541
542    this.loadModules = function () {
543        this.loadedModules = [];
544        var manager = this;
545
546        this.supportedModules.forEach(function (moduleId) {
547            // moduleId = 'Standard' => className = 'PacketeryCheckoutModuleStandard'
548            var className = 'PacketeryCheckoutModule' + moduleId;
549
550            // if really loaded via hookDisplayHeader()
551            if (typeof window[className] !== 'undefined') {
552                manager.loadedModules.push(window[className]);
553            }
554        });
555    };
556
557    this.detectModule = function () {
558        if (this.detectedModule !== null) {
559            return this.detectedModule;
560        }
561
562        if (this.loadedModules.length === 0) {
563            this.loadModules();
564        }
565
566        var manager = this;
567        this.loadedModules.forEach(function (module) {
568            if ((manager.detectedModule === null) && module.isActive()) {
569                manager.detectedModule = module;
570            }
571        });
572
573        return this.detectedModule;
574    };
575
576    // in case we need to change this in the future
577    this.getCarrierId = function ($selectedInput) {
578        return $selectedInput.val().replace(',', '');
579    }
580
581    this.getWidgetParent = function ($selectedInput) {
582        return $('#packetery-carrier-' + this.getCarrierId($selectedInput));
583    }
584}
585
586var packeteryModulesManager = new PacketeryCheckoutModulesManager();
587
588
589/**
590 *  This function is called by third party checkout modules (e.g. Supercheckout) after shipping methods are fetched via AJAX
591 */
592function onShippingLoadedCallback()
593{
594    PacketaModule.runner.onShippingLoad();
595}
596
597PacketaModule.runner.onThisScriptLoad();

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.