Passed
Pull Request — master (#933)
by
unknown
05:48
created

LayoutHelper::makeBodyClasses()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1.2963

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 11
ccs 1
cts 3
cp 0.3333
crap 1.2963
rs 10
1
<?php
2
3
namespace JeroenNoten\LaravelAdminLte\Helpers;
4
5
use Illuminate\Support\Facades\View;
6
7
class LayoutHelper
8
{
9
    /**
10
     * Set of tokens related to screen sizes/breakpoints.
11
     *
12
     * @var array
13
     */
14
    protected static $screenBreakpoints = ['xs', 'sm', 'md', 'lg', 'xl'];
15
16
    /**
17
     * Set of tokens related to sidebar mini config values.
18
     *
19
     * @var array
20
     */
21
    protected static $sidebarMiniValues = ['xs', 'md', 'lg'];
22
23
    /**
24
     * Check if layout topnav is enabled.
25
     *
26
     * @return bool
27
     */
28 12
    public static function isLayoutTopnavEnabled()
29
    {
30 12
        return config('adminlte.layout_topnav') || View::getSection('layout_topnav');
31
    }
32
33
    /**
34
     * Check if layout boxed is enabled.
35
     *
36
     * @return bool
37
     */
38 11
    public static function isLayoutBoxedEnabled()
39
    {
40 11
        return config('adminlte.layout_boxed') || View::getSection('layout_boxed');
41
    }
42
43
    /**
44
     * Make and return the set of classes related to the body tag.
45
     *
46
     * @return string
47
     */
48
    public static function makeBodyClasses()
49
    {
50
        $classes = [];
51
52
        $classes = array_merge($classes, self::makeLayoutClasses());
53
        $classes = array_merge($classes, self::makeSidebarClasses());
54
        $classes = array_merge($classes, self::makeRightSidebarClasses());
55
        $classes = array_merge($classes, self::makeCustomBodyClasses());
56
        $classes = array_merge($classes, self::makeDarkModeClasses());
57
58 11
        return trim(implode(' ', $classes));
59
    }
60 11
61
    /**
62 11
     * Make and return the set of data attributes related to the body tag.
63 11
     *
64 11
     * @return string
65 11
     */
66 11
    public static function makeBodyData()
67
    {
68 11
        $data = [];
69
70
        // Add data related to the "sidebar_scrollbar_theme" configuration.
71
72
        $sbTheme = config('adminlte.sidebar_scrollbar_theme', 'os-theme-light');
73
74
        if ($sbTheme != 'os-theme-light') {
75
            $data[] = "data-scrollbar-theme={$sbTheme}";
76 1
        }
77
78 1
        // Add data related to the "sidebar_scrollbar_auto_hide" configuration.
79
80
        $sbAutoHide = config('adminlte.sidebar_scrollbar_auto_hide', 'l');
81
82 1
        if ($sbAutoHide != 'l') {
83
            $data[] = "data-scrollbar-auto-hide={$sbAutoHide}";
84 1
        }
85 1
86
        return trim(implode(' ', $data));
87
    }
88
89
    /**
90 1
     * Make and return the set of classes related to the layout configuration.
91
     *
92 1
     * @return array
93 1
     */
94
    private static function makeLayoutClasses()
95
    {
96 1
        $classes = [];
97
98
        // Add classes related to the "layout_topnav" configuration.
99
100
        if (self::isLayoutTopnavEnabled()) {
101
            $classes[] = 'layout-top-nav';
102
        }
103
104 11
        // Add classes related to the "layout_boxed" configuration.
105
106 11
        if (self::isLayoutBoxedEnabled()) {
107
            $classes[] = 'layout-boxed';
108
        }
109
110 11
        // Add classes related to fixed sidebar layout configuration. The fixed
111 2
        // sidebar is not compatible with layout topnav.
112
113
        if (! self::isLayoutTopnavEnabled() && config('adminlte.layout_fixed_sidebar')) {
114
            $classes[] = 'layout-fixed';
115
        }
116 11
117 3
        // Add classes related to fixed navbar/footer configuration. The fixed
118
        // navbar/footer is not compatible with layout boxed.
119
120
        if (! self::isLayoutBoxedEnabled()) {
121
            $classes = array_merge($classes, self::makeFixedResponsiveClasses('navbar'));
122
            $classes = array_merge($classes, self::makeFixedResponsiveClasses('footer'));
123 11
        }
124 1
125
        return $classes;
126
    }
127
128
    /**
129
     * Make the set of classes related to a fixed responsive configuration.
130 11
     *
131 11
     * @param string $section The layout section (navbar or footer)
132 11
     * @return array
133
     */
134
    private static function makeFixedResponsiveClasses($section)
135 11
    {
136
        $classes = [];
137
        $cfg = config("adminlte.layout_fixed_{$section}");
138
139
        if ($cfg === true) {
140
            $cfg = ['xs' => true];
141
        }
142
143
        // At this point the config should be an array.
144 11
145
        if (! is_array($cfg)) {
146 11
            return $classes;
147 11
        }
148
149 11
        // Make the set of responsive classes in relation to the config.
150 2
151
        foreach ($cfg as $breakpoint => $enabled) {
152
            if (in_array($breakpoint, self::$screenBreakpoints)) {
153
                $classes[] = self::makeFixedResponsiveClass(
154
                    $section, $breakpoint, $enabled
155 11
                );
156 11
            }
157
        }
158
159
        return $classes;
160
    }
161 2
162 2
    /**
163 2
     * Make a responsive class for the navbar/footer fixed mode on a particular
164 2
     * breakpoint token.
165
     *
166
     * @param string $section The layout section (navbar or footer)
167
     * @param string $bp The screen breakpoint (xs, sm, md, lg, xl)
168
     * @param bool $enabled Whether to enable fixed mode (true, false)
169 2
     * @return string
170
     */
171
    private static function makeFixedResponsiveClass($section, $bp, $enabled)
172
    {
173
        // Create the class prefix.
174
175
        $prefix = ($bp === 'xs') ? 'layout' : "layout-{$bp}";
176
177
        // Create the class suffix.
178
179
        $suffix = $enabled ? 'fixed' : 'not-fixed';
180
181 2
        // Return the responsice class for fixed mode.
182
183
        return "{$prefix}-{$section}-{$suffix}";
184
    }
185 2
186
    /**
187
     * Make the set of classes related to the main left sidebar configuration.
188
     *
189 2
     * @return array
190
     */
191
    private static function makeSidebarClasses()
192
    {
193 2
        $classes = [];
194
195
        // Add classes related to the "sidebar_mini" configuration.
196
197
        $sidebarMiniCfg = config('adminlte.sidebar_mini', 'lg');
198
199
        if (in_array($sidebarMiniCfg, self::$sidebarMiniValues)) {
200
            $suffix = $sidebarMiniCfg === 'lg' ? '' : "-{$sidebarMiniCfg}";
201 11
            $classes[] = "sidebar-mini${suffix}";
202
        }
203 11
204
        // Add classes related to the "sidebar_collapse" configuration.
205
206
        if (config('adminlte.sidebar_collapse') || View::getSection('sidebar_collapse')) {
207 11
            $classes[] = 'sidebar-collapse';
208
        }
209 11
210 11
        return $classes;
211 11
    }
212
213
    /**
214
     * Make the set of classes related to the right sidebar configuration.
215
     *
216 11
     * @return array
217 1
     */
218
    private static function makeRightSidebarClasses()
219
    {
220 11
        $classes = [];
221
222
        // Add classes related to the "right_sidebar" configuration.
223
224
        if (config('adminlte.right_sidebar') && config('adminlte.right_sidebar_push')) {
225
            $classes[] = 'control-sidebar-push';
226
        }
227
228 11
        return $classes;
229
    }
230 11
231
    /**
232
     * Make the set of classes related to custom body classes configuration.
233
     *
234 11
     * @return array
235 1
     */
236
    private static function makeCustomBodyClasses()
237
    {
238 11
        $classes = [];
239
        $cfg = config('adminlte.classes_body', '');
240
241
        if (is_string($cfg) && $cfg) {
242
            $classes[] = $cfg;
243
        }
244
245
        return $classes;
246 11
    }
247
248 11
    /**
249 11
     * Make the set of classes related to the dark mode.
250
     *
251 11
     * @return array
252 1
     */
253
    private static function makeDarkModeClasses()
254
    {
255 11
        $classes = [];
256
        $cfg = config('adminlte.layout_dark_mode', false);
257
258
        if (is_bool($cfg) && $cfg) {
259
            $classes[] = 'dark-mode';
260
        }
261
262
        return $classes;
263 11
    }
264
}
265