Conditions | 1 |
Paths | 8 |
Total Lines | 65 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
1 | (function($){ |
||
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 | |||
104 |