QuickCreate.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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->width('200px'));
  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. ->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. ->width('100px');
  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. ->width('100px');
  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. ->width('120px');
  116. }
  117. /**
  118. * @param string $column
  119. * @param string $placeholder
  120. *
  121. * @return Select
  122. */
  123. public function select($column, $placeholder = '')
  124. {
  125. Admin::style(
  126. <<<'CSS'
  127. .quick-create .select2-selection--single {
  128. padding: 3px 12px !important;
  129. height: 30px !important;
  130. width: 150px !important;
  131. }
  132. CSS
  133. );
  134. $field = new Select($column, $this->formatPlaceholder($placeholder));
  135. $this->addField($field);
  136. return $field;
  137. }
  138. /**
  139. * @param string $column
  140. * @param string $placeholder
  141. *
  142. * @return MultipleSelect
  143. */
  144. public function multipleSelect($column, $placeholder = '')
  145. {
  146. Admin::style(
  147. <<<'CSS'
  148. .quick-create .select2-selection--multiple {
  149. padding: 0 !important;
  150. height: 30px !important;
  151. width: 200px !important;
  152. min-height: 30px !important;
  153. }
  154. CSS
  155. );
  156. $field = new MultipleSelect($column, $this->formatPlaceholder($placeholder));
  157. $this->addField($field);
  158. return $field;
  159. }
  160. /**
  161. * @param string $column
  162. * @param string $placeholder
  163. *
  164. * @return Field\Date
  165. */
  166. public function datetime($column, $placeholder = '')
  167. {
  168. return $this->date($column, $placeholder)->format('YYYY-MM-DD HH:mm:ss');
  169. }
  170. /**
  171. * @param string $column
  172. * @param string $placeholder
  173. *
  174. * @return Field\Date
  175. */
  176. public function time($column, $placeholder = '')
  177. {
  178. return $this->date($column, $placeholder)->format('HH:mm:ss');
  179. }
  180. /**
  181. * @param string $column
  182. * @param string $placeholder
  183. *
  184. * @return Field\Date
  185. */
  186. public function date($column, $placeholder = '')
  187. {
  188. $field = new Field\Date($column, $this->formatPlaceholder($placeholder));
  189. $this->addField($field);
  190. return $field;
  191. }
  192. /**
  193. * @param Field $field
  194. *
  195. * @return Field
  196. */
  197. protected function addField(Field $field)
  198. {
  199. $elementClass = array_merge(['quick-create'], $field->elementClass());
  200. $field->addElementClass($elementClass);
  201. $field->view($this->resolveView(get_class($field)));
  202. $this->fields->push($field);
  203. return $field;
  204. }
  205. /**
  206. * @param string $class
  207. *
  208. * @return string
  209. */
  210. protected function resolveView($class)
  211. {
  212. $path = explode('\\', $class);
  213. $name = strtolower(array_pop($path));
  214. return "admin::grid.quick-create.{$name}";
  215. }
  216. protected function script()
  217. {
  218. $url = request()->url();
  219. $uniqueName = $this->parent->getName();
  220. $script = <<<JS
  221. (function () {
  222. var ctr = $('.{$uniqueName}-quick-create'),
  223. btn = $('.{$uniqueName}-quick-create-button');
  224. btn.click(function () {
  225. ctr.toggle().click();
  226. });
  227. ctr.click(function () {
  228. ctr.find('.create-form').show();
  229. ctr.find('.create').hide();
  230. });
  231. ctr.find('.cancel').click(function () {
  232. if (btn.length) {
  233. ctr.hide();
  234. return;
  235. }
  236. ctr.find('.create-form').hide();
  237. ctr.find('.create').show();
  238. return false;
  239. });
  240. ctr.find('.create-form').submit(function (e) {
  241. e.preventDefault();
  242. if (ctr.attr('working')) {
  243. return;
  244. }
  245. ctr.attr('working', 1);
  246. LA.NP.start();
  247. $.ajax({
  248. url: '{$url}',
  249. type: 'POST',
  250. data: $(this).serialize(),
  251. success: function(data) {
  252. LA.NP.done();
  253. ctr.attr('working', '');
  254. console.info(data);
  255. if (data.status == true) {
  256. LA.success(data.message);
  257. LA.reload();
  258. return;
  259. }
  260. if (typeof data.validation !== 'undefined') {
  261. LA.warning(data.message)
  262. }
  263. },
  264. error:function(xhq){
  265. LA.NP.done();
  266. ctr.attr('working', '');
  267. var json = xhq.responseJSON;
  268. if (typeof json === 'object') {
  269. if (json.message) {
  270. LA.error(json.message);
  271. } else if (json.errors) {
  272. var i, errors = [];
  273. for (i in json.errors) {
  274. errors.push(json.errors[i].join("<br>"));
  275. }
  276. LA.error(errors.join("<br>"));
  277. }
  278. }
  279. }
  280. });
  281. return false;
  282. });
  283. })();
  284. JS;
  285. Admin::script($script);
  286. }
  287. /**
  288. * @param int $columnCount
  289. *
  290. * @return array|string
  291. */
  292. public function render($columnCount = 0)
  293. {
  294. if ($this->fields->isEmpty()) {
  295. return '';
  296. }
  297. $this->script();
  298. $vars = [
  299. 'columnCount' => $columnCount,
  300. 'fields' => $this->fields,
  301. 'elementClass' => $this->parent->getName().'-quick-create',
  302. 'hidden' => $this->parent->option('create_mode') === Grid::CREATE_MODE_QUICK,
  303. ];
  304. return view('admin::grid.quick-create.form', $vars)->render();
  305. }
  306. }