浏览代码

重写Show tool

jqh 5 年之前
父节点
当前提交
aa151e1edd
共有 4 个文件被更改,包括 117 次插入3 次删除
  1. 4 1
      src/Grid/Actions/QuickEdit.php
  2. 24 0
      src/Show.php
  3. 71 0
      src/Show/AbstractTool.php
  4. 18 2
      src/Show/Tools.php

+ 4 - 1
src/Grid/Actions/QuickEdit.php

@@ -24,9 +24,12 @@ class QuickEdit extends RowAction
 
             [$width, $height] = $this->parent->option('dialog_form_area');
 
-            Form::modal(trans('admin.edit'))
+            $title = trans('admin.edit').' - '.$this->key();
+
+            Form::modal($title)
                 ->click(".{$this->elementClass()}")
                 ->dimensions($width, $height)
+                ->forceRefresh()
                 ->success('LA.reload()')
                 ->render();
         }

+ 24 - 0
src/Show.php

@@ -3,6 +3,7 @@
 namespace Dcat\Admin;
 
 use Dcat\Admin\Contracts\Repository;
+use Dcat\Admin\Show\AbstractTool;
 use Dcat\Admin\Show\Divider;
 use Dcat\Admin\Show\Field;
 use Dcat\Admin\Show\Newline;
@@ -10,6 +11,7 @@ use Dcat\Admin\Show\Panel;
 use Dcat\Admin\Show\Relation;
 use Dcat\Admin\Traits\HasBuilderEvents;
 use Illuminate\Contracts\Support\Arrayable;
+use Illuminate\Contracts\Support\Htmlable;
 use Illuminate\Contracts\Support\Renderable;
 use Illuminate\Database\Eloquent\Builder;
 use Illuminate\Database\Eloquent\Model;
@@ -272,6 +274,28 @@ class Show implements Renderable
         return $this->panel;
     }
 
+    /**
+     * @param \Closure|array|AbstractTool|Renderable|Htmlable|string $callback
+     *
+     * @return $this
+     */
+    public function tools($callback)
+    {
+        if ($callback instanceof \Closure) {
+            $callback->call($this->model, $this->panel->tools());
+
+            return $this;
+        }
+
+        if (! is_array($callback)) {
+            $callback = [$callback];
+        }
+
+        foreach ($callback as $tool) {
+            $this->panel->tools()->append($tool);
+        }
+    }
+
     /**
      * Add a model field to show.
      *

+ 71 - 0
src/Show/AbstractTool.php

@@ -0,0 +1,71 @@
+<?php
+
+namespace Dcat\Admin\Show;
+
+use Dcat\Admin\Actions\Action;
+use Dcat\Admin\Show;
+
+abstract class AbstractTool extends Action
+{
+    /**
+     * @var Show
+     */
+    protected $parent;
+
+    /**
+     * @var string
+     */
+    public $selectorPrefix = '.show-tool-action-';
+
+    /**
+     * @var string
+     */
+    protected $style = 'btn btn-sm btn-primary';
+
+    /**
+     * @param Show $show
+     *
+     * @return void
+     */
+    public function setParent(Show $show)
+    {
+        $this->parent = $show;
+    }
+
+    /**
+     * @return array|mixed|string|null
+     */
+    public function key()
+    {
+        if ($this->primaryKey) {
+            return $this->primaryKey;
+        }
+
+        return $this->parent ? $this->parent->key() : null;
+    }
+
+    /**
+     * @return string|void
+     */
+    protected function href()
+    {
+    }
+
+    /**
+     * @return string|void
+     */
+    public function html()
+    {
+        if ($href = $this->href()) {
+            $this->disabledHandler = true;
+        }
+
+        $this->setHtmlAttribute([
+            'data-_key' => $this->key(),
+            'href'      => $href ?: 'javascript:void(0);',
+            'class'     => $this->style.' '.$this->elementClass(),
+        ]);
+
+        return "<a {$this->formatHtmlAttributes()}>{$this->title()}</a>";
+    }
+}

+ 18 - 2
src/Show/Tools.php

@@ -85,12 +85,14 @@ class Tools implements Renderable
     /**
      * Append a tools.
      *
-     * @param string|\Closure|Renderable|Htmlable $tool
+     * @param string|\Closure|AbstractTool|Renderable|Htmlable $tool
      *
      * @return $this
      */
     public function append($tool)
     {
+        $this->prepareTool($tool);
+
         $this->appends->push($tool);
 
         return $this;
@@ -99,17 +101,31 @@ class Tools implements Renderable
     /**
      * Prepend a tool.
      *
-     * @param string|\Closure|Renderable|Htmlable $tool
+     * @param string|\Closure|AbstractTool|Renderable|Htmlable $tool
      *
      * @return $this
      */
     public function prepend($tool)
     {
+        $this->prepareTool($tool);
+
         $this->prepends->push($tool);
 
         return $this;
     }
 
+    /**
+     * @param $tool
+     *
+     * @return void
+     */
+    protected function prepareTool($tool)
+    {
+        if ($tool instanceof AbstractTool) {
+            $tool->setParent($this->panel->getParent());
+        }
+    }
+
     /**
      * Get resource path.
      *