Browse Source

代码生成器增加拖动字段排序功能

Yu 4 years ago
parent
commit
c6c6e5aba6

File diff suppressed because it is too large
+ 1 - 0
resources/assets/dcat/plugins/sortable/Sortable.min.js


File diff suppressed because it is too large
+ 1 - 0
resources/dist/dcat/plugins/sortable/Sortable.min.js


+ 25 - 4
resources/views/helpers/scaffold.blade.php

@@ -112,7 +112,7 @@
                     <th>{{trans('admin.action')}}</th>
                 </tr>
                 </thead>
-                <tbody>
+                <tbody id="table-fields-sortable">
                 @if(old('fields'))
                     @foreach(old('fields') as $index => $field)
                         <tr>
@@ -149,7 +149,10 @@
                             </td>
                             <td><input type="text" class="form-control" placeholder="{{trans('admin.scaffold.default')}}" name="fields[{{$index}}][default]" value="{{$field['default']}}"/></td>
                             <td><input type="text" class="form-control" placeholder="{{trans('admin.scaffold.comment')}}" name="fields[{{$index}}][comment]" value="{{$field['comment']}}" /></td>
-                            <td><button class="btn btn-sm btn-white table-field-remove"><i class="feather icon-trash"></i></button></td>
+                            <td>
+                                <button class="btn btn-sm btn-white table-field-sort-handle" type="button" title="{{trans('admin.order')}}"><i class="fa fa-sort"></i></button>
+                                <button class="btn btn-sm btn-white table-field-remove"><i class="feather icon-trash"></i></button>
+                            </td>
                         </tr>
                     @endforeach
                 @else
@@ -186,7 +189,10 @@
                         </td>
                         <td><input type="text" class="form-control" placeholder="{{trans('admin.scaffold.default')}}" name="fields[0][default]"></td>
                         <td><input type="text" class="form-control" placeholder="{{trans('admin.scaffold.comment')}}" name="fields[0][comment]"></td>
-                        <td><button class="btn btn-sm btn-white table-field-remove"><i class="feather icon-trash"></i></button></td>
+                        <td>
+                            <button class="btn btn-sm btn-white table-field-sort-handle" type="button" title="{{trans('admin.order')}}"><i class="fa fa-sort"></i></button>
+                            <button class="btn btn-sm btn-white table-field-remove"><i class="feather icon-trash"></i></button>
+                        </td>
                     </tr>
                 @endif
                 </tbody>
@@ -279,7 +285,10 @@
         </td>
         <td><input value="{default}" type="text" class="form-control" placeholder="{{trans('admin.scaffold.default')}}" name="fields[__index__][default]"></td>
         <td><input value="{comment}" type="text" class="form-control" placeholder="{{trans('admin.scaffold.comment')}}" name="fields[__index__][comment]"></td>
-        <td><button class="btn btn-sm btn-white table-field-remove"><i class="feather icon-trash"></i></button></td>
+        <td>
+            <button class="btn btn-sm btn-white table-field-sort-handle" type="button" title="{{trans('admin.order')}}"><i class="fa fa-sort"></i></button>
+            <button class="btn btn-sm btn-white table-field-remove"><i class="feather icon-trash"></i></button>
+        </td>
     </tr>
 </template>
 
@@ -309,6 +318,18 @@
 
         $('select').select2();
 
+        var sortable = Sortable.create(document.getElementById("table-fields-sortable"),{
+            handle:'.table-field-sort-handle',
+            onEnd: function () {
+                getTR().each(function(index){
+                    $(this).find("[name^='fields']").each(function(){
+                        var newName = $(this).attr('name').replace(/fields\[(\d)\]/, `fields[${index}]`);
+                        $(this).attr('name', newName);
+                    })
+                });
+            }
+        });
+
         $('#add-table-field').click(function (event) {
             addField();
         });

+ 12 - 11
src/Http/Controllers/ScaffoldController.php

@@ -3,22 +3,22 @@
 namespace Dcat\Admin\Http\Controllers;
 
 use Dcat\Admin\Admin;
-use Dcat\Admin\Http\Auth\Permission;
+use Illuminate\Support\Arr;
+use Illuminate\Support\Str;
+use Illuminate\Http\Request;
 use Dcat\Admin\Layout\Content;
-use Dcat\Admin\Scaffold\ControllerCreator;
-use Dcat\Admin\Scaffold\LangCreator;
-use Dcat\Admin\Scaffold\MigrationCreator;
-use Dcat\Admin\Scaffold\ModelCreator;
-use Dcat\Admin\Scaffold\RepositoryCreator;
 use Dcat\Admin\Support\Helper;
-use Illuminate\Http\Request;
 use Illuminate\Routing\Controller;
-use Illuminate\Support\Arr;
-use Illuminate\Support\Facades\Artisan;
 use Illuminate\Support\Facades\DB;
-use Illuminate\Support\Facades\URL;
 use Illuminate\Support\MessageBag;
-use Illuminate\Support\Str;
+use Illuminate\Support\Facades\URL;
+use Dcat\Admin\Http\Auth\Permission;
+use Dcat\Admin\Scaffold\LangCreator;
+use Dcat\Admin\Scaffold\ModelCreator;
+use Illuminate\Support\Facades\Artisan;
+use Dcat\Admin\Scaffold\MigrationCreator;
+use Dcat\Admin\Scaffold\ControllerCreator;
+use Dcat\Admin\Scaffold\RepositoryCreator;
 
 class ScaffoldController extends Controller
 {
@@ -73,6 +73,7 @@ class ScaffoldController extends Controller
         }
 
         Admin::requireAssets('select2');
+        Admin::requireAssets('sortable');
 
         $dbTypes = static::$dbTypes;
         $dataTypeMap = static::$dataTypeMap;

+ 24 - 23
src/Layout/Asset.php

@@ -168,6 +168,9 @@ class Asset
         '@qrcode' => [
             'js' => '@admin/dcat/plugins/jquery-qrcode/dist/jquery-qrcode.min.js',
         ],
+        '@sortable' => [
+            'js' => '@admin/dcat/plugins/sortable/Sortable.min.js',
+        ],
     ];
 
     /**
@@ -311,7 +314,7 @@ class Asset
         }
 
         if (mb_strpos($name, '@') !== 0) {
-            $name = '@'.$name;
+            $name = '@' . $name;
         }
 
         $this->alias[$name] = $value;
@@ -333,7 +336,7 @@ class Asset
         }
 
         if (mb_strpos($alias, '@') !== 0) {
-            $alias = '@'.$alias;
+            $alias = '@' . $alias;
         }
 
         $this->js($this->alias[$alias]['js'] ?? null);
@@ -347,7 +350,7 @@ class Asset
      */
     public function css($css)
     {
-        if (! $css) {
+        if (!$css) {
             return;
         }
         $this->css = array_merge(
@@ -377,7 +380,7 @@ class Asset
      */
     public function js($js)
     {
-        if (! $js) {
+        if (!$js) {
             return;
         }
         $this->js = array_merge(
@@ -402,7 +405,7 @@ class Asset
 
         $paths = isset($this->alias[$path][$type]) ? (array) $this->alias[$path][$type] : null;
 
-        if (! $paths) {
+        if (!$paths) {
             return $paths;
         }
 
@@ -422,14 +425,14 @@ class Asset
      */
     public function url($path)
     {
-        if (! $path) {
+        if (!$path) {
             return $path;
         }
 
         $path = $this->getRealPath($path);
 
         if (mb_strpos($path, '//') === false) {
-            $path = config('admin.assets_server').'/'.trim($path, '/');
+            $path = config('admin.assets_server') . '/' . trim($path, '/');
         }
 
         return (config('admin.https') || config('admin.secure')) ? secure_asset($path) : asset($path);
@@ -444,7 +447,7 @@ class Asset
      */
     public function getRealPath(?string $path)
     {
-        if (! $this->containsAlias($path)) {
+        if (!$this->containsAlias($path)) {
             return $path;
         }
 
@@ -452,7 +455,7 @@ class Asset
             '/',
             array_map(
                 function ($v) {
-                    if (! $this->isPathAlias($v)) {
+                    if (!$this->isPathAlias($v)) {
                         return $v;
                     }
 
@@ -506,7 +509,7 @@ class Asset
      */
     public function headerJs($js)
     {
-        if (! $js) {
+        if (!$js) {
             return;
         }
 
@@ -536,7 +539,7 @@ class Asset
      */
     public function script($script, bool $direct = false)
     {
-        if (! $script) {
+        if (!$script) {
             return;
         }
         if ($direct) {
@@ -553,7 +556,7 @@ class Asset
      */
     public function style($style)
     {
-        if (! $style) {
+        if (!$style) {
             return;
         }
         $this->style = array_merge($this->style, (array) $style);
@@ -564,12 +567,10 @@ class Asset
      */
     protected function addFontCss()
     {
-        $this->fonts && (
-            $this->baseCss = array_merge(
+        $this->fonts && ($this->baseCss = array_merge(
                 $this->baseCss,
                 (array) $this->fonts
-            )
-        );
+        ));
     }
 
     /**
@@ -596,7 +597,7 @@ class Asset
         $html = '';
 
         foreach (array_unique($this->css) as &$v) {
-            if (! $paths = $this->get($v, 'css')) {
+            if (!$paths = $this->get($v, 'css')) {
                 continue;
             }
 
@@ -615,13 +616,13 @@ class Asset
      */
     public function withVersionQuery($url)
     {
-        if (! Str::contains($url, '?')) {
+        if (!Str::contains($url, '?')) {
             $url .= '?';
         }
 
-        $ver = 'v'.Admin::VERSION;
+        $ver = 'v' . Admin::VERSION;
 
-        return Str::endsWith($url, '?') ? $url.$ver : $url.'&'.$ver;
+        return Str::endsWith($url, '?') ? $url . $ver : $url . '&' . $ver;
     }
 
     /**
@@ -646,7 +647,7 @@ class Asset
         $html = '';
 
         foreach (array_unique($this->js) as &$v) {
-            if (! $paths = $this->get($v, 'js')) {
+            if (!$paths = $this->get($v, 'js')) {
                 continue;
             }
 
@@ -666,7 +667,7 @@ class Asset
         $html = '';
 
         foreach (array_unique($this->headerJs) as &$v) {
-            if (! $paths = $this->get($v, 'js')) {
+            if (!$paths = $this->get($v, 'js')) {
                 continue;
             }
 
@@ -695,7 +696,7 @@ class Asset
         console.error(e)
     }
 })();
-Dcat.ready(function () { 
+Dcat.ready(function () {
     try {
         {$script}
     } catch (e) {

Some files were not shown because too many files changed in this diff