Passed
Push — master ( 7c331b...3c78de )
by Jonathan
18:06
created

Tree.initJsTree   C

Complexity

Conditions 11

Size

Total Lines 50
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 30
c 0
b 0
f 0
dl 0
loc 50
rs 5.4
cc 11

How to fix   Complexity   

Complexity

Complex classes like Tree.initJsTree often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
export class Tree {
2
    constructor() {
3
        this.initJsTree();
4
    }
5
6
    initJsTree() {
7
        let moduleTree = $('#treeview')
8
        moduleTree.jstree({
9
            "core" : {
10
                "themes" : {
11
                    "icons": false
12
                },
13
                "data" : {
14
                    "url" : function (node) {
15
                        return node.id === '#' ?
16
                        $("meta[name='module-tree-default-url']").attr('content') :
17
                        $("meta[name='module-tree-children-url']").attr('content');
18
                    },
19
                    "data" : function (node) {
20
                        return { 'id' : node.id };
21
                    }
22
                }
23
            },
24
            "plugins" : ['search', 'sort'],
25
            "search" : {
26
                "show_only_matches" : true
27
            }
28
        })
29
30
        // Open tree automatically
31
        .on('ready.jstree', () => {
32
            if ($("meta[name='module-tree-open-all']").attr('content')) {
33
                moduleTree.jstree('open_all')
34
            }
35
        })
36
37
        // Switch on detail view on click
38
        .on('changed.jstree', (e, data) => {
39
            if (data.node.a_attr.href !== '#') {
40
                document.location.href = data.node.a_attr.href
41
            }
42
        })
43
44
        let to = false
45
        $('.treeview-search-bar #record-name').keyup(() => {
46
            if(to) {
47
                clearTimeout(to)
48
            }
49
50
            to = setTimeout(() => {
51
                let v = $('#record-name').val()
52
                moduleTree.jstree(true).search(v)
53
            }, 250)
54
        })
55
    }
56
}