-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add consent management feature
- Loading branch information
1 parent
43cc908
commit b5cab3c
Showing
6 changed files
with
238 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
"use strict"; | ||
|
||
// consent manager | ||
(() => { | ||
function Consent() {} | ||
var C = new Consent(); | ||
|
||
// array loop helper | ||
Consent.prototype.each = function (items, callback) { | ||
for (var i = 0; i < items.length; i++) callback(items[i]); | ||
}; | ||
|
||
// get object from localstorage | ||
Consent.prototype.getStorage = function () { | ||
let data = localStorage.getItem("rockfrontend-consent") || "{}"; | ||
return JSON.parse(data); | ||
}; | ||
|
||
// get enabled state for given item | ||
Consent.prototype.isEnabled = function (name) { | ||
return this.getStorage()[name]; | ||
}; | ||
|
||
// init all containers and checkboxes | ||
Consent.prototype.init = function () { | ||
// show containers for enabled services | ||
let show = document.querySelectorAll("[data-rfc-show]"); | ||
this.each(show, function (container) { | ||
let name = container.getAttribute("data-rfc-show"); | ||
if (!C.isEnabled(name)) return; | ||
container.removeAttribute("hidden"); | ||
let el = container.querySelector("[data-src]"); | ||
let src = el.getAttribute("data-src"); | ||
el.setAttribute("src", src); | ||
}); | ||
|
||
// hide alternate containers for enabled services | ||
let hide = document.querySelectorAll("[data-rfc-hide]"); | ||
this.each(hide, function (container) { | ||
let name = container.getAttribute("data-rfc-hide"); | ||
if (C.isEnabled(name)) { | ||
container.setAttribute("hidden", true); | ||
} else { | ||
container.removeAttribute("hidden"); | ||
} | ||
}); | ||
|
||
// toggle checkboxes based on enabled state | ||
let checkboxes = document.querySelectorAll(".rf-consent-checkbox"); | ||
this.each(checkboxes, (cb) => { | ||
let name = cb.getAttribute("data-name"); | ||
if (this.isEnabled(name)) { | ||
cb.setAttribute("checked", "checked"); | ||
} else { | ||
cb.removeAttribute("checked"); | ||
} | ||
}); | ||
}; | ||
|
||
// save consent state to localstorage | ||
Consent.prototype.save = function (name, value) { | ||
let storage = this.getStorage(); | ||
storage[name] = value; | ||
localStorage.setItem("rockfrontend-consent", JSON.stringify(storage)); | ||
}; | ||
|
||
// monitor checkbox changes | ||
document.addEventListener("change", function (e) { | ||
let cb = e.target; | ||
if (!cb.classList.contains("rf-consent-checkbox")) return; | ||
let name = cb.getAttribute("data-name"); | ||
C.save(name, cb.checked); | ||
C.init(); | ||
}); | ||
|
||
// monitor clicks on consent buttons | ||
document.addEventListener("click", (e) => { | ||
let el = e.target.closest("[rfc-allow]"); | ||
if (!el) return; | ||
e.preventDefault(); | ||
let name = el.getAttribute("rfc-allow"); | ||
C.save(name, true); | ||
C.init(); | ||
}); | ||
|
||
RockFrontend.Consent = C; | ||
C.init(); | ||
})(); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
namespace ProcessWire; | ||
|
||
/** | ||
* @author Bernhard Baumrock, 28.03.2023 | ||
* @license Licensed under MIT | ||
* @link https://www.baumrock.com | ||
*/ | ||
class TextformatterRockFrontend extends Textformatter implements ConfigurableModule | ||
{ | ||
|
||
public $checkboxclass = 'uk-checkbox'; | ||
|
||
public static function getModuleInfo() | ||
{ | ||
return [ | ||
'title' => 'RockFrontend Textformatter', | ||
'version' => '1.0.0', | ||
'summary' => 'Textformatter to manage consent', | ||
]; | ||
} | ||
|
||
public function format(&$str) | ||
{ | ||
if (strpos($str, "[rf-consent=") === false) return; | ||
$str = preg_replace_callback("/\[rf-consent=(.*?)\](.*?)\[\/rf-consent\]/", function ($matches) { | ||
$name = $matches[1]; | ||
$str = $matches[2]; | ||
return "<label> | ||
<input type='checkbox' class='rf-consent-checkbox {$this->checkboxclass}' data-name='$name'> | ||
$str | ||
</label>"; | ||
}, $str); | ||
} | ||
|
||
|
||
/** | ||
* Config inputfields | ||
* @param InputfieldWrapper $inputfields | ||
*/ | ||
public function getModuleConfigInputfields($inputfields) | ||
{ | ||
$inputfields->add([ | ||
'type' => 'text', | ||
'name' => 'checkboxclass', | ||
'label' => 'Checkbox Class', | ||
'value' => $this->checkboxclass, | ||
'notes' => 'Here you can adjust the class that is applied to consent checkboxes. Default is "uk-checkbox".', | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters