4
4
5
5
namespace Yajra \DataTables \Html ;
6
6
7
+ use Illuminate \Contracts \Support \Renderable ;
8
+ use Illuminate \Support \Facades \Blade ;
7
9
use Illuminate \Support \Fluent ;
8
10
use Illuminate \Support \Traits \Macroable ;
11
+ use Illuminate \View \Component ;
12
+ use InvalidArgumentException ;
13
+ use Livewire \Livewire ;
14
+ use Throwable ;
9
15
use Yajra \DataTables \Html \Enums \LayoutPosition ;
10
16
11
17
class Layout extends Fluent
@@ -17,79 +23,128 @@ public static function make(array $options = []): static
17
23
return new static ($ options );
18
24
}
19
25
20
- public function topStart ( string | array |null $ options , int $ order = 0 ): static
26
+ public function top ( array |string | null $ options , ? int $ order = null ): static
21
27
{
22
- return $ this ->top ($ options , $ order , LayoutPosition::Start);
28
+ $ this ->attributes [LayoutPosition::Top->withOrder ($ order )] = $ options ;
29
+
30
+ return $ this ;
23
31
}
24
32
25
- public function top ( array | string |null $ options , ?int $ order = null , ? LayoutPosition $ position = null ): static
33
+ public function topStart ( string |array | null $ options , ?int $ order = null ): static
26
34
{
27
- if ($ order > 0 ) {
28
- $ this ->attributes ["top {$ order }{$ position ?->value}" ] = $ options ;
29
- } else {
30
- $ this ->attributes ["top {$ position ?->value}" ] = $ options ;
31
- }
35
+ $ this ->attributes [LayoutPosition::TopStart->withOrder ($ order )] = $ options ;
32
36
33
37
return $ this ;
34
38
}
35
39
36
- public function topEnd (string |array |null $ options , int $ order = 0 ): static
40
+ public function topEnd (string |array |null $ options , ? int $ order = null ): static
37
41
{
38
- return $ this ->top ($ options , $ order , LayoutPosition::End);
42
+ $ this ->attributes [LayoutPosition::TopEnd->withOrder ($ order )] = $ options ;
43
+
44
+ return $ this ;
39
45
}
40
46
41
- public function topEndView (string $ selector , int $ order = 0 ): static
47
+ public function topView (string $ selector , ? int $ order = null ): static
42
48
{
43
- return $ this ->topView ( $ selector , $ order, LayoutPosition::End );
49
+ return $ this ->top ( $ this -> renderCustomElement ( $ selector) , $ order );
44
50
}
45
51
46
- public function topView (string $ selector , int $ order = 0 , ? LayoutPosition $ position = null ): static
52
+ public function topStartView (string $ selector , ? int $ order = null ): static
47
53
{
48
- $ script = "function() { return $(' {$ selector }').html(); } " ;
54
+ return $ this ->topStart ($ this ->renderCustomElement ($ selector ), $ order );
55
+ }
49
56
50
- return $ this ->top ($ script , $ order , $ position );
57
+ public function topEndView (string $ selector , ?int $ order = null ): static
58
+ {
59
+ return $ this ->topEnd ($ this ->renderCustomElement ($ selector ), $ order );
51
60
}
52
61
53
- public function bottomStartView ( string $ selector , int $ order = 0 ): static
62
+ public function bottom ( array | string | null $ options , ? int $ order = null ): static
54
63
{
55
- return $ this ->bottomView ($ selector , $ order , LayoutPosition::Start);
64
+ $ this ->attributes [LayoutPosition::Bottom->withOrder ($ order )] = $ options ;
65
+
66
+ return $ this ;
56
67
}
57
68
58
- public function bottomView (string $ selector , int $ order = 0 , ? LayoutPosition $ position = null ): static
69
+ public function bottomStart (string | array | null $ options , ? int $ order = null ): static
59
70
{
60
- $ script = " function() { return $(' { $ selector } ').html(); } " ;
71
+ $ this -> attributes [LayoutPosition::BottomStart-> withOrder ( $ order )] = $ options ;
61
72
62
- return $ this -> bottom ( $ script , $ order , $ position ) ;
73
+ return $ this ;
63
74
}
64
75
65
- public function bottom ( array | string |null $ options , ?int $ order = null , ? LayoutPosition $ position = null ): static
76
+ public function bottomEnd ( string |array | null $ options , ?int $ order = null ): static
66
77
{
67
- if ($ order > 0 ) {
68
- $ this ->attributes ["bottom {$ order }{$ position ?->value}" ] = $ options ;
69
- } else {
70
- $ this ->attributes ["bottom {$ position ?->value}" ] = $ options ;
71
- }
78
+ $ this ->attributes [LayoutPosition::BottomEnd->withOrder ($ order )] = $ options ;
72
79
73
80
return $ this ;
74
81
}
75
82
76
- public function bottomEndView (string $ selector , int $ order = 0 ): static
83
+ public function bottomView (string $ selector , ? int $ order = null ): static
77
84
{
78
- return $ this ->bottomView ( $ selector , $ order, LayoutPosition::End );
85
+ return $ this ->bottom ( $ this -> renderCustomElement ( $ selector) , $ order );
79
86
}
80
87
81
- public function topStartView (string $ selector , int $ order = 0 ): static
88
+ public function bottomStartView (string $ selector , ? int $ order = null ): static
82
89
{
83
- return $ this ->topView ( $ selector , $ order, LayoutPosition::Start );
90
+ return $ this ->bottomStart ( $ this -> renderCustomElement ( $ selector) , $ order );
84
91
}
85
92
86
- public function bottomStart (string | array | null $ options , int $ order = 0 ): static
93
+ public function bottomEndView (string $ selector , ? int $ order = null ): static
87
94
{
88
- return $ this ->bottom ($ options , $ order , LayoutPosition::Start);
95
+ return $ this ->bottomEnd ($ this ->renderCustomElement ($ selector ), $ order );
96
+ }
97
+
98
+ /**
99
+ * @throws Throwable
100
+ */
101
+ public function addView (
102
+ Component |Renderable |string $ view ,
103
+ LayoutPosition $ layoutPosition ,
104
+ ?int $ order = null
105
+ ): static {
106
+ if ($ view instanceof Component) {
107
+ $ view = Blade::renderComponent ($ view );
108
+ }
109
+
110
+ $ html = $ view instanceof Renderable ? $ view ->render () : Blade::render ($ view );
111
+
112
+ $ element = json_encode ($ html );
113
+
114
+ if ($ element === false ) {
115
+ throw new InvalidArgumentException ("Cannot render view [ $ html] to json. " );
116
+ }
117
+
118
+ $ this ->attributes [$ layoutPosition ->withOrder ($ order )] = $ this ->renderCustomElement ($ element , false );
119
+
120
+ return $ this ;
89
121
}
90
122
91
- public function bottomEnd (string |array |null $ options , int $ order = 0 ): static
123
+ /**
124
+ * @param class-string $component
125
+ *
126
+ * @throws Throwable
127
+ */
128
+ public function addLivewire (
129
+ string $ component ,
130
+ LayoutPosition $ layoutPosition ,
131
+ ?int $ order = null
132
+ ): static {
133
+ $ html = json_encode (Livewire::mount ($ component ));
134
+
135
+ if ($ html === false ) {
136
+ throw new InvalidArgumentException ("Cannot render Livewire component [ $ component] to json. " );
137
+ }
138
+
139
+ $ this ->attributes [$ layoutPosition ->withOrder ($ order )] = $ this ->renderCustomElement ($ html , false );
140
+
141
+ return $ this ;
142
+ }
143
+
144
+ private function renderCustomElement (string $ element , bool $ asJsSelector = true ): string
92
145
{
93
- return $ this ->bottom ($ options , $ order , LayoutPosition::End);
146
+ $ html = $ asJsSelector ? "$(' {$ element }').html() " : $ element ;
147
+
148
+ return "function() { return $ html; } " ;
94
149
}
95
150
}
0 commit comments