QuickSearch.php 3.6 KB

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