@@ -27,24 +27,15 @@ public static function makeComponentDot(Form $form,$column,$title,$select=[],$se
27
27
{
28
28
$ select =self ::safeJson ($ select );
29
29
$ 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'));
43
32
EOF
44
- ,$ title );
33
+ );
34
+ $ form ->html ("<div id=' {$ column }'></div> " ,$ title );
45
35
}
46
36
47
37
/**
38
+ * 线
48
39
* @param Form $form
49
40
* @param $column
50
41
* @param $title
@@ -55,21 +46,11 @@ public static function makeComponentLine(Form $form,$column,$title,array $settin
55
46
{
56
47
$ setting = self ::safeJson ($ setting );
57
48
$ 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'));
71
51
EOF
72
- ,$ title );
52
+ );
53
+ $ form ->html ("<div id=' {$ column }'></div> " ,$ title );
73
54
}
74
55
75
56
/**
@@ -163,9 +144,16 @@ public static function makeForm(Content $content)
163
144
$ items = [
164
145
'_content_ ' => str_replace ('pjax-container ' , '' , $ content ->build ())
165
146
];
166
- return view ('component:: content ' , $ items )->render ();
147
+ return view ('component. content ' , $ items )->render ();
167
148
}
168
149
150
+ /**
151
+ * 表单提交ajax返回数据格式
152
+ * @param bool $success
153
+ * @param string $message
154
+ * @param array $data
155
+ * @return \Illuminate\Http\JsonResponse
156
+ */
169
157
public static function result ($ success =true ,$ message ='OK ' ,$ data =[])
170
158
{
171
159
return response ()->json ([
@@ -177,8 +165,41 @@ public static function result($success=true,$message='OK',$data=[])
177
165
->header ('Access-Control-Allow-Origin ' , '* ' );
178
166
}
179
167
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 )
181
174
{
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 );
182
192
return strip_tags (json_encode ($ data ,JSON_UNESCAPED_UNICODE |JSON_HEX_QUOT |JSON_HEX_TAG |JSON_HEX_AMP |JSON_HEX_APOS ));
183
193
}
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
+ }
184
205
}
0 commit comments