DialogForm.php 6.2 KB

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