Passed
Pull Request — master (#1255)
by Diego
08:36 queued 04:38
created

LayoutHelper::makePreloaderClasses()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

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