QuickCreate.php 8.5 KB

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