Passed
Pull Request — master (#1112)
by kostas
08:20
created

LayoutHelper::isPreloaderEnabled()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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