FilterButton.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. namespace Dcat\Admin\Grid\Tools;
  3. use Dcat\Admin\Admin;
  4. use Dcat\Admin\Grid\Filter;
  5. use Illuminate\Support\Str;
  6. class FilterButton extends AbstractTool
  7. {
  8. /**
  9. * @var string
  10. */
  11. protected $view = 'admin::filter.button';
  12. /**
  13. * @var string
  14. */
  15. protected $btnClassName;
  16. /**
  17. * @return \Dcat\Admin\Grid\Filter
  18. */
  19. protected function filter()
  20. {
  21. return $this->parent->filter();
  22. }
  23. /**
  24. * Get button class name.
  25. *
  26. * @return string
  27. */
  28. protected function getElementClassName()
  29. {
  30. if (! $this->btnClassName) {
  31. $this->btnClassName = 'filter-btn-'.Str::random(8);
  32. }
  33. return $this->btnClassName;
  34. }
  35. /**
  36. * Set up script for filter button.
  37. */
  38. protected function setupScripts()
  39. {
  40. $filter = $this->filter();
  41. $id = $filter->filterID();
  42. if ($filter->mode() === Filter::MODE_RIGHT_SIDE) {
  43. if ($this->filter()->grid()->model()->getCurrentPage() > 1) {
  44. $expand = 'false';
  45. } else {
  46. $expand = $filter->expand ? 'true' : 'false';
  47. }
  48. $script = <<<JS
  49. (function () {
  50. var slider,
  51. expand = {$expand};
  52. function initSlider() {
  53. slider = new Dcat.Slider({
  54. target: '#{$id}',
  55. });
  56. slider
  57. .\$container
  58. .find('.right-side-filter-container .header')
  59. .width(slider.\$container.width() - 20);
  60. expand && setTimeout(slider.open.bind(slider), 10);
  61. }
  62. expand && setTimeout(initSlider, 10);
  63. $('.{$this->getElementClassName()}').on('click', function () {
  64. if (! slider) {
  65. initSlider()
  66. }
  67. slider.toggle();
  68. return false
  69. });
  70. $('.wrapper').on('click', '.modal', function (e) {
  71. if (typeof e.cancelBubble != "undefined") {
  72. e.cancelBubble = true;
  73. }
  74. if (typeof e.stopPropagation != "undefined") {
  75. e.stopPropagation();
  76. }
  77. });
  78. $(document).on('click', '.wrapper', function (e) {
  79. if (slider && slider.close) {
  80. slider.close();
  81. }
  82. });
  83. })();
  84. JS;
  85. } else {
  86. $script = <<<JS
  87. $('.{$this->getElementClassName()}').on('click', function(){
  88. $('#{$id}').parent().toggleClass('d-none');
  89. });
  90. JS;
  91. }
  92. Admin::script($script);
  93. }
  94. /**
  95. * @return mixed
  96. */
  97. protected function renderScopes()
  98. {
  99. return $this->filter()->scopes()->map->render()->implode("\r\n");
  100. }
  101. /**
  102. * Get label of current scope.
  103. *
  104. * @return string
  105. */
  106. protected function currentScopeLabel()
  107. {
  108. if ($scope = $this->filter()->getCurrentScope()) {
  109. return "&nbsp;{$scope->getLabel()}&nbsp;";
  110. }
  111. return '';
  112. }
  113. /**
  114. * {@inheritdoc}
  115. */
  116. public function render()
  117. {
  118. $filter = $this->filter();
  119. $scopres = $filter->scopes();
  120. $filters = $filter->filters();
  121. $valueCount = $filter->mode() === Filter::MODE_RIGHT_SIDE
  122. ? count($this->parent->filter()->getConditions()) : 0;
  123. if ($scopres->isEmpty() && ! $filters) {
  124. return;
  125. }
  126. $this->setupScripts();
  127. $onlyScopes = ((! $filters || $this->parent->option('show_filter') === false) && ! $scopres->isEmpty()) ? true : false;
  128. $variables = [
  129. 'scopes' => $scopres,
  130. 'current_label' => $this->currentScopeLabel(),
  131. 'url_no_scopes' => $filter->urlWithoutScopes(),
  132. 'btn_class' => $this->getElementClassName(),
  133. 'expand' => $filter->expand,
  134. 'show_filter_text' => true,
  135. 'only_scopes' => $onlyScopes,
  136. 'valueCount' => $valueCount,
  137. ];
  138. return view($this->view, $variables)->render();
  139. }
  140. }