Skip to content

Commit 9c5fcf7

Browse files
committed
� Conflicts: � resources/assets/component.js � resources/assets/component.min.js
2 parents e449a08 + df9ec2f commit 9c5fcf7

File tree

2 files changed

+138
-86
lines changed

2 files changed

+138
-86
lines changed

src/DLPHelper.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace DLP;
4+
5+
6+
/**
7+
* Class DLPHelper
8+
* @package DLP
9+
*/
10+
class DLPHelper
11+
{
12+
/**
13+
* @param array $data
14+
* @return false|string
15+
*/
16+
public static function safeJson(array $data)
17+
{
18+
self::recursiveJsonArray($data);
19+
return json_encode($data, JSON_UNESCAPED_UNICODE);
20+
}
21+
22+
/**
23+
* @param array $data
24+
*/
25+
private static function recursiveJsonArray(array &$data)
26+
{
27+
foreach ($data as &$d) {
28+
if (is_array($d)) {
29+
self::recursiveJsonArray($d);
30+
} else {
31+
$d = str_replace(['"', '\'', ':', '\\', '{', '}', '[', ']','`'], '', $d);
32+
}
33+
}
34+
}
35+
}

src/DLPViewer.php

Lines changed: 103 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -18,85 +18,117 @@ class DLPViewer
1818
/**
1919
* 点
2020
* @param Form $form
21-
* @param string $column 字段名
22-
* @param string $title 名称
23-
* @param array $select 全部选项
21+
* @param string $column 数据字段名
22+
* @param string $title 名称
23+
* @param array $select 全部选项
2424
* @param array $selected 已选择选项
25-
* @param bool $strict json严格模式 消除json敏感字符问题
25+
* @param array $settings 配置项
26+
* $settings = [
27+
* 'strict'=>false, boolean json严格模式消除json敏感字符问题
28+
* 'width'=>'100%' string 容器宽度设置
29+
* 'height'=>'200px', string 容器高度设置
30+
* ]
2631
*/
27-
public static function makeComponentDot(Form $form, string $column, string $title, array $select = [], array $selected = [],bool $strict = false)
32+
public static function makeComponentDot(Form $form, string $column, string $title, array $select = [], array $selected = [], array $settings = [])
2833
{
34+
$strict = isset($settings['strict']) && $settings['strict'] ? true : false;
35+
$width = isset($settings['width']) ? $settings['width'] : '100%';
36+
$hight = isset($settings['height']) ? $settings['height'] : '200px';
2937
if ($strict) {
30-
$select = self::safeJson($select);
31-
$selected = self::safeJson($selected);
38+
$select = DLPHelper::safeJson($select);
39+
$selected = DLPHelper::safeJson($selected);
3240
} else {
3341
$select = json_encode($select, JSON_UNESCAPED_UNICODE | JSON_HEX_QUOT | JSON_HEX_APOS);
3442
$selected = json_encode($selected, JSON_UNESCAPED_UNICODE | JSON_HEX_QUOT | JSON_HEX_APOS);
3543
}
3644
Admin::script(<<<EOF
37-
componentDot("{$column}",JSON.parse('{$selected}'),JSON.parse('{$select}'));
45+
new ComponentDot("{$column}",JSON.parse('{$selected}'),JSON.parse('{$select}'));
3846
EOF
3947
);
40-
$form->html("<div id='{$column}'></div>", $title);
48+
$form->html("<div id='{$column}' style='width:{$width};height: {$hight};'></div>", $title);
4149
}
4250

4351
/**
4452
* 线
4553
* @param Form $form
46-
* @param string $column 字段名
47-
* @param string $title 名称
48-
* @param array $settings 设置项
49-
* @param array $data 数据
50-
* @param bool $strict json严格模式 消除json敏感字符问题
54+
* @param string $column 数据字段名
55+
* @param string $title 名称
56+
* @param array $data 数据
57+
* @param array $settings 配置项[setting,...]
58+
* $settings = [
59+
* 'columns'=>[
60+
* 'name' => ['name' => '名称', 'type' => 'input'],
61+
* 'name1' => ['name1' => '名称1', 'type' => 'text', style=>'width:50px'],
62+
* 'name2' => ['name2' => '名称2', 'type' => 'hidden'],
63+
* ...], array 多列配置项 (必须填)
64+
* 'strict'=>false, boolean json严格模式消除json敏感字符问题 (选填)
65+
* 'width'=>'100%', string 容器宽度设置 (选填)
66+
* 'height'=>'450px', string 容器高度设置 (选填)
67+
* 'options'=>[
68+
* 'sortable'=>true,
69+
* 'delete'=>true
70+
* ] array 多列操作设置 (选填)
71+
* ]
5172
*/
52-
public static function makeComponentLine(Form $form, string $column, string $title, array $settings = [], array $data = [], bool $strict = false)
73+
public static function makeComponentLine(Form $form, string $column, string $title, array $data = [], array $settings = [])
5374
{
54-
if($strict) {
55-
$data = self::safeJson($data);
56-
}else{
75+
$strict = isset($settings['strict']) && $settings['strict'] ? true : false;
76+
$width = isset($settings['width']) ? $settings['width'] : '100%';
77+
$hight = isset($settings['height']) ? $settings['height'] : '450px';
78+
$options = isset($settings['options']) ? json_encode($settings['options']) : '[]';
79+
if (!isset($settings['columns'])) return;
80+
$columns = $settings['columns'];
81+
if ($strict) {
82+
$data = DLPHelper::safeJson($data);
83+
} else {
5784
$data = json_encode($data, JSON_UNESCAPED_UNICODE | JSON_HEX_QUOT | JSON_HEX_APOS);
5885
}
59-
$settings = json_encode($settings, JSON_UNESCAPED_UNICODE | JSON_HEX_QUOT | JSON_HEX_APOS);
86+
$columns = json_encode($columns, JSON_UNESCAPED_UNICODE | JSON_HEX_QUOT | JSON_HEX_APOS);
6087
Admin::script(<<<EOF
61-
componentLine("{$column}",JSON.parse('{$settings}'),JSON.parse('{$data}'));
88+
new ComponentLine("{$column}",JSON.parse('{$columns}'),JSON.parse('{$data}'),JSON.parse('{$options}'));
6289
EOF
6390
);
64-
$form->html("<div id='{$column}'></div>", $title);
91+
$form->html("<div id='{$column}' style='width:{$width};height:{$hight};'></div>", $title);
6592
}
6693

6794
/**
6895
* 头部-多操作添加
6996
* @param Grid $grid
70-
* @param array $settings [setting,...]
71-
* setting.document_id 自定义节点ID
72-
* setting.title 自定义按钮名
73-
* setting.url 加载页地址
74-
* setting.xhr_url ajax提交地址
75-
* setting.method ajax提交方法
97+
* @param array $settings 配置项[setting,...]
98+
* settings.document_id dom节点id (必须填)
99+
* settings.title 自定义按钮名 (必须填)
100+
* settings.url 加载页地址 url/{id}加参数匹配id (必须填)
101+
* settings.xhr_url ajax提交地址 url/{id}加参数匹配id (选填)
102+
* settings.method ajax提交方法 (选填)
103+
* settings.options 弹窗配置项 (选填)
104+
* options = ['W'=>0.8,'H'=>0.8]
76105
*/
77-
public static function makeHeadPlaneAction(Grid $grid,array $settings = [
78-
['document_id'=>'','title'=>'','url'=>'','xhr_url'=>'','method'=>'POST']
106+
public static function makeHeadPlaneAction(Grid $grid, array $settings = [
107+
['document_id' => '', 'title' => '', 'url' => '', 'xhr_url' => '', 'method' => 'POST', 'options' => []]
79108
])
80109
{
81110
$script = '';
82-
foreach ($settings as $setting){
111+
foreach ($settings as $setting) {
83112
$xhr_url = isset($setting['xhr_url']) ? $setting['xhr_url'] : $setting['url'];
84113
$method = isset($setting['method']) ? $setting['method'] : 'POST';
85-
$script.=<<<EOF
114+
$options = isset($setting['options']) ? json_encode($setting['options']) : '[]';
115+
$script .= <<<EOF
86116
$('#{$setting['document_id']}').click(function(){
87-
componentPlane('{$setting['url']}','{$xhr_url}','{$method}');
117+
new ComponentPlane('{$setting['url']}','{$xhr_url}','{$method}',null,JSON.parse('{$options}'));
88118
});
89119
EOF;
90120
Admin::script($script);
91-
$grid->tools->append(new class($setting['title'],$setting['document_id']) extends RowAction {
121+
$grid->tools->append(new class($setting['title'], $setting['document_id']) extends RowAction {
92122
private $title;
93123
private $document_id;
94-
public function __construct($title,$document_id)
124+
125+
public function __construct($title, $document_id)
95126
{
96127
parent::__construct();
97128
$this->title = $title;
98129
$this->document_id = $document_id;
99130
}
131+
100132
public function render()
101133
{
102134
return <<<EOF
@@ -115,50 +147,55 @@ public function render()
115147
* 列-多操作添加
116148
* @param Grid $grid
117149
* @param array $settings [setting,...]
118-
* setting.document_class 自定义类名
119-
* setting.title 自定义按钮名
120-
* setting.url 加载页地址 url/{id}加参数匹配id
121-
* setting.xhr_url ajax提交地址 url/{id}加参数匹配id
122-
* setting.method ajax提交方法
150+
* setting.document_class dom节点classname (必须填)
151+
* setting.title 自定义按钮名 (必须填)
152+
* setting.url 加载页地址 url/{id}加参数匹配id (必须填)
153+
* setting.xhr_url ajax提交地址 url/{id}加参数匹配id (选填)
154+
* setting.method ajax提交方法 (选填)
155+
* setting.options 弹窗配置项 (选填)
156+
* options = ['W'=>0.8,'H'=>0.8]
123157
* @param array $disable ['view','edit','delete']
124158
*/
125-
public static function makeRowPlaneAction(Grid $grid,array $settings = [
126-
['document_class'=>'','title'=>'','url'=>'','xhr_url'=>'','method'=>'POST']
127-
],array $disable=[])
159+
public static function makeRowPlaneAction(Grid $grid, array $settings = [
160+
['document_class' => '', 'title' => '', 'url' => '', 'xhr_url' => '', 'method' => 'POST', 'options' => []]
161+
], array $disable = [])
128162
{
129163
$script = '';
130-
foreach ($settings as $setting){
164+
foreach ($settings as $setting) {
131165
$url = $setting['url'];
132166
$method = isset($setting['method']) ? $setting['method'] : 'POST';
133167
$xhr_url = isset($setting['xhr_url']) ? $setting['xhr_url'] : $url;
134-
$script.=<<<EOF
168+
$options = isset($setting['options']) ? json_encode($setting['options']) : '[]';
169+
$script .= <<<EOF
135170
$('.{$setting['document_class']}').click(function(){
136171
let url = '$url'.replace('{id}',$(this).attr('data-id'));
137172
let xhr_url = '$xhr_url'.replace('{id}',$(this).attr('data-id'));
138-
componentPlane(url,xhr_url,'{$method}');
173+
new ComponentPlane(url,xhr_url,'{$method}',null,JSON.parse('{$options}'));
139174
});
140175
EOF;
141176
}
142177
Admin::script($script);
143-
$grid->actions(function ($actions)use($settings,$disable) {
178+
$grid->actions(function ($actions) use ($settings, $disable) {
144179
foreach ($settings as $setting) {
145180
$actions->add(new
146181
class($setting['document_class'], $setting['title']) extends RowAction {
147182
private $title;
148183
private $document_class;
184+
149185
public function __construct($document_class, $title)
150186
{
151187
parent::__construct();
152188
$this->document_class = $document_class;
153189
$this->title = $title;
154190
}
191+
155192
public function render()
156193
{
157194
return "<a href='javascript:void(0);' class='{$this->document_class}' data-id='{$this->getKey()}'>{$this->title}</a>";
158195
}
159196
});
160197
}
161-
foreach ($disable as $dis){
198+
foreach ($disable as $dis) {
162199
$dis == 'view' && $actions->disableView();
163200
$dis == 'edit' && $actions->disableEdit();
164201
$dis == 'delete' && $actions->disableDelete();
@@ -167,38 +204,42 @@ public function render()
167204
}
168205

169206
/**
170-
* 列-多操作添加 (旧版图标按钮模式)
207+
* 列-多操作添加 (旧版图标按钮模式)
171208
* @param Grid $grid
172209
* @param array $settings [setting,...]
173-
* setting.document_class 自定义类名
174-
* setting.title 自定义按钮名 (图标css类 fa-edit fa-...)
175-
* setting.url 加载页地址
176-
* setting.xhr_url ajax提交地址
177-
* setting.method ajax提交方法
210+
* setting.document_class dom节点classname (必须填)
211+
* setting.title 自定义按钮名 (必须填)
212+
* setting.url 加载页地址 url/{id}加参数匹配id (必须填)
213+
* setting.xhr_url ajax提交地址 url/{id}加参数匹配id (选填)
214+
* setting.method ajax提交方法 (选填)
215+
* setting.options 弹窗配置项 (选填)
216+
* options = ['W'=>0.8,'H'=>0.8]
178217
* @param array $disable ['view','edit','delete']
179218
*/
180-
public static function _makeRowPlaneAction(Grid $grid,array $settings = [
181-
['document_class'=>'','title'=>'','url'=>'','xhr_url'=>'','method'=>'POST']
182-
],array $disable=[])
219+
public static function _makeRowPlaneAction(Grid $grid, array $settings = [
220+
['document_class' => '', 'title' => '', 'url' => '', 'xhr_url' => '', 'method' => 'POST', 'options' => []]
221+
], array $disable = [])
183222
{
184223
$script = '';
185-
foreach ($settings as $setting){
224+
foreach ($settings as $setting) {
186225
$url = $setting['url'];
187226
$method = isset($setting['method']) ? $setting['method'] : 'POST';
188227
$xhr_url = isset($setting['xhr_url']) ? $setting['xhr_url'] : $url;
189-
$script.=<<<EOF
228+
$options = isset($setting['options']) ? json_encode($setting['options']) : '[]';
229+
$script .= <<<EOF
190230
$('.{$setting['document_class']}').click(function(){
191231
let url = '$url'.replace('{id}',$(this).attr('data-id'));
192-
componentPlane(url,'{$xhr_url}','{$method}');
232+
let xhr_url = '$xhr_url'.replace('{id}',$(this).attr('data-id'));
233+
new ComponentPlane(url,xhr_url,'{$method}',null,JSON.parse('{$options}'));
193234
});
194235
EOF;
195236
}
196237
Admin::script($script);
197-
$grid->actions(function ($actions)use($settings,$disable) {
238+
$grid->actions(function ($actions) use ($settings, $disable) {
198239
foreach ($settings as $setting) {
199240
$actions->append("<a data-id='{$actions->getKey()}' href='javascript:void(0);' class='{$setting['document_class']}'><i class='fa {$setting['title']}'></i></a>");
200241
}
201-
foreach ($disable as $dis){
242+
foreach ($disable as $dis) {
202243
$dis == 'view' && $actions->disableView();
203244
$dis == 'edit' && $actions->disableEdit();
204245
$dis == 'delete' && $actions->disableDelete();
@@ -249,28 +290,4 @@ public static function result($success = true, $message = 'OK', $data = [])
249290
->header('Content-Type', 'application/json;charset=utf-8')
250291
->header('Access-Control-Allow-Origin', '*');
251292
}
252-
253-
/**
254-
* @param array $data
255-
* @return false|string
256-
*/
257-
public static function safeJson(array $data)
258-
{
259-
self::recursiveJsonArray($data);
260-
return json_encode($data, JSON_UNESCAPED_UNICODE);
261-
}
262-
263-
/**
264-
* @param array $data
265-
*/
266-
private static function recursiveJsonArray(array &$data)
267-
{
268-
foreach ($data as &$d) {
269-
if (is_array($d)) {
270-
self::recursiveJsonArray($d);
271-
} else {
272-
$d = str_replace(['"', '\'', ':', '\\', '{', '}', '[', ']','`'], '', $d);
273-
}
274-
}
275-
}
276-
}
293+
}

0 commit comments

Comments
 (0)