<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2020/6/19
* Time: 16:12
*/
namespace App\Admin\Extensions\Form\CheckboxTree;
use Encore\Admin\Form\Field;
class CheckboxTree extends Field
{
protected $view = 'admin.form.treecheckbox';
protected $_showLabel = true;
protected $_unique = '';
protected $_hide_checkbox = false;
protected $_javascript_func = '';
protected $_spread_checked = false;
protected $_spread = false;
protected $_onchange_event = null;
protected $_min_width = '100%';
protected $_min_height = '100%';
protected $_max_width = '100%';
protected $_max_height = '100%';
protected $_disabled = false;
protected static $css = [
'/wenruns/css/tree.min.css',
];
protected static $js = [
'/wenruns/js/tree.js',
];
/**
* 追加类名
* @param $option
* @return string
*/
protected function appendClass($option)
{
return isset($option['className']) ? $option['className'] : '';
}
/**
* 是否禁用
* @param $option
* @return bool
*/
protected function isDisabled($option)
{
return isset($option['disabled']) ? $option['disabled'] : $this->_disabled;
}
/**
* 附加数据
* @param $option
* @return string
*/
protected function appendData($option)
{
return isset($option['datas']) ? $option['datas'] : '';
}
/**
* 是否选中
* @param $option
* @return bool
*/
protected function isChecked($option)
{
return isset($option['checked']) ? $option['checked'] : $this->isDefault($option);
}
/**
* 默认值检测
* @param $option
* @return bool
*/
protected function isDefault($option)
{
$name = isset($option['name']) && $option['name'] ? $option['name'] : $this->column;
$value = $option['value'];
$defaults = $this->default;
if (is_array($defaults)) {
if (isset($defaults[$name])) {
return in_array($value, $defaults[$name]);
} else {
return in_array($value, $defaults);
}
} else {
return $defaults == $value;
}
}
/**
* 字段名称检测
* @param $option
* @return array|mixed|string
*/
protected function checkName($option)
{
return isset($option['name']) && $option['name'] ? $option['name'] : $this->column;
}
/**
* 值检测
* @param $option
* @return string
*/
protected function checkValue($option)
{
return isset($option['value']) ? $option['value'] : '';
}
/**
* 文本检测
* @param $option
* @return string
*/
protected function checkText($option)
{
return isset($option['text']) ? $option['text'] : '';
}
/**
* 是否显示复选框
* @param $option
* @return bool
*/
protected function isShow($option)
{
return isset($option['isShow']) ? $option['isShow'] : !$this->_hide_checkbox;
}
/**
* 格式化选项
* @param $options
* @return mixed
*/
protected function formatOptions($options)
{
foreach ($options as $key => $option) {
$option['checked'] = $this->isChecked($option);
$option['className'] = $this->appendClass($option);
$option['disabled'] = $this->isDisabled($option);
$option['datas'] = $this->appendData($option);
$option['name'] = $this->checkName($option);
$option['value'] = $this->checkValue($option);
$option['text'] = $this->checkText($option);
if (isset($option['sub']) && !empty($option['sub'])) {
$option['sub'] = $this->formatOptions($option['sub']);
}
$options[$key] = $option;
}
return $options;
}
/**
* 获取选项
* @return false|string
*/
protected function getOptions()
{
return json_encode($this->formatOptions($this->options));
}
/**
* CheckBox constructor.
* @param string $column
* @param array $arguments
*/
public function __construct(string $column = '', array $arguments = [])
{
$this->checkViewFile()->checkCssFile()->checkJsFile();
parent::__construct($column, $arguments);
// if (empty($this->label)) {
// $this->_showLabel = false;
// }
$this->_unique = md5($this->column . mt_rand(10000, 99999) . time());
}
/**
* @return $this
*/
protected function checkViewFile()
{
$dirPath = resource_path('views/admin/form');
$filePath = $dirPath . '/treecheckbox.blade.php';
$originPath = __DIR__ . '/treecheckbox.blade.php';
if (!is_file($filePath) || filemtime($originPath) > filemtime($filePath)) {
if (!is_dir($dirPath)) {
mkdir($dirPath, 0777, true);
}
file_put_contents($filePath, file_get_contents($originPath));
}
return $this;
}
/**
* @return $this
*/
protected function checkCssFile()
{
$dirPath = public_path('wenruns/css');
$filePath = $dirPath . '/tree.min.css';
$originPath = __DIR__ . '/tree.min.css';
if (!is_file($filePath) || filemtime($originPath) > filemtime($filePath)) {
if (!is_dir($dirPath)) {
mkdir($dirPath, 0777, true);
}
file_put_contents($filePath, file_get_contents($originPath));
}
return $this;
}
/**
* @return $this
*/
protected function checkJsFile()
{
$dirPath = public_path('wenruns/js');
$filePath = $dirPath . '/tree.js';
$originPath = __DIR__ . '/tree.js';
if (!is_file($filePath) || filemtime($originPath) > filemtime($filePath)) {
if (!is_dir($dirPath)) {
mkdir($dirPath, 0777, true);
}
file_put_contents($filePath, file_get_contents($originPath));
}
return $this;
}
/**
* 不显示label
* @param bool $enable
* @return $this
*/
public function disableLabel($enable = false)
{
$this->_showLabel = $enable;
return $this;
}
/**
* 选项数组
* @param array $options
* @return $this|Field
*/
public function options($options = [])
{
$this->options = $options;
return $this;
}
/**
* 隐藏复选框
* @param bool $hideCheckBox
* @return $this
*/
public function hideCheckBox($hideCheckBox = true)
{
$this->_hide_checkbox = $hideCheckBox;
return $this;
}
/**
* javascript函数
* @param $jsCallback
* @return $this
*/
public function onReady($jsCallback)
{
$this->_javascript_func = $this->compressHtml($jsCallback);
return $this;
}
/**
* 设置最小宽高
* @param $width
* @param $height
* @return $this
*/
public function min($width, $height)
{
$this->_min_height = $height;
$this->_min_width = $width;
return $this;
}
/**
* 设置最大宽高
* @param $width
* @param $height
* @return $this
*/
public function max($width, $height)
{
$this->_max_height = $height;
$this->_max_width = $width;
return $this;
}
/**
* 展开已选选项
* @param bool $spreadChecked
laravel-admin:form表单树状复选框组件(CheckboxTree).rar
需积分: 50 127 浏览量
2021-03-01
15:55:59
上传
评论
收藏 12KB RAR 举报
wenruns
- 粉丝: 23
- 资源: 9
最新资源
- 前端开发-什么是前端开发-关于前端开发的一些相关介绍
- Sora AI-关于文生视频的使用场景说明
- suno AI文生视频的相关教程和介绍使用
- 什么是后端开发-关于后端开发的一些小介绍分享
- Jurassic Pack Vol. II Dinosaurs 侏罗纪包卷恐龙二号Unity游戏模型资源unitypackage
- Jurassic Pack Vol. III Dinosaurs 侏罗纪包卷恐龙三号Unity游戏模型资源unitypackag
- Ultimate Seating Controller 终极座椅控制器Unity游戏开发插件资源unitypackage
- 什么是人工智能-关于人工智能的相关介绍说明
- Figma Converter for Unity适用Unity的Figma转换器Unity游戏开发插件unitypackage
- Creepy Animatronic Anims 令人毛骨悚然的电子动画Unity游戏动画插件资源unitypackage
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
评论0