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");
}
/**
* @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(
<<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));
}
}