Passed
Branch master (32b4c5)
by Askupa
01:33
created

src/js/components/dropdown.js   A

Complexity

Total Complexity 10
Complexity/F 1.67

Size

Lines of Code 38
Function Count 6

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 0
c 3
b 0
f 0
nc 1
dl 0
loc 38
rs 10
wmc 10
mnd 2
bc 8
fnc 6
bpm 1.3333
cpm 1.6666
noi 5
1
Mivhak.component('dropdown', {
2
    template: '<div class="mivhak-dropdown"></div>',
3
    props: {
4
        items: [],
5
        mivhakInstance: null,
6
        visible: false
7
    },
8
    created: function() {
9
        var $this = this;
10
        $.each(this.items, function(i, item) {
11
            if( typeof item === 'string') item = Mivhak.buttons[item];
12
            var button = $('<div>',{class: 'mivhak-dropdown-button', text: item.text, click: function(e){item.click.call($this.mivhakInstance,e);}});
13
            if(item.toggle) 
14
            {
15
                button.$toggle = Mivhak.render('toggle');
16
                
17
                // Toggle only if not clicking on the toggle itself (which makes it toggle as it is)
18
                button.click(function(e){if($(e.target).parents('.mivhak-dropdown-button').length !== 1)button.$toggle.toggle();});
19
                button.append(button.$toggle.$el);
20
            }
21
            $this.$el.append(button);
22
        });
23
        
24
        // Hide dropdown on outside click
25
        $(window).click(function(e){
26
            if(!$(e.target).closest('.mivhak-icon-cog').length) {
27
                $this.$el.removeClass('mivhak-dropdown-visible');
28
                $this.$el.parent().removeClass('mivhak-button-active');
29
            }
30
        });
31
    },
32
    methods: {
33
        toggle: function() {
34
            this.visible = !this.visible;
35
            this.$el.toggleClass('mivhak-dropdown-visible');
36
        }
37
    }
38
});