Skip to content

Commit

Permalink
fix(#831): checkboxes display when user accept or deny cookies (#856)
Browse files Browse the repository at this point in the history
  • Loading branch information
MewenLeHo authored Oct 28, 2021
1 parent 71a95e3 commit 18fb2da
Showing 1 changed file with 48 additions and 12 deletions.
60 changes: 48 additions & 12 deletions site/assets/js/tac.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,55 @@
alert.querySelector('.tarteaucitronDeny').innerHTML = tarteaucitron.lang.denyAll
}, { once: true })

window.addEventListener('tac.open_panel', function () {
document.querySelectorAll('#tarteaucitronServices_api button').forEach(function (button) {
button.classList.add('btn', 'btn-sm', 'ms-2')
})
}, { once: true })
window.addEventListener('tac.open_panel',
function () {
document.querySelectorAll('#tarteaucitronServices_api button').forEach(function (button) {
button.classList.add('btn', 'btn-sm', 'ms-2')
})
var allowConsentButton = document.querySelector('#tarteaucitronAllAllowed')
var denyConsentButton = document.querySelector('#tarteaucitronAllDenied')
var checkboxes = document.querySelectorAll('.form-check-input')

document.addEventListener('googletagmanager_added', function () {
var ask = document.querySelector('#googletagmanagerLine .tarteaucitronAsk')
var choiceEvent = 'tarteaucitron.userInterface.respond(document.getElementById(\'googletagmanagerAllowed\'),document.getElementById(\'googletagmanagerAllowed\').checked);'
var toggle = document.createRange().createContextualFragment('<div class="form-check form-switch my-2 my-lg-0"><input class="form-check-input ms-0" type="checkbox" id="googletagmanagerAllowed" aria-describedby="tacCLgoogletagmanager" onchange="' + choiceEvent + '"><label class="form-check-label visually-hidden" for="googletagmanagerAllowed">Google Tag Manager</label></div>')
ask.innerHTML = ''
ask.append(toggle)
}, { once: true })
allowConsentButton.addEventListener('click', function () {
for (var i = 0; i < checkboxes.length; i++) {
if (!checkboxes.checked) {
checkboxes[i].checked = true
}
}
})
denyConsentButton.addEventListener('click', function () {
for (var i = 0; i < checkboxes.length; i++) {
if (checkboxes.checked !== true) {
checkboxes[i].checked = false
}
}
})
},
{ once: true }
)

document.addEventListener(
'googletagmanager_added',
function () {
var ask = document.querySelector(
'#googletagmanagerLine .tarteaucitronAsk'
)
var choiceEvent =
'tarteaucitron.userInterface.respond(document.getElementById(\'googletagmanagerAllowed\'),document.getElementById(\'googletagmanagerAllowed\').checked);'
var toggle = document
.createRange()
.createContextualFragment(
'<div class="form-check form-switch my-2 my-lg-0"><input class="form-check-input ms-0" type="checkbox" id="googletagmanagerAllowed" aria-describedby="tacCLgoogletagmanager" onchange="' +
choiceEvent +
'"' +
((document.cookie.match(/^(?:.*;)?\s*cookie-consent\s*=\s*([^;]+)(?:.*)?$/) || [null])[1] === '!googletagmanager=true' ? 'checked' : '') +
'><label class="form-check-label visually-hidden" for="googletagmanagerAllowed">Google Tag Manager</label></div>'
)
ask.innerHTML = ''
ask.append(toggle)
},
{ once: true }
)
})()

if (typeof tarteaucitron !== 'undefined') {
Expand Down

0 comments on commit 18fb2da

Please sign in to comment.