DialogTable.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. use Illuminate\Support\Str;
  7. class DialogTable extends Widget
  8. {
  9. /**
  10. * @var string
  11. */
  12. protected $title;
  13. /**
  14. * @var LazyTable
  15. */
  16. protected $table;
  17. /**
  18. * @var string
  19. */
  20. protected $width = '800px';
  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->class('dialog-table');
  42. $this->id('dialog-table-'.Str::random(8));
  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. protected function addScript()
  155. {
  156. if ($this->events['load']) {
  157. $this->table->onLoad($this->events['load']);
  158. }
  159. $this->script = <<<JS
  160. (function () {
  161. var id = replaceNestedFormIndex('{$this->id()}'),
  162. _id, _tempId, _btnId, _tb;
  163. setId(id);
  164. function hidden(index) {
  165. {$this->events['hidden']}
  166. $(_id).trigger('dialog:hidden');
  167. }
  168. function open(btn) {
  169. var index = layer.open({
  170. type: 1,
  171. title: '{$this->title}',
  172. area: '{$this->width}',
  173. offset: '70px',
  174. maxmin: false,
  175. resize: false,
  176. content: $(_tempId).html(),
  177. success: function(layero, index) {
  178. $(_id).attr('layer', index);
  179. setDataId($(_id));
  180. {$this->events['shown']}
  181. setTimeout(function () {
  182. Dcat.grid.AsyncTable({container: _tb});
  183. $(_tb).trigger('table:load');
  184. }, 100);
  185. $(_id).trigger('dialog:shown');
  186. $(_id).on('dialog:open', openDialog);
  187. $(_id).on('dialog:close', closeDialog)
  188. },
  189. cancel: function (index, layero) {
  190. btn && btn.removeAttr('layer');
  191. hidden(index)
  192. }
  193. });
  194. btn && btn.attr('layer', index);
  195. }
  196. function setDataId(obj) {
  197. if (! obj.attr('data-id')) {
  198. obj.attr('data-id', id);
  199. }
  200. }
  201. function setId(val) {
  202. if (! val) return;
  203. id = val;
  204. _id = '#'+id;
  205. _tempId = '#temp-'+id;
  206. _btnId = '#button-'+id;
  207. _tb = _id+' .async-table';
  208. }
  209. function openDialog () {
  210. setId($(this).attr('data-id'));
  211. setDataId($(this));
  212. if (! $(this).attr('layer')) {
  213. open($(this));
  214. }
  215. }
  216. function closeDialog() {
  217. var index = $(this).attr('layer');
  218. $(_id).removeAttr('layer');
  219. $(_btnId).removeAttr('layer');
  220. if (index) {
  221. layer.close(index);
  222. hidden(index);
  223. }
  224. }
  225. $(_btnId).on('click', openDialog);
  226. })();
  227. JS;
  228. }
  229. public function html()
  230. {
  231. $table = $this->renderTable();
  232. $this->addScript();
  233. return <<<HTML
  234. {$this->renderButton()}
  235. <template id="temp-{$this->id()}">
  236. <div {$this->formatHtmlAttributes()}>
  237. <div class="p-2 dialog-body">{$table}</div>
  238. {$this->renderFooter()}
  239. </div>
  240. </template>
  241. HTML;
  242. }
  243. protected function renderTable()
  244. {
  245. return <<<HMLT
  246. {$this->table->render()}
  247. HMLT;
  248. }
  249. protected function renderFooter()
  250. {
  251. $footer = Helper::render($this->footer);
  252. if (! $footer) {
  253. return;
  254. }
  255. return <<<HTML
  256. <div class="dialog-footer layui-layer-btn">{$footer}</div>
  257. HTML;
  258. }
  259. protected function renderButton()
  260. {
  261. if (! $this->button) {
  262. return;
  263. }
  264. $button = Helper::render($this->button);
  265. // 如果没有HTML标签则添加一个 a 标签
  266. if (! preg_match('/(\<\/[\d\w]+\s*\>+)/i', $button)) {
  267. $button = "<a href=\"javascript:void(0)\">{$button}</a>";
  268. }
  269. return <<<HTML
  270. <span style="cursor: pointer" id="button-{$this->id()}">{$button}</span>
  271. HTML;
  272. }
  273. }