jqh 5 years ago
parent
commit
f278a3a0c4
4 changed files with 103 additions and 50 deletions
  1. 15 3
      src/Controllers/RoleController.php
  2. 1 1
      src/Controllers/UserController.php
  3. 50 46
      src/Show/Field.php
  4. 37 0
      src/Support/Helper.php

+ 15 - 3
src/Controllers/RoleController.php

@@ -10,6 +10,7 @@ use Dcat\Admin\Models\Role as RoleModel;
 use Dcat\Admin\Show;
 use Dcat\Admin\SimpleGrid;
 use Dcat\Admin\Support\Helper;
+use Dcat\Admin\Widgets\Tree;
 
 class RoleController extends AdminController
 {
@@ -76,9 +77,20 @@ class RoleController extends AdminController
             $show->slug;
             $show->name;
 
-            $show->permissions->as(function ($permission) {
-                return collect($permission)->pluck('name');
-            })->label('primary');
+            $show->permissions->unescape()->as(function ($permission) {
+                $permissionModel = config('admin.database.permissions_model');
+                $permissionModel = new $permissionModel();
+                $nodes = $permissionModel->allNodes();
+
+                $tree = Tree::make($nodes);
+
+                $keyName = $permissionModel->getKeyName();
+                $tree->check(
+                    array_column(Helper::array($permission), $keyName)
+                );
+
+                return $tree->render();
+            });
 
             $show->created_at;
             $show->updated_at;

+ 1 - 1
src/Controllers/UserController.php

@@ -112,7 +112,7 @@ class UserController extends AdminController
                 }
 
                 return collect($roles)->pluck('name');
-            })->label('primary');
+            })->chip();
 
             $show->permissions->unescape()->as(function () {
                 $roles = (array) $this->roles;

+ 50 - 46
src/Show/Field.php

@@ -3,6 +3,7 @@
 namespace Dcat\Admin\Show;
 
 use Dcat\Admin\Show;
+use Dcat\Admin\Support\Helper;
 use Dcat\Admin\Traits\HasBuilderEvents;
 use Dcat\Admin\Traits\HasDefinitions;
 use Dcat\Admin\Widgets\Dump;
@@ -10,7 +11,6 @@ use Illuminate\Contracts\Support\Arrayable;
 use Illuminate\Contracts\Support\Renderable;
 use Illuminate\Support\Arr;
 use Illuminate\Support\Collection;
-use Illuminate\Support\Facades\File;
 use Illuminate\Support\Facades\Storage;
 use Illuminate\Support\Fluent;
 use Illuminate\Support\Str;
@@ -93,22 +93,6 @@ class Field implements Renderable
      */
     protected $width = 8;
 
-    /**
-     * @var array
-     */
-    protected $fileTypes = [
-        'image'      => 'png|jpg|jpeg|tmp|gif',
-        'word'       => 'doc|docx',
-        'excel'      => 'xls|xlsx|csv',
-        'powerpoint' => 'ppt|pptx',
-        'pdf'        => 'pdf',
-        'code'       => 'php|js|java|python|ruby|go|c|cpp|sql|m|h|json|html|aspx',
-        'archive'    => 'zip|tar\.gz|rar|rpm',
-        'txt'        => 'txt|pac|log|md',
-        'audio'      => 'mp3|wav|flac|3pg|aa|aac|ape|au|m4a|mpc|ogg',
-        'video'      => 'mkv|rmvb|flv|mp4|avi|wmv|rm|asf|mpeg',
-    ];
-
     /**
      * Field constructor.
      *
@@ -290,10 +274,12 @@ class Field implements Renderable
                 return '';
             }
 
+            $icon = Helper::getFileIcon($name);
+
             return <<<HTML
 <ul class="mailbox-attachments clearfix">
     <li style="margin-bottom: 0;">
-      <span class="mailbox-attachment-icon"><i class="fa {$field->getFileIcon($name)}"></i></span>
+      <span class="mailbox-attachment-icon"><i class="{$icon}"></i></span>
       <div class="mailbox-attachment-info">
         <div class="mailbox-attachment-name">
             <i class="fa fa-paperclip"></i> {$name}
@@ -336,12 +322,16 @@ HTML;
     public function label($style = 'success')
     {
         return $this->unescape()->as(function ($value) use ($style) {
-            if ($value instanceof Arrayable) {
-                $value = $value->toArray();
+            $class = $style;
+            $background = '';
+
+            if (strpos($style, '#') === 0 || strpos($style, '(') !== false) {
+                $class = '';
+                $background = "style='background:{$style}'";
             }
 
-            return collect((array) $value)->map(function ($name) use ($style) {
-                return "<span class='label bg-{$style}'>$name</span>";
+            return collect($value)->map(function ($name) use ($class, $background) {
+                return "<span class='label bg-{$class}' $background>$name</span>";
             })->implode('&nbsp;');
         });
     }
@@ -356,12 +346,46 @@ HTML;
     public function badge($style = 'blue')
     {
         return $this->unescape()->as(function ($value) use ($style) {
-            if ($value instanceof Arrayable) {
-                $value = $value->toArray();
+            $class = $style;
+            $background = '';
+
+            if (strpos($style, '#') === 0 || strpos($style, '(') !== false) {
+                $class = '';
+                $background = "style='background:{$style}'";
             }
 
-            return collect((array) $value)->map(function ($name) use ($style) {
-                return "<span class='badge bg-{$style}'>$name</span>";
+            return collect($value)->map(function ($name) use ($class, $background) {
+                return "<span class='badge bg-{$class}' $background>$name</span>";
+            })->implode('&nbsp;');
+        });
+    }
+
+    /**
+     * @param string $style
+     *
+     * @return $this
+     */
+    public function chip($style = 'primary')
+    {
+        return $this->unescape()->as(function ($value) use ($style) {
+            $class = $style;
+            $background = '';
+            $textColor = '';
+
+            if (strpos($style, '#') === 0 || strpos($style, '(') !== false) {
+                $class = '';
+                $background = "style='background:{$style}'";
+                $textColor = 'text-white';
+            }
+
+            return collect($value)->map(function ($name) use ($class, $background, $textColor) {
+                return <<<HTML
+<div class="chip chip-{$class}" {$background}>
+  <div class="chip-body">
+    <div class="chip-text {$textColor}">{$name}</div>
+  </div>
+</div>
+HTML;
             })->implode('&nbsp;');
         });
     }
@@ -460,26 +484,6 @@ HTML;
         });
     }
 
-    /**
-     * Get file icon.
-     *
-     * @param string $file
-     *
-     * @return string
-     */
-    public function getFileIcon($file = '')
-    {
-        $extension = File::extension($file);
-
-        foreach ($this->fileTypes as $type => $regex) {
-            if (preg_match("/^($regex)$/i", $extension) !== 0) {
-                return "fa-file-{$type}-o";
-            }
-        }
-
-        return 'fa-file-o';
-    }
-
     /**
      * Set escape or not for this field.
      *

+ 37 - 0
src/Support/Helper.php

@@ -9,10 +9,27 @@ use Illuminate\Contracts\Support\Jsonable;
 use Illuminate\Contracts\Support\Renderable;
 use Illuminate\Support\Arr;
 use Illuminate\Support\Facades\Artisan;
+use Illuminate\Support\Facades\File;
 use Illuminate\Support\Str;
 
 class Helper
 {
+    /**
+     * @var array
+     */
+    public static $fileTypes = [
+        'image'      => 'png|jpg|jpeg|tmp|gif',
+        'word'       => 'doc|docx',
+        'excel'      => 'xls|xlsx|csv',
+        'powerpoint' => 'ppt|pptx',
+        'pdf'        => 'pdf',
+        'code'       => 'php|js|java|python|ruby|go|c|cpp|sql|m|h|json|html|aspx',
+        'archive'    => 'zip|tar\.gz|rar|rpm',
+        'txt'        => 'txt|pac|log|md',
+        'audio'      => 'mp3|wav|flac|3pg|aa|aac|ape|au|m4a|mpc|ogg',
+        'video'      => 'mkv|rmvb|flv|mp4|avi|wmv|rm|asf|mpeg',
+    ];
+
     /**
      * Update extension config.
      *
@@ -481,4 +498,24 @@ class Helper
     {
         return preg_match('/^[\w\-_]+\/[\w\-_]+$/', $name);
     }
+
+    /**
+     * Get file icon.
+     *
+     * @param string $file
+     *
+     * @return string
+     */
+    public static function getFileIcon($file = '')
+    {
+        $extension = File::extension($file);
+
+        foreach (static::$fileTypes as $type => $regex) {
+            if (preg_match("/^($regex)$/i", $extension) !== 0) {
+                return "fa fa-file-{$type}-o";
+            }
+        }
+
+        return 'fa fa-file-o';
+    }
 }