Passed
Push — master ( bf7a7b...641691 )
by Florian
02:46
created

LayoutHelper::makeDarkModeClasses()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 2
nop 0
dl 0
loc 10
ccs 6
cts 6
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace JeroenNoten\LaravelAdminLte\Helpers;
4
5
use Illuminate\Support\Facades\View;
6
7
class LayoutHelper
8
{
9
    /**
10
     * Set of tokens related to screen sizes/breakpoints.
11
     *
12
     * @var array
13
     */
14
    protected static $screenBreakpoints = ['xs', 'sm', 'md', 'lg', 'xl'];
15
16
    /**
17
     * Check if layout topnav is enabled.
18
     *
19
     * @return bool
20
     */
21 12
    public static function isLayoutTopnavEnabled()
22
    {
23 12
        return config('adminlte.layout_topnav') || View::getSection('layout_topnav');
24
    }
25
26
    /**
27
     * Check if layout boxed is enabled.
28
     *
29
     * @return bool
30
     */
31 11
    public static function isLayoutBoxedEnabled()
32
    {
33 11
        return config('adminlte.layout_boxed') || View::getSection('layout_boxed');
34
    }
35
36
    /**
37
     * Make and return the set of classes related to the body tag.
38
     *
39
     * @return string
40
     */
41 11
    public static function makeBodyClasses()
42
    {
43 11
        $classes = [];
44
45 11
        $classes = array_merge($classes, self::makeLayoutClasses());
46 11
        $classes = array_merge($classes, self::makeSidebarClasses());
47 11
        $classes = array_merge($classes, self::makeRightSidebarClasses());
48 11
        $classes = array_merge($classes, self::makeCustomBodyClasses());
49 11
        $classes = array_merge($classes, self::makeDarkModeClasses());
50
51 11
        return trim(implode(' ', $classes));
52
    }
53
54
    /**
55
     * Make and return the set of data attributes related to the body tag.
56
     *
57
     * @return string
58
     */
59 1
    public static function makeBodyData()
60
    {
61 1
        $data = [];
62
63
        // Add data related to the "sidebar_scrollbar_theme" configuration.
64
65 1
        $sbTheme = config('adminlte.sidebar_scrollbar_theme', 'os-theme-light');
66
67 1
        if ($sbTheme != 'os-theme-light') {
68 1
            $data[] = "data-scrollbar-theme={$sbTheme}";
69
        }
70
71
        // Add data related to the "sidebar_scrollbar_auto_hide" configuration.
72
73 1
        $sbAutoHide = config('adminlte.sidebar_scrollbar_auto_hide', 'l');
74
75 1
        if ($sbAutoHide != 'l') {
76 1
            $data[] = "data-scrollbar-auto-hide={$sbAutoHide}";
77
        }
78
79 1
        return trim(implode(' ', $data));
80
    }
81
82
    /**
83
     * Make and return the set of classes related to the layout configuration.
84
     *
85
     * @return array
86
     */
87 11
    private static function makeLayoutClasses()
88
    {
89 11
        $classes = [];
90
91
        // Add classes related to the "layout_topnav" configuration.
92
93 11
        if (self::isLayoutTopnavEnabled()) {
94 2
            $classes[] = 'layout-top-nav';
95
        }
96
97
        // Add classes related to the "layout_boxed" configuration.
98
99 11
        if (self::isLayoutBoxedEnabled()) {
100 3
            $classes[] = 'layout-boxed';
101
        }
102
103
        // Add classes related to fixed sidebar layout configuration. The fixed
104
        // sidebar is not compatible with layout topnav.
105
106 11
        if (! self::isLayoutTopnavEnabled() && config('adminlte.layout_fixed_sidebar')) {
107 1
            $classes[] = 'layout-fixed';
108
        }
109
110
        // Add classes related to fixed navbar/footer configuration. The fixed
111
        // navbar/footer is not compatible with layout boxed.
112
113 11
        if (! self::isLayoutBoxedEnabled()) {
114 11
            $classes = array_merge($classes, self::makeFixedResponsiveClasses('navbar'));
115 11
            $classes = array_merge($classes, self::makeFixedResponsiveClasses('footer'));
116
        }
117
118 11
        return $classes;
119
    }
120
121
    /**
122
     * Make the set of classes related to a fixed responsive configuration.
123
     *
124
     * @param string $section The layout section (navbar or footer)
125
     * @return array
126
     */
127 11
    private static function makeFixedResponsiveClasses($section)
128
    {
129 11
        $classes = [];
130 11
        $cfg = config("adminlte.layout_fixed_{$section}");
131
132 11
        if ($cfg === true) {
133 2
            $cfg = ['xs' => true];
134
        }
135
136
        // At this point the config should be an array.
137
138 11
        if (! is_array($cfg)) {
139 11
            return $classes;
140
        }
141
142
        // Make the set of responsive classes in relation to the config.
143
144 2
        foreach ($cfg as $breakpoint => $enabled) {
145 2
            if (in_array($breakpoint, self::$screenBreakpoints)) {
146 2
                $classes[] = self::makeFixedResponsiveClass(
147 2
                    $section, $breakpoint, $enabled
148
                );
149
            }
150
        }
151
152 2
        return $classes;
153
    }
154
155
    /**
156
     * Make a responsive class for the navbar/footer fixed mode on a particular
157
     * breakpoint token.
158
     *
159
     * @param string $section The layout section (navbar or footer)
160
     * @param string $bp The screen breakpoint (xs, sm, md, lg, xl)
161
     * @param bool $enabled Whether to enable fixed mode (true, false)
162
     * @return string
163
     */
164 2
    private static function makeFixedResponsiveClass($section, $bp, $enabled)
165
    {
166
        // Create the class prefix.
167
168 2
        $prefix = ($bp === 'xs') ? 'layout' : "layout-{$bp}";
169
170
        // Create the class suffix.
171
172 2
        $suffix = $enabled ? 'fixed' : 'not-fixed';
173
174
        // Return the responsice class for fixed mode.
175
176 2
        return "{$prefix}-{$section}-{$suffix}";
177
    }
178
179
    /**
180
     * Make the set of classes related to the left sidebar configuration.
181
     *
182
     * @return array
183
     */
184 11
    private static function makeSidebarClasses()
185
    {
186 11
        $classes = [];
187
188
        // Add classes related to the "sidebar_mini" configuration.
189
190 11
        if (config('adminlte.sidebar_mini', true) === true) {
191 11
            $classes[] = 'sidebar-mini';
192 1
        } elseif (config('adminlte.sidebar_mini', true) == 'md') {
193 1
            $classes[] = 'sidebar-mini sidebar-mini-md';
194
        }
195
196
        // Add classes related to the "sidebar_collapse" configuration.
197
198 11
        if (config('adminlte.sidebar_collapse') || View::getSection('sidebar_collapse')) {
199 1
            $classes[] = 'sidebar-collapse';
200
        }
201
202 11
        return $classes;
203
    }
204
205
    /**
206
     * Make the set of classes related to the right sidebar configuration.
207
     *
208
     * @return array
209
     */
210 11
    private static function makeRightSidebarClasses()
211
    {
212 11
        $classes = [];
213
214
        // Add classes related to the "right_sidebar" configuration.
215
216 11
        if (config('adminlte.right_sidebar') && config('adminlte.right_sidebar_push')) {
217 1
            $classes[] = 'control-sidebar-push';
218
        }
219
220 11
        return $classes;
221
    }
222
223
    /**
224
     * Make the set of classes related to custom body classes configuration.
225
     *
226
     * @return array
227
     */
228 11
    private static function makeCustomBodyClasses()
229
    {
230 11
        $classes = [];
231 11
        $cfg = config('adminlte.classes_body', '');
232
233 11
        if (is_string($cfg) && $cfg) {
234 1
            $classes[] = $cfg;
235
        }
236
237 11
        return $classes;
238
    }
239
240
    /**
241
     * Make the set of classes related to the dark mode.
242
     *
243
     * @return array
244
     */
245 11
    private static function makeDarkModeClasses()
246
    {
247 11
        $classes = [];
248 11
        $cfg = config('adminlte.layout_dark_mode', false);
249
250 11
        if (is_bool($cfg) && $cfg) {
251 1
            $classes[] = 'dark-mode';
252
        }
253
254 11
        return $classes;
255
    }
256
}
257