TableModal.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. namespace Dcat\Admin\Widgets;
  3. use Dcat\Admin\Grid\LazyRenderable;
  4. use Illuminate\Contracts\Support\Renderable;
  5. /**
  6. * Class TableModal.
  7. *
  8. * @method $this title(string $title)
  9. * @method $this button(string|\Closure $title)
  10. * @method $this join(bool $value = true)
  11. * @method $this xl()
  12. * @method $this on(string $script)
  13. * @method $this onShown(string $script)
  14. * @method $this onShow(string $script)
  15. * @method $this onHidden(string $script)
  16. * @method $this onHide(string $script)
  17. * @method $this footer(string|\Closure|Renderable $footer)
  18. * @method $this getElementSelector()
  19. */
  20. class TableModal extends Widget
  21. {
  22. /**
  23. * @var Modal
  24. */
  25. protected $modal;
  26. /**
  27. * @var AsyncTable
  28. */
  29. protected $table;
  30. /**
  31. * @var array
  32. */
  33. protected $allowMethods = [
  34. 'id',
  35. 'title',
  36. 'button',
  37. 'join',
  38. 'xl',
  39. 'on',
  40. 'onShown',
  41. 'onShow',
  42. 'onHidden',
  43. 'onHide',
  44. 'getElementSelector',
  45. 'footer',
  46. ];
  47. /**
  48. * @var string
  49. */
  50. protected $loadScript;
  51. /**
  52. * TableModal constructor.
  53. *
  54. * @param null $title
  55. * @param \Dcat\Admin\Grid\LazyRenderable|null $table
  56. */
  57. public function __construct($title = null, LazyRenderable $table = null)
  58. {
  59. $this->modal = Modal::make()
  60. ->lg()
  61. ->class('grid-modal', true);
  62. if ($title instanceof LazyRenderable) {
  63. $table = $title;
  64. $title = null;
  65. }
  66. $this->title($title);
  67. $this->body($table);
  68. }
  69. /**
  70. * 设置异步表格实例.
  71. *
  72. * @param LazyRenderable|null $renderable
  73. *
  74. * @return $this
  75. */
  76. public function body(?LazyRenderable $renderable)
  77. {
  78. if (! $renderable) {
  79. return $this;
  80. }
  81. $this->table = AsyncTable::make($renderable, false)->simple();
  82. $this->modal->body($this->table);
  83. return $this;
  84. }
  85. /**
  86. * 设置或获取ID.
  87. *
  88. * @param string|null $id
  89. *
  90. * @return |string
  91. */
  92. public function id(string $id = null)
  93. {
  94. $result = $this->modal->id($id);
  95. if ($id === null) {
  96. return $result;
  97. }
  98. return $this;
  99. }
  100. /**
  101. * 监听弹窗异步渲染完成事件.
  102. *
  103. * @param string $script
  104. *
  105. * @return $this
  106. */
  107. public function onLoad(string $script)
  108. {
  109. $this->loadScript .= $script.';';
  110. return $this;
  111. }
  112. /**
  113. * {@inheritdoc}
  114. */
  115. public function html()
  116. {
  117. if ($this->runScript) {
  118. $this->modal->onShow($this->table->getLoadScript());
  119. }
  120. if ($this->loadScript) {
  121. $this->table->onLoad($this->loadScript);
  122. }
  123. $this->table->runScript($this->runScript);
  124. $this->modal->runScript($this->runScript);
  125. return $this->modal->render();
  126. }
  127. /**
  128. * {@inheritdoc}
  129. */
  130. public function getScript()
  131. {
  132. return parent::getScript()
  133. .$this->modal->getScript()
  134. .$this->table->getScript();
  135. }
  136. /**
  137. * @return Modal
  138. */
  139. public function getModal()
  140. {
  141. return $this->modal;
  142. }
  143. /**
  144. * @return AsyncTable
  145. */
  146. public function getTable()
  147. {
  148. return $this->table;
  149. }
  150. public static function __callStatic($method, $arguments)
  151. {
  152. return static::make()->$method(...$arguments);
  153. }
  154. public function __call($method, $parameters)
  155. {
  156. if (in_array($method, $this->allowMethods, true)) {
  157. $result = $this->modal->$method(...$parameters);
  158. if (in_array($method, ['getElementSelector'], true)) {
  159. return $result;
  160. }
  161. return $this;
  162. }
  163. throw new \Exception(
  164. sprintf('Call to undefined method "%s::%s"', static::class, $method)
  165. );
  166. }
  167. }