4
4
5
5
namespace Yajra \DataTables \Html ;
6
6
7
+ use Illuminate \Contracts \Support \Renderable ;
7
8
use Illuminate \Support \Fluent ;
8
9
use Illuminate \Support \Traits \Macroable ;
10
+ use InvalidArgumentException ;
11
+ use Throwable ;
9
12
use Yajra \DataTables \Html \Enums \LayoutPosition ;
10
13
11
14
class Layout extends Fluent
@@ -17,79 +20,104 @@ public static function make(array $options = []): static
17
20
return new static ($ options );
18
21
}
19
22
20
- public function topStart ( string | array |null $ options , int $ order = 0 ): static
23
+ public function top ( array |string | null $ options , ? int $ order = null ): static
21
24
{
22
- return $ this ->top ($ options , $ order , LayoutPosition::Start);
25
+ $ this ->attributes [LayoutPosition::Top->withOrder ($ order )] = $ options ;
26
+
27
+ return $ this ;
23
28
}
24
29
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
26
31
{
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 ;
32
33
33
34
return $ this ;
34
35
}
35
36
36
- public function topEnd (string |array |null $ options , int $ order = 0 ): static
37
+ public function topEnd (string |array |null $ options , ? int $ order = null ): static
37
38
{
38
- return $ this ->top ($ options , $ order , LayoutPosition::End);
39
+ $ this ->attributes [LayoutPosition::TopEnd->withOrder ($ order )] = $ options ;
40
+
41
+ return $ this ;
39
42
}
40
43
41
- public function topEndView (string $ selector , int $ order = 0 ): static
44
+ public function topView (string $ selector , ? int $ order = null ): static
42
45
{
43
- return $ this ->topView ( $ selector , $ order, LayoutPosition::End );
46
+ return $ this ->top ( $ this -> renderCustomElement ( $ selector) , $ order );
44
47
}
45
48
46
- public function topView (string $ selector , int $ order = 0 , ? LayoutPosition $ position = null ): static
49
+ public function topStartView (string $ selector , ? int $ order = null ): static
47
50
{
48
- $ script = "function() { return $(' {$ selector }').html(); } " ;
51
+ return $ this ->topStart ($ this ->renderCustomElement ($ selector ), $ order );
52
+ }
49
53
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 );
51
57
}
52
58
53
- public function bottomStartView ( string $ selector , int $ order = 0 ): static
59
+ public function bottom ( array | string | null $ options , ? int $ order = null ): static
54
60
{
55
- return $ this ->bottomView ($ selector , $ order , LayoutPosition::Start);
61
+ $ this ->attributes [LayoutPosition::Bottom->withOrder ($ order )] = $ options ;
62
+
63
+ return $ this ;
56
64
}
57
65
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
59
67
{
60
- $ script = " function() { return $(' { $ selector } ').html(); } " ;
68
+ $ this -> attributes [LayoutPosition::BottomStart-> withOrder ( $ order )] = $ options ;
61
69
62
- return $ this -> bottom ( $ script , $ order , $ position ) ;
70
+ return $ this ;
63
71
}
64
72
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
66
74
{
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 ;
72
76
73
77
return $ this ;
74
78
}
75
79
76
- public function bottomEndView (string $ selector , int $ order = 0 ): static
80
+ public function bottomView (string $ selector , ? int $ order = null ): static
77
81
{
78
- return $ this ->bottomView ( $ selector , $ order, LayoutPosition::End );
82
+ return $ this ->bottom ( $ this -> renderCustomElement ( $ selector) , $ order );
79
83
}
80
84
81
- public function topStartView (string $ selector , int $ order = 0 ): static
85
+ public function bottomStartView (string $ selector , ? int $ order = null ): static
82
86
{
83
- return $ this ->topView ( $ selector , $ order, LayoutPosition::Start );
87
+ return $ this ->bottomStart ( $ this -> renderCustomElement ( $ selector) , $ order );
84
88
}
85
89
86
- public function bottomStart (string | array | null $ options , int $ order = 0 ): static
90
+ public function bottomEndView (string $ selector , ? int $ order = null ): static
87
91
{
88
- return $ this ->bottom ( $ options , $ order, LayoutPosition::Start );
92
+ return $ this ->bottomEnd ( $ this -> renderCustomElement ( $ selector ) , $ order );
89
93
}
90
94
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
92
118
{
93
- return $ this ->bottom ($ options , $ order , LayoutPosition::End);
119
+ $ html = $ asJsSelector ? "$(' {$ element }').html() " : $ element ;
120
+
121
+ return "function() { return $ html; } " ;
94
122
}
95
123
}
0 commit comments