resources/js/admin/utils/admin-menu-fix.js   A
last analyzed

Complexity

Total Complexity 5
Complexity/F 1.67

Size

Lines of Code 29
Function Count 3

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 29
rs 10
c 0
b 0
f 0
wmc 5
mnd 2
bc 2
fnc 3
bpm 0.6666
cpm 1.6666
noi 1
1
/**
2
 * As we are using hash based navigation, hack fix
3
 * to highlight the current selected menu
4
 *
5
 * Requires jQuery
6
 */
7
function menuFix(slug) {
8
    var $ = jQuery;
9
10
    let menuRoot = $('#toplevel_page_' + slug);
11
    let currentUrl = window.location.href;
12
    let currentPath = currentUrl.substr( currentUrl.indexOf('admin.php') );
13
14
    menuRoot.on('click', 'a', function() {
15
        var self = $(this);
16
17
        $('ul.wp-submenu li', menuRoot).removeClass('current');
18
19
        if ( self.hasClass('wp-has-submenu') ) {
20
            $('li.wp-first-item', menuRoot).addClass('current');
21
        } else {
22
            self.parents('li').addClass('current');
23
        }
24
    });
25
26
    $('ul.wp-submenu a', menuRoot).each(function(index, el) {
27
        if ( $(el).attr( 'href' ) === currentPath ) {
28
            $('ul.wp-submenu a', menuRoot).parent().removeClass('current');
29
            $(el).parent().addClass('current');
30
            return;
0 ignored issues
show
Unused Code introduced by
This return has no effect and can be removed.
Loading history...
31
        }
32
    });
33
}
34
35
export default menuFix;