Skip to content

Commit

Permalink
fix: RockFrontend script tag in markup when not needed
Browse files Browse the repository at this point in the history
  • Loading branch information
BernhardBaumrock committed Jul 4, 2023
1 parent 75e6699 commit 25364be
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 23 deletions.
40 changes: 19 additions & 21 deletions RockFrontend.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"use strict";

if (typeof RockFrontend === "undefined") var RockFrontend = {};

// RockFrontend helper functions
(() => {
/**
Expand Down Expand Up @@ -193,31 +195,27 @@
* <a href="/foo/bar.jpg" id="foo">Show Image</a>
* </div>
*/
(() => {
document.addEventListener("click", function (e) {
let click = e.target.closest("[rf-click]");
if (!click) return;
e.preventDefault();
let selector = click.getAttribute("rf-click");
let target = document.querySelector(selector);
if (target) target.click();
});
})();
document.addEventListener("click", function (e) {
let click = e.target.closest("[rf-click]");
if (!click) return;
e.preventDefault();
let selector = click.getAttribute("rf-click");
let target = document.querySelector(selector);
if (target) target.click();
});

/**
* Toggle the "hidden" attribute of another element
* Usage:
* <a href=# rf-toggle='#foo'>toggle foo</a>
* <div id='foo'>hello world</div>
*/
(() => {
document.addEventListener("click", function (e) {
let toggle = e.target.closest("[rf-toggle]");
if (!toggle) return;
e.preventDefault();
let selector = toggle.getAttribute("rf-toggle");
let target = document.querySelector(selector);
if (target.hasAttribute("hidden")) target.removeAttribute("hidden");
else target.setAttribute("hidden", "");
});
})();
document.addEventListener("click", function (e) {
let toggle = e.target.closest("[rf-toggle]");
if (!toggle) return;
e.preventDefault();
let selector = toggle.getAttribute("rf-toggle");
let target = document.querySelector(selector);
if (target.hasAttribute("hidden")) target.removeAttribute("hidden");
else target.setAttribute("hidden", "");
});
7 changes: 5 additions & 2 deletions RockFrontend.module.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ function (HookEvent $event) {
// early exit if asset injection is disabled
// Usage: place "$rockfrontend->noAssets = true" somewhere in your
// template file to prevent loading of any rockfrontend assets
// note that this may break some of the features of RockFrontend
if ($this->noAssets) return;

// early exit if html does not contain a head section
Expand Down Expand Up @@ -275,8 +276,10 @@ function (HookEvent $event) {

// at the very end we inject the js variables
$assets = '';
$json = count($this->js) ? json_encode($this->js) : '{}';
$assets .= "\n <script>let RockFrontend = $json</script>\n";
if (count($this->js)) {
$json = json_encode($this->js);
$assets .= "\n <script>var RockFrontend = $json</script>\n";
}
foreach ($this->autoloadScripts as $script) $assets .= $script->render();
foreach ($this->autoloadStyles as $style) $assets .= $style->render();
// return replaced markup
Expand Down

0 comments on commit 25364be

Please sign in to comment.