TableModal.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 $renderable
  56. */
  57. public function __construct($title = null, LazyRenderable $renderable = null)
  58. {
  59. $this->modal = Modal::make()
  60. ->lg()
  61. ->title($title)
  62. ->class('grid-modal', true);
  63. $this->body($renderable);
  64. }
  65. /**
  66. * 设置异步表格实例.
  67. *
  68. * @param LazyRenderable|null $renderable
  69. *
  70. * @return $this
  71. */
  72. public function body(?LazyRenderable $renderable)
  73. {
  74. if (! $renderable) {
  75. return $this;
  76. }
  77. $this->table = AsyncTable::make($renderable, false)->simple();
  78. $this->modal->body($this->table);
  79. return $this;
  80. }
  81. /**
  82. * 设置或获取ID.
  83. *
  84. * @param string|null $id
  85. *
  86. * @return |string
  87. */
  88. public function id(string $id = null)
  89. {
  90. $result = $this->modal->id($id);
  91. if ($id === null) {
  92. return $result;
  93. }
  94. return $this;
  95. }
  96. /**
  97. * 监听弹窗异步渲染完成事件.
  98. *
  99. * @param string $script
  100. *
  101. * @return $this
  102. */
  103. public function onLoad(string $script)
  104. {
  105. $this->loadScript .= $script.';';
  106. return $this;
  107. }
  108. /**
  109. * {@inheritdoc}
  110. */
  111. public function html()
  112. {
  113. if ($this->runScript) {
  114. $this->modal->onShow($this->table->getLoadScript());
  115. }
  116. if ($this->loadScript) {
  117. $this->table->onLoad($this->loadScript);
  118. }
  119. $this->table->runScript($this->runScript);
  120. $this->modal->runScript($this->runScript);
  121. return $this->modal->render();
  122. }
  123. /**
  124. * {@inheritdoc}
  125. */
  126. public function getScript()
  127. {
  128. return parent::getScript()
  129. .$this->modal->getScript()
  130. .$this->table->getScript();
  131. }
  132. /**
  133. * @return Modal
  134. */
  135. public function getModal()
  136. {
  137. return $this->modal;
  138. }
  139. /**
  140. * @return AsyncTable
  141. */
  142. public function getTable()
  143. {
  144. return $this->table;
  145. }
  146. public static function __callStatic($method, $arguments)
  147. {
  148. return static::make()->$method(...$arguments);
  149. }
  150. public function __call($method, $parameters)
  151. {
  152. if (in_array($method, $this->allowMethods, true)) {
  153. $result = $this->modal->$method(...$parameters);
  154. if (in_array($method, ['getElementSelector'], true)) {
  155. return $result;
  156. }
  157. return $this;
  158. }
  159. throw new \Exception(
  160. sprintf('Call to undefined method "%s::%s"', static::class, $method)
  161. );
  162. }
  163. }