Passed
Push — develop ( 74cbde...8ba7ca )
by Nikolay
04:56
created

sites/admin-cabinet/assets/js/src/main/sidebar-menu-show-active.js   A

Complexity

Total Complexity 5
Complexity/F 1.25

Size

Lines of Code 47
Function Count 4

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 21
mnd 1
bc 1
fnc 4
dl 0
loc 47
rs 10
bpm 0.25
cpm 1.25
noi 0
c 0
b 0
f 0
1
/*
2
 * MikoPBX - free phone system for small business
3
 * Copyright © 2017-2023 Alexey Portnov and Nikolay Beketov
4
 *
5
 * This program is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License along with this program.
16
 * If not, see <https://www.gnu.org/licenses/>.
17
 */
18
19
/* global globalRootUrl, globalTranslate, globalWebAdminLanguage, sessionStorage */
20
21
/**
22
 * sidebarMenuShowActive object for managing the highlighting of the active sidebar menu item.
23
 * @module sidebarMenuShowActive
24
 */
25
const sidebarMenuShowActive = {
26
27
    /**
28
     * jQuery object for the sidebar menu.
29
     * @type {jQuery}
30
     */
31
    $sidebarMenu: $('#sidebar-menu'),
32
33
    /**
34
     * Initializes the sidebar menu and sets the active menu item.
35
     */
36
    initialize() {
37
        sidebarMenuShowActive.$sidebarMenu
38
            .sidebar({})
39
            .sidebar('setting', 'transition', 'push')
40
            .sidebar('setting', 'dimPage', false);
41
        sidebarMenuShowActive.makeMenuActiveElement();
42
    },
43
44
    /**
45
     * Sets the active menu item in the sidebar based on the current URL.
46
     */
47
    makeMenuActiveElement() {
48
        const current = window.location.href;
49
        $.each($('#sidebar-menu a'), (index, value) => {
50
            const $this = $(value);
51
            $this.removeClass('active');
52
            // if the current path is like this link, make it active
53
            const needle = $this.attr('href')
54
                .replace('/index', '')
55
                .replace('/modify', '');
56
57
            if (current.indexOf(needle) !== -1
58
                && !$this.hasClass('logo')) {
59
                $this.addClass('active');
60
            }
61
        });
62
    },
63
};
64
65
66
/**
67
 * Initialize highlighting the active sidebar menu item.
68
 */
69
$(document).ready(() => {
70
    sidebarMenuShowActive.initialize();
71
});
72