Assets.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. namespace Dcat\Admin\Layout;
  3. class Assets
  4. {
  5. /**
  6. * @var array
  7. */
  8. protected $script = [];
  9. /**
  10. * @var array
  11. */
  12. protected $style = [];
  13. /**
  14. * @var array
  15. */
  16. protected $css = [];
  17. /**
  18. * @var array
  19. */
  20. protected $js = [];
  21. /**
  22. * @var array
  23. */
  24. protected $headerJs = [
  25. 'vendors' => 'dcat-admin/vendors/js/vendors.min.js',
  26. 'dcat' => 'dcat-admin/dcat/js/app.js',
  27. ];
  28. /**
  29. * @var array
  30. */
  31. protected $baseCss = [
  32. 'vendors' => 'dcat-admin/vendors/css/vendors.min.css',
  33. 'bootstrap' => 'dcat-admin/css/bootstrap.css',
  34. 'bootstrap-extended' => 'dcat-admin/css/bootstrap-extended.css',
  35. 'toastr' => 'dcat-admin/vendors/css/extensions/toastr.css',
  36. 'colors' => 'dcat-admin/css/colors.css',
  37. 'components' => 'dcat-admin/css/components.css',
  38. 'palette-gradient' => 'dcat-admin/css/core/colors/palette-gradient.css',
  39. //'custom' => 'dcat-admin/css/custom-laravel.css',
  40. 'dcat' => 'dcat-admin/dcat/css/app.css',
  41. ];
  42. /**
  43. * @var array
  44. */
  45. protected $baseJs = [
  46. 'menu' => 'dcat-admin/js/core/app-menu.js',
  47. 'app' => 'dcat-admin/js/core/app.js',
  48. 'toastr' => 'dcat-admin/vendors/js/extensions/toastr.min.js',
  49. 'pjax' => 'dcat-admin/plugins/jquery-pjax/jquery.pjax.min.js',
  50. 'layer' => 'dcat-admin/plugins/layer/layer.js',
  51. ];
  52. /**
  53. * @var array
  54. */
  55. public $components = [];
  56. /**
  57. * @var string
  58. */
  59. public $fonts = 'https://fonts.googleapis.com/css?family=Nunito:200,200i,300,300i,400,400i,600,600i,800,800i,900,900i';
  60. /**
  61. * @var bool
  62. */
  63. protected $isPjax = false;
  64. /**
  65. * @var bool
  66. */
  67. protected $usingFullPage = false;
  68. /**
  69. * Assets constructor.
  70. */
  71. public function __construct()
  72. {
  73. $this->isPjax = request()->pjax();
  74. }
  75. public function full(bool $value = true)
  76. {
  77. $this->usingFullPage = $value;
  78. return $this;
  79. }
  80. public function collect(string $name)
  81. {
  82. $this->js($this->components[$name]['js'] ?? null);
  83. $this->css($this->components[$name]['css'] ?? null);
  84. }
  85. public function css($css)
  86. {
  87. if (! $css) {
  88. return;
  89. }
  90. $this->css = array_merge($this->css, (array) $css);
  91. }
  92. public function baseCss(array $css)
  93. {
  94. $this->baseCss = $css;
  95. }
  96. public function js($js)
  97. {
  98. if (! $js) {
  99. return;
  100. }
  101. $this->js = array_merge($this->js, (array) $js);
  102. }
  103. public function headerJs($js)
  104. {
  105. if (! $js) {
  106. return;
  107. }
  108. $this->headerJs = array_merge($this->headerJs, (array) $js);
  109. }
  110. public function baseJs(array $js)
  111. {
  112. if (! $js) {
  113. return;
  114. }
  115. $this->baseJs = $js;
  116. }
  117. public function script($script)
  118. {
  119. if (! $script) {
  120. return;
  121. }
  122. $this->script = array_merge($this->script, (array) $script);
  123. }
  124. public function style($style)
  125. {
  126. if (! $style) {
  127. return;
  128. }
  129. $this->style = array_merge($this->style, (array) $style);
  130. }
  131. protected function addLayoutCss()
  132. {
  133. if ($this->usingFullPage) {
  134. return;
  135. }
  136. if (config('admin.layout.main_layout_type') === 'horizontal') {
  137. $this->baseCss[] = 'dcat-admin/css/core/menu/menu-types/horizontal-menu.css';
  138. }
  139. $this->baseCss[] = 'dcat-admin/css/core/menu/menu-types/vertical-menu.css';
  140. }
  141. protected function addThemeCss()
  142. {
  143. if (! $theme = config('admin.layout.theme')) {
  144. return;
  145. }
  146. $this->baseCss[] = "dcat-admin/css/themes/{$theme}.css";
  147. }
  148. protected function addFontCss()
  149. {
  150. $this->fonts && ($this->baseCss[] = $this->fonts);
  151. }
  152. protected function mergeBaseCss()
  153. {
  154. if ($this->isPjax) {
  155. return;
  156. }
  157. $this->addLayoutCss();
  158. $this->addThemeCss();
  159. $this->addFontCss();
  160. $this->css = array_merge($this->baseCss, $this->css);
  161. }
  162. public function renderCss()
  163. {
  164. $this->mergeBaseCss();
  165. $html = '';
  166. foreach (array_unique($this->css) as &$v) {
  167. $v = admin_asset($v);
  168. $html .= "<link rel=\"stylesheet\" href=\"{$v}\">";
  169. }
  170. return $html;
  171. }
  172. protected function mergeBaseJs()
  173. {
  174. if ($this->isPjax) {
  175. return;
  176. }
  177. $this->js = array_merge($this->baseJs, $this->js);
  178. }
  179. public function renderJs()
  180. {
  181. $this->mergeBaseJs();
  182. $html = '';
  183. foreach (array_unique($this->js) as &$v) {
  184. $v = admin_asset($v);
  185. $html .= "<script src=\"$v\"></script>";
  186. }
  187. return $html;
  188. }
  189. public function renderHeaderJs()
  190. {
  191. $html = '';
  192. foreach (array_unique($this->headerJs) as &$v) {
  193. $v = admin_asset($v);
  194. $html .= "<script src=\"$v\"></script>";
  195. }
  196. return $html;
  197. }
  198. public function renderScript()
  199. {
  200. $script = implode(';', array_unique($this->script));
  201. return "<script data-exec-on-popstate>Dcat.ready(function () { {$script} });</script>";
  202. }
  203. public function renderStyle()
  204. {
  205. $style = implode('', array_unique($this->style));
  206. return "<style>$style</style>";
  207. }
  208. }