Passed
Push — master ( 1def5f...54f3b5 )
by Florian
03:54
created

LayoutHelper::makeFixedResponsiveClasses()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 26
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5

Importance

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