Passed
Pull Request — master (#901)
by Padam
03:18
created

LayoutHelper::isLayoutBoxedEnabled()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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