DialogTable.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. namespace Dcat\Admin\Widgets;
  3. use Dcat\Admin\Grid\LazyRenderable;
  4. use Dcat\Admin\Support\Helper;
  5. use Illuminate\Contracts\Support\Renderable;
  6. class DialogTable extends Widget
  7. {
  8. protected $view = 'admin::widgets.dialogtable';
  9. /**
  10. * @var string
  11. */
  12. protected $title;
  13. /**
  14. * @var LazyTable
  15. */
  16. protected $table;
  17. /**
  18. * @var string
  19. */
  20. protected $width = '825px';
  21. /**
  22. * @var string|\Closure|Renderable
  23. */
  24. protected $button;
  25. /**
  26. * @var string|\Closure|Renderable
  27. */
  28. protected $footer;
  29. /**
  30. * @var array
  31. */
  32. protected $events = ['shown' => null, 'hidden' => null, 'load' => null];
  33. public function __construct($title = null, LazyRenderable $table = null)
  34. {
  35. if ($title instanceof LazyRenderable) {
  36. $table = $title;
  37. $title = null;
  38. }
  39. $this->title($title);
  40. $this->from($table);
  41. $this->elementClass = 'dialog-table-container';
  42. $this->class('dialog-table');
  43. }
  44. /**
  45. * 设置异步表格实例.
  46. *
  47. * @param LazyRenderable|null $renderable
  48. *
  49. * @return $this
  50. */
  51. public function from(?LazyRenderable $renderable)
  52. {
  53. if (! $renderable) {
  54. return $this;
  55. }
  56. $this->table = LazyTable::make($renderable)->simple()->runScript(false);
  57. return $this;
  58. }
  59. /**
  60. * 设置弹窗标题.
  61. *
  62. * @param string $title
  63. *
  64. * @return $this
  65. */
  66. public function title($title)
  67. {
  68. $this->title = $title;
  69. return $this;
  70. }
  71. /**
  72. * 设置弹窗宽度.
  73. *
  74. * @example
  75. * $this->width('500px');
  76. * $this->width('50%');
  77. *
  78. * @param string $width
  79. *
  80. * @return $this
  81. */
  82. public function width($width)
  83. {
  84. $this->width = $width;
  85. return $this;
  86. }
  87. /**
  88. * 设置点击按钮HTML.
  89. *
  90. * @param string|\Closure|Renderable $button
  91. *
  92. * @return $this
  93. */
  94. public function button($button)
  95. {
  96. $this->button = $button;
  97. return $this;
  98. }
  99. /**
  100. * 监听弹窗打开事件.
  101. *
  102. * @param string $script
  103. *
  104. * @return $this
  105. */
  106. public function onShown(string $script)
  107. {
  108. $this->events['shown'] .= ';'.$script;
  109. return $this;
  110. }
  111. /**
  112. * 监听弹窗隐藏事件.
  113. *
  114. * @param string $script
  115. *
  116. * @return $this
  117. */
  118. public function onHidden(string $script)
  119. {
  120. $this->events['hidden'] .= ';'.$script;
  121. return $this;
  122. }
  123. /**
  124. * 监听表格加载完毕事件.
  125. *
  126. * @param string $script
  127. *
  128. * @return $this
  129. */
  130. public function onLoad(string $script)
  131. {
  132. $this->events['load'] .= ';'.$script;
  133. return $this;
  134. }
  135. /**
  136. * 设置弹窗底部内容.
  137. *
  138. * @param string|\Closure|Renderable $footer
  139. *
  140. * @return $this
  141. */
  142. public function footer($footer)
  143. {
  144. $this->footer = $footer;
  145. return $this;
  146. }
  147. /**
  148. * @return LazyTable
  149. */
  150. public function getTable()
  151. {
  152. return $this->table;
  153. }
  154. public function render()
  155. {
  156. $this->addVariables([
  157. 'title' => $this->title,
  158. 'width' => $this->width,
  159. 'button' => $this->renderButton(),
  160. 'table' => $this->renderTable(),
  161. 'footer' => $this->renderFooter(),
  162. 'events' => $this->events,
  163. ]);
  164. return parent::render();
  165. }
  166. protected function renderTable()
  167. {
  168. return $this->table->render();
  169. }
  170. protected function renderFooter()
  171. {
  172. return Helper::render($this->footer);
  173. }
  174. protected function renderButton()
  175. {
  176. if (! $this->button) {
  177. return;
  178. }
  179. $button = Helper::render($this->button);
  180. // 如果没有HTML标签则添加一个 a 标签
  181. if (! preg_match('/(\<\/[\d\w]+\s*\>+)/i', $button)) {
  182. $button = "<a href=\"javascript:void(0)\">{$button}</a>";
  183. }
  184. return $button;
  185. }
  186. }