94 lines
		
	
	
		
			2.6 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			94 lines
		
	
	
		
			2.6 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php
 | |
| namespace nur\v\bs3\plugins;
 | |
| 
 | |
| use nur\v\BasePlugin;
 | |
| use nur\v\js;
 | |
| 
 | |
| class formfilePlugin extends BasePlugin {
 | |
|   const AUTOSUBMIT_ON_CHANGE = '$(this).closest("form").submit();';
 | |
| 
 | |
|   function __construct(?string $readyLabelPrefix=null, ?string $readyLabelSuffix=null, ?string $afterModified_js=null) {
 | |
|     $this->readyLabelPrefix = $readyLabelPrefix;
 | |
|     $this->readyLabelSuffix = $readyLabelSuffix;
 | |
|     $this->afterModified_js = $afterModified_js;
 | |
|   }
 | |
| 
 | |
|   /** @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 active";
 | |
| 
 | |
|   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 files = this.files;
 | |
|     var filename;
 | |
|     if (files !== undefined) {
 | |
|       var count = files.length;
 | |
|       if (count === 1) {
 | |
|         filename = files[0].name;
 | |
|       } else if (count === 2) {
 | |
|         filename = "".concat(files[0].name, ", ", files[1].name, " (", count, ")");
 | |
|       } else {
 | |
|         filename = "".concat(files[0].name, ", ... (", count, ")");
 | |
|       }
 | |
|     } else {
 | |
|       filename = $this.prop("value");
 | |
|       var posas = filename.lastIndexOf("\\");
 | |
|       var posfs = filename.lastIndexOf("/");
 | |
|       if (posas !== -1) filename = filename.substring(posas + 1);
 | |
|       else if (posfs !== -1) filename = filename.substring(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();
 | |
|   }
 | |
| }
 |