Passed
Push — master ( 6f7775...21b94f )
by Florian
05:07
created

LayoutHelper::makeBodyData()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 21
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

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