QuickSearch.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. namespace Dcat\Admin\Grid\Tools;
  3. use Dcat\Admin\Admin;
  4. use Dcat\Admin\Grid;
  5. use Dcat\Admin\Support\Helper;
  6. class QuickSearch extends AbstractTool
  7. {
  8. /**
  9. * @var string
  10. */
  11. protected $view = 'admin::grid.quick-search';
  12. /**
  13. * @var string
  14. */
  15. protected $placeholder = null;
  16. /**
  17. * @var string
  18. */
  19. protected $queryName = '_search_';
  20. /**
  21. * @var int rem
  22. */
  23. protected $width = 19;
  24. /**
  25. * @var bool
  26. */
  27. protected $autoSubmit = true;
  28. /**
  29. * @param string|null $name
  30. *
  31. * @return $this
  32. */
  33. public function setQueryName(?string $name)
  34. {
  35. $this->queryName = $name;
  36. return $this;
  37. }
  38. public function setGrid(Grid $grid)
  39. {
  40. $grid->setQuickSearchQueryName();
  41. return parent::setGrid($grid);
  42. }
  43. /**
  44. * @return string
  45. */
  46. public function getQueryName()
  47. {
  48. return $this->queryName;
  49. }
  50. /**
  51. * @param int $width
  52. *
  53. * @return $this
  54. */
  55. public function width(int $width)
  56. {
  57. $this->width = $width;
  58. return $this;
  59. }
  60. /**
  61. * Set placeholder.
  62. *
  63. * @param string $text
  64. *
  65. * @return $this
  66. */
  67. public function placeholder(?string $text = '')
  68. {
  69. $this->placeholder = $text;
  70. return $this;
  71. }
  72. /**
  73. * @return string
  74. */
  75. public function value()
  76. {
  77. return trim(request($this->queryName));
  78. }
  79. /**
  80. * @return string
  81. */
  82. public function formAction()
  83. {
  84. return Helper::fullUrlWithoutQuery([
  85. $this->queryName,
  86. $this->parent->model()->getPageName(),
  87. '_pjax',
  88. ]);
  89. }
  90. /**
  91. * @param bool $value
  92. *
  93. * @return $this
  94. */
  95. public function auto(bool $value = true)
  96. {
  97. $this->autoSubmit = $value;
  98. return $this;
  99. }
  100. /**
  101. * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
  102. */
  103. public function render()
  104. {
  105. $this->setupScript();
  106. $data = [
  107. 'action' => $this->formAction(),
  108. 'key' => $this->queryName,
  109. 'value' => $this->value(),
  110. 'placeholder' => $this->placeholder ?: trans('admin.search'),
  111. 'width' => $this->width,
  112. 'auto' => $this->autoSubmit,
  113. ];
  114. return view($this->view, $data);
  115. }
  116. protected function setupScript()
  117. {
  118. $script = <<<'JS'
  119. (function () {
  120. var inputting = false,
  121. $ipt = $('input.quick-search-input'),
  122. val = $ipt.val(),
  123. ignoreKeys = [16, 17, 18, 20, 35, 36, 37, 38, 39, 40, 45, 144],
  124. auto = $ipt.attr('auto');
  125. var submit = Dcat.helpers.debounce(function (input) {
  126. inputting || $(input).parents('form').submit()
  127. }, 1200);
  128. function toggleBtn() {
  129. var t = $(this),
  130. btn = t.parent().parent().find('.quick-search-clear');
  131. if (t.val()) {
  132. btn.css({color: '#333', cursor: 'pointer'});
  133. } else {
  134. btn.css({color: '#fff', cursor: 'none'});
  135. }
  136. return false;
  137. }
  138. $ipt.on('focus', toggleBtn)
  139. .on('mousemove', toggleBtn)
  140. .on('mouseout', toggleBtn)
  141. .on('compositionstart', function(){
  142. inputting = true
  143. })
  144. .on('compositionend', function() {
  145. inputting = false
  146. });
  147. if (auto > 0) {
  148. $ipt.on('keyup', function (e) {
  149. toggleBtn.apply(this);
  150. ignoreKeys.indexOf(e.keyCode) == -1 && submit(this)
  151. })
  152. }
  153. val !== '' && $ipt.val('').focus().val(val);
  154. $('.quick-search-clear').on('click', function () {
  155. $(this).parent().find('.quick-search-input').val('');
  156. $(this).closest('form').submit();
  157. });
  158. })()
  159. JS;
  160. Admin::script($script);
  161. }
  162. }