소스 검색

重构RowSelector

jqh 5 년 전
부모
커밋
a521adb1e7
6개의 변경된 파일179개의 추가작업 그리고 121개의 파일을 삭제
  1. 2 61
      src/Grid.php
  2. 53 0
      src/Grid/Concerns/HasRowSelector.php
  3. 1 1
      src/Grid/Displayers/DropdownActions.php
  4. 0 58
      src/Grid/Displayers/RowSelector.php
  5. 122 0
      src/Grid/Tools/RowSelector.php
  6. 1 1
      src/SimpleGrid.php

+ 2 - 61
src/Grid.php

@@ -30,9 +30,10 @@ class Grid
         Concerns\HasPaginator,
         Concerns\HasExporter,
         Concerns\HasMultipleHeaders,
-        Concerns\HasQuickSearch,
         Concerns\HasSelector,
+        Concerns\HasRowSelector,
         Concerns\HasQuickCreate,
+        Concerns\HasQuickSearch,
         Macroable {
             __call as macroCall;
         }
@@ -168,12 +169,6 @@ class Grid
         'show_exporter'            => false,
         'create_mode'              => self::CREATE_MODE_DEFAULT,
 
-        'row_selector_style'     => 'primary',
-        'row_selector_circle'    => true,
-        'row_selector_clicktr'   => false,
-        'row_selector_label_key' => null,
-        'row_selector_bg'        => null,
-
         'dialog_form_area'   => ['700px', '670px'],
         'table_header_style' => 'table-header-gray',
     ];
@@ -332,60 +327,6 @@ class Grid
         return $this->columnNames;
     }
 
-    /**
-     * @param array $options
-     *
-     * @return $this
-     */
-    public function setRowSelectorOptions(array $options = [])
-    {
-        if (isset($options['style'])) {
-            $this->options['row_selector_style'] = $options['style'];
-        }
-        if (isset($options['circle'])) {
-            $this->options['row_selector_circle'] = $options['circle'];
-        }
-        if (isset($options['clicktr'])) {
-            $this->options['row_selector_clicktr'] = $options['clicktr'];
-        }
-        if (isset($options['label'])) {
-            $this->options['row_selector_label_key'] = $options['label_name'];
-        }
-        if (isset($options['bg'])) {
-            $this->options['row_selector_bg'] = $options['bg'];
-        }
-
-        return $this;
-    }
-
-    /**
-     * Prepend checkbox column for grid.
-     *
-     * @return void
-     */
-    protected function prependRowSelectorColumn()
-    {
-        if (! $this->options['show_row_selector']) {
-            return;
-        }
-
-        $circle = $this->options['row_selector_circle'] ? 'checkbox-circle' : '';
-
-        $column = new Column(
-            Column::SELECT_COLUMN_NAME,
-            <<<HTML
-<div class="checkbox checkbox-{$this->options['row_selector_style']} $circle checkbox-grid">
-    <input type="checkbox" class="select-all {$this->selectAllName()}"><label></label>
-</div>
-HTML
-        );
-        $column->setGrid($this);
-
-        $column->displayUsing(Displayers\RowSelector::class);
-
-        $this->columns->prepend($column, Column::SELECT_COLUMN_NAME);
-    }
-
     /**
      * Apply column filter to grid query.
      */

+ 53 - 0
src/Grid/Concerns/HasRowSelector.php

@@ -0,0 +1,53 @@
+<?php
+
+namespace Dcat\Admin\Grid\Concerns;
+
+use Dcat\Admin\Grid;
+
+/**
+ * @mixin Grid
+ */
+trait HasRowSelector
+{
+    /**
+     * @var Grid\Tools\RowSelector
+     */
+    protected $_rowSelector;
+
+    /**
+     * @param \Closure $closure
+     *
+     * @return Grid\Tools\RowSelector
+     */
+    public function rowSelector()
+    {
+        return $this->_rowSelector ?: ($this->_rowSelector = new Grid\Tools\RowSelector($this));
+    }
+
+    /**
+     * Prepend checkbox column for grid.
+     *
+     * @return void
+     */
+    protected function prependRowSelectorColumn()
+    {
+        if (! $this->options['show_row_selector']) {
+            return;
+        }
+
+        $rowSelector = $this->rowSelector();
+        $keyName = $this->keyName();
+
+        $column = new Grid\Column(
+            Grid\Column::SELECT_COLUMN_NAME,
+            $rowSelector->renderHeader()
+        );
+        $column->setGrid($this);
+
+        $column->display(function () use ($rowSelector, $keyName)  {
+            return $rowSelector->render($this, $this->{$keyName});
+        });
+
+        $this->columns->prepend($column, Grid\Column::SELECT_COLUMN_NAME);
+    }
+}

+ 1 - 1
src/Grid/Displayers/DropdownActions.php

@@ -39,7 +39,7 @@ class DropdownActions extends Actions
      */
     protected function addScript()
     {
-        $background = $this->grid->option('row_selector_bg') ?: Color::dark20();
+        $background = Color::dark20();
         $checkbox = ".{$this->grid->rowName()}-checkbox";
 
         $script = <<<JS

+ 0 - 58
src/Grid/Displayers/RowSelector.php

@@ -1,58 +0,0 @@
-<?php
-
-namespace Dcat\Admin\Grid\Displayers;
-
-use Dcat\Admin\Admin;
-use Dcat\Admin\Widgets\Color;
-
-class RowSelector extends AbstractDisplayer
-{
-    public function display()
-    {
-        $this->setupScript();
-
-        $style = $this->grid->option('row_selector_style');
-        $circle = $this->grid->option('row_selector_circle') ? 'checkbox-circle' : '';
-
-        return <<<EOT
-<div class="checkbox $circle checkbox-$style checkbox-grid">
-    <input type="checkbox" class="{$this->grid->rowName()}-checkbox" data-id="{$this->key()}" data-label="{$this->label()}">
-    <label></label>
-</div>
-EOT;
-    }
-
-    protected function setupScript()
-    {
-        $clickTr = $this->grid->option('row_selector_clicktr') ? 'true' : 'false';
-        $background = $this->grid->option('row_selector_bg') ?: Color::dark20();
-
-        Admin::script(
-            <<<JS
-var selector = LA.RowSelector({
-    checkbox: '.{$this->grid->rowName()}-checkbox',
-    selectAll: '.{$this->grid->selectAllName()}', 
-    clickTr: {$clickTr},
-    bg: '{$background}',
-});
-LA.grid.addSelector(selector, '{$this->grid->getName()}');
-JS
-        );
-    }
-
-    protected function label()
-    {
-        if ($column = $this->grid->option('row_selector_label_key')) {
-            $label = $this->row->{$column};
-            if ($label !== null && $label !== '') {
-                return $label;
-            }
-
-            return $this->key();
-        }
-
-        $label = $this->row->name ?: $this->row->title;
-
-        return $label ?: ($this->row->username ?: $this->key());
-    }
-}

+ 122 - 0
src/Grid/Tools/RowSelector.php

@@ -0,0 +1,122 @@
+<?php
+
+namespace Dcat\Admin\Grid\Tools;
+
+use Dcat\Admin\Admin;
+use Dcat\Admin\Grid;
+use Dcat\Admin\Widgets\Color;
+
+class RowSelector
+{
+    protected $grid;
+
+    protected $style = 'primary';
+
+    protected $circle = true;
+
+    protected $background;
+
+    protected $rowClickable = false;
+
+    protected $titleKey;
+
+    public function __construct(Grid $grid)
+    {
+        $this->grid = $grid;
+    }
+
+    public function style(string $style)
+    {
+        $this->style = $style;
+
+        return $this;
+    }
+
+    public function circle(bool $value = true)
+    {
+        $this->circle = $value;
+
+        return $this;
+    }
+
+    public function background(string $value)
+    {
+        $this->background = $value;
+
+        return $this;
+    }
+
+    public function rowClickable(bool $value = true)
+    {
+        $this->rowClickable = $value;
+
+        return $this;
+    }
+
+    public function titleKey(string $value)
+    {
+        $this->titleKey = $value;
+
+        return $this;
+    }
+
+    public function renderHeader()
+    {
+        $circle = $this->circle ? 'checkbox-circle' : '';
+
+        return <<<HTML
+<div class="checkbox checkbox-{$this->style} {$circle} checkbox-grid">
+    <input type="checkbox" class="select-all {$this->grid->selectAllName()}"><label></label>
+</div>
+HTML;
+
+    }
+
+    public function render($row, $id)
+    {
+        $this->setupScript();
+
+        $circle = $this->circle ? 'checkbox-circle' : '';
+
+        return <<<EOT
+<div class="checkbox {$circle} checkbox-{$this->style} checkbox-grid">
+    <input type="checkbox" class="{$this->grid->rowName()}-checkbox" data-id="{$id}" data-label="{$this->title($row, $id)}">
+    <label></label>
+</div>
+EOT;
+    }
+
+    protected function setupScript()
+    {
+        $clickable = $this->rowClickable ? 'true' : 'false';
+        $background = $this->background ?: Color::dark20();
+
+        Admin::script(
+            <<<JS
+var selector = LA.RowSelector({
+    checkbox: '.{$this->grid->rowName()}-checkbox',
+    selectAll: '.{$this->grid->selectAllName()}', 
+    clickTr: {$clickable},
+    bg: '{$background}',
+});
+LA.grid.addSelector(selector, '{$this->grid->getName()}');
+JS
+        );
+    }
+
+    protected function title($row, $id)
+    {
+        if ($key = $this->titleKey) {
+            $label = $row->{$key};
+            if ($label !== null && $label !== '') {
+                return $label;
+            }
+
+            return $id;
+        }
+
+        $label = $row->name ?: $row->title;
+
+        return $label ?: ($row->username ?: $id);
+    }
+}

+ 1 - 1
src/SimpleGrid.php

@@ -20,7 +20,7 @@ class SimpleGrid extends Grid
         $this->disableBatchActions();
         $this->disableFilterButton();
 
-        $this->option('row_selector_clicktr', true);
+        $this->rowSelector()->rowClickable(true);
 
         Content::composing(function (Content $content) {
             $content->simple();