DialogTable.php 6.7 KB

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