nur-sery/nur_src/v/plugins/autorefreshPlugin.php

65 lines
1.6 KiB
PHP

<?php
namespace nur\v\plugins;
use nur\v\BasePlugin;
class autorefreshPlugin extends BasePlugin {
const HAVE_JQUERY = true;
const CHECKED = true;
const TEXT = "Rafraîchir cette page automatiquement";
/** @var int délai en secondes avant le rafraichissement */
const DELAY = 15;
function __construct(?array $params=null) {
$this->checked = $params["checked"] ?? static::CHECKED;
$this->text = $params["text"] ?? static::TEXT;
$this->delay = intval($params["delay"] ?? static::DELAY) * 1000;
}
protected bool $checked;
protected string $text;
protected int $delay;
function printJquery(): void {
?>
<script type="text/javascript">
jQuery.noConflict()(function($) {
var autorefreshTimeout = null;
function autorefreshEnabled() {
return $("#autorefresh").is(":checked");
}
function autorefreshUpdate(enabled) {
if (typeof(enabled) === 'undefined') enabled = autorefreshEnabled();
if (enabled && autorefreshTimeout == null) {
autorefreshTimeout = setTimeout(function() { location.reload(); }, <?=$this->delay?>);
} else if (!enabled && autorefreshTimeout != null) {
clearTimeout(autorefreshTimeout);
autorefreshTimeout = null;
}
}
if ($("#autorefresh").length > 0) {
$("#autorefresh").click(function() {
autorefreshUpdate($(this).is(":checked"));
});
autorefreshUpdate();
}
});
</script>
<?php
}
function print(): void {
?>
<label>
<input type="checkbox" id="autorefresh"<?=$this->checked? " checked": false?>/>
<?=htmlspecialchars($this->text)?>
</label>
<?php
}
}