44
55namespace Yajra \DataTables \Html ;
66
7+ use Illuminate \Contracts \Support \Renderable ;
78use Illuminate \Support \Fluent ;
89use Illuminate \Support \Traits \Macroable ;
10+ use InvalidArgumentException ;
11+ use Throwable ;
912use Yajra \DataTables \Html \Enums \LayoutPosition ;
1013
1114class Layout extends Fluent
@@ -17,79 +20,104 @@ public static function make(array $options = []): static
1720 return new static ($ options );
1821 }
1922
20- public function topStart ( string | array |null $ options , int $ order = 0 ): static
23+ public function top ( array |string | null $ options , ? int $ order = null ): static
2124 {
22- return $ this ->top ($ options , $ order , LayoutPosition::Start);
25+ $ this ->attributes [LayoutPosition::Top->withOrder ($ order )] = $ options ;
26+
27+ return $ this ;
2328 }
2429
25- public function top ( array | string |null $ options , ?int $ order = null , ? LayoutPosition $ position = null ): static
30+ public function topStart ( string |array | null $ options , ?int $ order = null ): static
2631 {
27- if ($ order > 0 ) {
28- $ this ->attributes ["top {$ order }{$ position ?->value}" ] = $ options ;
29- } else {
30- $ this ->attributes ["top {$ position ?->value}" ] = $ options ;
31- }
32+ $ this ->attributes [LayoutPosition::TopStart->withOrder ($ order )] = $ options ;
3233
3334 return $ this ;
3435 }
3536
36- public function topEnd (string |array |null $ options , int $ order = 0 ): static
37+ public function topEnd (string |array |null $ options , ? int $ order = null ): static
3738 {
38- return $ this ->top ($ options , $ order , LayoutPosition::End);
39+ $ this ->attributes [LayoutPosition::TopEnd->withOrder ($ order )] = $ options ;
40+
41+ return $ this ;
3942 }
4043
41- public function topEndView (string $ selector , int $ order = 0 ): static
44+ public function topView (string $ selector , ? int $ order = null ): static
4245 {
43- return $ this ->topView ( $ selector , $ order, LayoutPosition::End );
46+ return $ this ->top ( $ this -> renderCustomElement ( $ selector) , $ order );
4447 }
4548
46- public function topView (string $ selector , int $ order = 0 , ? LayoutPosition $ position = null ): static
49+ public function topStartView (string $ selector , ? int $ order = null ): static
4750 {
48- $ script = "function() { return $(' {$ selector }').html(); } " ;
51+ return $ this ->topStart ($ this ->renderCustomElement ($ selector ), $ order );
52+ }
4953
50- return $ this ->top ($ script , $ order , $ position );
54+ public function topEndView (string $ selector , ?int $ order = null ): static
55+ {
56+ return $ this ->topEnd ($ this ->renderCustomElement ($ selector ), $ order );
5157 }
5258
53- public function bottomStartView ( string $ selector , int $ order = 0 ): static
59+ public function bottom ( array | string | null $ options , ? int $ order = null ): static
5460 {
55- return $ this ->bottomView ($ selector , $ order , LayoutPosition::Start);
61+ $ this ->attributes [LayoutPosition::Bottom->withOrder ($ order )] = $ options ;
62+
63+ return $ this ;
5664 }
5765
58- public function bottomView (string $ selector , int $ order = 0 , ? LayoutPosition $ position = null ): static
66+ public function bottomStart (string | array | null $ options , ? int $ order = null ): static
5967 {
60- $ script = " function() { return $(' { $ selector } ').html(); } " ;
68+ $ this -> attributes [LayoutPosition::BottomStart-> withOrder ( $ order )] = $ options ;
6169
62- return $ this -> bottom ( $ script , $ order , $ position ) ;
70+ return $ this ;
6371 }
6472
65- public function bottom ( array | string |null $ options , ?int $ order = null , ? LayoutPosition $ position = null ): static
73+ public function bottomEnd ( string |array | null $ options , ?int $ order = null ): static
6674 {
67- if ($ order > 0 ) {
68- $ this ->attributes ["bottom {$ order }{$ position ?->value}" ] = $ options ;
69- } else {
70- $ this ->attributes ["bottom {$ position ?->value}" ] = $ options ;
71- }
75+ $ this ->attributes [LayoutPosition::BottomEnd->withOrder ($ order )] = $ options ;
7276
7377 return $ this ;
7478 }
7579
76- public function bottomEndView (string $ selector , int $ order = 0 ): static
80+ public function bottomView (string $ selector , ? int $ order = null ): static
7781 {
78- return $ this ->bottomView ( $ selector , $ order, LayoutPosition::End );
82+ return $ this ->bottom ( $ this -> renderCustomElement ( $ selector) , $ order );
7983 }
8084
81- public function topStartView (string $ selector , int $ order = 0 ): static
85+ public function bottomStartView (string $ selector , ? int $ order = null ): static
8286 {
83- return $ this ->topView ( $ selector , $ order, LayoutPosition::Start );
87+ return $ this ->bottomStart ( $ this -> renderCustomElement ( $ selector) , $ order );
8488 }
8589
86- public function bottomStart (string | array | null $ options , int $ order = 0 ): static
90+ public function bottomEndView (string $ selector , ? int $ order = null ): static
8791 {
88- return $ this ->bottom ( $ options , $ order, LayoutPosition::Start );
92+ return $ this ->bottomEnd ( $ this -> renderCustomElement ( $ selector ) , $ order );
8993 }
9094
91- public function bottomEnd (string |array |null $ options , int $ order = 0 ): static
95+ /**
96+ * @param Renderable|view-string $view
97+ *
98+ * @throws Throwable
99+ */
100+ public function addView (
101+ Renderable |string $ view ,
102+ LayoutPosition $ layoutPosition ,
103+ ?int $ order = null
104+ ): static {
105+ $ html = $ view instanceof Renderable ? $ view ->render () : view ($ view )->render ();
106+ $ element = json_encode ($ html );
107+
108+ if ($ element === false ) {
109+ throw new InvalidArgumentException ("Cannot render view [ $ html] to json. " );
110+ }
111+
112+ $ this ->attributes [$ layoutPosition ->withOrder ($ order )] = $ this ->renderCustomElement ($ element , false );
113+
114+ return $ this ;
115+ }
116+
117+ private function renderCustomElement (string $ element , bool $ asJsSelector = true ): string
92118 {
93- return $ this ->bottom ($ options , $ order , LayoutPosition::End);
119+ $ html = $ asJsSelector ? "$(' {$ element }').html() " : $ element ;
120+
121+ return "function() { return $ html; } " ;
94122 }
95123}
0 commit comments