Implement toggle in main controller.
This commit is contained in:
@@ -1,7 +1,22 @@
|
|||||||
<?php
|
<?php
|
||||||
require_once '../vendor/autoload.php';
|
require_once '../vendor/autoload.php';
|
||||||
|
|
||||||
if (\FooBar\Configuration::loadConfig()->isOpen()) {
|
$config = \FooBar\Configuration::loadConfig();
|
||||||
|
|
||||||
|
if (isset($_POST['auth_check'])) {
|
||||||
|
if (!$config->isAuthorized()) {
|
||||||
|
http_response_code(403);
|
||||||
|
}
|
||||||
|
} elseif (isset($_POST['toggle']) && $config->isAuthorized()) {
|
||||||
|
$file = $config->stateFile();
|
||||||
|
if ($config->isOpen()) {
|
||||||
|
unlink($file);
|
||||||
|
} else {
|
||||||
|
touch($file);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($config->isOpen()) {
|
||||||
require '../templates/open.html';
|
require '../templates/open.html';
|
||||||
} else {
|
} else {
|
||||||
require '../templates/closed.html';
|
require '../templates/closed.html';
|
||||||
|
|||||||
@@ -1,31 +0,0 @@
|
|||||||
<?php
|
|
||||||
require_once '../vendor/autoload.php';
|
|
||||||
|
|
||||||
$configuration = \FooBar\Configuration::loadConfig();
|
|
||||||
|
|
||||||
if (isset($_POST['toggle']) && $configuration->isAuthorized()) {
|
|
||||||
$file = $configuration->stateFile();
|
|
||||||
if ($configuration->isOpen()) {
|
|
||||||
unlink($file);
|
|
||||||
} else {
|
|
||||||
touch($file);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
?>
|
|
||||||
<!DOCTYPE HTML>
|
|
||||||
<html>
|
|
||||||
<head>
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
<title>Verander de status van de foobar</title>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<h1>De foobar is nu
|
|
||||||
<?php
|
|
||||||
echo $configuration->isOpen() ? ' ' : ' niet ';
|
|
||||||
?>
|
|
||||||
open.</h1>
|
|
||||||
<form method="post">
|
|
||||||
<input type="submit" name="toggle" value="Verander dit">
|
|
||||||
</form>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
@@ -1,3 +1,10 @@
|
|||||||
* {
|
* {
|
||||||
font-family: verdana, sans-serif;
|
font-family: verdana, sans-serif;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#toggle-button {
|
||||||
|
z-index: 2;
|
||||||
|
position: absolute;
|
||||||
|
top: 0;
|
||||||
|
left: 0;
|
||||||
|
}
|
||||||
|
|||||||
43
src/index.js
43
src/index.js
@@ -7,4 +7,47 @@ window.addEventListener('load', function () {
|
|||||||
} else {
|
} else {
|
||||||
startClosedAnimation();
|
startClosedAnimation();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
doIfAuthorized(showToggleButton);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
function showToggleButton() {
|
||||||
|
console.log('Should show toggle button.');
|
||||||
|
const template = `
|
||||||
|
<div id="toggle-button">
|
||||||
|
<form method="post">
|
||||||
|
<button name="toggle">Verander dit</button>
|
||||||
|
</form>
|
||||||
|
</div>`;
|
||||||
|
|
||||||
|
const div = document.createElement('div');
|
||||||
|
div.innerHTML = template.trim();
|
||||||
|
const content = div.firstChild;
|
||||||
|
div.removeChild(content);
|
||||||
|
document.body.appendChild(content);
|
||||||
|
console.log(content);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Run the specified callback if the user is authorized
|
||||||
|
*
|
||||||
|
* @param callback
|
||||||
|
*/
|
||||||
|
function doIfAuthorized(callback) {
|
||||||
|
const request = new XMLHttpRequest();
|
||||||
|
request.open('POST', window.location); // Post to self should work
|
||||||
|
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
|
||||||
|
request.onreadystatechange = function () {
|
||||||
|
switch (request.readyState) {
|
||||||
|
case XMLHttpRequest.DONE:
|
||||||
|
if (request.status === 200) {
|
||||||
|
callback();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
// Wait for completion.
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
request.send('auth_check=1');
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user