DialogTable.php 6.5 KB

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