Skip to content

Commit f6d6b6a

Browse files
committed
v1
1 parent ac66334 commit f6d6b6a

File tree

2 files changed

+101
-59
lines changed

2 files changed

+101
-59
lines changed

org/ComponentViewer.php

Lines changed: 50 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,15 @@ public static function makeComponentDot(Form $form,$column,$title,$select=[],$se
2727
{
2828
$select =self::safeJson($select);
2929
$selected=self::safeJson($selected);
30-
$form->html(<<<EOF
31-
<div id="{$column}"></div>
32-
<script>
33-
new Promise((resolve, reject) => {
34-
while (true){
35-
if(document.getElementById('{$column}') instanceof HTMLElement){
36-
return resolve();
37-
}
38-
}
39-
}).then(function() {
40-
componentDot("{$column}",JSON.parse('$selected'),JSON.parse('$select'));
41-
});
42-
</script>
30+
self::script(<<<EOF
31+
componentDot("{$column}",JSON.parse('$selected'),JSON.parse('$select'));
4332
EOF
44-
,$title);
33+
);
34+
$form->html("<div id='{$column}'></div>",$title);
4535
}
4636

4737
/**
38+
* 线
4839
* @param Form $form
4940
* @param $column
5041
* @param $title
@@ -55,21 +46,11 @@ public static function makeComponentLine(Form $form,$column,$title,array $settin
5546
{
5647
$setting = self::safeJson($setting);
5748
$data = self::safeJson($data);
58-
$form->html(<<<EOF
59-
<div id="{$column}"></div>
60-
<script>
61-
new Promise((resolve, reject) => {
62-
while (true){
63-
if(document.getElementById('{$column}') instanceof HTMLElement){
64-
return resolve();
65-
}
66-
}
67-
}).then(function() {
68-
componentLine("{$column}",JSON.parse('$setting'),JSON.parse('$data'));
69-
});
70-
</script>
49+
self::script(<<<EOF
50+
componentLine("{$column}",JSON.parse('$setting'),JSON.parse('$data'));
7151
EOF
72-
,$title);
52+
);
53+
$form->html("<div id='{$column}'></div>",$title);
7354
}
7455

7556
/**
@@ -166,6 +147,13 @@ public static function makeForm(Content $content)
166147
return view('component.content', $items)->render();
167148
}
168149

150+
/**
151+
* 表单提交ajax返回数据格式
152+
* @param bool $success
153+
* @param string $message
154+
* @param array $data
155+
* @return \Illuminate\Http\JsonResponse
156+
*/
169157
public static function result($success=true,$message='OK',$data=[])
170158
{
171159
return response()->json([
@@ -177,8 +165,41 @@ public static function result($success=true,$message='OK',$data=[])
177165
->header('Access-Control-Allow-Origin', '*');
178166
}
179167

180-
private static function safeJson(array $data)
168+
/**
169+
* 表单代码段插入js片段代码
170+
* @param $script
171+
* @return array|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
172+
*/
173+
public static function script($script)
181174
{
175+
return Admin::script(<<<EOF
176+
new Promise((resolve, reject) => {
177+
while (true){
178+
if(document.getElementById('component') instanceof HTMLElement){
179+
return resolve();
180+
}
181+
}
182+
}).then(function() {
183+
{$script}
184+
});
185+
EOF
186+
);
187+
}
188+
189+
protected static function safeJson(array $data)
190+
{
191+
self::recursiveJsonArray($data);
182192
return strip_tags(json_encode($data,JSON_UNESCAPED_UNICODE|JSON_HEX_QUOT|JSON_HEX_TAG|JSON_HEX_AMP|JSON_HEX_APOS));
183193
}
194+
195+
private static function recursiveJsonArray(array &$data)
196+
{
197+
foreach ($data as &$d){
198+
if(is_array($d)){
199+
self::recursiveJsonArray($d);
200+
}else{
201+
$d = str_replace(['"','\'',':','\\','/','{','}','[',']'],'',$d);
202+
}
203+
}
204+
}
184205
}

src/DPLViewer.php

Lines changed: 51 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,15 @@ public static function makeComponentDot(Form $form,$column,$title,$select=[],$se
2727
{
2828
$select =self::safeJson($select);
2929
$selected=self::safeJson($selected);
30-
$form->html(<<<EOF
31-
<div id="{$column}"></div>
32-
<script>
33-
new Promise((resolve, reject) => {
34-
while (true){
35-
if(document.getElementById('{$column}') instanceof HTMLElement){
36-
return resolve();
37-
}
38-
}
39-
}).then(function() {
40-
componentDot("{$column}",JSON.parse('$selected'),JSON.parse('$select'));
41-
});
42-
</script>
30+
self::script(<<<EOF
31+
componentDot("{$column}",JSON.parse('$selected'),JSON.parse('$select'));
4332
EOF
44-
,$title);
33+
);
34+
$form->html("<div id='{$column}'></div>",$title);
4535
}
4636

4737
/**
38+
* 线
4839
* @param Form $form
4940
* @param $column
5041
* @param $title
@@ -55,21 +46,11 @@ public static function makeComponentLine(Form $form,$column,$title,array $settin
5546
{
5647
$setting = self::safeJson($setting);
5748
$data = self::safeJson($data);
58-
$form->html(<<<EOF
59-
<div id="{$column}"></div>
60-
<script>
61-
new Promise((resolve, reject) => {
62-
while (true){
63-
if(document.getElementById('{$column}') instanceof HTMLElement){
64-
return resolve();
65-
}
66-
}
67-
}).then(function() {
68-
componentLine("{$column}",JSON.parse('$setting'),JSON.parse('$data'));
69-
});
70-
</script>
49+
self::script(<<<EOF
50+
componentLine("{$column}",JSON.parse('$setting'),JSON.parse('$data'));
7151
EOF
72-
,$title);
52+
);
53+
$form->html("<div id='{$column}'></div>",$title);
7354
}
7455

7556
/**
@@ -163,9 +144,16 @@ public static function makeForm(Content $content)
163144
$items = [
164145
'_content_' => str_replace('pjax-container', '', $content->build())
165146
];
166-
return view('component::content', $items)->render();
147+
return view('component.content', $items)->render();
167148
}
168149

150+
/**
151+
* 表单提交ajax返回数据格式
152+
* @param bool $success
153+
* @param string $message
154+
* @param array $data
155+
* @return \Illuminate\Http\JsonResponse
156+
*/
169157
public static function result($success=true,$message='OK',$data=[])
170158
{
171159
return response()->json([
@@ -177,8 +165,41 @@ public static function result($success=true,$message='OK',$data=[])
177165
->header('Access-Control-Allow-Origin', '*');
178166
}
179167

180-
private static function safeJson(array $data)
168+
/**
169+
* 表单代码段插入js片段代码
170+
* @param $script
171+
* @return array|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
172+
*/
173+
public static function script($script)
181174
{
175+
return Admin::script(<<<EOF
176+
new Promise((resolve, reject) => {
177+
while (true){
178+
if(document.getElementById('component') instanceof HTMLElement){
179+
return resolve();
180+
}
181+
}
182+
}).then(function() {
183+
{$script}
184+
});
185+
EOF
186+
);
187+
}
188+
189+
protected static function safeJson(array $data)
190+
{
191+
self::recursiveJsonArray($data);
182192
return strip_tags(json_encode($data,JSON_UNESCAPED_UNICODE|JSON_HEX_QUOT|JSON_HEX_TAG|JSON_HEX_AMP|JSON_HEX_APOS));
183193
}
194+
195+
private static function recursiveJsonArray(array &$data)
196+
{
197+
foreach ($data as &$d){
198+
if(is_array($d)){
199+
self::recursiveJsonArray($d);
200+
}else{
201+
$d = str_replace(['"','\'',':','\\','/','{','}','[',']'],'',$d);
202+
}
203+
}
204+
}
184205
}

0 commit comments

Comments
 (0)