PageSourceSearch

https://megre.ru/local/templates/megre/components/bitrix/menu/header.user.bar/script.js

js megre.ru collected 2026-10-04 00:14:01 UTC 7,968 bytes, 223 lines download raw bytes

1/*
2 * ********************************************************************************************
3 *
4 *      file             : script.js
5 *      modified         : 04.08.2022
6 *
7 *      author           : Denis Artamonov
8 *      authorEmail      : [email protected]
9 *      authorUrl        : https://artamonov.pro
10 *
11 *      company          : Webco - web solutions development company
12 *      companyEmail     : [email protected]
13 *      companyUrl       : https://webco.io
14 *      partnerUrl       : https://www.1c-bitrix.ru/partners/1469188.php
15 *      marketplaceUrl   : https://marketplace.1c-bitrix.ru/partners/detail.php?ID=1469188.php
16 *
17 *      copyright        : 2018 - 2023
18 *
19 * ********************************************************************************************
20 */
21
22'use strict'
23
24const UserBar = {
25    init: {},
26    popup: {},
27    favorites: {},
28    basket: {},
29}
30
31UserBar.run = function (params = {}) {
32    UserBar.param = params
33    UserBar.bxPanel = document.getElementById('bx-panel')
34    UserBar.header = document.querySelector('header.header')
35    UserBar.basket.total = document.querySelector('.header-user-bar [data-code="shopping-bag"] .header-user-bar-count')
36    UserBar.init.favorites()
37    UserBar.init.search()
38}
39
40UserBar.init.favorites = function () {
41    UserBar.favorites.icon = document.querySelector('.header-user-bar [data-code="favorites"]')
42    UserBar.favorites.total = document.querySelector('.header-user-bar [data-code="favorites"] .header-user-bar-count')
43    UserBar.favorites.popup = document.querySelector('.user-bar-popup[data-code="favorites"]')
44    UserBar.favorites.popupContent = UserBar.favorites.popup.querySelector('.user-bar-popup-content')
45    UserBar.favorites.popupClose = document.querySelector('.user-bar-popup[data-code="favorites"] .user-bar-popup-header-close')
46    UserBar.favorites.icon.onclick = function () {
47        if (UserBar.popup.favorites.displayed === false) {
48            UserBar.popup.favorites.show()
49        } else {
50            UserBar.popup.favorites.hide()
51        }
52    }
53    UserBar.favorites.popupClose.onclick = function () {
54        UserBar.popup.favorites.hide()
55    }
56}
57
58UserBar.init.search = function () {
59    document.querySelector('.header-user-bar [data-code="search"]').onclick = function () {
60        if (Search.displayed === true) {
61            Search.hide()
62        } else {
63            Search.show()
64        }
65    }
66}
67
68UserBar.basket.render = function () {
69    if (typeof BASKET === 'undefined') {
70        return
71    }
72    let total = Object.keys(BASKET).length
73    if (total === 0) {
74        UserBar.basket.total.style.display = 'none'
75        return
76    }
77    UserBar.basket.total.innerText = total
78    UserBar.basket.total.style.display = 'flex'
79}
80
81UserBar.basket.add = function (id) {
82    const quantity = document.querySelector('.user-bar-popup-product-wrapper[data-id="' + id + '"] .user-bar-popup-product-change-quantity span')
83    let total = +quantity.innerText || 1
84    BX.ajax.post(
85        SITE_TEMPLATE_PATH + '/ajax/product.php',
86        {
87            action: 'addToBasket',
88            ID: id,
89            QUANTITY: total,
90        },
91        function (response) {
92            response = JSON.parse(response)
93            // Если находимся на странице оформления заказа, тогда обновим компонент
94            if (window.location.pathname === '/order/') {
95                BX.showWait()
96                window.location.reload()
97            }
98            if (response.status !== 'success') {
99                if (response.hasOwnProperty('error')) {
100                    for (let i = 0, l = response.error.length; i < l; i++) {
101                        BX.UI.Notification.Center.notify({
102                            content: response.error[i],
103                            autoHideDelay: 2000
104                        })
105                    }
106                }
107                return
108            }
109            const items = document.querySelectorAll('.product-preview-wrapper[data-id="' + id + '"]')
110            if (!BASKET.hasOwnProperty(id)) {
111                BASKET[id] = {
112                    ITEM_ID: response['itemId'],
113                    ID: id,
114                    QUANTITY: total,
115                }
116                if (items.length > 0) {
117                    for (let i = 0; i < items.length; i++) {
118                        items[i].setAttribute('data-in-basket', 'Y')
119                        items[i].querySelector('.product-added-wrapper span').innerText = total
120                    }
121                }
122                UserBar.basket.render()
123                return
124            }
125            BASKET[id]['QUANTITY'] = +BASKET[id]['QUANTITY'] + total
126            if (items.length > 0) {
127                for (let i = 0; i < items.length; i++) {
128                    items[i].querySelector('.product-added-wrapper span').innerText = BASKET[id]['QUANTITY']
129                }
130            }
131        }
132    )
133}
134
135UserBar.basket.changeQuantity = function (id, direction) {
136    const quantity = document.querySelector('.user-bar-popup-product-wrapper[data-id="' + id + '"] .user-bar-popup-product-change-quantity span')
137    let total = +quantity.innerText || 1
138    if (direction === 'reduce') {
139        if (total > 1) {
140            total -= 1
141        }
142    } else {
143        total += 1
144    }
145    quantity.innerText = total
146}
147
148UserBar.favorites.render = function () {
149    if (typeof FAVORITES === 'undefined') {
150        return
151    }
152    let total = Object.keys(FAVORITES).length
153    if (total === 0) {
154        UserBar.favorites.total.style.display = 'none'
155        UserBar.popup.favorites.hide()
156        return
157    }
158    UserBar.favorites.total.innerText = total
159    UserBar.favorites.total.style.display = 'flex'
160    const items = UserBar.favorites.popup.querySelectorAll('[data-id]')
161    const exists = {}
162    for (let i = 0; i < items.length; i++) {
163        if (FAVORITES.hasOwnProperty(items[i].dataset.id)) {
164            exists[items[i].dataset.id] = true
165            continue
166        }
167        items[i].remove()
168    }
169    for (let id in FAVORITES) {
170        if (exists.hasOwnProperty(id)) {
171            continue
172        }
173        const product = document.createElement('div')
174        BX.ajax.post(
175            UserBar.param['TEMPLATE_PATH'] + '/ajax.php',
176            {
177                action: 'getTemplate',
178                ID: id
179            },
180            function (response) {
181                product.innerHTML = response
182                UserBar.favorites.popupContent.appendChild(product)
183            }
184        )
185    }
186}
187
188UserBar.favorites.delete = function (id) {
189    BX.ajax.post(
190        SITE_TEMPLATE_PATH + '/ajax/product.php',
191        {
192            action: 'deleteFromFavorites',
193            ID: id
194        },
195        function (response) {
196            response = JSON.parse(response)
197            if (response.status !== 'success') {
198                return
199            }
200            $('.user-bar-popup-product-wrapper[data-id="' + id + '"]').remove();
201            delete FAVORITES[id]
202            UserBar.favorites.render()
203            const items = document.querySelectorAll('[data-id="' + id + '"][data-in-favorites="Y"]')
204            if (items.length > 0) {
205                for (let i = 0; i < items.length; i++) {
206                    items[i].removeAttribute('data-in-favorites')
207                }
208            }
209        }
210    )
211}
212
213UserBar.popup.favorites = {
214    displayed: false,
215    show: function () {
216        UserBar.favorites.popup.style.display = 'block'
217        UserBar.popup.favorites.displayed = true
218    },
219    hide: function () {
220        UserBar.favorites.popup.style.display = 'none'
221        UserBar.popup.favorites.displayed = false
222    },
223}

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.