LayoutHelper::makeLayoutClasses()   A
last analyzed

Complexity

Conditions 6
Paths 16

Size

Total Lines 34
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 12
c 1
b 0
f 0
nc 16
nop 0
dl 0
loc 34
ccs 13
cts 13
cp 1
crap 6
rs 9.2222
1
<?php
2
3
namespace JeroenNoten\LaravelAdminLte\Helpers;
4
5
use Illuminate\Support\Facades\View;
6
use JeroenNoten\LaravelAdminLte\Events\ReadingDarkModePreference;
7
use JeroenNoten\LaravelAdminLte\Http\Controllers\DarkModeController;
8
9
class LayoutHelper
10
{
11
    /**
12
     * Holds the set of tokens related to screen sizes/breakpoints.
13
     *
14
     * @var array
15
     */
16
    protected static $screenBreakpoints = ['xs', 'sm', 'md', 'lg', 'xl'];
17
18
    /**
19
     * Holds the set of tokens related to the valid config values of the
20
     * sidebar mini option.
21
     *
22
     * @var array
23
     */
24
    protected static $sidebarMiniValues = ['xs', 'md', 'lg'];
25
26
    /**
27
     * Checks if layout topnav is enabled.
28
     *
29
     * @return bool
30
     */
31 12
    public static function isLayoutTopnavEnabled()
32
    {
33 12
        return config('adminlte.layout_topnav', false)
34 12
            || ! empty(View::getSection('layout_topnav'));
35
    }
36
37
    /**
38
     * Checks if layout boxed is enabled.
39
     *
40
     * @return bool
41
     */
42 11
    public static function isLayoutBoxedEnabled()
43
    {
44 11
        return config('adminlte.layout_boxed', false)
45 11
            || ! empty(View::getSection('layout_boxed'));
46
    }
47
48
    /**
49
     * Checks if right sidebar is enabled.
50
     *
51
     * @return bool
52
     */
53 1
    public static function isRightSidebarEnabled()
54
    {
55 1
        return config('adminlte.right_sidebar', false)
56 1
            || ! empty(View::getSection('right_sidebar'));
57
    }
58
59
    /**
60
     * Makes and return the set of classes related to the body tag.
61
     *
62
     * @return string
63
     */
64 11
    public static function makeBodyClasses()
65
    {
66 11
        $classes = [];
67 11
        array_push($classes, ...self::makeLayoutClasses());
68 11
        array_push($classes, ...self::makeSidebarClasses());
69 11
        array_push($classes, ...self::makeRightSidebarClasses());
70 11
        array_push($classes, ...self::makeCustomBodyClasses());
71 11
        array_push($classes, ...self::makeDarkModeClasses());
72
73 11
        return trim(implode(' ', $classes));
74
    }
75
76
    /**
77
     * Makes and return the set of data attributes related to the body tag.
78
     *
79
     * @return string
80
     */
81 1
    public static function makeBodyData()
82
    {
83 1
        $data = [];
84
85
        // Add data related to the "sidebar_scrollbar_theme" configuration.
86
87 1
        $sbTheme = config('adminlte.sidebar_scrollbar_theme', 'os-theme-light');
88
89 1
        if ($sbTheme != 'os-theme-light') {
90 1
            $data[] = "data-scrollbar-theme={$sbTheme}";
91
        }
92
93
        // Add data related to the "sidebar_scrollbar_auto_hide" configuration.
94
95 1
        $sbAutoHide = config('adminlte.sidebar_scrollbar_auto_hide', 'l');
96
97 1
        if ($sbAutoHide != 'l') {
98 1
            $data[] = "data-scrollbar-auto-hide={$sbAutoHide}";
99
        }
100
101 1
        return trim(implode(' ', $data));
102
    }
103
104
    /**
105
     * Makes and return the set of classes related to the content-wrapper
106
     * element.
107
     *
108
     * @return string
109
     */
110 1
    public static function makeContentWrapperClasses()
111
    {
112 1
        $classes = ['content-wrapper'];
113
114
        // Add classes from the configuration file.
115
116 1
        $cfg = config('adminlte.classes_content_wrapper', '');
117
118 1
        if (is_string($cfg) && ! empty($cfg)) {
119 1
            $classes[] = $cfg;
120
        }
121
122
        // Add position-relative when using a content-wrapper preloader.
123
124 1
        if (PreloaderHelper::isPreloaderEnabled('cwrapper')) {
125 1
            $classes[] = 'position-relative';
126
        }
127
128 1
        return trim(implode(' ', $classes));
129
    }
130
131
    /**
132
     * Makes and return the set of classes related to the layout configuration.
133
     *
134
     * @return array
135
     */
136 11
    private static function makeLayoutClasses()
137
    {
138 11
        $classes = [];
139
140
        // Add classes related to the "layout_topnav" configuration.
141
142 11
        if (self::isLayoutTopnavEnabled()) {
143 2
            $classes[] = 'layout-top-nav';
144
        }
145
146
        // Add classes related to the "layout_boxed" configuration.
147
148 11
        if (self::isLayoutBoxedEnabled()) {
149 3
            $classes[] = 'layout-boxed';
150
        }
151
152
        // Add classes related to fixed sidebar layout configuration. The fixed
153
        // sidebar is not compatible with layout topnav.
154
155 11
        $fixedSidebar = config('adminlte.layout_fixed_sidebar', false);
156
157 11
        if (! self::isLayoutTopnavEnabled() && $fixedSidebar) {
158 1
            $classes[] = 'layout-fixed';
159
        }
160
161
        // Add classes related to fixed navbar/footer configuration. The fixed
162
        // navbar/footer is not compatible with layout boxed.
163
164 11
        if (! self::isLayoutBoxedEnabled()) {
165 11
            array_push($classes, ...self::makeFixedResponsiveClasses('navbar'));
166 11
            array_push($classes, ...self::makeFixedResponsiveClasses('footer'));
167
        }
168
169 11
        return $classes;
170
    }
171
172
    /**
173
     * Makes the set of classes related to a fixed responsive configuration.
174
     *
175
     * @param  string  $section  The layout section (navbar or footer)
176
     * @return array
177
     */
178 11
    private static function makeFixedResponsiveClasses($section)
179
    {
180 11
        $classes = [];
181 11
        $cfg = config("adminlte.layout_fixed_{$section}", false);
182
183 11
        if ($cfg === true) {
184 2
            $cfg = ['xs' => true];
185
        }
186
187
        // At this point the config should be an array.
188
189 11
        if (! is_array($cfg)) {
190 11
            return $classes;
191
        }
192
193
        // Make the set of responsive classes in relation to the config.
194
195 2
        foreach ($cfg as $breakpoint => $enabled) {
196 2
            if (in_array($breakpoint, self::$screenBreakpoints)) {
197 2
                $classes[] = self::makeFixedResponsiveClass(
198 2
                    $section, $breakpoint, $enabled
199 2
                );
200
            }
201
        }
202
203 2
        return $classes;
204
    }
205
206
    /**
207
     * Makes a responsive class for the navbar/footer fixed mode on a particular
208
     * breakpoint token.
209
     *
210
     * @param  string  $section  The layout section (navbar or footer)
211
     * @param  string  $bp  The screen breakpoint (xs, sm, md, lg, xl)
212
     * @param  bool  $enabled  Whether to enable fixed mode (true, false)
213
     * @return string
214
     */
215 2
    private static function makeFixedResponsiveClass($section, $bp, $enabled)
216
    {
217
        // Create the class prefix.
218
219 2
        $prefix = ($bp === 'xs') ? 'layout' : "layout-{$bp}";
220
221
        // Create the class suffix.
222
223 2
        $suffix = $enabled ? 'fixed' : 'not-fixed';
224
225
        // Return the responsice class for fixed mode.
226
227 2
        return "{$prefix}-{$section}-{$suffix}";
228
    }
229
230
    /**
231
     * Makes the set of classes related to the main left sidebar configuration.
232
     *
233
     * @return array
234
     */
235 11
    private static function makeSidebarClasses()
236
    {
237 11
        $classes = [];
238
239
        // Add classes related to the "sidebar_mini" configuration.
240
241 11
        $sidebarMiniCfg = config('adminlte.sidebar_mini', 'lg');
242
243 11
        if (in_array($sidebarMiniCfg, self::$sidebarMiniValues)) {
244 11
            $suffix = $sidebarMiniCfg === 'lg' ? '' : "-{$sidebarMiniCfg}";
245 11
            $classes[] = "sidebar-mini{$suffix}";
246
        }
247
248
        // Add classes related to the "sidebar_collapse" configuration.
249
250 11
        $sidebarCollapse = config('adminlte.sidebar_collapse', false)
251 11
            || ! empty(View::getSection('sidebar_collapse'));
252
253 11
        if ($sidebarCollapse) {
254 1
            $classes[] = 'sidebar-collapse';
255
        }
256
257 11
        return $classes;
258
    }
259
260
    /**
261
     * Makes the set of classes related to the right sidebar configuration.
262
     *
263
     * @return array
264
     */
265 11
    private static function makeRightSidebarClasses()
266
    {
267 11
        $classes = [];
268
269
        // Add classes related to the "right_sidebar" configuration.
270
271 11
        $rightSidebarPush = config('adminlte.right_sidebar', false)
272 11
            && config('adminlte.right_sidebar_push', false);
273
274 11
        if ($rightSidebarPush) {
275 1
            $classes[] = 'control-sidebar-push';
276
        }
277
278 11
        return $classes;
279
    }
280
281
    /**
282
     * Makes the set of classes related to custom body classes configuration.
283
     *
284
     * @return array
285
     */
286 11
    private static function makeCustomBodyClasses()
287
    {
288 11
        $classes = [];
289 11
        $cfg = config('adminlte.classes_body', '');
290
291 11
        if (is_string($cfg) && ! empty($cfg)) {
292 1
            $classes[] = $cfg;
293
        }
294
295 11
        return $classes;
296
    }
297
298
    /**
299
     * Makes the set of classes related to the dark mode.
300
     *
301
     * @return array
302
     */
303 11
    private static function makeDarkModeClasses()
304
    {
305 11
        $classes = [];
306
307
        // Use the dark mode controller to check if dark mode is enabled.
308
309 11
        $darkModeCtrl = new DarkModeController();
310
311
        // Dispatch an event to notify we are about to read the dark mode
312
        // preference. A listener may catch this event in order to setup the
313
        // dark mode initial state using the methods provided by the controller.
314
315 11
        event(new ReadingDarkModePreference($darkModeCtrl));
316
317
        // Now, check if dark mode is enabled.
318
319 11
        if ($darkModeCtrl->isEnabled()) {
320 1
            $classes[] = 'dark-mode';
321
        }
322
323 11
        return $classes;
324
    }
325
}
326