Parcourir la source

update widgets

jqh il y a 6 ans
Parent
commit
788d850542

+ 7 - 2
src/Widgets/Accordion.php

@@ -2,10 +2,9 @@
 
 namespace Dcat\Admin\Widgets;
 
-use Illuminate\Contracts\Support\Renderable;
 use Illuminate\Support\Str;
 
-class Accordion extends Widget implements Renderable
+class Accordion extends Widget
 {
     /**
      * @var string
@@ -31,11 +30,17 @@ class Accordion extends Widget implements Renderable
         $this->id('accordion-'.Str::random(8));
     }
 
+    /**
+     * @return $this
+     */
     public function white()
     {
         return $this->panelStyle('white');
     }
 
+    /**
+     * @return $this
+     */
     public function panelStyle(string $style)
     {
         $this->panelStyle = 'panel-'.$style;

+ 49 - 10
src/Widgets/AjaxRequestBuilder.php

@@ -9,21 +9,29 @@ trait AjaxRequestBuilder
     /**
      * @var string
      */
-    protected $url;
+    protected $__url;
 
     /**
      * @var array
      */
     protected $buttonSelectors = [];
 
+    /**
+     * @var string
+     */
     protected $fn;
 
+    /**
+     * @var array
+     */
     protected $javascripts = [
         'fetching' => [],
         'fetched'  => [],
     ];
 
     /**
+     * Set request url.
+     *
      * @param string $url
      * @return $this
      */
@@ -32,27 +40,38 @@ trait AjaxRequestBuilder
         return $this->setUrl($url);
     }
 
+    /**
+     * Set current url to request.
+     *
+     * @param string $url
+     * @return $this
+     */
     public function requestCurrent(array $query = [])
     {
-        $this->url = url(request()->getPathInfo()).'?'.http_build_query($query);
+        $this->__url = url(request()->getPathInfo()).'?'.http_build_query($query);
 
         return $this;
     }
 
     /**
+     * Set request url.
+     *
      * @param string $url
      * @return $this
      */
     public function setUrl(string $url)
     {
-        $this->url = admin_url($url);
+        $this->__url = admin_url($url);
 
         return $this;
     }
 
+    /**
+     * @return string
+     */
     public function getUrl()
     {
-        return $this->url;
+        return $this->__url;
     }
 
     /**
@@ -64,7 +83,7 @@ trait AjaxRequestBuilder
     }
 
     /**
-     * 绑定重新获取数据按钮css选择器
+     * Set css selectors of refetch links.
      *
      * @param string|array $selector
      * @return $this
@@ -85,6 +104,12 @@ trait AjaxRequestBuilder
         return $this->buttonSelectors;
     }
 
+    /**
+     * Set the script before fetch data.
+     *
+     * @param string $script
+     * @return $this
+     */
     public function fetching(string $script)
     {
         $this->javascripts['fetching'][] = $script;
@@ -92,6 +117,12 @@ trait AjaxRequestBuilder
         return $this;
     }
 
+    /**
+     * Set the script after fetch data.
+     *
+     * @param string $script
+     * @return $this
+     */
     public function fetched(string $script)
     {
         $this->javascripts['fetched'][] = $script;
@@ -99,11 +130,17 @@ trait AjaxRequestBuilder
         return $this;
     }
 
+    /**
+     * @return bool
+     */
     public function allowBuildFetchingScript()
     {
-        return $this->url === null ? false : true;
+        return $this->__url === null ? false : true;
     }
 
+    /**
+     * @return bool|string
+     */
     public function buildFetchingScript()
     {
         if (!$this->allowBuildFetchingScript()) {
@@ -120,26 +157,28 @@ trait AjaxRequestBuilder
             $binding .= "$('{$v}').click(function () { {$this->fn}($(this).data()) });";
         }
 
-        return <<<SCRIPT
+        return <<<JS
 window.{$this->fn} = function (p) {
     $fetching;     
-    $.getJSON('{$this->url}', $.extend({_token:LA.token}, p || {}), function (result) {
+    $.getJSON('{$this->__url}', $.extend({_token:LA.token}, p || {}), function (result) {
         {$fetched};
     });
 }
 {$this->fn}();
 $binding;
-SCRIPT;
+JS;
 
     }
 
     /**
+     * Copy the given AjaxRequestBuilder.
+     *
      * @param AjaxRequestBuilder $fetcher
      * @return $this
      */
     public function copy($fetcher)
     {
-        $this->url = $fetcher->getUrl();
+        $this->__url = $fetcher->getUrl();
 
         $this->buttonSelectors = $fetcher->getButtonSelectors();
 

+ 36 - 3
src/Widgets/Alert.php

@@ -4,7 +4,7 @@ namespace Dcat\Admin\Widgets;
 
 use Illuminate\Contracts\Support\Renderable;
 
-class Alert extends Widget implements Renderable
+class Alert extends Widget
 {
     /**
      * @var string
@@ -52,6 +52,12 @@ class Alert extends Widget implements Renderable
         $this->style($style);
     }
 
+    /**
+     * Set title.
+     *
+     * @param string $title
+     * @return $this
+     */
     public function title($title)
     {
         $this->title = $title;
@@ -59,6 +65,12 @@ class Alert extends Widget implements Renderable
         return $this;
     }
 
+    /**
+     * Set contents.
+     *
+     * @param string|\Closure|Renderable $content
+     * @return $this
+     */
     public function content($content)
     {
         $this->content = $this->toString($content);
@@ -66,24 +78,45 @@ class Alert extends Widget implements Renderable
         return $this;
     }
 
+    /**
+     * Set info style.
+     *
+     * @return $this
+     */
     public function info()
     {
         return $this->style('info')->icon('fa fa-info');
     }
 
+    /**
+     * Set success style.
+     *
+     * @return $this
+     */
     public function success()
     {
         return $this->style('success')->icon('fa fa-check');
     }
 
+    /**
+     * Set warning style.
+     *
+     * @return $this
+     */
     public function warning()
     {
         return $this->style('warning')->icon('fa fa-warning');
     }
 
-    public function disableCloseButton()
+    /**
+     * Disable close button.
+     *
+     * @param bool $value
+     * @return $this
+     */
+    public function disableCloseButton(bool $value = true)
     {
-        $this->showCloseBtn = false;
+        $this->showCloseBtn = ! $value;
 
         return $this;
     }

+ 1 - 1
src/Widgets/Box.php

@@ -4,7 +4,7 @@ namespace Dcat\Admin\Widgets;
 
 use Illuminate\Contracts\Support\Renderable;
 
-class Box extends Widget implements Renderable
+class Box extends Widget
 {
     /**
      * @var string

+ 63 - 10
src/Widgets/Chart/Chart.php

@@ -43,6 +43,10 @@ abstract class Chart extends Widget
 
     protected $containerStyle = '';
 
+    /**
+     * Chart constructor.
+     * @param mixed ...$params
+     */
     public function __construct(...$params)
     {
         if (count($params) == 2) {
@@ -60,9 +64,7 @@ abstract class Chart extends Widget
             }
         }
 
-        if (!$this->colors) {
-            $this->colors = Colors::$charts['blue'];
-        }
+        $this->setDefaultColors();
     }
 
     /**
@@ -167,11 +169,17 @@ abstract class Chart extends Widget
         return $this;
     }
 
+    /**
+     * @return $this
+     */
     public function disableLegend()
     {
         return $this->legend(['display' => false]);
     }
 
+    /**
+     * @return $this
+     */
     public function legendPosition(string $val)
     {
         return $this->legend(['position' => $val]);
@@ -192,6 +200,11 @@ abstract class Chart extends Widget
         return $this;
     }
 
+    /**
+     * Disable tooltip.
+     *
+     * @return $this
+     */
     public function disableTooltip()
     {
         return $this->tooltips(['enabled' => false]);
@@ -268,12 +281,23 @@ abstract class Chart extends Widget
         return $this;
     }
 
+    /**
+     * Set width of container.
+     *
+     * @param string $width
+     * @return Chart
+     */
     public function width($width)
     {
         return $this->setContainerStyle('width:'.$width);
-
     }
 
+    /**
+     * Set height of container.
+     *
+     * @param string $height
+     * @return Chart
+     */
     public function height($height)
     {
         return $this->setContainerStyle('height:'.$height);
@@ -299,6 +323,7 @@ abstract class Chart extends Widget
      * Fill default color.
      *
      * @param array $colors
+     * @return void
      */
     protected function fillColor(array $colors = [])
     {
@@ -313,6 +338,8 @@ abstract class Chart extends Widget
 
     /**
      * Make element id.
+     *
+     * @return void
      */
     protected function makeId()
     {
@@ -343,8 +370,15 @@ abstract class Chart extends Widget
         ];
         $options = json_encode($config);
 
+        // Global configure.
+        $globalSettings = '';
+        foreach (self::$globalSettings as $k => $v) {
+            $globalSettings .= sprintf('Chart.defaults.global.%s="%s";', $k, $v);
+        }
+
         if (!$this->allowBuildFetchingScript()) {
             return <<<JS
+{$globalSettings}
 setTimeout(function(){ new Chart($("#{$this->id}").get(0).getContext("2d"), $options) },60)
 JS;
         }
@@ -363,7 +397,7 @@ window['obj'+id] = new Chart($("#"+id).get(0).getContext("2d"), opt);
 JS
         );
 
-        return $this->buildFetchingScript();
+        return $globalSettings.$this->buildFetchingScript();
 
     }
 
@@ -384,16 +418,14 @@ JS
 
     }
 
+    /**
+     * @return string
+     */
     public function render()
     {
         $this->makeId();
         $this->fillColor();
 
-        // Global configure.
-        foreach (self::$globalSettings as $k => $v) {
-            Admin::script(sprintf('Chart.defaults.global.%s="%s";', $k, $v));
-        }
-
         $this->script = $this->script();
 
         $this->setHtmlAttribute([
@@ -411,6 +443,11 @@ JS
 HTML;
     }
 
+    /**
+     * @param string $method
+     * @param array $parameters
+     * @return $this
+     */
     public function __call($method, $parameters)
     {
         if (isset(Colors::$charts[$method])) {
@@ -441,9 +478,25 @@ HTML;
         ));
     }
 
+    /**
+     * @return void
+     */
+    protected function setDefaultColors()
+    {
+        if (! $this->colors) {
+            $this->colors = Colors::$charts['blue'];
+        }
+    }
+
+    /**
+     * Collect assets.
+     *
+     * @return void
+     */
     public function collectAssets()
     {
         $this->script && Admin::script($this->script);
+
         Admin::collectComponentAssets('chartjs');
     }
 }

+ 2 - 9
src/Widgets/Colors.php

@@ -59,27 +59,20 @@ class Colors
             '#483D8B', // blue darker
         ],
 
-       'green' => [ // 绿色系
+       'green' => [
            'rgba(64,153,222,.5)', // primary
             '#21b978', // success
             '#47C1BF', // tear
             '#8FC15D', // green
         ],
 
-        'orange' => [ // 橙色系
+        'orange' => [
             'rgba(64,153,222,.5)', // primary
             '#F99037', // orange
             '#F5573B', // red
             '#F2CB22', // yellow
         ],
 
-//        'red2' => [ // 红色系
-//            '#F99037', // orange
-//            '#F5573B', // red
-//            '#F2CB22', // yellow
-//            '#ff5b5b', // danger
-//        ],
-
         'purple' => [
             'rgba(64,153,222,.5)', // primary
             'rgba(121,134,203, 1)', // purple

+ 43 - 11
src/Widgets/DataCard/Card.php

@@ -6,6 +6,7 @@ use Dcat\Admin\Admin;
 use Dcat\Admin\Widgets\Dropdown;
 use Dcat\Admin\Widgets\AjaxRequestBuilder;
 use Dcat\Admin\Widgets\Widget;
+use Illuminate\Contracts\Support\Renderable;
 use Illuminate\Support\Str;
 
 class Card extends Widget
@@ -65,26 +66,31 @@ class Card extends Widget
     }
 
     /**
-     * @param $val
+     * @param string|\Closure|Renderable $content
      * @param string $position
      * @return $this
      */
-    public function content($val, string $position = 'left')
+    public function content($content, string $position = 'left')
     {
-        $this->options['content'][$position] = $this->toString($val);
+        $this->options['content'][$position] = $this->toString($content);
 
         return $this;
     }
 
     /**
-     * @param $val
+     * @param string|\Closure|Renderable $content
      * @return $this
      */
-    public function rightContent($val)
+    public function rightContent($content)
     {
-        return $this->content($val, 'right');
+        return $this->content($content, 'right');
     }
 
+    /**
+     * @param int $number
+     * @param string $style
+     * @return $this
+     */
     public function progress($number, $style = 'primary')
     {
         $this->options['progress'] = [
@@ -95,6 +101,10 @@ class Card extends Widget
         return $this;
     }
 
+    /**
+     * @param string|\Closure|Renderable $content
+     * @return $this
+     */
     public function tool($content)
     {
         $this->options['tools'][] = $this->toString($content);
@@ -102,6 +112,12 @@ class Card extends Widget
         return $this;
     }
 
+    /**
+     * @param array $options
+     * @param \Closure $builder
+     * @param string|null $defaultLabel
+     * @return $this
+     */
     public function dropdown(array $options, \Closure $builder, ?string $defaultLabel = null)
     {
         return $this->tool(
@@ -113,6 +129,11 @@ class Card extends Widget
         );
     }
 
+    /**
+     * Setup scripts.
+     *
+     * @return string
+     */
     protected function script()
     {
         if (!$this->allowBuildFetchingScript()) {
@@ -124,19 +145,22 @@ class Card extends Widget
         return $this->buildFetchingScript();
     }
 
+    /**
+     * @return void
+     */
     protected function setupFetchScript()
     {
         $id = $this->getHtmlAttribute('id');
 
         $this->fetching(
-            <<<SCRIPT
+            <<<JS
 var card = $('#{$id}');   
 card.loading({style:'bottom:20px'})      
-SCRIPT
+JS
         );
 
         $this->fetched(
-            <<<SCRIPT
+            <<<JS
 if (!result.status) {
     return LA.error(result.message || 'Server internal error.');
 }     
@@ -148,11 +172,13 @@ card.find('.main-content').html(result.content.left || '');
 pg.css({width: 0});
 setTimeout(function(){ pg.css({width: w});}, 150);
 card.find('number').counterUp({time: 550});
-SCRIPT
-
+JS
         );
     }
 
+    /**
+     * @return string
+     */
     public function render()
     {
         $this->script = $this->script();
@@ -160,6 +186,9 @@ SCRIPT
         return parent::render(); // TODO: Change the autogenerated stub
     }
 
+    /**
+     * @return array
+     */
     public function buildJsonResponseArray()
     {
         return [
@@ -180,6 +209,9 @@ SCRIPT
         return response()->json(array_merge($this->buildJsonResponseArray(), $data));
     }
 
+    /**
+     * @return void
+     */
     public function collectAssets()
     {
         parent::collectAssets();

+ 9 - 4
src/Widgets/DataCard/DoughnutChartCard.php

@@ -15,17 +15,22 @@ class DoughnutChartCard extends Card
      */
     protected $chart;
 
-    protected $dotColors = [];
+    /**
+     * @var array
+     */
+    public $dotColors = [];
 
+    /**
+     * @var array
+     */
     protected $dots = [];
 
     public function __construct($title = null, $description = null)
     {
-        parent::__construct($title, $description);
-
         $this->setupChart();
-
         $this->setupDotColors();
+
+        parent::__construct($title, $description);
     }
 
     protected function setupChart()

+ 79 - 1
src/Widgets/Dropdown.php

@@ -4,6 +4,7 @@ namespace Dcat\Admin\Widgets;
 
 use Dcat\Admin\Admin;
 use Illuminate\Contracts\Support\Arrayable;
+use Illuminate\Contracts\Support\Renderable;
 use Illuminate\Support\Arr;
 use Illuminate\Support\Str;
 
@@ -11,9 +12,15 @@ class Dropdown extends Widget
 {
     const DIVIDER = '_divider';
 
+    /**
+     * @var string
+     */
     protected static $dividerHtml = '<li class="divider"></li>';
 
-    public $template = '<span class="dropdown" style="display:inline-block">%s<ul class="dropdown-menu">%s</ul></span>';
+    /**
+     * @var string
+     */
+    protected $template = '<span class="dropdown" style="display:inline-block">%s<ul class="dropdown-menu">%s</ul></span>';
 
     /**
      * @var array
@@ -54,6 +61,13 @@ class Dropdown extends Widget
         $this->options($options);
     }
 
+    /**
+     * Set the options of dropdown menus.
+     *
+     * @param array $options
+     * @param string|null $title
+     * @return $this
+     */
     public function options($options = [], string $title = null)
     {
         if (!$options) return $this;
@@ -73,6 +87,12 @@ class Dropdown extends Widget
         return $this;
     }
 
+    /**
+     * Set the button text.
+     *
+     * @param string|null $text
+     * @return $this
+     */
     public function button(?string $text)
     {
         $this->button['text'] = $text;
@@ -80,11 +100,22 @@ class Dropdown extends Widget
         return $this;
     }
 
+    /**
+     * Without text of button.
+     *
+     * @return $this
+     */
     public function withoutTextButton()
     {
         return $this->button('');
     }
 
+    /**
+     * Set the button class.
+     *
+     * @param string $class
+     * @return $this
+     */
     public function buttonClass(string $class)
     {
         $this->button['class'] = $class;
@@ -92,6 +123,12 @@ class Dropdown extends Widget
         return $this;
     }
 
+    /**
+     * Set the button style.
+     *
+     * @param string $class
+     * @return $this
+     */
     public function buttonStyle(string $style)
     {
         $this->button['style'] = $style;
@@ -99,6 +136,12 @@ class Dropdown extends Widget
         return $this;
     }
 
+    /**
+     * Show divider.
+     *
+     * @param string $class
+     * @return $this
+     */
     public function divider()
     {
         $this->divider = true;
@@ -106,6 +149,12 @@ class Dropdown extends Widget
         return $this;
     }
 
+    /**
+     * Applies the callback to the elements of the options.
+     *
+     * @param string $class
+     * @return $this
+     */
     public function map(\Closure $builder)
     {
         $this->builder = $builder;
@@ -113,6 +162,12 @@ class Dropdown extends Widget
         return $this;
     }
 
+    /**
+     * Add click event listener.
+     *
+     * @param string|null $defaultLabel
+     * @return $this
+     */
     public function click(?string $defaultLabel = null)
     {
         $this->click = true;
@@ -126,6 +181,12 @@ class Dropdown extends Widget
         return $this;
     }
 
+    /**
+     * Set the template of dropdown menu.
+     *
+     * @param string|\Closure|Renderable $template
+     * @return $this
+     */
     public function template($template)
     {
         $this->template = $this->toString($template);
@@ -133,6 +194,9 @@ class Dropdown extends Widget
         return $this;
     }
 
+    /**
+     * @return string
+     */
     protected function renderButton()
     {
         if (is_null($this->button['text']) && !$this->click) return;
@@ -170,11 +234,17 @@ HTML
         );
     }
 
+    /**
+     * @return string
+     */
     public function getButtonId()
     {
         return $this->buttonId;
     }
 
+    /**
+     * @return string
+     */
     protected function renderOptions()
     {
         $opt = '';
@@ -194,6 +264,11 @@ HTML
         return $opt;
     }
 
+    /**
+     * @param mixed $k
+     * @param mixed $v
+     * @return mixed|string
+     */
     protected function renderOption($k, $v)
     {
         if ($v === static::DIVIDER) {
@@ -215,6 +290,9 @@ HTML
         return $v;
     }
 
+    /**
+     * @return string
+     */
     public function render()
     {
         if (is_null($this->button['text']) && !$this->options) {

+ 8 - 0
src/Widgets/Dump.php

@@ -50,6 +50,10 @@ class Dump extends Widget
         return $this;
     }
 
+    /**
+     * @param string|null $padding
+     * @return $this
+     */
     public function padding(?string $padding)
     {
         if ($padding) {
@@ -59,6 +63,10 @@ class Dump extends Widget
         return $this;
     }
 
+    /**
+     * @param string $width
+     * @return $this
+     */
     public function maxWidth($width)
     {
         $this->maxWidth = $width;

+ 2 - 1
src/Widgets/Markdown.php

@@ -4,6 +4,7 @@ namespace Dcat\Admin\Widgets;
 
 use Dcat\Admin\Admin;
 use Illuminate\Contracts\Support\Renderable;
+use Illuminate\Support\Str;
 
 class Markdown extends Widget
 {
@@ -84,7 +85,7 @@ EOF;
 
     public function render()
     {
-        $id = uniqid();
+        $id = 'mkd-'.Str::random();
 
         $this->defaultHtmlAttribute('id', $id);
 

+ 54 - 7
src/Widgets/Sparkline/Sparkline.php

@@ -84,6 +84,12 @@ class Sparkline extends Widget
         $this->options['type'] = $this->type;
     }
 
+    /**
+     * Get or set the sparkline values.
+     *
+     * @param mixed|null $values
+     * @return $this|array
+     */
     public function values($values = null)
     {
         if ($values === null) {
@@ -101,6 +107,12 @@ class Sparkline extends Widget
         return $this;
     }
 
+    /**
+     * Set width of sparkline.
+     *
+     * @param int $width
+     * @return $this
+     */
     public function width($width)
     {
         $this->options['width'] = $width;
@@ -108,6 +120,12 @@ class Sparkline extends Widget
         return $this;
     }
 
+    /**
+     * Set height of sparkline.
+     *
+     * @param int $width
+     * @return $this
+     */
     public function height($height)
     {
         $this->options['height'] = $height;
@@ -117,6 +135,12 @@ class Sparkline extends Widget
         return $this;
     }
 
+    /**
+     * Composite the given sparkline.
+     *
+     * @param int $width
+     * @return $this
+     */
     public function composite(Sparkline $chart)
     {
         $options = $chart->getOptions();
@@ -128,7 +152,12 @@ class Sparkline extends Widget
         return $this;
     }
 
-
+    /**
+     * Setup scripts.
+     *
+     * @param int $width
+     * @return string
+     */
     protected function script()
     {
         $values  = json_encode($this->values);
@@ -165,6 +194,9 @@ JS
         return $this->buildFetchingScript();
     }
 
+    /**
+     * @return string
+     */
     public function render()
     {
         $this->makeId();
@@ -182,6 +214,23 @@ HTML;
 
     }
 
+    /**
+     * Get element id.
+     *
+     * @return string
+     */
+    public function getId()
+    {
+        $this->makeId();
+
+        return $this->id;
+    }
+
+    /**
+     * @param string $method
+     * @param array $parameters
+     * @return Sparkline|Widget
+     */
     public function __call($method, $parameters)
     {
         if (in_array($method, static::$optionMethods)) {
@@ -191,15 +240,11 @@ HTML;
         return parent::__call($method, $parameters); // TODO: Change the autogenerated stub
     }
 
-    public function getId()
-    {
-        $this->makeId();
-
-        return $this->id;
-    }
 
     /**
      * Make element id.
+     *
+     * @return void
      */
     protected function makeId()
     {
@@ -230,6 +275,8 @@ HTML;
 
     /**
      * Collect assets.
+     *
+     * @return void
      */
     protected function collectAssets()
     {

+ 1 - 1
src/Widgets/Tab.php

@@ -4,7 +4,7 @@ namespace Dcat\Admin\Widgets;
 
 use Illuminate\Contracts\Support\Renderable;
 
-class Tab extends Widget implements Renderable
+class Tab extends Widget
 {
     const TYPE_CONTENT = 1;
     const TYPE_LINK = 2;

+ 1 - 2
src/Widgets/Table.php

@@ -2,10 +2,9 @@
 
 namespace Dcat\Admin\Widgets;
 
-use Illuminate\Contracts\Support\Renderable;
 use Illuminate\Support\Arr;
 
-class Table extends Widget implements Renderable
+class Table extends Widget
 {
     /**
      * @var string

+ 0 - 1
src/Widgets/Terminal.php

@@ -2,7 +2,6 @@
 
 namespace Dcat\Admin\Widgets;
 
-use Dcat\Admin\Admin;
 use Dcat\Admin\Support\StringOutput;
 use Illuminate\Support\Facades\Artisan;
 

+ 1 - 1
src/Widgets/Widget.php

@@ -6,7 +6,6 @@ use Dcat\Admin\Admin;
 use Dcat\Admin\Support\Helper;
 use Dcat\Admin\Traits\HasHtmlAttributes;
 use Illuminate\Contracts\Support\Arrayable;
-use Illuminate\Contracts\Support\Htmlable;
 use Illuminate\Contracts\Support\Renderable;
 
 /**
@@ -99,6 +98,7 @@ abstract class Widget implements Renderable
     protected function collectAssets()
     {
         $this->script && Admin::script($this->script);
+
         static::$js && Admin::js(static::$js);
         static::$css && Admin::css(static::$css);
     }