Passed
Pull Request — master (#1255)
by Diego
05:13
created

LayoutHelper::makeContentWrapperClasses()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

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