| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- <?php
- namespace Dcat\Admin\Widgets\DataCard;
- use Dcat\Admin\Admin;
- use Dcat\Admin\Widgets\Dropdown;
- use Dcat\Admin\Widgets\HasAjaxRequest;
- use Dcat\Admin\Widgets\Widget;
- use Illuminate\Contracts\Support\Renderable;
- use Illuminate\Support\Str;
- class Card extends Widget
- {
- use HasAjaxRequest;
- public static $js = [
- 'waypoints',
- 'jquery.counterup',
- ];
- public static $css = [
- 'waypoints',
- 'jquery.counterup',
- ];
- protected $view = 'admin::widgets.data-card';
- protected $options = [
- 'title' => null,
- 'description' => null,
- 'tools' => [],
- 'progress' => [],
- 'content' => ['left' => '', 'right' => ''],
- 'show_tool_shadow' => false,
- ];
- public function __construct($title = null, $description = null)
- {
- $this->title($title);
- $this->description($description);
- $this->class('card-box')
- ->style('height:160px')
- ->id('smc_'.Str::random(8));
- }
- public function showToolShadow()
- {
- $this->options['show_tool_shadow'] = true;
- return $this;
- }
- public function title($title)
- {
- $this->options['title'] = $title;
- return $this;
- }
- public function description($content)
- {
- $this->options['description'] = $content;
- return $this;
- }
- /**
- * @param $number
- *
- * @return $this
- */
- public function number($number)
- {
- return $this->content("<number>$number</number>");
- }
- /**
- * @param string|\Closure|Renderable $content
- * @param string $position
- *
- * @return $this
- */
- public function content($content, string $position = 'left')
- {
- $this->options['content'][$position] = $this->toString($content);
- return $this;
- }
- /**
- * @param string|\Closure|Renderable $content
- *
- * @return $this
- */
- public function rightContent($content)
- {
- return $this->content($content, 'right');
- }
- /**
- * @param int $number
- * @param string $style
- *
- * @return $this
- */
- public function progress($number, $style = 'primary')
- {
- $this->options['progress'] = [
- 'percent' => $number,
- 'style' => $style,
- ];
- return $this;
- }
- /**
- * @param string|\Closure|Renderable $content
- *
- * @return $this
- */
- public function tool($content)
- {
- $this->options['tools'][] = $this->toString($content);
- 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(
- Dropdown::make($options)
- ->click()
- ->button($defaultLabel)
- ->buttonClass('btn btn-xs btn-light')
- ->map($builder)
- );
- }
- /**
- * Setup scripts.
- *
- * @return string
- */
- protected function script()
- {
- if (! $this->allowBuildRequestScript()) {
- return;
- }
- $this->setupFetchScript();
- return $this->buildRequestScript();
- }
- /**
- * @return void
- */
- protected function setupFetchScript()
- {
- $id = $this->getHtmlAttribute('id');
- $this->fetching(
- <<<JS
- var card = $('#{$id}');
- card.loading({style:'bottom:20px'})
- JS
- );
- $this->fetched(
- <<<'JS'
- if (!response.status) {
- return Dcat.error(response.message || 'Server internal error.');
- }
- var w = (response.progress || 0) + '%', pg = card.find('.progress-bar');
-
- card.loading(false)
- card.find('.right-content').html(response.content.right || '');
- card.find('.main-content').html(response.content.left || '');
- pg.css({width: 0});
- setTimeout(function(){ pg.css({width: w});}, 150);
- card.find('number').counterUp({time: 550});
- JS
- );
- }
- /**
- * @return string
- */
- public function render()
- {
- $this->script = $this->script();
- return parent::render(); // TODO: Change the autogenerated stub
- }
- /**
- * @return array
- */
- public function buildJsonResponseArray()
- {
- return [
- 'status' => 1,
- 'content' => &$this->options['content'],
- 'progress' => $this->options['progress']['percent'] ?? 0,
- ];
- }
- /**
- * Return JsonResponse instance.
- *
- * @param array $data
- *
- * @return \Illuminate\Http\JsonResponse
- */
- public function toJsonResponse(array $data = [])
- {
- return response()->json(array_merge($this->buildJsonResponseArray(), $data));
- }
- }
|