PageSourceSearch

https://4glaza.ru/local/templates/main/components/bitrix/main.user…/feedback_form_consent/user_consent.js

js 4glaza.ru collected 2026-09-24 21:56:05 UTC 12,150 bytes, 535 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			if (item.config.url)
116			{
117				return;
118			}
119
120			this.requestForItem(item);
121			e.preventDefault();
122		},
123		onSubmit: function (item, e)
124		{
125			this.isFormSubmitted = true;
126			if (this.check(item))
127			{
128				return true;
129			}
130			else
131			{
132				if (e)
133				{
134					e.preventDefault();
135				}
136
137				return false;
138			}
139		},
140		check: function (item)
141		{
142			if (item.inputNode.checked)
143			{
144				this.saveConsent(item);
145				return true;
146			}
147
148			this.requestForItem(item);
149			return false;
150		},
151		requestForItem: function (item)
152		{
153			this.setCurrent(item);
154			this.requestConsent(
155				item.config.id,
156				{
157					'sec': item.config.sec,
158					'replace': item.config.replace
159				},
160				this.onAccepted,
161				this.onRefused
162			);
163		},
164		setCurrent: function (item)
165		{
166			this.current = item;
167			this.autoSave = item.config.autoSave;
168			this.actionRequestUrl = item.config.actionUrl;
169		},
170		onAccepted: function ()
171		{
172			if (!this.current)
173			{
174				return;
175			}
176
177			var item = this.current;
178			this.saveConsent(
179				this.current,
180				function ()
181				{
182					BX.onCustomEvent(item, this.events.accepted, []);
183					BX.onCustomEvent(this, this.events.accepted, [item]);
184
185					this.isConsentSaved = true;
186
187					if (this.isFormSubmitted && item.formNode && !item.config.submitEventName)
188					{
189						BX.submit(item.formNode);
190					}
191				}
192			);
193
194			this.current.inputNode.checked = true;
195			this.current = null;
196		},
197		onRefused: function ()
198		{
199			BX.onCustomEvent(this.current, this.events.refused, []);
200			BX.onCustomEvent(this, this.events.refused, [this.current]);
201			this.current.inputNode.checked = false;
202			this.current = null;
203			this.isFormSubmitted = false;
204		},
205		initPopup: function ()
206		{
207			if (this.popup)
208			{
209				return;
210			}
211
212
213			this.popup = {
214
215			};
216		},
217		popup: {
218			isInit: false,
219			caller: null,
220			nodes: {
221				container: null,
222				shadow: null,
223				head: null,
224				loader: null,
225				content: null,
226				textarea: null,
227				buttonAccept: null,
228				buttonReject: null
229			},
230			onAccept: function ()
231			{
232				this.hide();
233				BX.onCustomEvent(this, 'accept', []);
234			},
235			onReject: function ()
236			{
237				this.hide();
238				BX.onCustomEvent(this, 'reject', []);
239			},
240			init: function ()
241			{
242				if (this.isInit)
243				{
244					return true;
245				}
246
247				var tmplNode = document.querySelector('script[data-bx-template]');
248				if (!tmplNode)
249				{
250					return false;
251				}
252
253				var popup = document.createElement('DIV');
254				popup.innerHTML = tmplNode.innerHTML;
255				popup = popup.children[0];
256				if (!popup)
257				{
258					return false;
259				}
260				document.body.insertBefore(popup, document.body.children[0]);
261
262				this.isInit = true;
263				this.nodes.container = popup;
264				this.nodes.shadow = this.nodes.container.querySelector('[data-bx-shadow]');
265				this.nodes.head = this.nodes.container.querySelector('[data-bx-head]');
266				this.nodes.loader = this.nodes.container.querySelector('[data-bx-loader]');
267				this.nodes.content = this.nodes.container.querySelector('[data-bx-content]');
268				this.nodes.textarea = this.nodes.container.querySelector('[data-bx-textarea]');
269				this.nodes.link = this.nodes.container.querySelector('[data-bx-link]');
270				this.nodes.linkA = this.nodes.link ? this.nodes.link.querySelector('a') : null;
271
272				this.nodes.buttonAccept = this.nodes.container.querySelector('[data-bx-btn-accept]');
273				this.nodes.buttonReject = this.nodes.container.querySelector('[data-bx-btn-reject]');
274				this.nodes.buttonAccept.textContent = BX.message(this.caller.msg.btnAccept);
275				this.nodes.buttonReject.textContent = BX.message(this.caller.msg.btnReject);
276				BX.bind(this.nodes.buttonAccept, 'click', this.onAccept.bind(this));
277				BX.bind(this.nodes.buttonReject, 'click', this.onReject.bind(this));
278
279				return true;
280			},
281			setTitle: function (text)
282			{
283				if (!this.nodes.head)
284				{
285					return;
286				}
287				this.nodes.head.innerHTML = text;
288			},
289			setContent: function (text)
290			{
291				if (!this.nodes.textarea)
292				{
293					return;
294				}
295				this.nodes.textarea.innerHTML = text;
296
297				this.nodes.link.style.display = 'none';
298				this.nodes.textarea.style.display = '';
299			},
300			setUrl: function (url)
301			{
302				if (!this.nodes.link)
303				{
304					return;
305				}
306
307				this.nodes.linkA.textContent = url;
308				this.nodes.linkA.href = url;
309
310				this.nodes.link.style.display = '';
311				this.nodes.textarea.style.display = 'none';
312			},
313			show: function (isContentVisible)
314			{
315				if (typeof isContentVisible == 'boolean')
316				{
317					this.nodes.loader.style.display = !isContentVisible ? '' : 'none';
318					this.nodes.content.style.display = isContentVisible ? '' : 'none';
319				}
320
321				this.nodes.container.style.display = '';
322			},
323			hide: function ()
324			{
325				this.nodes.container.style.display = 'none';
326			}
327		},
328
329		cache: {
330			list: [],
331			stringifyKey: function (key)
332			{
333				return BX.type.isString(key) ? key : JSON.stringify({'key': key});
334			},
335			set: function (key, data)
336			{
337				var item = this.get(key);
338				if (item)
339				{
340					item.data = data;
341				}
342				else
343				{
344					this.list.push({
345						'key': this.stringifyKey(key),
346						'data': data
347					});
348				}
349			},
350			getData: function (key)
351			{
352				var item = this.get(key);
353				return item ? item.data : null;
354			},
355			get: function (key)
356			{
357				key = this.stringifyKey(key);
358				var filtered = this.list.filter(function (item) {
359					return (item.key == key);
360				});
361				return (filtered.length > 0 ? filtered[0] : null);
362			},
363			has: function (key)
364			{
365				return !!this.get(key);
366			}
367		},
368		requestConsent: function (id, sendData, onAccepted, onRefused)
369		{
370			sendData = sendData || {};
371			sendData.id = id;
372
373			var cacheHash = this.cache.stringifyKey(sendData);
374
375			if (!this.popup.isInit)
376			{
377				this.popup.caller = this;
378				if (!this.popup.init())
379				{
380					return;
381				}
382
383				BX.addCustomEvent(this.popup, 'accept', onAccepted.bind(this));
384				BX.addCustomEvent(this.popup, 'reject', onRefused.bind(this));
385			}
386
387			if (this.current && this.current.config.text)
388			{
389				this.cache.set(cacheHash, this.current.config.text);
390			}
391
392			if (this.current && this.current.config.url)
393			{
394				this.setTextToPopup('', this.current.config.url);
395			}
396			else if (this.cache.has(cacheHash))
397			{
398				this.setTextToPopup(this.cache.getData(cacheHash));
399			}
400			else
401			{
402				this.popup.setTitle(BX.message(this.msg.loading));
403				this.popup.show(false);
404				this.sendActionRequest(
405					'getText', sendData,
406					function (data)
407					{
408						this.cache.set(cacheHash, data.text || '');
409						this.setTextToPopup(this.cache.getData(cacheHash));
410					},
411					function ()
412					{
413						this.popup.hide();
414						alert(BX.message(this.msg.errTextLoad));
415					}
416				);
417			}
418		},
419		setTextToPopup: function (text, url)
420		{
421			// set title from a first line from text.
422			var titleBar = '';
423			var textTitlePos = text.indexOf("\n");
424			var textTitleDotPos = text.indexOf(".");
425			textTitlePos = textTitlePos < textTitleDotPos ? textTitlePos : textTitleDotPos;
426			if (textTitlePos >= 0 && textTitlePos <= 100)
427			{
428				titleBar = text.substr(0, textTitlePos).trim();
429				titleBar  = titleBar.split(".").map(Function.prototype.call, String.prototype.trim).filter(String)[0];
430			}
431			this.popup.setTitle(titleBar ? titleBar : BX.message(this.msg.title));
432			if (url)
433			{
434				this.popup.setUrl(url);
435			}
436			else
437			{
438				this.popup.setContent(text);
439			}
440			this.popup.show(true);
441		},
442		saveConsent: function (item, callback)
443		{
444			this.setCurrent(item);
445
446			var data = {
447				'id': item.config.id,
448				'sec': item.config.sec,
449				'url': window.location.href
450			};
451			if (item.config.originId)
452			{
453				var originId = item.config.originId;
454				if (item.formNode && originId.indexOf('%') >= 0)
455				{
456					var inputs = item.formNode.querySelectorAll('input[type="text"], input[type="hidden"]');
457					inputs = BX.convert.nodeListToArray(inputs);
458					inputs.forEach(function (input) {
459						if (!input.name)
460						{
461							return;
462						}
463						originId = originId.replace('%' + input.name +  '%', input.value ? input.value : '');
464					});
465				}
466				data.originId = originId;
467			}
468			if (item.config.originatorId)
469			{
470				data.originatorId = item.config.originatorId;
471			}
472
473			BX.onCustomEvent(item, this.events.save, [data]);
474			BX.onCustomEvent(this, this.events.save, [item, data]);
475
476			if (this.isConsentSaved || !item.config.autoSave)
477			{
478				if (callback)
479				{
480					callback.apply(this, []);
481				}
482			}
483			else
484			{
485				this.sendActionRequest(
486					'saveConsent',
487					data,
488					callback,
489					callback
490				);
491			}
492		},
493		sendActionRequest: function (action, sendData, callbackSuccess, callbackFailure)
494		{
495			callbackSuccess = callbackSuccess || null;
496			callbackFailure = callbackFailure || null;
497
498			sendData.action = action;
499			sendData.sessid = BX.bitrix_sessid();
500			sendData.action = action;
501
502			BX.ajax({
503				url: this.actionRequestUrl,
504				method: 'POST',
505				data: sendData,
506				timeout: 10,
507				dataType: 'json',
508				processData: true,
509				onsuccess: BX.proxy(function(data){
510					data = data || {};
511					if(data.error)
512					{
513						callbackFailure.apply(this, [data]);
514					}
515					else if(callbackSuccess)
516					{
517						callbackSuccess.apply(this, [data]);
518					}
519				}, this),
520				onfailure: BX.proxy(function(){
521					var data = {'error': true, 'text': ''};
522					if (callbackFailure)
523					{
524						callbackFailure.apply(this, [data]);
525					}
526				}, this)
527			});
528		}
529	};
530
531	BX.ready(function () {
532		BX.UserConsent.loadFromForms();
533	});
534
535})();

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.