AjaxRequestBuilder.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. namespace Dcat\Admin\Widgets;
  3. use Illuminate\Support\Str;
  4. trait AjaxRequestBuilder
  5. {
  6. /**
  7. * @var string
  8. */
  9. protected $__url;
  10. /**
  11. * @var array
  12. */
  13. protected $buttonSelectors = [];
  14. /**
  15. * @var string
  16. */
  17. protected $fn;
  18. /**
  19. * @var array
  20. */
  21. protected $javascripts = [
  22. 'fetching' => [],
  23. 'fetched' => [],
  24. ];
  25. /**
  26. * Set request url.
  27. *
  28. * @param string $url
  29. * @return $this
  30. */
  31. public function request(string $url)
  32. {
  33. return $this->setUrl($url);
  34. }
  35. /**
  36. * Set current url to request.
  37. *
  38. * @param string $url
  39. * @return $this
  40. */
  41. public function requestCurrent(array $query = [])
  42. {
  43. $this->__url = url(request()->getPathInfo()).'?'.http_build_query($query);
  44. return $this;
  45. }
  46. /**
  47. * Set request url.
  48. *
  49. * @param string $url
  50. * @return $this
  51. */
  52. public function setUrl(string $url)
  53. {
  54. $this->__url = admin_url($url);
  55. return $this;
  56. }
  57. /**
  58. * @return string
  59. */
  60. public function getUrl()
  61. {
  62. return $this->__url;
  63. }
  64. /**
  65. * @return array
  66. */
  67. public function getJavascripts()
  68. {
  69. return $this->javascripts;
  70. }
  71. /**
  72. * Set css selectors of refetch links.
  73. *
  74. * @param string|array $selector
  75. * @return $this
  76. */
  77. public function refetch($selector)
  78. {
  79. $this->buttonSelectors =
  80. array_merge($this->buttonSelectors, (array)$selector);
  81. return $this;
  82. }
  83. /**
  84. * @return array
  85. */
  86. public function getButtonSelectors()
  87. {
  88. return $this->buttonSelectors;
  89. }
  90. /**
  91. * Set the script before fetch data.
  92. *
  93. * @param string $script
  94. * @return $this
  95. */
  96. public function fetching(string $script)
  97. {
  98. $this->javascripts['fetching'][] = $script;
  99. return $this;
  100. }
  101. /**
  102. * Set the script after fetch data.
  103. *
  104. * @param string $script
  105. * @return $this
  106. */
  107. public function fetched(string $script)
  108. {
  109. $this->javascripts['fetched'][] = $script;
  110. return $this;
  111. }
  112. /**
  113. * @return bool
  114. */
  115. public function allowBuildFetchingScript()
  116. {
  117. return $this->__url === null ? false : true;
  118. }
  119. /**
  120. * @return bool|string
  121. */
  122. public function buildFetchingScript()
  123. {
  124. if (!$this->allowBuildFetchingScript()) {
  125. return false;
  126. }
  127. $this->fn = 'frd_'.Str::random(8);
  128. $fetching = implode(';', $this->javascripts['fetching']);
  129. $fetched = implode(';', $this->javascripts['fetched']);
  130. $binding = '';
  131. foreach ($this->buttonSelectors as $v) {
  132. $binding .= "$('{$v}').click(function () { {$this->fn}($(this).data()) });";
  133. }
  134. return <<<JS
  135. window.{$this->fn} = function (p) {
  136. $fetching;
  137. $.getJSON('{$this->__url}', $.extend({_token:LA.token}, p || {}), function (result) {
  138. {$fetched};
  139. });
  140. }
  141. {$this->fn}();
  142. $binding;
  143. JS;
  144. }
  145. /**
  146. * Copy the given AjaxRequestBuilder.
  147. *
  148. * @param AjaxRequestBuilder $fetcher
  149. * @return $this
  150. */
  151. public function copy($fetcher)
  152. {
  153. $this->__url = $fetcher->getUrl();
  154. $this->buttonSelectors = $fetcher->getButtonSelectors();
  155. $scripts = $fetcher->getJavascripts();
  156. $this->javascripts['fetching'] = array_merge($this->javascripts['fetching'], $scripts['fetching']);
  157. $this->javascripts['fetched'] = array_merge($this->javascripts['fetched'], $scripts['fetched']);
  158. return $this;
  159. }
  160. }