Select.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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\Database\Eloquent\Model;
  7. use Illuminate\Support\Arr;
  8. use Illuminate\Support\Str;
  9. class Select extends Field
  10. {
  11. public static $js = '@select2';
  12. public static $css = '@select2';
  13. /**
  14. * @var array
  15. */
  16. protected $groups = [];
  17. /**
  18. * @var array
  19. */
  20. protected $config = [];
  21. /**
  22. * Set options.
  23. *
  24. * @param array|\Closure|string $options
  25. *
  26. * @return $this|mixed
  27. */
  28. public function options($options = [])
  29. {
  30. if ($options instanceof \Closure) {
  31. $this->options = $options;
  32. return $this;
  33. }
  34. // remote options
  35. if (is_string($options)) {
  36. // reload selected
  37. if (class_exists($options) && in_array(Model::class, class_parents($options))) {
  38. return $this->model(...func_get_args());
  39. }
  40. return $this->loadRemoteOptions(...func_get_args());
  41. }
  42. $this->options = Helper::array($options);
  43. return $this;
  44. }
  45. /**
  46. * @param array $groups
  47. */
  48. /**
  49. * Set option groups.
  50. *
  51. * eg: $group = [
  52. * [
  53. * 'label' => 'xxxx',
  54. * 'options' => [
  55. * 1 => 'foo',
  56. * 2 => 'bar',
  57. * ...
  58. * ],
  59. * ...
  60. * ]
  61. *
  62. * @param array $groups
  63. *
  64. * @return $this
  65. */
  66. public function groups(array $groups)
  67. {
  68. $this->groups = $groups;
  69. return $this;
  70. }
  71. /**
  72. * Load options for other select on change.
  73. *
  74. * @param string $field
  75. * @param string $sourceUrl
  76. * @param string $idField
  77. * @param string $textField
  78. *
  79. * @return $this
  80. */
  81. public function load($field, $sourceUrl, $idField = 'id', $textField = 'text')
  82. {
  83. if (Str::contains($field, '.')) {
  84. $field = $this->formatName($field);
  85. $class = str_replace(['[', ']'], '_', $field);
  86. } else {
  87. $class = $field;
  88. }
  89. $sourceUrl = admin_url($sourceUrl);
  90. $script = <<<JS
  91. $(document).off('change', "{$this->getElementClassSelector()}");
  92. $(document).on('change', "{$this->getElementClassSelector()}", function () {
  93. var target = $(this).closest('.fields-group').find(".$class");
  94. $.ajax("$sourceUrl?q="+this.value).then(function (data) {
  95. target.find("option").remove();
  96. $(target).select2({
  97. data: $.map(data, function (d) {
  98. d.id = d.$idField;
  99. d.text = d.$textField;
  100. return d;
  101. })
  102. }).val(target.attr('data-value')).trigger('change');
  103. });
  104. });
  105. JS;
  106. Admin::script($script);
  107. return $this;
  108. }
  109. /**
  110. * Load options for other selects on change.
  111. *
  112. * @param string $fields
  113. * @param string $sourceUrls
  114. * @param string $idField
  115. * @param string $textField
  116. *
  117. * @return $this
  118. */
  119. public function loads($fields = [], $sourceUrls = [], $idField = 'id', $textField = 'text')
  120. {
  121. $fieldsStr = implode('.', $fields);
  122. $urlsStr = implode('^', array_map(function ($url) {
  123. return admin_url($url);
  124. }, (array) $sourceUrls));
  125. $script = <<<JS
  126. (function () {
  127. var fields = '$fieldsStr'.split('.');
  128. var urls = '$urlsStr'.split('^');
  129. var refreshOptions = function(url, target) {
  130. $.ajax(url).then(function(data) {
  131. target.find("option").remove();
  132. $(target).select2({
  133. data: $.map(data, function (d) {
  134. d.id = d.$idField;
  135. d.text = d.$textField;
  136. return d;
  137. })
  138. }).trigger('change');
  139. });
  140. };
  141. $(document).off('change', "{$this->getElementClassSelector()}");
  142. $(document).on('change', "{$this->getElementClassSelector()}", function () {
  143. var _this = this;
  144. var promises = [];
  145. fields.forEach(function(field, index){
  146. var target = $(_this).closest('.fields-group').find('.' + fields[index]);
  147. promises.push(refreshOptions(urls[index] + "?q="+ _this.value, target));
  148. });
  149. $.when(promises).then(function() {
  150. console.log('开始更新其它select的选择options');
  151. });
  152. });
  153. })()
  154. JS;
  155. Admin::script($script);
  156. return $this;
  157. }
  158. /**
  159. * Load options from current selected resource(s).
  160. *
  161. * @param string $model
  162. * @param string $idField
  163. * @param string $textField
  164. *
  165. * @return $this
  166. */
  167. public function model($model, $idField = 'id', $textField = 'name')
  168. {
  169. if (! class_exists($model)
  170. || ! in_array(Model::class, class_parents($model))
  171. ) {
  172. throw new \InvalidArgumentException("[$model] must be a valid model class");
  173. }
  174. $this->options = function ($value) use ($model, $idField, $textField) {
  175. if (empty($value)) {
  176. return [];
  177. }
  178. $resources = [];
  179. if (is_array($value)) {
  180. if (Arr::isAssoc($value)) {
  181. $resources[] = Arr::get($value, $idField);
  182. } else {
  183. $resources = array_column($value, $idField);
  184. }
  185. } else {
  186. $resources[] = $value;
  187. }
  188. return $model::find($resources)->pluck($textField, $idField)->toArray();
  189. };
  190. return $this;
  191. }
  192. /**
  193. * Load options from remote.
  194. *
  195. * @param string $url
  196. * @param array $parameters
  197. * @param array $options
  198. *
  199. * @return $this
  200. */
  201. protected function loadRemoteOptions($url, $parameters = [], $options = [])
  202. {
  203. $ajaxOptions = [
  204. 'url' => admin_url($url.'?'.http_build_query($parameters)),
  205. ];
  206. $configs = array_merge([
  207. 'allowClear' => true,
  208. 'placeholder' => [
  209. 'id' => '',
  210. 'text' => trans('admin.choose'),
  211. ],
  212. ], $this->config);
  213. $configs = json_encode($configs);
  214. $configs = substr($configs, 1, strlen($configs) - 2);
  215. $ajaxOptions = json_encode(array_merge($ajaxOptions, $options));
  216. $this->script = <<<JS
  217. $.ajax({$ajaxOptions}).done(function(data) {
  218. $("{$this->getElementClassSelector()}").each(function (_, select) {
  219. select = $(select);
  220. select.select2({
  221. data: data,
  222. $configs
  223. });
  224. var value = select.data('value') + '';
  225. if (value) {
  226. value = value.split(',');
  227. select.select2('val', value);
  228. }
  229. });
  230. });
  231. JS;
  232. return $this;
  233. }
  234. /**
  235. * Load options from ajax results.
  236. *
  237. * @param string $url
  238. * @param $idField
  239. * @param $textField
  240. *
  241. * @return $this
  242. */
  243. public function ajax($url, $idField = 'id', $textField = 'text')
  244. {
  245. $configs = array_merge([
  246. 'allowClear' => true,
  247. 'placeholder' => $this->label,
  248. 'minimumInputLength' => 1,
  249. ], $this->config);
  250. $configs = json_encode($configs);
  251. $configs = substr($configs, 1, strlen($configs) - 2);
  252. $url = admin_url($url);
  253. $this->script = <<<JS
  254. $("{$this->getElementClassSelector()}").select2({
  255. ajax: {
  256. url: "$url",
  257. dataType: 'json',
  258. delay: 250,
  259. data: function (params) {
  260. return {
  261. q: params.term,
  262. page: params.page
  263. };
  264. },
  265. processResults: function (data, params) {
  266. params.page = params.page || 1;
  267. return {
  268. results: $.map(data.data, function (d) {
  269. d.id = d.$idField;
  270. d.text = d.$textField;
  271. return d;
  272. }),
  273. pagination: {
  274. more: data.next_page_url
  275. }
  276. };
  277. },
  278. cache: true
  279. },
  280. $configs,
  281. escapeMarkup: function (markup) {
  282. return markup;
  283. }
  284. });
  285. JS;
  286. return $this;
  287. }
  288. /**
  289. * Set config for select2.
  290. *
  291. * all configurations see https://select2.org/configuration/options-api
  292. *
  293. * @param string $key
  294. * @param mixed $val
  295. *
  296. * @return $this
  297. */
  298. public function config($key, $val)
  299. {
  300. $this->config[$key] = $val;
  301. return $this;
  302. }
  303. /**
  304. * Disable clear button.
  305. *
  306. * @return $this
  307. */
  308. public function disableClearButton()
  309. {
  310. return $this->config('allowClear', false);
  311. }
  312. /**
  313. * {@inheritdoc}
  314. */
  315. public function render()
  316. {
  317. static::defineLang();
  318. $configs = array_merge([
  319. 'allowClear' => true,
  320. 'placeholder' => [
  321. 'id' => '',
  322. 'text' => $this->label,
  323. ],
  324. ], $this->config);
  325. $configs = json_encode($configs);
  326. if (empty($this->script)) {
  327. $this->script = "$(\"{$this->getElementClassSelector()}\").select2($configs);";
  328. }
  329. if ($this->options instanceof \Closure) {
  330. $this->options = $this->options->bindTo($this->values());
  331. $this->options(call_user_func($this->options, $this->value(), $this));
  332. }
  333. $this->options = array_filter($this->options, 'strlen');
  334. $this->addVariables([
  335. 'options' => $this->options,
  336. 'groups' => $this->groups,
  337. ]);
  338. $this->attribute('data-value', implode(',', Helper::array($this->value())));
  339. return parent::render();
  340. }
  341. /**
  342. * @return void
  343. */
  344. public static function defineLang()
  345. {
  346. $lang = trans('select2');
  347. if (! is_array($lang) || empty($lang)) {
  348. return;
  349. }
  350. $locale = config('app.locale');
  351. Admin::script(
  352. <<<JS
  353. (function () {
  354. if (! $.fn.select2) {
  355. return;
  356. }
  357. var e = $.fn.select2.amd;
  358. return e.define("select2/i18n/{$locale}", [], function () {
  359. return {
  360. errorLoading: function () {
  361. return "{$lang['error_loading']}"
  362. }, inputTooLong: function (e) {
  363. return "{$lang['input_too_long']}".replace(':num', e.input.length - e.maximum)
  364. }, inputTooShort: function (e) {
  365. return "{$lang['input_too_short']}".replace(':num', e.minimum - e.input.length)
  366. }, loadingMore: function () {
  367. return "{$lang['loading_more']}"
  368. }, maximumSelected: function (e) {
  369. return "{$lang['maximum_selected']}".replace(':num', e.maximum)
  370. }, noResults: function () {
  371. return "{$lang['no_results']}"
  372. }, searching: function () {
  373. return "{$lang['searching']}"
  374. }
  375. }
  376. }), {define: e.define, require: e.require}
  377. })()
  378. JS
  379. );
  380. }
  381. }