PageSourceSearch

https://servis-ford.ru/bitrix/templates/aspro-allcorp3/components/…in.userconsent.request/main/user_consent.js

js servis-ford.ru collected 2026-10-08 18:41:25 UTC 14,706 bytes, 497 lines download raw bytes

1;(function () {
2  function UserConsentControl(params) {
3    this.caller = params.caller;
4    this.formNode = params.formNode;
5    this.controlNode = params.controlNode;
6    this.inputNode = params.inputNode;
7    this.config = params.config;
8  }
9  UserConsentControl.prototype = {
10
11  };
12
13  BX.UserConsent = {
14    msg: {
15      'title': 'MAIN_USER_CONSENT_REQUEST_TITLE',
16      'btnAccept': 'MAIN_USER_CONSENT_REQUEST_BTN_ACCEPT',
17      'btnReject': 'MAIN_USER_CONSENT_REQUEST_BTN_REJECT',
18      'loading': 'MAIN_USER_CONSENT_REQUEST_LOADING',
19      'errTextLoad': 'MAIN_USER_CONSENT_REQUEST_ERR_TEXT_LOAD'
20    },
21    events: {
22      'save': 'main-user-consent-request-save',
23      'refused': 'main-user-consent-request-refused',
24      'accepted': 'main-user-consent-request-accepted'
25    },
26    current: null,
27    autoSave: false,
28    isFormSubmitted: false,
29    isConsentSaved: false,
30    attributeControl: 'data-bx-user-consent',
31    load: function (context) {
32      var item = this.find(context)[0];
33      if (!item) {
34        return null;
35      }
36
37      this.bind(item);
38      return item;
39    },
40    loadAll: function (context, limit) {
41      this.find(context, limit).forEach(this.bind, this);
42    },
43    loadFromForms: function () {
44      var formNodes = document.getElementsByTagName('FORM');
45      formNodes = BX.convert.nodeListToArray(formNodes);
46      formNodes.forEach(this.loadAll, this);
47    },
48    find: function (context) {
49      if (!context) {
50        return [];
51      }
52
53      var controlNodes = context.querySelectorAll('[' + this.attributeControl + ']:not(.consent-inited)');
54      controlNodes = BX.convert.nodeListToArray(controlNodes);
55      return controlNodes.map(this.createItem.bind(this, context)).filter(function (item) { return !!item });
56    },
57    bind: function (item) {
58      if (item.config.submitEventName) {
59        BX.addCustomEvent(item.config.submitEventName, this.onSubmit.bind(this, item));
60      }
61      else if (item.formNode) {
62        BX.bind(item.formNode, 'submit', this.onSubmit.bind(this, item));
63      }
64
65      BX.bind(item.controlNode, 'click', this.onClick.bind(this, item));
66      item.controlNode.classList.add('consent-inited');
67
68      BX.addCustomEvent('onUserConsentHide', () => {
69        if (this.popup.nodes.container) {
70          this.popup.hide();
71        }
72      });
73    },
74    createItem: function (context, controlNode) {
75      var inputNode = controlNode.querySelector('input[type="checkbox"]');
76      if (!inputNode) {
77        return;
78      }
79
80      try {
81        var config = JSON.parse(controlNode.getAttribute(this.attributeControl));
82        var parameters = {
83          'formNode': null,
84          'controlNode': controlNode,
85          'inputNode': inputNode,
86          'config': config
87        };
88
89        if (context.tagName == 'FORM') {
90          parameters.formNode = context;
91        }
92        else {
93          parameters.formNode = BX.findParent(inputNode, { tagName: 'FORM' })
94        }
95
96        parameters.caller = this;
97        return new UserConsentControl(parameters);
98      }
99      catch (e) {
100        return null;
101      }
102    },
103    onClick: function (item, e) {
104      if (item.config.url) {
105        return;
106      }
107
108      this.requestForItem(item);
109      e.preventDefault();
110    },
111    onSubmit: function (item, e) {
112      this.isFormSubmitted = true;
113      if (this.check(item)) {
114        return true;
115      }
116      else {
117        if (e) {
118          e.preventDefault();
119        }
120
121        return false;
122      }
123    },
124    check: function (item) {
125      if (item.inputNode.checked) {
126        this.saveConsent(item);
127        return true;
128      }
129
130      this.requestForItem(item);
131      return false;
132    },
133    requestForItem: function (item) {
134      this.setCurrent(item);
135      this.requestConsent(
136        item.config.id,
137        {
138          'sec': item.config.sec,
139          'replace': item.config.replace
140        },
141        this.onAccepted,
142        this.onRefused
143      );
144    },
145    setCurrent: function (item) {
146      this.current = item;
147      this.autoSave = item.config.autoSave;
148      this.actionRequestUrl = item.config.actionUrl;
149    },
150    onAccepted: function () {
151      if (!this.current) {
152        return;
153      }
154
155      var item = this.current;
156      this.saveConsent(
157        this.current,
158        function () {
159          BX.onCustomEvent(item, this.events.accepted, []);
160          BX.onCustomEvent(this, this.events.accepted, [item]);
161
162          this.isConsentSaved = true;
163
164          if (this.isFormSubmitted && item.formNode && !item.config.submitEventName) {
165            BX.submit(item.formNode);
166          }
167        }
168      );
169
170      this.current.inputNode.checked = true;
171
172      if (
173        typeof $.validator === "function"
174        && $(this.current.formNode).data("validator")
175      ) {
176        $(this.current.inputNode).valid();
177      } else {
178        let eventChange = new Event("change");
179        this.current.inputNode.dispatchEvent(eventChange);
180      }
181
182      this.current = null;
183    },
184    onRefused: function () {
185      BX.onCustomEvent(this.current, this.events.refused, []);
186      BX.onCustomEvent(this, this.events.refused, [this.current]);
187      this.current.inputNode.checked = false;
188
189      if (
190        typeof $.validator === "function"
191        && $(this.current.formNode).data("validator")
192      ) {
193        $(this.current.inputNode).valid();
194      } else {
195        let eventChange = new Event("change");
196        this.current.inputNode.dispatchEvent(eventChange);
197      }
198
199      this.current = null;
200      this.isFormSubmitted = false;
201    },
202    initPopup: function () {
203      if (this.popup) {
204        return;
205      }
206
207
208      this.popup = {
209
210      };
211    },
212    popup: {
213      isInit: false,
214      caller: null,
215      nodes: {
216        container: null,
217        shadow: null,
218        head: null,
219        loader: null,
220        content: null,
221        textarea: null,
222        buttonAccept: null,
223        buttonReject: null
224      },
225      onAccept: function () {
226        this.hide();
227        BX.onCustomEvent(this, 'accept', []);
228      },
229      onReject: function () {
230        this.hide();
231        BX.onCustomEvent(this, 'reject', []);
232      },
233      init: function () {
234        if (this.isInit) {
235          return true;
236        }
237
238        var tmplNode = document.querySelector('div[data-bx-template]');
239        if (!tmplNode) {
240          return false;
241        }
242
243        var popup = document.createElement('DIV');
244        popup.innerHTML = tmplNode.innerHTML;
245        popup = popup.children[0];
246        if (!popup) {
247          return false;
248        }
249        document.body.insertBefore(popup, document.body.children[0]);
250
251        this.isInit = true;
252        this.nodes.container = popup;
253        this.nodes.shadow = this.nodes.container.querySelector('[data-bx-shadow]');
254        this.nodes.head = this.nodes.container.querySelector('[data-bx-head]');
255        this.nodes.loader = this.nodes.container.querySelector('[data-bx-loader]');
256        this.nodes.content = this.nodes.container.querySelector('[data-bx-content]');
257        this.nodes.textarea = this.nodes.container.querySelector('[data-bx-textarea]');
258        this.nodes.link = this.nodes.container.querySelector('[data-bx-link]');
259        this.nodes.linkA = this.nodes.link ? this.nodes.link.querySelector('a') : null;
260
261        this.nodes.buttonAccept = this.nodes.container.querySelector('[data-bx-btn-accept]');
262        this.nodes.buttonReject = this.nodes.container.querySelector('[data-bx-btn-reject]');
263        this.nodes.buttonAccept.textContent = BX.message(this.caller.msg.btnAccept);
264        this.nodes.buttonReject.textContent = BX.message(this.caller.msg.btnReject);
265        BX.bind(this.nodes.buttonAccept, 'click', this.onAccept.bind(this));
266        BX.bind(this.nodes.buttonReject, 'click', this.onReject.bind(this));
267
268        return true;
269      },
270      setTitle: function (text) {
271        if (!this.nodes.head) {
272          return;
273        }
274        this.nodes.head.innerHTML = text;
275      },
276      setContent: function (text) {
277        if (!this.nodes.textarea) {
278          return;
279        }
280        this.nodes.textarea.innerHTML = text;
281
282        this.nodes.link.style.display = 'none';
283        this.nodes.textarea.style.display = '';
284      },
285      setUrl: function (url) {
286        if (!this.nodes.link) {
287          return;
288        }
289
290        this.nodes.linkA.textContent = url;
291        this.nodes.linkA.href = url;
292
293        this.nodes.link.style.display = '';
294        this.nodes.textarea.style.display = 'none';
295      },
296      show: function (isContentVisible) {
297        if (typeof isContentVisible == 'boolean') {
298          this.nodes.loader.style.display = !isContentVisible ? '' : 'none';
299          this.nodes.content.style.display = isContentVisible ? '' : 'none';
300        }
301
302        this.nodes.container.style.display = '';
303
304        // Delay blur to ensure focus is properly removed on iOS Safari,
305        // where calling blur() synchronously may not dismiss the keyboard.
306        setTimeout(() => {
307          if (document.activeElement instanceof HTMLElement) {
308            document.activeElement.blur();
309          }
310        }, 0);
311        document.body.classList.add('overflow-block');
312      },
313      hide: function () {
314        this.nodes.container.style.display = 'none';
315        document.querySelector('body').classList.remove('overflow-block');
316      }
317    },
318
319    cache: {
320      list: [],
321      stringifyKey: function (key) {
322        return BX.type.isString(key) ? key : JSON.stringify({ 'key': key });
323      },
324      set: function (key, data) {
325        var item = this.get(key);
326        if (item) {
327          item.data = data;
328        }
329        else {
330          this.list.push({
331            'key': this.stringifyKey(key),
332            'data': data
333          });
334        }
335      },
336      getData: function (key) {
337        var item = this.get(key);
338        return item ? item.data : null;
339      },
340      get: function (key) {
341        key = this.stringifyKey(key);
342        var filtered = this.list.filter(function (item) {
343          return (item.key == key);
344        });
345        return (filtered.length > 0 ? filtered[0] : null);
346      },
347      has: function (key) {
348        return !!this.get(key);
349      }
350    },
351    requestConsent: function (id, sendData, onAccepted, onRefused) {
352      sendData = sendData || {};
353      sendData.id = id;
354
355      var cacheHash = this.cache.stringifyKey(sendData);
356
357      if (!this.popup.isInit) {
358        this.popup.caller = this;
359        if (!this.popup.init()) {
360          return;
361        }
362
363        BX.addCustomEvent(this.popup, 'accept', onAccepted.bind(this));
364        BX.addCustomEvent(this.popup, 'reject', onRefused.bind(this));
365      }
366
367      if (this.current && this.current.config.text) {
368        this.cache.set(cacheHash, this.current.config.text);
369      }
370
371      if (this.current && this.current.config.url) {
372        this.setTextToPopup('', this.current.config.url);
373      }
374      else if (this.cache.has(cacheHash)) {
375        this.setTextToPopup(this.cache.getData(cacheHash));
376      }
377      else {
378        this.popup.setTitle(BX.message(this.msg.loading));
379        this.popup.show(false);
380        BX.ajax.runAction('main.agreement.get', {
381          data: sendData,
382        })
383          .then((response) => {
384            this.cache.set(cacheHash, response.data.content.html || '');
385            this.setTextToPopup(this.cache.getData(cacheHash));
386          })
387          .catch(() => {
388            this.popup.hide();
389            alert(BX.message(this.msg.errTextLoad));
390          })
391      }
392    },
393    setTextToPopup: function (text, url) {
394      // set title from a first line from text.
395      var titleBar = '';
396      var textTitlePos = text.indexOf("\n");
397      var textTitleDotPos = text.indexOf(".");
398      textTitlePos = textTitlePos < textTitleDotPos ? textTitlePos : textTitleDotPos;
399      if (textTitlePos >= 0 && textTitlePos <= 100) {
400        titleBar = text.substr(0, textTitlePos).trim();
401        titleBar = titleBar.split(".").map(Function.prototype.call, String.prototype.trim).filter(String)[0];
402      }
403      this.popup.setTitle(BX.message(this.msg.title));
404      if (url) {
405        this.popup.setUrl(url);
406      }
407      else {
408        this.popup.setContent(text);
409      }
410      this.popup.show(true);
411    },
412    saveConsent: function (item, callback) {
413      this.setCurrent(item);
414
415      var data = {
416        'id': item.config.id,
417        'sec': item.config.sec,
418        'url': window.location.href
419      };
420      if (item.config.originId) {
421        var originId = item.config.originId;
422        if (item.formNode && originId.indexOf('%') >= 0) {
423          var inputs = item.formNode.querySelectorAll('input[type="text"], input[type="hidden"]');
424          inputs = BX.convert.nodeListToArray(inputs);
425          inputs.forEach(function (input) {
426            if (!input.name) {
427              return;
428            }
429            originId = originId.replace('%' + input.name + '%', input.value ? input.value : '');
430          });
431        }
432        data.originId = originId;
433      }
434      if (item.config.originatorId) {
435        data.originatorId = item.config.originatorId;
436      }
437
438      BX.onCustomEvent(item, this.events.save, [data]);
439      BX.onCustomEvent(this, this.events.save, [item, data]);
440
441      if (this.isConsentSaved || !item.config.autoSave) {
442        if (callback) {
443          callback.apply(this, []);
444        }
445      }
446      else {
447        this.sendActionRequest(
448          'saveConsent',
449          data,
450          callback,
451          callback
452        );
453      }
454    },
455    sendActionRequest: function (action, sendData, callbackSuccess, callbackFailure) {
456      callbackSuccess = callbackSuccess || null;
457      callbackFailure = callbackFailure || null;
458
459      sendData.action = action;
460      sendData.sessid = BX.bitrix_sessid();
461      sendData.action = action;
462
463      BX.ajax({
464        url: this.actionRequestUrl,
465        method: 'POST',
466        data: sendData,
467        timeout: 10,
468        dataType: 'json',
469        processData: true,
470        onsuccess: BX.proxy(function (data) {
471          data = data || {};
472          if (data.error) {
473            callbackFailure.apply(this, [data]);
474          }
475          else if (callbackSuccess) {
476            callbackSuccess.apply(this, [data]);
477          }
478        }, this),
479        onfailure: BX.proxy(function () {
480          var data = { 'error': true, 'text': '' };
481          if (callbackFailure) {
482            callbackFailure.apply(this, [data]);
483          }
484        }, this)
485      });
486    }
487  };
488
489  BX.ready(function () {
490    BX.UserConsent.loadFromForms();
491  });
492
493  BX.addCustomEvent('onUserConsentReload', () => {
494    BX.UserConsent.loadFromForms();
495  });
496
497})();

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.