123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- <?php
- namespace Dcat\Admin\Form\Field;
- use Dcat\Admin\Admin;
- use Dcat\Admin\Form\Field;
- use Dcat\Admin\Support\Helper;
- use Illuminate\Contracts\Support\Arrayable;
- use Illuminate\Support\Facades\URL;
- class SelectResource extends Field
- {
- use PlainInput;
- protected static $js = [
- 'vendor/dcat-admin/dcat-admin/select-resource.min.js'
- ];
- protected $area = ['60%', '68%'];
- protected $source;
- protected $maxItem = 1;
- protected $style = 'primary';
- /**
- * Set window's area.
- *
- * @param string $width
- * @param string $height
- * @return $this
- */
- public function area(string $width, string $height)
- {
- $this->area = [$width, $height];
- return $this;
- }
- /**
- * Set button style.
- *
- * @param string $style
- * @return $this
- */
- public function style(string $style = 'primary')
- {
- $this->style = $style;
- return $this;
- }
- /**
- * Set the field options.
- *
- * @param array|\Closure $options
- *
- * @return $this
- */
- public function options($options = [])
- {
- if ($options instanceof Arrayable) {
- $options = $options->toArray();
- }
- $this->options = $options;
- return $this;
- }
- protected function formatOptions()
- {
- if ($this->options instanceof \Closure) {
- $value = Helper::array(old($this->column, $this->value));
- $this->options = $this->options->call($this->getFormModel(), $value, $this);
- }
- $this->options = Helper::array($this->options);
- }
- /**
- * Multiple select.
- *
- * @param int|null|null $max
- * @return SelectResource
- */
- public function multiple(?int $max = null)
- {
- return $this->max($max);
- }
- /**
- *
- * @param ?int $max
- * @return $this
- */
- public function max(?int $max)
- {
- $this->maxItem = $max;
- return $this;
- }
- /**
- * Set source path.
- *
- * @param string $source
- *
- * @return $this
- */
- public function path($source)
- {
- $this->source = admin_url($source);
- return $this;
- }
- protected function formatValue()
- {
- $value = Helper::array(old($this->column, $this->value));
- $this->value = [];
- foreach ($this->options as $id => $label) {
- foreach ($value as $v) {
- if ($v == $id && $v !== null) {
- $this->value[$v] = $label;
- }
- }
- }
- $this->value = json_encode((object)$this->value);
- }
- protected function setDefaultSource()
- {
- if (!$this->source) {
- if (strpos($this->column, '.')) {
- $this->path(str_replace('_id', '', last(explode('.', $this->column))));
- } else {
- $this->path(str_replace('_id', '', $this->column));
- }
- }
- }
- public function prepare($value)
- {
- if ($this->maxItem == 1) {
- if ($value === null || $value === '') {
- return 0;
- }
- return $value;
- }
- return Helper::array($value, true);
- }
- /**
- * {@inheritdoc}
- */
- public function render()
- {
- $this->formatOptions();
- $this->formatValue();
- $this->setDefaultSource();
- if (!$this->maxItem || $this->maxItem > 1) {
- Admin::style('.select-resource .nav li a{padding:8px 10px;font-size:13px;font-weight:bold;color:var(--primary-dark)}.select-resource .nav li a.red{cursor:pointer}.select-resource .nav-stacked>li{border-bottom:1px solid #eee;background: #fff;}.select-resource .nav {border: 1px solid #eee;margin-bottom:5px;}');
- }
- $this->defaultAttribute('class', 'form-control '.$this->getElementClassString());
- $name = $this->elementName ?: $this->formatName($this->column);
- $this->prepend('<i class="fa fa-long-arrow-up"></i>')
- ->defaultAttribute('type', 'text')
- ->defaultAttribute('id', $this->id.$this->getFormId())
- ->defaultAttribute('name', $name);
- $this->addVariables([
- 'className' => str_replace(['[', ']'], '_', $name),
- 'prepend' => $this->prepend,
- 'append' => $this->append,
- 'area' => json_encode($this->area),
- 'maxItem' => $this->maxItem,
- 'source' => $this->source,
- 'placeholder' => $this->getPlaceholder(),
- 'style' => $this->style,
- 'disabled' => empty($this->attributes['disabled']) ? '' : 'disabled',
- 'inputContainerId' => $this->id.$this->getFormId(),
- ]);
- return parent::render();
- }
- }
|