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