Completed
Push — master ( 309357...7ad46f )
by Andreas
18:00
created

$.fn.extend.render_privilege   A

Complexity

Conditions 2
Paths 5

Size

Total Lines 59

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
c 1
b 0
f 0
nc 5
nop 0
dl 0
loc 59
rs 9.597

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
(function($){
2
    /**
3
     * privilege renderer - jQuery plugin
4
     */
5
    $.fn.extend({
6
        /**
7
         * Create a multiface checkbox interface out of a simple form structure.
8
         *
9
         * @example jQuery('#select-holder').render_privilege();
10
         * @cat plugin
11
         * @type jQuery
12
         */
13
        render_privilege: function(settings) {
14
            settings = $.extend({
15
                maxIndex: 3
16
            }, settings || {});
17
18
            return this.each(function() {
19
                if ($(this).hasClass('privilege_rendered')) {
20
                    return;
21
                }
22
                $(this).addClass('privilege_rendered');
23
24
                var div = $("<div/>").insertAfter(this),
25
                    list_menu = $(this).find("select")[0],
26
                    selected_index = 0;
27
28
                $(list_menu).on('change', function() {
29
                    div.find('div.privilege_val').trigger('click');
30
                });
31
32
                $(this).find("select option").each(function() {
33
                    var classes = [null, 'allow', 'deny', 'inherited'],
34
                        block_style = this.selected ? "style='display: block;'" : "",
35
                        css_class = '', icon;
36
                    if (this.value === null || this.value == 3) {
37
                        css_class = 'inherited';
38
                    } else if (classes[this.value] !== settings.effective_value) {
39
                        css_class = 'ineffectual';
40
                    }
41
42
                    if (this.value == 1) {
43
                        icon = 'check-square';
44
                    } else if (this.value == 2) {
45
                        icon = 'minus-square';
46
                    } else {
47
                        if (settings.effective_value == 'allow') {
48
                            icon = 'check-square-o';
49
                        } else {
50
                            icon = 'minus-square-o';
51
                        }
52
                    }
53
54
                    div.append( "<div class='privilege_val' " + block_style + "><a class='" + css_class + "' title='" + this.innerHTML + "'><i class='fa fa-" + icon + "'></i></a></div>" );
55
                });
56
57
                var selects = div.find('div.privilege_val').click(function() {
58
                    selected_index = selects.index(this) + 1;
59
60
                    if (selected_index == settings.maxIndex) {
61
                        selected_index = 0;
62
                    }
63
64
                    showNext(this);
65
66
                    list_menu.selectedIndex = selected_index;
67
68
                    return false;
69
                });
70
71
                function showNext(elem) {
72
                    $(elem).hide();
73
                    $(selects[selected_index]).show();
74
                }
75
76
            }).hide();
77
        },
78
79
        privilege_actions: function(privilege_key) {
80
            return this.each(function() {
81
                if ($(this).find('.privilege_action').length > 0) {
82
                    return;
83
                }
84
85
                var row = this,
86
                    actions_holder = $('#privilege_row_actions_' + privilege_key, row),
87
                    clear_action = $('<div class="privilege_action" />').insertAfter( actions_holder );
88
89
                $('<i class="fa fa-trash"></i>').attr({
90
                        title: "Clear privileges"
91
                    }).appendTo(clear_action);
92
93
                clear_action.on('click', function() {
94
                    $('select', row).each(function(i, n) {
95
                        $(n).val(3).trigger('onchange', [0]);
96
                        $(row).hide();
97
                    });
98
                });
99
            });
100
        }
101
    });
102
103
})(jQuery);
104