SelectResource.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. namespace Dcat\Admin\Form\Field;
  3. use Dcat\Admin\Admin;
  4. use Dcat\Admin\Form\Field;
  5. use Dcat\Admin\Support\Helper;
  6. use Illuminate\Contracts\Support\Arrayable;
  7. use Illuminate\Support\Facades\URL;
  8. class SelectResource extends Field
  9. {
  10. use PlainInput;
  11. protected static $js = [
  12. 'vendor/dcat-admin/dcat-admin/select-resource.min.js'
  13. ];
  14. protected $area = ['60%', '68%'];
  15. protected $source;
  16. protected $maxItem = 1;
  17. protected $style = 'primary';
  18. /**
  19. * Set window's area.
  20. *
  21. * @param string $width
  22. * @param string $height
  23. * @return $this
  24. */
  25. public function area(string $width, string $height)
  26. {
  27. $this->area = [$width, $height];
  28. return $this;
  29. }
  30. /**
  31. * Set button style.
  32. *
  33. * @param string $style
  34. * @return $this
  35. */
  36. public function style(string $style = 'primary')
  37. {
  38. $this->style = $style;
  39. return $this;
  40. }
  41. /**
  42. * Set the field options.
  43. *
  44. * @param array|\Closure $options
  45. *
  46. * @return $this
  47. */
  48. public function options($options = [])
  49. {
  50. if ($options instanceof Arrayable) {
  51. $options = $options->toArray();
  52. }
  53. $this->options = $options;
  54. return $this;
  55. }
  56. protected function formatOptions()
  57. {
  58. if ($this->options instanceof \Closure) {
  59. $value = Helper::array(old($this->column, $this->value));
  60. $this->options = $this->options->call($this->getFormModel(), $value, $this);
  61. }
  62. $this->options = Helper::array($this->options);
  63. }
  64. /**
  65. * Multiple select.
  66. *
  67. * @param int|null|null $max
  68. * @return SelectResource
  69. */
  70. public function multiple(?int $max = null)
  71. {
  72. return $this->max($max);
  73. }
  74. /**
  75. *
  76. * @param ?int $max
  77. * @return $this
  78. */
  79. public function max(?int $max)
  80. {
  81. $this->maxItem = $max;
  82. return $this;
  83. }
  84. /**
  85. * Set source path.
  86. *
  87. * @param string $source
  88. *
  89. * @return $this
  90. */
  91. public function path($source)
  92. {
  93. $this->source = admin_url($source);
  94. return $this;
  95. }
  96. protected function formatValue()
  97. {
  98. $value = Helper::array(old($this->column, $this->value));
  99. $this->value = [];
  100. foreach ($this->options as $id => $label) {
  101. foreach ($value as $v) {
  102. if ($v == $id && $v !== null) {
  103. $this->value[$v] = $label;
  104. }
  105. }
  106. }
  107. $this->value = json_encode((object)$this->value);
  108. }
  109. protected function setDefaultSource()
  110. {
  111. if (!$this->source) {
  112. if (strpos($this->column, '.')) {
  113. $this->path(str_replace('_id', '', last(explode('.', $this->column))));
  114. } else {
  115. $this->path(str_replace('_id', '', $this->column));
  116. }
  117. }
  118. }
  119. public function prepare($value)
  120. {
  121. if ($this->maxItem == 1) {
  122. if ($value === null || $value === '') {
  123. return 0;
  124. }
  125. return $value;
  126. }
  127. return Helper::array($value, true);
  128. }
  129. /**
  130. * {@inheritdoc}
  131. */
  132. public function render()
  133. {
  134. $this->formatOptions();
  135. $this->formatValue();
  136. $this->setDefaultSource();
  137. if (!$this->maxItem || $this->maxItem > 1) {
  138. 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;}');
  139. }
  140. $this->defaultAttribute('class', 'form-control '.$this->getElementClassString());
  141. $name = $this->elementName ?: $this->formatName($this->column);
  142. $this->prepend('<i class="fa fa-long-arrow-up"></i>')
  143. ->defaultAttribute('type', 'text')
  144. ->defaultAttribute('id', $this->id.$this->getFormId())
  145. ->defaultAttribute('name', $name);
  146. $this->addVariables([
  147. 'className' => str_replace(['[', ']'], '_', $name),
  148. 'prepend' => $this->prepend,
  149. 'append' => $this->append,
  150. 'area' => json_encode($this->area),
  151. 'maxItem' => $this->maxItem,
  152. 'source' => $this->source,
  153. 'placeholder' => $this->getPlaceholder(),
  154. 'style' => $this->style,
  155. 'disabled' => empty($this->attributes['disabled']) ? '' : 'disabled',
  156. 'inputContainerId' => $this->id.$this->getFormId(),
  157. ]);
  158. return parent::render();
  159. }
  160. }