Conditions | 14 |
Total Lines | 83 |
Code Lines | 48 |
Lines | 0 |
Ratio | 0 % |
Changes | 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:
Complex classes like dialog.js ➔ attach_to_parent_dialog 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 | const dialog = window.frameElement ? window.parent.$(window.frameElement.parentNode) : null; |
||
67 | function attach_to_parent_dialog(dialog) { |
||
68 | let buttons = []; |
||
69 | if (document.title) { |
||
70 | dialog.dialog('option', 'title', document.title); |
||
71 | } |
||
72 | dialog.css('visibility', 'visible'); |
||
73 | |||
74 | $(window).on('unload', function() { |
||
75 | dialog.nextAll('.ui-dialog-buttonpane').find('button') |
||
76 | .prop('disabled', true) |
||
77 | .addClass('ui-state-disabled'); |
||
78 | }); |
||
79 | |||
80 | if ($('.midcom-view-toolbar li').length > 0) { |
||
81 | $('.midcom-view-toolbar li').each(function() { |
||
82 | var btn = $(this).find('a'), |
||
83 | options = { |
||
84 | click: function() { |
||
85 | btn.get(0).click(); |
||
86 | btn.addClass('active'); |
||
87 | } |
||
88 | }; |
||
89 | |||
90 | add_dialog_button(btn.attr('href'), btn.text(), options); |
||
91 | }); |
||
92 | } |
||
93 | |||
94 | if ($('.datamanager2 .form_toolbar > *').length > 0) { |
||
95 | $('.datamanager2 .form_toolbar > *').each(function() { |
||
96 | var btn = $(this); |
||
97 | buttons.push({ |
||
98 | text: btn.val() || btn.text(), |
||
99 | click: function() { |
||
100 | if (btn.hasClass('cancel')) { |
||
101 | dialog.dialog('close'); |
||
102 | } else { |
||
103 | btn.click(); |
||
104 | } |
||
105 | } |
||
106 | }); |
||
107 | }); |
||
108 | } |
||
109 | if (extra_buttons.length > 0) { |
||
110 | buttons = extra_buttons.concat(buttons); |
||
111 | } |
||
112 | |||
113 | // This doesn't work under certain circumstances when flexbox is used somewhere in the page: |
||
114 | // dialog.dialog('option', 'buttons', buttons); |
||
115 | // @todo: The root of the problem seems to be that jquery can't set the content element |
||
116 | // to a height of 0, so at some point this could be filed as a bug against their repo. Latest |
||
117 | // stable (3.4.1) is affected. For now, we just copy the relevant part from jqueryui's |
||
118 | // _createButtons method.. |
||
119 | |||
120 | var buttonset = dialog.nextAll('.ui-dialog-buttonpane').find('.ui-dialog-buttonset').empty(); |
||
121 | |||
122 | $.each(buttons, function (name, props) { |
||
123 | var click, buttonOptions; |
||
124 | props = $.isFunction(props) ? {click: props, text: name} : props; |
||
125 | |||
126 | // Default to a non-submitting button |
||
127 | props = $.extend({type: "button"}, props); |
||
128 | |||
129 | // Change the context for the click callback to be the main element |
||
130 | click = props.click; |
||
131 | buttonOptions = { |
||
132 | icon: props.icon, |
||
133 | iconPosition: props.iconPosition, |
||
134 | label: props.text |
||
135 | }; |
||
136 | |||
137 | delete props.click; |
||
138 | delete props.icon; |
||
139 | delete props.iconPosition; |
||
140 | delete props.text; |
||
141 | |||
142 | $('<button></button>', props) |
||
143 | .button(buttonOptions) |
||
144 | .appendTo(buttonset) |
||
145 | .on('click', function() { |
||
146 | click.apply(dialog[0], arguments); |
||
147 | }); |
||
148 | }); |
||
149 | } |
||
150 | |||
185 |