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