nur-sery/nur_src/v/bs3/plugins/formfilePlugin.php

78 lines
2.1 KiB
PHP

<?php
namespace nur\v\bs3\plugins;
use nur\v\BasePlugin;
use nur\v\js;
class formfilePlugin extends BasePlugin {
function __construct(?string $readyLabelPrefix=null, ?string $readyLabelSuffix=null) {
$this->readyLabelPrefix = $readyLabelPrefix;
$this->readyLabelSuffix = $readyLabelSuffix;
}
/** @var bool */
private $enabled = true;
function setEnabled(bool $enabled=true): void {
$this->enabled = $enabled;
}
/** @var string */
private $readyLabelPrefix;
function setReadyLabelPrefix(string $readyLabelPrefix): void {
$this->readyLabelPrefix = $readyLabelPrefix;
}
/** @var string */
private $readyLabelSuffix;
function setReadyLabelSuffix(string $readyLabelSuffix): void {
$this->readyLabelSuffix = $readyLabelSuffix;
}
private $readyLabelClass = "btn-success";
function setReadyLabelClass(string $readyLabelClass): void {
$this->readyLabelClass = $readyLabelClass;
}
private $afterModified_js;
function setAfterModified_js(string $afterModified_js): void {
$this->afterModified_js = $afterModified_js;
}
const HAVE_JQUERY = true;
function printJquery(): void {
if (!$this->enabled) return;
?>
<script type="text/javascript">
jQuery.noConflict()(function($) {
$("input[type='file']").change(function() {
var $this = $(this);
var filename = $this.prop("value");
var posas = filename.lastIndexOf("\\");
var posfs = filename.lastIndexOf("/");
if (posas !== -1) filename = filename.substr(posas + 1);
else if (posfs !== -1) filename = filename.substr(posfs + 1);
var $label = $this.parent("label");
$label.addClass(<?=js::qv($this->readyLabelClass)?>);
var prefix = <?=js::qv($this->readyLabelPrefix)?>;
var suffix = <?=js::qv($this->readyLabelSuffix)?>;
$label.find(".file-label").html(prefix + filename + suffix);
<?=$this->afterModified_js?>
});
});
</script>
<?php
}
function print($setModified=null, $prefix=null, $cond=true) {
$this->enabled = $cond;
$this->readyLabelPrefix = $prefix;
$this->afterModified_js = $setModified;
$this->printJquery();
}
}