123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- <?php
- namespace Dcat\Admin\Grid\Tools;
- use Dcat\Admin\Admin;
- use Dcat\Admin\Support\Helper;
- use Illuminate\Support\Traits\Macroable;
- class QuickSearch extends AbstractTool
- {
- use Macroable;
- /**
- * @var string
- */
- protected $view = 'admin::grid.quick-search';
- /**
- * @var string
- */
- protected $placeholder = null;
- /**
- * @var string
- */
- protected $queryName = '_search_';
- /**
- * @var int rem
- */
- protected $width = 18;
- /**
- * @var bool
- */
- protected $autoSubmit = true;
- /**
- * @return string
- */
- public function getQueryName()
- {
- return $this->parent->makeName($this->queryName);
- }
- /**
- * @param int $width
- *
- * @return $this
- */
- public function width(int $width)
- {
- $this->width = $width;
- return $this;
- }
- /**
- * Set placeholder.
- *
- * @param string $text
- *
- * @return $this
- */
- public function placeholder(?string $text = '')
- {
- $this->placeholder = $text;
- return $this;
- }
- /**
- * @return string
- */
- public function value()
- {
- return trim(request($this->getQueryName()));
- }
- /**
- * @return string
- */
- public function formAction()
- {
- return Helper::fullUrlWithoutQuery([
- $this->getQueryName(),
- $this->parent->model()->getPageName(),
- '_pjax',
- ]);
- }
- /**
- * @param bool $value
- *
- * @return $this
- */
- public function auto(bool $value = true)
- {
- $this->autoSubmit = $value;
- return $this;
- }
- /**
- * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
- */
- public function render()
- {
- $this->setupScript();
- $data = [
- 'action' => $this->formAction(),
- 'key' => $this->getQueryName(),
- 'value' => $this->value(),
- 'placeholder' => $this->placeholder ?: trans('admin.search'),
- 'width' => $this->width,
- 'auto' => $this->autoSubmit,
- ];
- return view($this->view, $data);
- }
- protected function setupScript()
- {
- $script = <<<'JS'
- (function () {
- var inputting = false,
- $ipt = $('input.quick-search-input'),
- val = $ipt.val(),
- ignoreKeys = [16, 17, 18, 20, 35, 36, 37, 38, 39, 40, 45, 144],
- auto = $ipt.attr('auto');
-
- var submit = Dcat.helpers.debounce(function (input) {
- inputting || $(input).parents('form').submit()
- }, 1200);
-
- function toggleBtn() {
- var t = $(this),
- btn = t.parent().parent().find('.quick-search-clear');
-
- if (t.val()) {
- btn.css({color: '#333', cursor: 'pointer'});
- } else {
- btn.css({color: '#fff', cursor: 'none'});
- }
- return false;
- }
-
- $ipt.on('focus', toggleBtn)
- .on('mousemove', toggleBtn)
- .on('mouseout', toggleBtn)
- .on('compositionstart', function(){
- inputting = true
- })
- .on('compositionend', function() {
- inputting = false
- });
- if (auto > 0) {
- $ipt.on('keyup', function (e) {
- toggleBtn.apply(this);
-
- ignoreKeys.indexOf(e.keyCode) == -1 && submit(this)
- })
- }
-
- val !== '' && $ipt.val('').focus().val(val);
-
- $('.quick-search-clear').on('click', function () {
- $(this).parent().find('.quick-search-input').val('');
-
- $(this).closest('form').submit();
- });
- })()
- JS;
- Admin::script($script);
- }
- }
|