jqh 5 anni fa
parent
commit
4afd35b054
4 ha cambiato i file con 98 aggiunte e 8 eliminazioni
  1. 9 2
      src/Show.php
  2. 25 5
      src/Tree.php
  3. 59 0
      src/Tree/AbstractTool.php
  4. 5 1
      src/Tree/Tools.php

+ 9 - 2
src/Show.php

@@ -9,6 +9,7 @@ use Dcat\Admin\Show\Field;
 use Dcat\Admin\Show\Newline;
 use Dcat\Admin\Show\Panel;
 use Dcat\Admin\Show\Relation;
+use Dcat\Admin\Show\Tools;
 use Dcat\Admin\Traits\HasBuilderEvents;
 use Illuminate\Contracts\Support\Arrayable;
 use Illuminate\Contracts\Support\Htmlable;
@@ -277,10 +278,14 @@ class Show implements Renderable
     /**
      * @param \Closure|array|AbstractTool|Renderable|Htmlable|string $callback
      *
-     * @return $this
+     * @return $this|Tools
      */
-    public function tools($callback)
+    public function tools($callback = null)
     {
+        if ($callback === null) {
+            return $this->panel->tools();
+        }
+
         if ($callback instanceof \Closure) {
             $callback->call($this->model, $this->panel->tools());
 
@@ -294,6 +299,8 @@ class Show implements Renderable
         foreach ($callback as $tool) {
             $this->panel->tools()->append($tool);
         }
+
+        return $this;
     }
 
     /**

+ 25 - 5
src/Tree.php

@@ -4,7 +4,9 @@ namespace Dcat\Admin;
 
 use Closure;
 use Dcat\Admin\Traits\HasBuilderEvents;
+use Dcat\Admin\Tree\AbstractTool;
 use Dcat\Admin\Tree\Tools;
+use Illuminate\Contracts\Support\Htmlable;
 use Illuminate\Contracts\Support\Renderable;
 use Illuminate\Database\Eloquent\Model;
 use Illuminate\Support\Traits\Macroable;
@@ -432,15 +434,33 @@ JS;
     }
 
     /**
-     * Setup grid tools.
+     * Setup tools.
      *
-     * @param Closure $callback
+     * @param Closure|array|AbstractTool|Renderable|Htmlable|string $callback
      *
-     * @return void
+     * @return $this|Tools
      */
-    public function tools(Closure $callback)
+    public function tools($callback = null)
     {
-        call_user_func($callback, $this->tools);
+        if ($callback === null) {
+            return $this->tools;
+        }
+
+        if ($callback instanceof \Closure) {
+            call_user_func($callback, $this->tools);
+
+            return $this;
+        }
+
+        if (! is_array($callback)) {
+            $callback = [$callback];
+        }
+
+        foreach ($callback as $tool) {
+            $this->tools->add($tool);
+        }
+
+        return $this;
     }
 
     /**

+ 59 - 0
src/Tree/AbstractTool.php

@@ -0,0 +1,59 @@
+<?php
+
+namespace Dcat\Admin\Tree;
+
+use Dcat\Admin\Actions\Action;
+use Dcat\Admin\Tree;
+
+abstract class AbstractTool extends Action
+{
+    /**
+     * @var Tree
+     */
+    protected $parent;
+
+    /**
+     * @var string
+     */
+    public $selectorPrefix = '.tree-tool-action-';
+
+    /**
+     * @var string
+     */
+    protected $style = 'btn btn-sm btn-primary';
+
+    /**
+     * @param Tree $parent
+     *
+     * @return void
+     */
+    public function setParent(Tree $parent)
+    {
+        $this->parent = $parent;
+    }
+
+    /**
+     * @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>";
+    }
+}

+ 5 - 1
src/Tree/Tools.php

@@ -36,12 +36,16 @@ class Tools implements Renderable
     /**
      * Prepend a tool.
      *
-     * @param string|\Closure|Renderable|Htmlable $tool
+     * @param string|\Closure|AbstractTool|Renderable|Htmlable $tool
      *
      * @return $this
      */
     public function add($tool)
     {
+        if ($tool instanceof AbstractTool) {
+            $tool->setParent($this->tree);
+        }
+
         $this->tools->push($tool);
 
         return $this;