Content.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. <?php
  2. namespace Dcat\Admin\Layout;
  3. use Closure;
  4. use Dcat\Admin\Admin;
  5. use Dcat\Admin\Traits\HasBuilderEvents;
  6. use Illuminate\Contracts\Support\Renderable;
  7. use Illuminate\Support\ViewErrorBag;
  8. class Content implements Renderable
  9. {
  10. use HasBuilderEvents;
  11. /**
  12. * @var string
  13. */
  14. protected $view = 'admin::layouts.content';
  15. /**
  16. * @var array
  17. */
  18. protected $variables = [];
  19. /**
  20. * Content title.
  21. *
  22. * @var string
  23. */
  24. protected $title = '';
  25. /**
  26. * Content description.
  27. *
  28. * @var string
  29. */
  30. protected $description = '';
  31. /**
  32. * Page breadcrumb.
  33. *
  34. * @var array
  35. */
  36. protected $breadcrumb = [];
  37. /**
  38. * @var Row[]
  39. */
  40. protected $rows = [];
  41. /**
  42. * @var array
  43. */
  44. protected $config = [];
  45. /**
  46. * @var bool
  47. */
  48. protected $usingPerfectScrollbar = false;
  49. /**
  50. * Content constructor.
  51. *
  52. * @param Closure|null $callback
  53. */
  54. public function __construct(\Closure $callback = null)
  55. {
  56. $this->callResolving();
  57. if ($callback instanceof Closure) {
  58. $callback($this);
  59. }
  60. }
  61. /**
  62. * Create a content instance.
  63. *
  64. * @param mixed ...$params
  65. *
  66. * @return $this
  67. */
  68. public static function make(...$params)
  69. {
  70. return new static(...$params);
  71. }
  72. /**
  73. * @param string $header
  74. *
  75. * @return $this
  76. */
  77. public function header($header = '')
  78. {
  79. return $this->title($header);
  80. }
  81. /**
  82. * Set title of content.
  83. *
  84. * @param string $title
  85. *
  86. * @return $this
  87. */
  88. public function title($title)
  89. {
  90. $this->title = $title;
  91. return $this;
  92. }
  93. /**
  94. * Set description of content.
  95. *
  96. * @param string $description
  97. *
  98. * @return $this
  99. */
  100. public function description($description = '')
  101. {
  102. $this->description = $description;
  103. return $this;
  104. }
  105. /**
  106. * Build full page.
  107. *
  108. * @return $this
  109. */
  110. public function full()
  111. {
  112. $this->view = 'admin::layouts.full-content';
  113. return $this->withConfig('blank_page', true);
  114. }
  115. /**
  116. * Set breadcrumb of content.
  117. *
  118. * @example
  119. * $this->breadcrumb('Menu', 'auth/menu', 'fa fa-align-justify');
  120. * $this->breadcrumb([
  121. * ['text' => 'Menu', 'url' => 'auth/menu', 'icon' => 'fa fa-align-justify']
  122. * ]);
  123. *
  124. * @param array ...$breadcrumb
  125. *
  126. * @return $this
  127. */
  128. public function breadcrumb(...$breadcrumb)
  129. {
  130. $this->formatBreadcrumb($breadcrumb);
  131. $this->breadcrumb = array_merge($this->breadcrumb, $breadcrumb);
  132. return $this;
  133. }
  134. /**
  135. * @param bool $value
  136. *
  137. * @return $this
  138. */
  139. public function perfectScrollbar(bool $value = true)
  140. {
  141. $this->usingPerfectScrollbar = $value;
  142. return $this;
  143. }
  144. /**
  145. * @param array $breadcrumb
  146. *
  147. * @throws \Exception
  148. *
  149. * @return void
  150. */
  151. protected function formatBreadcrumb(array &$breadcrumb)
  152. {
  153. if (! $breadcrumb) {
  154. throw new \Exception('Breadcrumb format error!');
  155. }
  156. $notArray = false;
  157. foreach ($breadcrumb as &$item) {
  158. $isArray = is_array($item);
  159. if ($isArray && ! isset($item['text'])) {
  160. throw new \Exception('Breadcrumb format error!');
  161. }
  162. if (! $isArray && $item) {
  163. $notArray = true;
  164. }
  165. }
  166. if (! $breadcrumb) {
  167. throw new \Exception('Breadcrumb format error!');
  168. }
  169. if ($notArray) {
  170. $breadcrumb = [
  171. [
  172. 'text' => $breadcrumb[0] ?? null,
  173. 'url' => $breadcrumb[1] ?? null,
  174. 'icon' => $breadcrumb[2] ?? null,
  175. ],
  176. ];
  177. }
  178. }
  179. /**
  180. * Alias of method row.
  181. *
  182. * @param mixed $content
  183. *
  184. * @return Content
  185. */
  186. public function body($content)
  187. {
  188. return $this->row($content);
  189. }
  190. /**
  191. * Add one row for content body.
  192. *
  193. * @param $content
  194. *
  195. * @return $this
  196. */
  197. public function row($content)
  198. {
  199. if ($content instanceof Closure) {
  200. $row = new Row();
  201. call_user_func($content, $row);
  202. $this->addRow($row);
  203. } else {
  204. $this->addRow(new Row($content));
  205. }
  206. return $this;
  207. }
  208. /**
  209. * @param $content
  210. *
  211. * @return $this
  212. */
  213. public function prepend($content)
  214. {
  215. if ($content instanceof Closure) {
  216. $row = new Row();
  217. call_user_func($content, $row);
  218. $this->prependRow($row);
  219. } else {
  220. $this->prependRow(new Row($content));
  221. }
  222. return $this;
  223. }
  224. protected function prependRow(Row $row)
  225. {
  226. array_unshift($this->rows, $row);
  227. }
  228. /**
  229. * Add Row.
  230. *
  231. * @param Row $row
  232. */
  233. protected function addRow(Row $row)
  234. {
  235. $this->rows[] = $row;
  236. }
  237. /**
  238. * Build html of content.
  239. *
  240. * @return string
  241. */
  242. public function build()
  243. {
  244. $html = '';
  245. foreach ($this->rows as $row) {
  246. $html .= $row->render();
  247. }
  248. return $html;
  249. }
  250. /**
  251. * Set success message for content.
  252. *
  253. * @param string $title
  254. * @param string $message
  255. *
  256. * @return $this
  257. */
  258. public function withSuccess($title = '', $message = '')
  259. {
  260. admin_success($title, $message);
  261. return $this;
  262. }
  263. /**
  264. * Set error message for content.
  265. *
  266. * @param string $title
  267. * @param string $message
  268. *
  269. * @return $this
  270. */
  271. public function withError($title = '', $message = '')
  272. {
  273. admin_error($title, $message);
  274. return $this;
  275. }
  276. /**
  277. * Set warning message for content.
  278. *
  279. * @param string $title
  280. * @param string $message
  281. *
  282. * @return $this
  283. */
  284. public function withWarning($title = '', $message = '')
  285. {
  286. admin_warning($title, $message);
  287. return $this;
  288. }
  289. /**
  290. * Set info message for content.
  291. *
  292. * @param string $title
  293. * @param string $message
  294. *
  295. * @return $this
  296. */
  297. public function withInfo($title = '', $message = '')
  298. {
  299. admin_info($title, $message);
  300. return $this;
  301. }
  302. /**
  303. * Set content view.
  304. *
  305. * @param null|string $view
  306. *
  307. * @return $this
  308. */
  309. public function view(?string $view)
  310. {
  311. $this->view = $view;
  312. return $this;
  313. }
  314. /**
  315. * @param string|array $key
  316. * @param mixed $value
  317. *
  318. * @return $this
  319. */
  320. public function with($key, $value = null)
  321. {
  322. if (is_array($key)) {
  323. $this->variables = array_merge($this->variables, $key);
  324. } else {
  325. $this->variables[$key] = $value;
  326. }
  327. return $this;
  328. }
  329. /**
  330. * @param string|array $key
  331. * @param mixed $value
  332. *
  333. * @return $this
  334. */
  335. public function withConfig($key, $value = null)
  336. {
  337. if (is_array($key)) {
  338. $this->config = array_merge($this->config, $key);
  339. } else {
  340. $this->config[$key] = $value;
  341. }
  342. return $this;
  343. }
  344. /**
  345. * @return void
  346. */
  347. protected function shareDefaultErrors()
  348. {
  349. if (! session()->all()) {
  350. view()->share(['errors' => new ViewErrorBag()]);
  351. }
  352. }
  353. /**
  354. * 页面滚动条优化.
  355. */
  356. protected function makePerfectScrollbar()
  357. {
  358. if (! $this->usingPerfectScrollbar) {
  359. return;
  360. }
  361. Admin::script(
  362. <<<'JS'
  363. (function () {
  364. if ($(window).width() > 768) {
  365. var ps, wps;
  366. if ($('.full-page .wrapper').length) {
  367. wps = new PerfectScrollbar('.full-page .wrapper');
  368. }
  369. ps = new PerfectScrollbar('html');
  370. $(document).one('pjax:send',function () {
  371. ps && ps.destroy();
  372. ps = null;
  373. wps && wps.destroy();
  374. wps = null;
  375. });
  376. }
  377. })()
  378. JS
  379. );
  380. }
  381. /**
  382. * @return array
  383. */
  384. protected function variables()
  385. {
  386. return array_merge([
  387. 'header' => $this->title,
  388. 'description' => $this->description,
  389. 'breadcrumb' => $this->breadcrumb,
  390. 'configData' => $this->applClasses(),
  391. 'content' => $this->build(),
  392. 'pjaxContainerId' => Admin::$pjaxContainerId,
  393. ], $this->variables);
  394. }
  395. /**
  396. * @return array
  397. */
  398. protected function applClasses()
  399. {
  400. // default data array
  401. $defaultData = [
  402. 'theme' => '',
  403. 'sidebar_collapsed' => false,
  404. 'sidebar_dark' => false,
  405. 'navbar_color' => '',
  406. 'navbar_class' => 'sticky',
  407. 'footer_type' => '',
  408. 'body_class' => '',
  409. ];
  410. $data = array_merge(
  411. config('admin.layout') ?: [],
  412. $this->config
  413. );
  414. $allOptions = [
  415. 'theme' => '',
  416. 'footer_type' => '',
  417. 'body_class' => '',
  418. 'sidebar_dark' => '',
  419. 'sidebar_collapsed' => [true, false],
  420. 'navbar_color' => ['bg-primary', 'bg-info', 'bg-warning', 'bg-success', 'bg-danger', 'bg-dark'],
  421. 'navbar_class' => ['floating' => 'floating-nav', 'sticky' => 'fixed-top', 'hidden' => 'd-none'],
  422. ];
  423. $maps = [
  424. 'footer_type' => 'footer_type',
  425. ];
  426. foreach ($allOptions as $key => $value) {
  427. if (! array_key_exists($key, $defaultData)) {
  428. continue;
  429. }
  430. if (! isset($data[$key])) {
  431. $data[$key] = $defaultData[$key];
  432. continue;
  433. }
  434. if (
  435. isset($maps[$key])
  436. && ! isset($allOptions[$maps[$key]][$data[$key]])
  437. ) {
  438. $data[$key] = $defaultData[$key];
  439. }
  440. }
  441. return [
  442. 'theme' => $data['theme'],
  443. 'sidebar_collapsed' => $data['sidebar_collapsed'],
  444. 'navbar_color' => $data['navbar_color'],
  445. 'navbar_class' => $allOptions['navbar_class'][$data['navbar_class']],
  446. 'sidebar_class' => $data['sidebar_collapsed'] ? 'sidebar-collapse' : '',
  447. 'body_class' => $data['body_class'],
  448. 'sidebar_dark' => $data['sidebar_dark'],
  449. ];
  450. }
  451. /**
  452. * Render this content.
  453. *
  454. * @return string
  455. */
  456. public function render()
  457. {
  458. $this->callComposing();
  459. $this->shareDefaultErrors();
  460. $variables = $this->variables();
  461. $this->callComposed();
  462. $this->makePerfectScrollbar();
  463. return view($this->view, $variables)->render();
  464. }
  465. /**
  466. * Register a composed event.
  467. *
  468. * @param callable $callback
  469. * @param bool $once
  470. */
  471. public static function composed(callable $callback, bool $once = false)
  472. {
  473. static::addBuilderListeners('builder.composed', $callback, $once);
  474. }
  475. /**
  476. * Call the composed callbacks.
  477. *
  478. * @param array ...$params
  479. */
  480. protected function callComposed(...$params)
  481. {
  482. $this->fireBuilderEvent('builder.composed', ...$params);
  483. }
  484. }