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

65 lines
1.6 KiB
PHP
Raw Normal View History

2024-06-25 20:51:22 +04:00
<?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";
2024-06-25 23:01:48 +04:00
/** @var int délai en secondes avant le rafraichissement */
const DELAY = 15;
2024-06-25 20:51:22 +04:00
function __construct(?array $params=null) {
$this->checked = $params["checked"] ?? static::CHECKED;
$this->text = $params["text"] ?? static::TEXT;
2024-06-25 23:01:48 +04:00
$this->delay = intval($params["delay"] ?? static::DELAY) * 1000;
2024-06-25 20:51:22 +04:00
}
protected bool $checked;
2024-06-25 23:01:48 +04:00
2024-06-25 20:51:22 +04:00
protected string $text;
2024-06-25 23:01:48 +04:00
protected int $delay;
2024-06-25 20:51:22 +04:00
function printJquery(): void {
?>
<script type="text/javascript">
jQuery.noConflict()(function($) {
var autorefreshTimeout = null;
2024-06-26 11:18:44 +04:00
function autorefreshEnabled() {
return $("#autorefresh").is(":checked");
}
2024-06-25 20:51:22 +04:00
function autorefreshUpdate(enabled) {
2024-06-26 11:18:44 +04:00
if (typeof(enabled) === 'undefined') enabled = autorefreshEnabled();
2024-06-25 20:51:22 +04:00
if (enabled && autorefreshTimeout == null) {
2024-06-25 23:01:48 +04:00
autorefreshTimeout = setTimeout(function() { location.reload(); }, <?=$this->delay?>);
2024-06-25 20:51:22 +04:00
} else if (!enabled && autorefreshTimeout != null) {
clearTimeout(autorefreshTimeout);
autorefreshTimeout = null;
}
}
if ($("#autorefresh").length > 0) {
$("#autorefresh").click(function() {
autorefreshUpdate($(this).is(":checked"));
});
2024-06-26 11:18:44 +04:00
autorefreshUpdate();
2024-06-25 20:51:22 +04:00
}
});
</script>
<?php
}
function print(): void {
?>
<label>
<input type="checkbox" id="autorefresh"<?=$this->checked? " checked": false?>/>
<?=htmlspecialchars($this->text)?>
</label>
<?php
}
}