DialogForm.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <?php
  2. namespace Dcat\Admin\Widgets;
  3. use Dcat\Admin\Admin;
  4. use Dcat\Admin\Form;
  5. use Dcat\Admin\Layout\Content;
  6. use Dcat\Admin\Support\Helper;
  7. use Illuminate\Contracts\Support\Arrayable;
  8. use Illuminate\Support\Facades\URL;
  9. class DialogForm
  10. {
  11. const QUERY_NAME = '_form_win_';
  12. /**
  13. * @var string
  14. */
  15. public static $contentView = 'admin::contents.dialog-form';
  16. /**
  17. * @var array
  18. */
  19. protected $options = [
  20. 'title' => 'Form',
  21. 'area' => ['700px', '670px'],
  22. 'defaultUrl' => null,
  23. 'buttonSelector' => null,
  24. 'query' => null,
  25. 'lang' => null,
  26. 'forceRefresh' => false,
  27. 'disableReset' => false,
  28. ];
  29. /**
  30. * @var array
  31. */
  32. protected $handlers = [
  33. 'saved' => null,
  34. 'success' => null,
  35. 'error' => null,
  36. ];
  37. public function __construct(?string $title = null, $url = null)
  38. {
  39. $this->title($title);
  40. $this->url($url);
  41. }
  42. /**
  43. *
  44. * @param array $options
  45. * @return $this
  46. */
  47. public function options($options = [])
  48. {
  49. if ($options instanceof Arrayable) {
  50. $options = $options->toArray();
  51. }
  52. $this->options = array_merge($this->options, $options);
  53. return $this;
  54. }
  55. /**
  56. * 设置弹窗标题
  57. *
  58. * @param string $title
  59. * @return $this
  60. */
  61. public function title(?string $title)
  62. {
  63. $this->options['title'] = $title;
  64. return $this;
  65. }
  66. /**
  67. * 绑定点击按钮
  68. *
  69. * @param string $buttonSelector
  70. * @return $this
  71. */
  72. public function click(string $buttonSelector)
  73. {
  74. $this->options['buttonSelector'] = $buttonSelector;
  75. return $this;
  76. }
  77. /**
  78. * 强制每次点击按钮都重新渲染表单弹窗
  79. *
  80. * @return $this
  81. */
  82. public function forceRefresh()
  83. {
  84. $this->options['forceRefresh'] = true;
  85. return $this;
  86. }
  87. /**
  88. * 禁用重置按钮
  89. *
  90. * @return $this
  91. */
  92. public function disableResetButton()
  93. {
  94. $this->options['disableReset'] = true;
  95. return $this;
  96. }
  97. /**
  98. * 保存后触发的js的代码(不论成功还是失败)
  99. *
  100. * @param string $script
  101. * @return $this
  102. */
  103. public function saved(string $script)
  104. {
  105. $this->handlers['saved'] = $script;
  106. return $this;
  107. }
  108. /**
  109. * 保存失败时触发的js代码
  110. *
  111. * @param string $script
  112. * @return $this
  113. */
  114. public function error(string $script)
  115. {
  116. $this->handlers['error'] = $script;
  117. return $this;
  118. }
  119. /**
  120. * 保存成功后触发的js代码
  121. *
  122. * @param string $script
  123. * @return $this
  124. */
  125. public function success(string $script)
  126. {
  127. $this->handlers['success'] = $script;
  128. return $this;
  129. }
  130. /**
  131. * 设置弹窗宽高
  132. * 支持百分比和"px"
  133. *
  134. * @param string $width
  135. * @param string $height
  136. * @return $this
  137. */
  138. public function dimensions(string $width, string $height)
  139. {
  140. $this->options['area'] = [$width, $height];
  141. return $this;
  142. }
  143. /**
  144. * 设置弹窗宽度
  145. * 支持百分比和"px"
  146. *
  147. * @param string|null $width
  148. * @return $this
  149. */
  150. public function width(?string $width)
  151. {
  152. $this->options['area'][0] = $width;
  153. return $this;
  154. }
  155. /**
  156. * 设置弹窗高度
  157. * 支持百分比和"px"
  158. *
  159. * @param string|null $height
  160. * @return $this
  161. */
  162. public function height(?string $height)
  163. {
  164. $this->options['area'][1] = $height;
  165. return $this;
  166. }
  167. /**
  168. * 设置默认的表单页面url
  169. *
  170. * @param null|string $url
  171. * @return $this
  172. */
  173. public function url(?string $url)
  174. {
  175. if ($url) {
  176. $this->options['defaultUrl'] = Helper::urlWithQuery(
  177. admin_url($url),
  178. [static::QUERY_NAME => 1]
  179. );
  180. }
  181. return $this;
  182. }
  183. /**
  184. * @return string
  185. */
  186. public function render()
  187. {
  188. $this->setupOptions();
  189. $opts = json_encode($this->options);
  190. Admin::script(
  191. <<<JS
  192. (function () {
  193. var opts = {$opts};
  194. opts.success = function (success, response) {
  195. {$this->handlers['success']}
  196. };
  197. opts.error = function (success, response) {
  198. {$this->handlers['error']}
  199. };
  200. opts.saved = function (success, response) {
  201. {$this->handlers['saved']}
  202. };
  203. LA.DialogForm(opts);
  204. })();
  205. JS
  206. );
  207. }
  208. protected function setupOptions()
  209. {
  210. $this->options['lang'] = [
  211. 'submit' => trans('admin.submit'),
  212. 'reset' => trans('admin.reset'),
  213. 'save_failed' => trans('admin.save_failed'),
  214. ];
  215. $this->options['query'] = static::QUERY_NAME;
  216. }
  217. /**
  218. * 判断是否是获取弹窗表单内容的请求
  219. *
  220. * @return bool
  221. */
  222. public static function is()
  223. {
  224. return request(static::QUERY_NAME) ? true : false;
  225. }
  226. /**
  227. * 对弹窗要渲染的表单对象进行前置处理
  228. *
  229. * @param Form $form
  230. */
  231. public static function prepare(Form $form)
  232. {
  233. if (!static::is()) {
  234. return;
  235. }
  236. Admin::$baseCss = [];
  237. Admin::$baseJs = [];
  238. Admin::$fonts = '';
  239. Admin::$disableSkinCss = true;
  240. $form->wrap(function ($v) {
  241. return $v;
  242. });
  243. $form->disableHeader();
  244. $form->disableFooter();
  245. $form->setWidth(9, 2);
  246. $form->hidden('_token')->value(csrf_token());
  247. Content::composing(function (Content $content) {
  248. $content->setView(static::$contentView);
  249. });
  250. }
  251. }