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 257 this.isInit = true; 258 this.nodes.container = popup; 259 this.nodes.shadow = this.nodes.container.querySelector('[data-bx-shadow]'); 260 this.nodes.head = this.nodes.container.querySelector('[data-bx-head]'); 261 this.nodes.loader = this.nodes.container.querySelector('[data-bx-loader]'); 262 this.nodes.content = this.nodes.container.querySelector('[data-bx-content]'); 263 this.nodes.textarea = this.nodes.container.querySelector('[data-bx-textarea]'); 264 265 this.nodes.buttonAccept = this.nodes.container.querySelector('[data-bx-btn-accept]'); 266 this.nodes.buttonReject = this.nodes.container.querySelector('[data-bx-btn-reject]'); 267 this.nodes.buttonAccept.textContent = BX.message(this.caller.msg.btnAccept); 268 this.nodes.buttonReject.textContent = BX.message(this.caller.msg.btnReject); 269 BX.bind(this.nodes.buttonAccept, 'click', this.onAccept.bind(this)); 270 BX.bind(this.nodes.buttonReject, 'click', this.onReject.bind(this)); 271 272 return true; 273 }, 274 setTitle: function (text) 275 { 276 if (!this.nodes.head) 277 { 278 return; 279 } 280 this.nodes.head.textContent = text; 281 }, 282 setContent: function (text) 283 { 284 if (!this.nodes.textarea) 285 { 286 return; 287 } 288 this.nodes.textarea.textContent = text; 289 }, 290 show: function (isContentVisible) 291 { 292 if (typeof isContentVisible == 'boolean') 293 { 294 this.nodes.loader.style.display = !isContentVisible ? '' : 'none'; 295 this.nodes.content.style.display = isContentVisible ? '' : 'none'; 296 } 297 298 this.nodes.container.style.display = ''; 299 }, 300 hide: function () 301 { 302 this.nodes.container.style.display = 'none'; 303 } 304 }, 305 306 cache: { 307 list: [], 308 stringifyKey: function (key) 309 { 310 return BX.type.isString(key) ? key : JSON.stringify({'key': key}); 311 },
312 set: function (key, data) 313 { 314 var item = this.get(key); 315 if (item) 316 { 317 item.data = data; 318 } 319 else 320 { 321 this.list.push({ 322 'key': this.stringifyKey(key), 323 'data': data 324 }); 325 } 326 }, 327 getData: function (key) 328 { 329 var item = this.get(key); 330 return item ? item.data : null; 331 }, 332 get: function (key) 333 { 334 key = this.stringifyKey(key); 335 var filtered = this.list.filter(function (item) { 336 return (item.key == key); 337 }); 338 return (filtered.length > 0 ? filtered[0] : null); 339 }, 340 has: function (key) 341 { 342 return !!this.get(key); 343 } 344 }, 345 requestConsent: function (id, sendData, onAccepted, onRefused) 346 { 347 sendData = sendData || {}; 348 sendData.id = id; 349 350 var cacheHash = this.cache.stringifyKey(sendData); 351 352 if (!this.popup.isInit) 353 { 354 this.popup.caller = this; 355 if (!this.popup.init()) 356 { 357 return; 358 } 359 360 BX.addCustomEvent(this.popup, 'accept', onAccepted.bind(this)); 361 BX.addCustomEvent(this.popup, 'reject', onRefused.bind(this)); 362 } 363 364 if (this.current && this.current.config.text) 365 { 366 this.cache.set(cacheHash, this.current.config.text); 367 } 368 369 if (this.cache.has(cacheHash)) 370 { 371 this.setTextToPopup(this.cache.getData(cacheHash)); 372 } 373 else 374 { 375 this.popup.setTitle(BX.message(this.msg.loading)); 376 this.popup.show(false); 377 this.sendActionRequest( 378 'getText', sendData, 379 function (data) 380 { 381 this.cache.set(cacheHash, data.text || ''); 382 this.setTextToPopup(this.cache.getData(cacheHash)); 383 }, 384 function () 385 { 386 this.popup.hide(); 387 alert(BX.message(this.msg.errTextLoad)); 388 } 389 ); 390 } 391 }, 392 setTextToPopup: function (text) 393 { 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 { 401 titleBar = text.substr(0, textTitlePos).trim(); 402 titleBar = titleBar.split(".").map(Function.prototype.call, String.prototype.trim).filter(String)[0]; 403 } 404 this.popup.setTitle(titleBar ? titleBar : BX.message(this.msg.title)); 405 this.popup.setContent(text); 406 this.popup.show(true); 407 }, 408 saveConsent: function (item, callback) 409 { 410 this.setCurrent(item); 411 412 var data = { 413 'id': item.config.id, 414 'sec': item.config.sec, 415 'url': window.location.href 416 }; 417 if (item.config.originId) 418 { 419 var originId = item.config.originId; 420 if (item.formNode && originId.indexOf('%') >= 0) 421 { 422 var inputs = item.formNode.querySelectorAll('input[type="text"], input[type="hidden"]'); 423 inputs = BX.convert.nodeListToArray(inputs); 424 inputs.forEach(function (input) { 425 if (!input.name) 426 { 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 { 436 data.originatorId = item.config.originatorId; 437 } 438 439 BX.onCustomEvent(item, this.events.save, [data]); 440 BX.onCustomEvent(this, this.events.save, [item, data]); 441 442 if (this.isConsentSaved || !item.config.autoSave) 443 { 444 if (callback) 445 { 446 callback.apply(this, []); 447 } 448 } 449 else 450 { 451 this.sendActionRequest( 452 'saveConsent', 453 data, 454 callback, 455 callback 456 ); 457 } 458 }, 459 sendActionRequest: function (action, sendData, callbackSuccess, callbackFailure) 460 { 461 callbackSuccess = callbackSuccess || null; 462 callbackFailure = callbackFailure || null; 463 464 sendData.action = action; 465 sendData.sessid = BX.bitrix_sessid();
466 sendData.action = action; 467 468 BX.ajax({ 469 url: this.actionRequestUrl, 470 method: 'POST', 471 data: sendData, 472 timeout: 10, 473 dataType: 'json', 474 processData: true, 475 onsuccess: BX.proxy(function(data){ 476 data = data || {}; 477 if(data.error) 478 { 479 callbackFailure.apply(this, [data]); 480 } 481 else if(callbackSuccess) 482 { 483 callbackSuccess.apply(this, [data]); 484 } 485 }, this), 486 onfailure: BX.proxy(function(){ 487 var data = {'error': true, 'text': ''}; 488 if (callbackFailure) 489 { 490 callbackFailure.apply(this, [data]); 491 } 492 }, this) 493 }); 494 } 495 }; 496 497 BX.ready(function () { 498 BX.UserConsent.loadFromForms(); 499 }); 500 501})();
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.