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