QuickCreate.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. <?php
  2. namespace Dcat\Admin\Grid\Tools;
  3. use Dcat\Admin\Admin;
  4. use Dcat\Admin\Form\Field;
  5. use Dcat\Admin\Form\Field\MultipleSelect;
  6. use Dcat\Admin\Form\Field\Select;
  7. use Dcat\Admin\Form\Field\Text;
  8. use Dcat\Admin\Grid;
  9. use Illuminate\Contracts\Support\Renderable;
  10. use Illuminate\Support\Collection;
  11. class QuickCreate implements Renderable
  12. {
  13. /**
  14. * @var Grid
  15. */
  16. protected $parent;
  17. /**
  18. * @var Collection
  19. */
  20. protected $fields;
  21. /**
  22. * @var string
  23. */
  24. protected $action;
  25. /**
  26. * @var string
  27. */
  28. protected $method = 'POST';
  29. /**
  30. * QuickCreate constructor.
  31. *
  32. * @param Grid $grid
  33. */
  34. public function __construct(Grid $grid)
  35. {
  36. $this->parent = $grid;
  37. $this->fields = Collection::make();
  38. $this->action = request()->url();
  39. }
  40. protected function formatPlaceholder($placeholder)
  41. {
  42. return array_filter((array) $placeholder);
  43. }
  44. /**
  45. * @param string $column
  46. * @param string $placeholder
  47. *
  48. * @return Text
  49. */
  50. public function text($column, $placeholder = '')
  51. {
  52. $field = new Text($column, $this->formatPlaceholder($placeholder));
  53. $this->addField($field->attribute('style', 'width:180px'));
  54. return $field;
  55. }
  56. /**
  57. * @param string $column
  58. * @param string $placeholder
  59. *
  60. * @return Text
  61. */
  62. public function email($column, $placeholder = '')
  63. {
  64. return $this->text($column, $placeholder)
  65. ->inputmask(['alias' => 'email']);
  66. }
  67. /**
  68. * @param string $column
  69. * @param string $placeholder
  70. *
  71. * @return Text
  72. */
  73. public function ip($column, $placeholder = '')
  74. {
  75. return $this->text($column, $placeholder)
  76. ->inputmask(['alias' => 'ip'])
  77. ->attribute('style', 'width:120px');
  78. }
  79. /**
  80. * @param string $column
  81. * @param string $placeholder
  82. *
  83. * @return Text
  84. */
  85. public function url($column, $placeholder = '')
  86. {
  87. return $this->text($column, $placeholder)
  88. ->inputmask(['alias' => 'url']);
  89. }
  90. /**
  91. * @param string $column
  92. * @param string $placeholder
  93. *
  94. * @return Text
  95. */
  96. public function password($column, $placeholder = '')
  97. {
  98. return $this->text($column, $placeholder)
  99. ->attribute('type', 'password')
  100. ->attribute('style', 'width:120px');
  101. }
  102. /**
  103. * @param string $column
  104. * @param string $placeholder
  105. *
  106. * @return Text
  107. */
  108. public function mobile($column, $placeholder = '')
  109. {
  110. return $this->text($column, $placeholder)
  111. ->inputmask(['mask' => '99999999999'])
  112. ->attribute('style', 'width:120px');
  113. }
  114. /**
  115. * @param string $column
  116. * @param string $placeholder
  117. *
  118. * @return Text
  119. */
  120. public function integer($column, $placeholder = '')
  121. {
  122. return $this->text($column, $placeholder)
  123. ->inputmask(['alias' => 'integer'])
  124. ->attribute('style', 'width:150px');
  125. }
  126. /**
  127. * @param string $column
  128. * @param string $placeholder
  129. *
  130. * @return Field\SelectResource
  131. */
  132. public function selectResource($column, $placeholder = '')
  133. {
  134. $field = new Field\SelectResource($column, $this->formatPlaceholder($placeholder));
  135. $this->addField($field->attribute('style', 'width:150px'));
  136. return $field;
  137. }
  138. /**
  139. * @param string $column
  140. * @param string $placeholder
  141. *
  142. * @return Select
  143. */
  144. public function select($column, $placeholder = '')
  145. {
  146. $field = new Select($column, $this->formatPlaceholder($placeholder));
  147. $this->addField($field);
  148. return $field;
  149. }
  150. /**
  151. * @param string $column
  152. * @param string $placeholder
  153. *
  154. * @return Field\Tags
  155. */
  156. public function tags($column, $placeholder = '')
  157. {
  158. $field = new Field\Tags($column, $this->formatPlaceholder($placeholder));
  159. $this->addField($field);
  160. return $field;
  161. }
  162. /**
  163. * @param string $column
  164. * @param string $placeholder
  165. *
  166. * @return MultipleSelect
  167. */
  168. public function multipleSelect($column, $placeholder = '')
  169. {
  170. $field = new MultipleSelect($column, $this->formatPlaceholder($placeholder));
  171. $this->addField($field);
  172. return $field;
  173. }
  174. /**
  175. * @param string $column
  176. * @param string $placeholder
  177. *
  178. * @return Field\Date
  179. */
  180. public function datetime($column, $placeholder = '')
  181. {
  182. return $this->date($column, $placeholder)->format('YYYY-MM-DD HH:mm:ss');
  183. }
  184. /**
  185. * @param string $column
  186. * @param string $placeholder
  187. *
  188. * @return Field\Date
  189. */
  190. public function time($column, $placeholder = '')
  191. {
  192. return $this->date($column, $placeholder)->format('HH:mm:ss');
  193. }
  194. /**
  195. * @param string $column
  196. * @param string $placeholder
  197. *
  198. * @return Field\Date
  199. */
  200. public function date($column, $placeholder = '')
  201. {
  202. $field = new Field\Date($column, $this->formatPlaceholder($placeholder));
  203. $this->addField($field);
  204. return $field;
  205. }
  206. /**
  207. * @param Field $field
  208. *
  209. * @return Field
  210. */
  211. protected function addField(Field $field)
  212. {
  213. $elementClass = array_merge([$this->getElementClass()], $field->getElementClass());
  214. $field->addElementClass($elementClass);
  215. $field->setView($this->resolveView(get_class($field)));
  216. $field::collectAssets();
  217. $this->fields->push($field);
  218. return $field;
  219. }
  220. /**
  221. * @param string $class
  222. *
  223. * @return string
  224. */
  225. protected function resolveView($class)
  226. {
  227. $path = explode('\\', $class);
  228. $name = strtolower(array_pop($path));
  229. return "admin::grid.quick-create.{$name}";
  230. }
  231. /**
  232. * @param string $action
  233. *
  234. * @return $this
  235. */
  236. public function action(?string $action)
  237. {
  238. $this->action = admin_url($action);
  239. return $this;
  240. }
  241. /**
  242. * @param string $method
  243. *
  244. * @return $this
  245. */
  246. public function method(?string $method = 'POST')
  247. {
  248. $this->method = $method;
  249. return $this;
  250. }
  251. protected function script()
  252. {
  253. $url = $this->action;
  254. $method = $this->method;
  255. $uniqueName = $this->parent->getName();
  256. $script = <<<JS
  257. (function () {
  258. var ctr = $('.{$this->getElementClass()}'),
  259. btn = $('.quick-create-button-{$uniqueName}');
  260. btn.on('click', function () {
  261. ctr.toggle().click();
  262. });
  263. ctr.on('click', function () {
  264. ctr.find('.create-form').show();
  265. ctr.find('.create').hide();
  266. });
  267. ctr.find('.cancel').on('click', function () {
  268. if (btn.length) {
  269. ctr.hide();
  270. return;
  271. }
  272. ctr.find('.create-form').hide();
  273. ctr.find('.create').show();
  274. return false;
  275. });
  276. ctr.find('.create-form').submit(function (e) {
  277. e.preventDefault();
  278. if (ctr.attr('submitting')) {
  279. return;
  280. }
  281. var btn = $(this).find(':submit').buttonLoading();
  282. ctr.attr('submitting', 1);
  283. $.ajax({
  284. url: '{$url}',
  285. type: '{$method}',
  286. data: $(this).serialize(),
  287. success: function(data) {
  288. ctr.attr('submitting', '');
  289. btn.buttonLoading(false);
  290. console.info(data);
  291. if (data.status == true) {
  292. Dcat.success(data.message);
  293. Dcat.reload();
  294. return;
  295. }
  296. if (typeof data.validation !== 'undefined') {
  297. Dcat.warning(data.message)
  298. }
  299. },
  300. error:function(xhq){
  301. btn.buttonLoading(false);
  302. ctr.attr('submitting', '');
  303. var json = xhq.responseJSON;
  304. if (typeof json === 'object') {
  305. if (json.message) {
  306. Dcat.error(json.message);
  307. } else if (json.errors) {
  308. var i, errors = [];
  309. for (i in json.errors) {
  310. errors.push(json.errors[i].join("<br>"));
  311. }
  312. Dcat.error(errors.join("<br>"));
  313. }
  314. }
  315. }
  316. });
  317. return false;
  318. });
  319. })();
  320. JS;
  321. Admin::script($script);
  322. }
  323. public function getElementClass()
  324. {
  325. $name = $this->parent->getName();
  326. return 'quick-create'.($name ? "-{$name}" : '');
  327. }
  328. /**
  329. * @param int $columnCount
  330. *
  331. * @return array|string
  332. */
  333. public function render($columnCount = 0)
  334. {
  335. if ($this->fields->isEmpty()) {
  336. return '';
  337. }
  338. $this->script();
  339. $vars = [
  340. 'columnCount' => $columnCount,
  341. 'fields' => $this->fields,
  342. 'elementClass' => $this->getElementClass(),
  343. ];
  344. return view('admin::grid.quick-create.form', $vars)->render();
  345. }
  346. }