PageSourceSearch

https://carturesti.md/assets/3fd72a50b26e96395f0f6077b7b946e7/js/top.controllers.js?v=1788945285

js carturesti.md collected 2026-10-02 13:32:29 UTC 20,352 bytes, 513 lines download raw bytes

1let wListToggleCalled = false;
2angular.module('top.controllers',[])
3    .controller('cartController', function ($scope, $mdToast, wishlist, $sce, $http) {
4        $scope.enableWishlist = window.constants.loggedIn
5        $scope.openCart = function(){
6            $('body').addClass('cartOpen');
7        };
8        $scope.closeCart = function () {
9            $('body').removeClass('cartOpen');
10        };
11        $scope.wishlist = wishlist;
12        var cart = $scope.cart;
13        $scope.total = 0;
14        $scope.quantity = 1;
15        $scope.init = function (product) {
16            if(product !== undefined) {
17                $scope.injectedProduct = product;
18            }
19            if(!$scope.cart.products.length) {
20                cart.load('json-cart').then(function (r) {
21                    $scope.setCart(r);
22                    applyCheckoutOverrides();
23                });
24            } else {
25                applyCheckoutOverrides();
26            }
27
28            bindJsonCartRefreshTriggers();
29        };
30
31        function refreshJsonCart() {
32            cart.get().then(function (r) {
33                $scope.setCart(r);
34                applyCheckoutOverrides();
35            });
36        }
37
38        function applyCheckoutOverrides() {
39            applyExactShippingIfPresent();
40            applyExactTotalIfPresent();
41        }
42
43        function applyExactTotalIfPresent() {
44            const el = document.querySelector('[data-exact-total]');
45            if (!el) return;
46            const value = parseFloat(el.getAttribute('data-exact-total'));
47            if (isNaN(value)) return;
48            cart.total = value;
49            $scope.cartTotal();
50        }
51
52        function applyExactShippingIfPresent() {
53            const el = document.querySelector('[data-shipping-value]');
54            if (!el) {
55                cart.shipping = null;
56                return;
57            }
58            const value = parseFloat(el.getAttribute('data-shipping-value'));
59            cart.shipping = isNaN(value) ? null : value;
60        }
61
62        function isJsonCartRefreshUrl(url) {
63            if (!url) return false;
64            return url.indexOf('/checkout/delivery-method-by-address') !== -1
65                || url.indexOf('/checkout/get-updated-cart-cost') !== -1;
66        }
67
68        function onCheckoutAjaxSuccess(e, xhr, settings) {
69            if (!isJsonCartRefreshUrl(settings && settings.url)) return;
70            // setTimeout with 0 as the second arg schedules a macrotask to run after the ones in progress
71            // this ensures that the refresh is done after the checkout sidebar .done() handlers update data-* attributes.
72            setTimeout(refreshJsonCart, 0);
73        }
74
75        function bindJsonCartRefreshTriggers() {
76            $(document).on('pjax:end.jsonCartRefresh', '#checkout-products-grid-pjax', refreshJsonCart);
77            $(document).on('ajaxSuccess.jsonCartRefresh', onCheckoutAjaxSuccess);
78            $scope.$on('$destroy', function () {
79                $(document).off('.jsonCartRefresh');
80            });
81        }
82        $scope.numberFormat = function (n) {
83            return $scope.h.numberFormat(n, 2, ',', '.');
84        };
85
86        $scope.toggleWishlist = function (e) {
87            $scope.wishlist.toggle(e);
88            $scope.$applyAsync(wishlist.closed);
89        }
90
91        $scope.setCart = function (products) {
92            $scope.cart.products = products;
93            $scope.cartTotal();
94        };
95
96        $scope.cartTotal = function () {
97            $scope.total = $scope.h.numberFormat(cart.total, 2, ',', '.');
98        };
99        $scope.checkoutClass = function () {
100            return cart.products.length ? 'hasProducts' : 'noProducts';
101        };
102
103        $scope.addToCart = function (id, qty, preventDefault = false) {
104            if (preventDefault === true) {
105                // noinspection JSUnresolvedVariable
106                var productData = window.productData;
107                // noinspection JSUnresolvedVariable
108                var vexLocationStockPopup = window.vexLocationStockPopup;
109                // vexLocationStockPopup will be a function only if cart contains location-only products
110                // otherwise, it will be undefined, hence, the condition won't be met
111                if (typeof productData === "object" && typeof vexLocationStockPopup === "function" && productData.hasOwnProperty('available_in_location') && productData.hasOwnProperty('cart_all_in_location')) {
112                    if (productData.cart_all_in_location === true && productData.available_in_location === false) {
113                        // noinspection JSUnresolvedFunction
114                        vexLocationStockPopup();
115                        return;
116                    }
117                }
118            }
119
120            cart.add(id, qty)
121                .then(function (r) {
122                    $scope.setCart(r);
123                    if(r[0].id === id && typeof r[0].offer !== 'object' && r[0].offer) {
124                        $scope.showToast('Produsul a fost adaugat cu oferta \'' + r[0].offer + '\'', 10000);
125                    } else if(r[0].id === id && typeof r[0].reminder == 'string'){
126
127                        $scope.showToast(r[0].reminder, 10000);
128                    } else  {
129                        $scope.showToast('Coș actualizat', 3000);
130                    }
131
132                    if (typeof defaultGTMScripts !== 'undefined') {
133                        defaultGTMScripts.addToCartEventGTM();
134                    }
135                    }
136                );
137        };
138
139        $scope.deleteFromCart = function (id, qty) {
140            cart.delete(id, qty)
141                .then(function (r) {
142                    $scope.setCart(r);
143                }
144            );
145        };
146
147        $scope.removeFromCart = function (id, qty) {
148            cart.remove(id, qty)
149                .then(function (r) {
150                    $scope.setCart(r);
151                }
152            );
153        };
154
155        $scope.emptyCart = function () {
156            cart.empty()
157                .then(function (r) {
158                    $scope.setCart(r);
159                }
160            );
161        };
162
163        $scope.addToWishlist = function (id, wId) {
164            fbq('track', 'AddToWishlist', {content_ids: [id.toString()]});
165
166            if ($scope.productInWishlist(id, wId)) {
167                return $scope.showToast('Produsul este deja in wishlist', 3000);
168            }
169
170            $scope.wListLoading = true
171
172            return $scope.wishlist.add(id, wId)
173                .then(function (r) {
174
175                    $scope.updateWishlists(function () {
176                        $scope.showToast(r.message, 3000);
177                        $scope.isInWishlist = $scope.productInWishlist(id, wId);
178                        $scope.getWishlists(id);
179                    });
180
181                    if (r.gtm !== "undefined" && typeof GTMCartu !== "undefined") {
182                        GTMCartu.addToWishlist(JSON.parse(r.gtm))
183                    }
184
185                    if (r.custobar !== "undefined" && typeof custobarScripts !== "undefined") {
186                        custobarScripts.addToWishlist(JSON.parse(r.custobar))
187                    }
188                });
189        };
190
191        $scope.removeFromWishlist = function (id, wId) {
192            fbq('trackCustom', 'RemoveFromWishlist', {
193                content_ids: [id.toString()],
194            });
195
196            if (!$scope.productInWishlist(id, wId)) {
197                return $scope.showToast('Produsul nu există în wishlist', 3000);
198            }
199            $scope.wListLoading = true
200
201            return $scope.wishlist.removeFrom(id, wId)
202                .then(function (r) {
203
204                    $scope.updateWishlists(function () {
205                        $scope.showToast(r.message, 3000);
206                        $scope.isInWishlist = $scope.productInWishlist(id, wId);
207                        $scope.getWishlists(id);
208                    });
209
210                    if (r.gtm !== "undefined" && typeof GTMCartu !== "undefined") {
211                        GTMCartu.removeFromWishlist(JSON.parse(r.gtm))
212                    }
213
214                    if (r.custobar !== "undefined" && typeof custobarScripts !== "undefined") {
215                        custobarScripts.removeFromWishlist(JSON.parse(r.custobar))
216                    }
217                });
218        };
219
220        $scope.wListLoading = false
221        $scope.updateWishlists = function (callback) {
222            $scope.wListLoading = true
223            $http.get('/wishlist/all').success(function (r) {
224                $scope.wishlists = window.Wishlists = r
225                callback()
226            }).finally(function () {
227                $scope.wListLoading = false
228            });
229        }
230
231        $scope.getWishlists = function(productId, addInFirstWishlist = false) {
232            if (window.Wishlists === undefined) {
233                return
234            }
235            // $scope.wishlist.getAll(productId)
236            //     .then(function(r){
237            //         $scope.wishlists = r;
238            //         $scope.isInWishlist = $scope.wishlists.some(function(wl) {
239            //             return wl.productInWishlist;
240            //         });
241            //
242            //         if (addInFirstWishlist === true) {
243            //             var wishlists = $scope.wishlists;
244            //             if (typeof wishlists === "undefined") return;
245            //             if (wishlists.length === 0) return;
246            //
247            //             $scope.addToWishlist(productId, wishlists[0].id);
248            //         }
249            //     }, function () {
250            //
251            //     });
252            $scope.wishlists = window.Wishlists
253
254            $scope.isInWishlist = $scope.wishlists.some(function (wishlist) {
255                return wishlist.products && wishlist.products.some(function (product) {
256                    return product.id == productId;
257                });
258            });
259
260            if (addInFirstWishlist === true) {
261                $scope.addToWishlist(productId, wishlists[0].id);
262            }
263        };
264
265        $scope.productInWishlist = function (productId, wlId) {
266            return $scope.wishlists.some(function (wishlist) {
267                return wishlist.id == wlId && wishlist.products && wishlist.products.some(function (product) {
268                    return product.id == productId;
269                });
270            });
271        };
272
273        $scope.$on('$includeContentLoaded', function (event, src) {
274            if (src && src.includes('/product/smarter-similar')) {
275                let attempts = 0;
276                const maxAttempts = 10;
277                const check = () => {
278                    if (defaultGTMScripts) {
279                        if (defaultGTMScripts.recommendationModuleViewedEvent()) {
280                        } else if (attempts < maxAttempts) {
281                            attempts++;
282                            setTimeout(check, 50);
283                        }
284                    }
285                };
286                check();
287            }
288        });
289
290        $scope.showToast = function (text, delay) {
291            $mdToast.show(
292                $mdToast.simple()
293                    .content(text)
294                    .position('top right')
295                    .hideDelay(delay)
296            );
297        };
298        $scope.showToast = function (text, delay) {
299
300            $mdToast.show(
301                {
302                    template: '<md-toast>' +
303                        '<div class="md-toast-content">' +
304                        $sce.trustAsHtml(text) +
305                        '</div>' +
306                        '</md-toast>',
307                    position: 'top right',
308                    hideDelay: delay
309                }
310            );
311        };
312    })
313    .controller('dMegaMenu', function ($scope, $element, $http, $q, $timeout, $rootScope) {
314        const _language = document.body.dataset.language || 'ro-RO';
315        $scope.isEn = _language === 'en-US';
316        var waitForRenderToOpenMenu = function() {
317            if($http.pendingRequests.length > 0 && (!$rootScope.mayRenderMenu) ) {
318                $timeout(waitForRenderToOpenMenu); // Wait for all templates to be loaded
319            } else {
320                $rootScope.mayRenderMenu = 1;
321            }
322        };
323        $timeout(waitForRenderToOpenMenu);
324        $scope.colonize = function (cat){
325            var height = cat.children.length * 20;
326            angular.forEach(cat.children, function(child){
327                if (child.children) {
328                    height += child.children.length * 17;
329                }
330            });
331            return (height > 585) ? 'colonizer':'';
332        };
333        $scope.getCats = function(url) {
334            var task = $q.defer();
335            var categorySourceUrl = "/site-category/menu?lang=" + _language;
336            if(url !== undefined) {
337                categorySourceUrl = url;
338            }
339            $http.get(categorySourceUrl)
340                .success(function (r) {
341                    task.resolve(r);
342                })
343                .error(function () {
344                    task.resolve('Could not load menu.');
345                });
346            return task.promise;
347        };
348        $scope.parseCats = function(url){
349            if (!$scope.categories) {
350                $scope.getCats(url)
351                    .then(function(r){
352                        $scope.categories = r;
353                        var pxH = r.length * 36,css = '.mega-menu.opened {max-height: '+(pxH + 12)+'px;}' +
354                                '.mega-menu > .container { max-height: '+pxH+'px; min-height: '+pxH+'px; }' +
355                                '.mega-menu > .container >.row { min-height: '+pxH+'px; }' +
356                                '.categoryContent .categorie{ min-height: '+pxH+'px; max-height: '+pxH+'px; }',
357                            head = document.head || document.getElementsByTagName('head')[0],
358                            style = document.createElement('style');
359                        style.type = 'text/css';
360                        if (style.styleSheet){
361                            style.styleSheet.cssText = css;
362                        } else {
363                            style.appendChild(document.createTextNode(css));
364                        }
365                        head.appendChild(style);
366                    })
367            }
368        };
369
370        $scope.showCategory = function (id) {
371            angular.forEach($scope.categories, function (cat) {
372                cat.show=cat.id==id;
373            });
374        };
375        $scope.backgroundImage = function (cat) {
376            if (cat.bgImg) {
377                return {
378                    'min-width': '100%',
379                    'position': 'relative',
380                    'z-index': 2,
381                    'background-color': 'white',
382                    'background-image': 'url(' + cat.bgImg + ')',
383                    'background-position': 'bottom right'
384                }
385            } else {
386                return {}
387            }
388        };
389        $scope.getCatName = function (cat) {
390            return (cat.friendly_name ? cat.friendly_name : cat.id);
391        }
392    })
393    .controller('mCatsMenu', function ($scope, $rootScope, $q, $http, $timeout) {
394        const _language = document.body.dataset.language || 'ro-RO';
395        var waitForRenderToOpenMenu = function() {
396            if($http.pendingRequests.length > 0 && (!$rootScope.mayRenderMenu) ) {
397                $timeout(waitForRenderToOpenMenu); // Wait for all templates to be loaded
398            } else {
399                $rootScope.mayRenderMenu = 1;
400            }
401        };
402        $timeout(waitForRenderToOpenMenu);
403        $scope.getCats = function(url) {
404            var task = $q.defer();
405            var categorySourceUrl = "/site-category/menu?lang=" + _language;
406            if(url !== undefined) {
407               categorySourceUrl = url;
408            }
409            $http.get(categorySourceUrl)
410                .success(function (r) {
411                    task.resolve(r);
412                })
413                .error(function () {
414                    task.resolve('Could not load menu.');
415                });
416            return task.promise;
417        };
418        $scope.parseCats = function (url) {
419            $scope.getCats(url).then(function(r){
420                var cats = r;
421                $scope.cats = [];
422                for (var i = 0; i < cats.length; i++) {
423                    var cat = cats[i];
424                    cat.parent = false;
425                    cat.ordering = parseInt(cat.ordering * 1000);
426                    $scope.cats.push(cat);
427                    if (cat.children) {
428                        for (var j = 0; j < cat.children.length; j++) {
429                            var child = cat.children[j];
430                            child.ordering = parseInt(cat.ordering) + parseInt(child.ordering) + 1;
431                            child.parent = cat.id;
432                        }
433                    }
434                }
435                $scope.catsOpen = [];
436                $scope.catsSelected = [];
437            });
438        };
439        $scope.catClass = function (cat) {
440            return (false == cat.parent ? 'parent' : 'child') +
441                ($scope.catsOpen.indexOf(cat.id) > -1 ? ' open' : '')
442        };
443        $scope.hasSubmenu = function (cat) {
444            return (!cat.parent) && (cat.children.length > 0);
445        };
446        $scope.toggleCat = function (cat) {
447            var index = $scope.catsOpen.indexOf(cat.id);
448            if (index > -1) {
449                $scope.catsOpen.splice(index, 1);
450            } else {
451                $scope.catsOpen.push(cat.id);
452            }
453        };
454        $scope.openModal = function (cat) {
455            $rootScope.selected = cat;
456        };
457    })
458    .controller("dBreadcrumbs", function ($scope, $window) {
459        $scope.siblingsModel = 0;
460        $scope.changeCat = function () {
461            var catLink = $('md-option[data-ng-value="' + $scope.siblingsModel + '"]').attr('data-target');
462            if (catLink != undefined) $window.location.href = catLink;
463        }
464    })
465    .controller("topSearch", function($scope, $rootScope, $timeout) {
466        $(document).on('focus', '#search-input', function(){
467            if ($rootScope.h.m.apple.tablet) {
468                // $('.cartu-top-bar').css({position:'absolute',top:(parseInt($(document).scrollTop())+45)+'px'});
469                $('#search-input').css({top: '-40px',width: '-webkit-calc(50% - 32px)',left: '5px'});
470            }
471        }).on('blur', '#search-input', function(){
472            if ($rootScope.h.m.apple.tablet) {
473                // $('.cartu-top-bar').css({position:'fixed',top:0});
474                $('#search-input').css({position:'relative',top:'0',left:'0',width:'100%'})
475            }
476        });
477        $scope.toggleSearch = function() {
478            if ($rootScope.bodyWidth > 768) { return; }
479            var src = $('.search-results-container'),
480                si = $('#search-input').eq(0),
481                ctb = $('.cartu-top-bar').eq(0),
482                ms = $('.meniu-stanga');
483            $('backdrop').remove();
484            if (src.hasClass('closed')) {
485                $rootScope.searchOpen = true;
486                src.removeClass('closed');
487                var mdb = $('<backdrop />');
488                setTimeout(function(){
489                    src.prepend(si);
490                    ctb.addClass('very-small-logo');
491                    si.focus();
492                    si.trigger('mouseup');
493                    setTimeout(function(){
494                        ms.append(mdb);
495                        setTimeout(function() {
496                            mdb.on('click tap', function () {
497                                $scope.toggleSearch();
498                            });
499                        }, 123);
500                    },123);
501                },123);
502            } else {
503                $rootScope.searchOpen = false;
504                src.addClass('closed');
505                $('.search-container').css({'z-index':'initial'});
506                $('.search-input').prepend(si);
507                ctb.removeClass('very-small-logo');
508                $('.search-container .material-icons').removeClass('bow-wow');
509            }
510
511        }
512    })
513;

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.