HasAjaxRequest.php 3.9 KB

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