Переглянути джерело

:hammer: 表格事件重构

jqh 5 роки тому
батько
коміт
bc38cba104

+ 4 - 4
resources/assets/dcat/sass/components/_table.scss

@@ -273,18 +273,18 @@ body.dark-mode {
     }
 
     table.custom-data-table.data-table tbody td {
-      height: 38px;
+      height: 56px;
     }
 
     .table.custom-data-table {
-      padding: 5px 10px 10px;
+      padding: 3px 10px 10px;
     }
 
     .table-fixed-left .table.custom-data-table {
-      padding: 5px 0 0 10px;
+      padding: 3px 0 0 10px;
     }
     .table-fixed-right .table.custom-data-table {
-      padding: 5px 10px 0 0;
+      padding: 3px 10px 0 0;
     }
   }
 

Різницю між файлами не показано, бо вона завелика
+ 0 - 0
resources/dist/dcat/css/dcat-app-blue-light.css


Різницю між файлами не показано, бо вона завелика
+ 0 - 0
resources/dist/dcat/css/dcat-app-blue.css


Різницю між файлами не показано, бо вона завелика
+ 0 - 0
resources/dist/dcat/css/dcat-app-green.css


Різницю між файлами не показано, бо вона завелика
+ 0 - 0
resources/dist/dcat/css/dcat-app.css


+ 1 - 0
resources/lang/zh-TW/global.php

@@ -21,6 +21,7 @@ return [
         'roles'                 => '角色',
         'path'                  => '路徑',
         'input'                 => '輸入',
+        'type'                  => '类型',
     ],
     'labels' => [
         'list'     => '列表',

+ 12 - 40
src/Grid.php

@@ -6,6 +6,7 @@ use Closure;
 use Dcat\Admin\Contracts\Repository;
 use Dcat\Admin\Grid\Column;
 use Dcat\Admin\Grid\Concerns;
+use Dcat\Admin\Grid\Events\Rows;
 use Dcat\Admin\Grid\Model;
 use Dcat\Admin\Grid\Row;
 use Dcat\Admin\Grid\Tools;
@@ -21,6 +22,7 @@ class Grid
 {
     use HasBuilderEvents;
     use HasVariables;
+    use Concerns\HasEvents;
     use Concerns\HasNames;
     use Concerns\HasFilter;
     use Concerns\HasTools;
@@ -70,13 +72,6 @@ class Grid
      */
     protected $rows;
 
-    /**
-     * Rows callable fucntion.
-     *
-     * @var \Closure[]
-     */
-    protected $rowsCallback = [];
-
     /**
      * All column names of the grid.
      *
@@ -193,6 +188,7 @@ class Grid
         $this->rows = new Collection();
         $this->builder = $builder;
         $this->request = $request ?: request();
+        $this->resourcePath = url($this->request->getPathInfo());
 
         if ($repository = $this->model->repository()) {
             $this->setKeyName($repository->getKeyName());
@@ -200,8 +196,8 @@ class Grid
 
         $this->model->setGrid($this);
 
-        $this->setupTools();
-        $this->setupFilter();
+        $this->setUpTools();
+        $this->setUpFilter();
 
         $this->callResolving();
     }
@@ -453,28 +449,16 @@ class Grid
         $this->rows = collect($data)->map(function ($model) {
             return new Row($this, $model);
         });
-
-        if ($this->rowsCallback) {
-            foreach ($this->rowsCallback as $value) {
-                $value($this->rows);
-            }
-        }
     }
 
     /**
      * Set grid row callback function.
      *
-     * @param Closure $callable
-     *
-     * @return Collection|void
+     * @return Collection
      */
-    public function rows(Closure $callable = null)
+    public function rows()
     {
-        if (is_null($callable)) {
-            return $this->rows;
-        }
-
-        $this->rowsCallback[] = $callable;
+        return $this->rows;
     }
 
     /**
@@ -763,23 +747,11 @@ HTML;
     /**
      * Get or set resource path.
      *
-     * @param string $path
-     *
-     * @return $this|string
+     * @return string
      */
-    public function resource(string $path = null)
+    public function resource()
     {
-        if ($path === null) {
-            return $this->resourcePath ?: (
-            $this->resourcePath = url(app('request')->getPathInfo())
-            );
-        }
-
-        if (! empty($path)) {
-            $this->resourcePath = admin_url($path);
-        }
-
-        return $this;
+        return $this->resourcePath;
     }
 
     /**
@@ -892,7 +864,7 @@ HTML;
      */
     public function setResource($path)
     {
-        $this->resourcePath = $path;
+        $this->resourcePath = admin_url($path);
 
         return $this;
     }

+ 58 - 0
src/Grid/Concerns/HasEvents.php

@@ -0,0 +1,58 @@
+<?php
+
+namespace Dcat\Admin\Grid\Concerns;
+
+use Dcat\Admin\Grid\Events;
+use Illuminate\Support\Facades\Event;
+
+trait HasEvents
+{
+    /**
+     * @var array
+     */
+    protected $dispatched = [];
+
+    /**
+     * 监听事件.
+     *
+     * @param string $class
+     *
+     * @param \Closure $callback
+     */
+    public function listen(string $class, \Closure $callback)
+    {
+        Event::listen($class, function (Events\Event $event) use ($callback) {
+            if ($event->grid !== $this) {
+                return;
+            }
+
+            return $callback($event->grid, ...$event->payload);
+        });
+    }
+
+    /**
+     * 触发事件.
+     *
+     * @param \Dcat\Admin\Grid\Events\Event $event
+     */
+    public function fire(Events\Event $event)
+    {
+        Event::dispatch($event);
+
+        $this->dispatched[get_class($event)] = $event;
+    }
+
+    /**
+     * 只触发一次.
+     *
+     * @param \Dcat\Admin\Grid\Events\Event $event
+     */
+    public function fireOnce(Events\Event $event)
+    {
+        if (isset($this->dispatched[get_class($event)])) {
+            return;
+        }
+
+        return $this->fire($event);
+    }
+}

+ 2 - 0
src/Grid/Concerns/HasExporter.php

@@ -75,6 +75,8 @@ trait HasExporter
 
         $this->callBuilder();
 
+        $this->fire(new Grid\Events\Exporting($this, [$scope]));
+
         // clear output buffer.
         if (ob_get_length()) {
             ob_end_clean();

+ 4 - 2
src/Grid/Concerns/HasFilter.php

@@ -25,7 +25,7 @@ trait HasFilter
      *
      * @return void
      */
-    protected function setupFilter()
+    protected function setUpFilter()
     {
         $this->filter = new Grid\Filter($this->model());
     }
@@ -41,7 +41,9 @@ trait HasFilter
     {
         $this->callBuilder();
         $this->handleExportRequest();
-        $this->callFetchingCallbacks();
+
+        $this->fireOnce(new Grid\Events\Fetching($this));
+
         $this->applyQuickSearch();
         $this->applyColumnFilter();
         $this->applySelectorQuery();

+ 5 - 31
src/Grid/Concerns/HasNames.php

@@ -16,18 +16,6 @@ trait HasNames
      */
     protected $_name;
 
-    /**
-     * HTML element names.
-     *
-     * @var array
-     */
-    protected $elementNames = [
-        'grid_row'        => 'grid-row',
-        'grid_select_all' => 'grid-select-all',
-        'grid_per_page'   => 'grid-per-pager',
-        'export_selected' => 'export-selected',
-    ];
-
     /**
      * Set name to grid.
      *
@@ -84,7 +72,7 @@ trait HasNames
             return;
         }
 
-        return $name.'-';
+        return $name.'_';
     }
 
     /**
@@ -92,7 +80,7 @@ trait HasNames
      */
     public function getRowName()
     {
-        return $this->getElementNameWithPrefix('grid_row');
+        return $this->makeName('grid-row');
     }
 
     /**
@@ -100,7 +88,7 @@ trait HasNames
      */
     public function getSelectAllName()
     {
-        return $this->getElementNameWithPrefix('grid_select_all');
+        return $this->makeName('grid-select-all');
     }
 
     /**
@@ -108,7 +96,7 @@ trait HasNames
      */
     public function getPerPageName()
     {
-        return $this->getElementNameWithPrefix('grid_per_page');
+        return $this->makeName('grid-per-page');
     }
 
     /**
@@ -116,20 +104,6 @@ trait HasNames
      */
     public function getExportSelectedName()
     {
-        return $this->getElementNameWithPrefix('export_selected');
-    }
-
-    /**
-     * @return string
-     */
-    protected function getElementNameWithPrefix($name)
-    {
-        $elementName = $this->elementNames[$name];
-
-        if ($this->_name) {
-            return sprintf('%s-%s', $this->_name, $elementName);
-        }
-
-        return $elementName;
+        return $this->makeName('export-selected');
     }
 }

+ 4 - 1
src/Grid/Concerns/HasQuickSearch.php

@@ -3,6 +3,7 @@
 namespace Dcat\Admin\Grid\Concerns;
 
 use Dcat\Admin\Grid\Column;
+use Dcat\Admin\Grid\Events\ApplyQuickSearch;
 use Dcat\Admin\Grid\Model;
 use Dcat\Admin\Grid\Tools;
 use Dcat\Admin\Support\Helper;
@@ -82,12 +83,14 @@ trait HasQuickSearch
             return;
         }
 
-        $query = request()->get($this->quickSearch->getQueryName());
+        $query = request($this->quickSearch->getQueryName());
 
         if ($query === '' || $query === null) {
             return;
         }
 
+        $this->fireOnce(new ApplyQuickSearch($this, [$query]));
+
         // 树表格子节点忽略查询条件
         $this->model()
             ->disableBindTreeQuery()

+ 2 - 0
src/Grid/Concerns/HasSelector.php

@@ -58,6 +58,8 @@ trait HasSelector
                 return;
             }
 
+            $this->fireOnce(new Grid\Events\ApplySelector($this));
+
             $values = $active[$key];
             if ($selector['type'] == 'one') {
                 $values = current($values);

+ 1 - 1
src/Grid/Concerns/HasTools.php

@@ -21,7 +21,7 @@ trait HasTools
     /**
      * Setup grid tools.
      */
-    public function setupTools()
+    public function setUpTools()
     {
         $this->tools = new Tools($this);
     }

+ 3 - 4
src/Grid/Concerns/HasTree.php

@@ -3,6 +3,7 @@
 namespace Dcat\Admin\Grid\Concerns;
 
 use Dcat\Admin\Admin;
+use Dcat\Admin\Grid\Events\Fetched;
 use Dcat\Admin\Support\Helper;
 use Illuminate\Support\Collection;
 
@@ -58,9 +59,9 @@ trait HasTree
             $this->addIgnoreQueries();
         });
 
-        $this->collection(function (Collection $collection) {
+        $this->grid()->listen(Fetched::class, function (Grid $grid, Collection $collection) {
             if (! $this->getParentIdFromRequest()) {
-                return $collection;
+                return;
             }
 
             if ($collection->isEmpty()) {
@@ -68,8 +69,6 @@ trait HasTree
             }
 
             $this->buildChildrenNodesPagination();
-
-            return $collection;
         });
     }
 

+ 7 - 0
src/Grid/Events/ApplyFilter.php

@@ -0,0 +1,7 @@
+<?php
+
+namespace Dcat\Admin\Grid\Events;
+
+class ApplyFilter extends Event
+{
+}

+ 7 - 0
src/Grid/Events/ApplyQuickSearch.php

@@ -0,0 +1,7 @@
+<?php
+
+namespace Dcat\Admin\Grid\Events;
+
+class ApplyQuickSearch extends Event
+{
+}

+ 7 - 0
src/Grid/Events/ApplySelector.php

@@ -0,0 +1,7 @@
+<?php
+
+namespace Dcat\Admin\Grid\Events;
+
+class ApplySelector extends Event
+{
+}

+ 21 - 0
src/Grid/Events/Event.php

@@ -0,0 +1,21 @@
+<?php
+
+namespace Dcat\Admin\Grid\Events;
+
+use Dcat\Admin\Grid;
+
+abstract class Event
+{
+    /**
+     * @var Grid
+     */
+    public $grid;
+
+    public $payload = [];
+
+    public function __construct(Grid $grid, array $payload = [])
+    {
+        $this->grid = $grid;
+        $this->payload = $payload;
+    }
+}

+ 7 - 0
src/Grid/Events/Exporting.php

@@ -0,0 +1,7 @@
+<?php
+
+namespace Dcat\Admin\Grid\Events;
+
+class Exporting extends Event
+{
+}

+ 7 - 0
src/Grid/Events/Fetched.php

@@ -0,0 +1,7 @@
+<?php
+
+namespace Dcat\Admin\Grid\Events;
+
+class Fetched extends Event
+{
+}

+ 7 - 0
src/Grid/Events/Fetching.php

@@ -0,0 +1,7 @@
+<?php
+
+namespace Dcat\Admin\Grid\Events;
+
+class Fetching extends Event
+{
+}

+ 3 - 0
src/Grid/Filter.php

@@ -4,6 +4,7 @@ namespace Dcat\Admin\Grid;
 
 use Dcat\Admin\Admin;
 use Dcat\Admin\Exception\RuntimeException;
+use Dcat\Admin\Grid\Events\ApplyFilter;
 use Dcat\Admin\Grid\Filter\AbstractFilter;
 use Dcat\Admin\Grid\Filter\Between;
 use Dcat\Admin\Grid\Filter\Date;
@@ -459,6 +460,8 @@ class Filter implements Renderable
             if (! empty($conditions)) {
                 $this->expand();
 
+                $this->grid()->fireOnce(new ApplyFilter($this, [$conditions]));
+
                 $this->grid()->model()->disableBindTreeQuery();
             }
 

+ 3 - 32
src/Grid/Model.php

@@ -5,7 +5,7 @@ namespace Dcat\Admin\Grid;
 use Dcat\Admin\Admin;
 use Dcat\Admin\Exception\AdminException;
 use Dcat\Admin\Grid;
-use Dcat\Admin\Middleware\Pjax;
+use Dcat\Admin\Http\Middleware\Pjax;
 use Dcat\Admin\Repositories\Repository;
 use Illuminate\Contracts\Support\Arrayable;
 use Illuminate\Database\Eloquent\Relations\Relation;
@@ -101,13 +101,6 @@ class Model
      */
     protected $sortName = '_sort';
 
-    /**
-     * Collection callback.
-     *
-     * @var callable[]
-     */
-    protected $collectionCallback = [];
-
     /**
      * @var Grid
      */
@@ -330,20 +323,6 @@ class Model
         return $this;
     }
 
-    /**
-     * Set collection callback.
-     *
-     * @param callable $callback
-     *
-     * @return $this
-     */
-    public function collection(callable $callback = null)
-    {
-        $this->collectionCallback[] = $callback;
-
-        return $this;
-    }
-
     /**
      * Build.
      *
@@ -355,18 +334,10 @@ class Model
     {
         if (is_null($this->data)) {
             $this->setData($this->fetch());
-
-            if ($this->collectionCallback) {
-                $data = $this->data;
-
-                foreach ($this->collectionCallback as $cb) {
-                    $data = call_user_func($cb, $data);
-                }
-
-                $this->setData($data);
-            }
         }
 
+        $this->grid->fireOnce(new Grid\Events\Fetched($this->grid(), [$this->data]));
+
         return $toArray ? $this->data->toArray() : $this->data;
     }
 

Деякі файли не було показано, через те що забагато файлів було змінено