PageSourceSearch

https://thermos.ru/bitrix/templates/aspro-premier/js/phone/phone_input.js

js thermos.ru collected 2026-10-04 22:02:09 UTC 11,573 bytes, 309 lines download raw bytes

1window.appAspro = window.appAspro || {}
2
3if (!window.appAspro.phone) {
4    window.appAspro.phone = {
5        get checkIntlPhone() {
6            return typeof window.intlTelInput === 'function'
7        },
8
9        get checkInputmask() {
10            return typeof window.Inputmask === 'function'
11        },
12
13        get checkIntlTelInputUtils() {
14            return typeof window.intlTelInputUtils === 'function'
15        },
16
17        errorMap: ["JS_INVALID_NUMBER", "JS_INVALID_COUNTRY_CODE", "JS_TOO_SHORT", "JS_TOO_LONG", "JS_INVALID_NUMBER", "JS_INVALID_NUMBER"],
18
19        get config() {
20            return this._config || null
21        },
22
23        set config(config) {
24            this._config = config
25        },
26
27        init: function(els, config) {
28            const self = this;
29
30            const defaultConfig = {
31                useValidate: true,
32                mask: arAsproOptions.THEME.PHONE_MASK,
33                onlyCountries: arAsproOptions.THEME.PHONE_CITIES,
34                preferredCountries: arAsproOptions.THEME.PHONE_CITIES_FAVORITE,
35                async: true,
36            };
37
38            if (!config && this.config) {
39                config = this.config
40            }
41
42            config = $.extend({}, defaultConfig, (config || {}))
43
44            try {
45                if (this.checkIntlPhone) {
46                    // load all countries
47                    this.loadIntlPhoneCountries(config.async)
48                    .then((countries) => {
49                        config.countries = countries
50                        this.initIntlPhone(els, config)
51                    })
52                    .catch(() => {
53                        // error when all countries load
54                        throw 0;
55                    });
56                }
57                else {
58                    throw 0;
59                }
60            }
61            catch (e) {
62                // use inputmask
63                if (
64                    this.checkInputmask &&
65                    config.mask
66                ) {
67                    this.initNormalPhone(els, config)
68                }
69            }
70        },
71
72        initIntlPhone: function(els, config) {
73            let defaultConfig = {
74                utilsScript: arAsproOptions.SITE_TEMPLATE_PATH + '/vendor/intl.phone/js/utils.js',
75                preferredCountries: ['ru'],
76                autoPlaceholder: 'aggressive',
77                nationalMode: false,
78                onlyCountries: ['ru'],
79                formatOnDisplay: true,
80                autoHideDialCode: true,
81            };
82            config = config || {}
83            const pluginOptions = $.extend({}, defaultConfig, config)
84
85            if (pluginOptions.onlyCountries.length && typeof pluginOptions.onlyCountries === 'string') {
86                pluginOptions.onlyCountries = pluginOptions.onlyCountries.split(',')
87            }
88            if (pluginOptions.preferredCountries.length && typeof pluginOptions.preferredCountries === 'string') {
89                pluginOptions.preferredCountries = pluginOptions.preferredCountries.split(',')
90            }
91
92            // load utils.js without waiting window.load event
93            window.intlTelInputGlobals.loadUtils(pluginOptions.utilsScript);
94
95            const self = this
96
97            els.each(function(i, node) {
98                let iti = window.intlTelInput(node, pluginOptions)
99                let _this = $(node)
100                _this.data('iti', iti)
101
102                // if (!~_this.val().indexOf('+')) {
103                //     _this.val('+'+_this.val())
104                // }
105
106                _this.on("change", function () {
107
108                    const inputVal = _this.val();
109                    if (!~inputVal.indexOf('+') && inputVal.length) {
110                        _this.val('+'+inputVal)
111                    }
112
113                    if (typeof intlTelInputUtils !== 'undefined') {
114                        var currentText = iti.getNumber(intlTelInputUtils.numberFormat.E164);
115                        if (typeof currentText === 'string') {
116                            iti.setNumber(currentText);
117                        }
118                    }
119                    else {
120                        let t = setInterval(() => {
121                            if (typeof intlTelInputUtils !== 'undefined') {
122                                clearInterval(t);
123
124                                var currentText = iti.getNumber(intlTelInputUtils.numberFormat.E164);
125                                if (typeof currentText === 'string') {
126                                    iti.setNumber(currentText);
127                                }
128                            }
129                        }, 100);
130                    }
131
132                    /* paste fix */
133                    if (!iti.getSelectedCountryData().name) {
134                        _this.val(_this.val().replace('+8', '+7'))
135                        iti.setNumber(_this.val());
136                    }
137                    /* */
138                });
139
140                _this.on("input", function (e) {
141                    const _this = $(this)
142                    const telInput = _this.data('iti');
143                    let inputVal = _this.val();
144
145                    // console.log('input',
146                    // telInput.getValidationError(),
147                    //     self.errorMap[telInput.getValidationError()],
148                    //     inputVal,
149                    //     telInput.isValidNumber(),
150                    //     telInput.getSelectedCountryData(),
151                    //     e
152                    // );
153
154                    if (inputVal.length >= 1 && !inputVal.includes('+')) {
155                        _this.val('+'+inputVal);
156                        inputVal = _this.val();
157                    }
158                    if (inputVal.length > 3) {
159                        if (!telInput.getSelectedCountryData().name) {
160                            _this.val(inputVal.replace('+8', '+7'))
161                        } else {
162                            // let inputCountryLength = _this.attr('placeholder').replace(/\D/g,'').length
163                            // let inputValLength = inputVal.replace(/\D/g,'').length
164                            // if (inputValLength > inputCountryLength) {
165                            //     // _this.val(inputVal.slice(0, inputCountryLength - inputValLength))
166                            // }
167                        }
168                    }
169
170                    if (typeof intlTelInputUtils !== 'undefined') {
171                        var currentText = iti.getNumber(intlTelInputUtils.numberFormat.E164);
172                        if (typeof currentText === 'string') {
173                            iti.setNumber(currentText);
174                            // _this[0].selectionStart =_this[0].selectionEnd = pos
175                        }
176                    }
177                });
178
179                _this.on("keypress", function (e) {
180                    let key = String.fromCharCode(!e.charCode ? e.which : e.charCode);
181
182                    if (e.target.value === '') {
183                        e.target.value = '+'
184                    }
185                    return /\d/.test(key)
186                });
187
188                //manual change trigger
189                _this.trigger('change')
190            });
191
192            this.bindPhoneMask(els)
193            if (config.useValidate) {
194                this.addValidationIntlPhone()
195            }
196        },
197
198        initNormalPhone: function(els, config) {
199            let base_mask = config.mask.replace( /(\d)/g, '_' );
200            els.inputmask("mask", { mask: config.mask });
201            els.blur(function() {
202                if( $(this).val() == base_mask || $(this).val() == '' ) {
203                    if( $(this).hasClass('required') ) {
204                        $(this).parent().find('label.error').html(BX.message('JS_REQUIRED'));
205                    }
206                }
207            });
208
209            if (config.useValidate) {
210                this.addValidationPhone()
211            }
212        },
213
214        addValidationIntlPhone: function() {
215            $.validator.addMethod(
216                "intl_phone",
217                function (value, element, param) {
218                    const telInput = $(element).data('iti');
219                    let valid = telInput.isValidNumber();
220                    if (value.length) {
221                        if (valid) {
222                            element.classList.remove('error')
223                        } else {
224                            element.classList.add('error')
225                        }
226                    } else {
227                        valid = true
228                    }
229                    return valid
230                },
231                function (param, element) {
232                    const telInput = $(element).data('iti');
233
234                    return BX.message(param[telInput.getValidationError()]) || BX.message(param[0])
235                }
236            );
237            $.validator.addClassRules({
238                phone: {
239                    intl_phone: this.errorMap,
240                },
241                phone_input: {
242                    intl_phone: this.errorMap,
243                },
244            })
245        },
246
247        addValidationPhone: function() {
248            if (arAsproOptions.THEME.VALIDATE_PHONE_MASK) {
249                $.validator.addClassRules({
250                    phone: {
251                        regexp: arAsproOptions.THEME.VALIDATE_PHONE_MASK,
252                    },
253                    phone_input: {
254                        regexp: arAsproOptions.THEME.VALIDATE_PHONE_MASK,
255                    },
256                })
257            }
258        },
259
260        bindPhoneMask: function(els) {
261            let _this = this
262            els.each(function(i, node) {
263                this.addEventListener('countrychange', function(e) {
264                    e.stopImmediatePropagation()
265                    const _this = $(e.target);
266
267                    if (typeof e.detail === 'object' && e.detail) {
268                        if (e.detail.type === "_selectListItem") {
269                            _this.trigger('input')
270                            _this.valid()
271                        }
272                    }
273                })
274            })
275        },
276
277        loadIntlPhoneCountries: function(async = true) {
278            if (!this.loadIntlPhoneCountriesPromise) {
279                this.loadIntlPhoneCountriesPromise = new Promise(
280					(resolve, reject) => {
281                        if (typeof window.allCountries !== 'undefined') {
282                            resolve(window.allCountries);
283                        }
284                        else {
285                            BX.ajax({
286                                url: arAsproOptions.SITE_TEMPLATE_PATH + '/vendor/intl.phone/js/data.php',
287                                data: {},
288                                method: 'GET',
289                                dataType: 'script',
290                                async: async,
291                                scriptsRunFirst: true,
292                                start: true,
293                                cache: false,
294                                onsuccess: function() {
295                                    resolve(window.allCountries);
296                                },
297                                onfailure: function() {
298                                    reject();
299                                }
300                            });
301                        }
302					}
303				);
304            }
305
306            return this.loadIntlPhoneCountriesPromise;
307        },
308    }
309}

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.