PageSourceSearch

https://offire.ru/bitrix/templates/winn_default/components/bitrix/…/.default/user_consent.js?160075994812766

js offire.ru collected 2026-10-07 18:04:52 UTC 12,766 bytes, 533 lines download raw bytes

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

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.