| Conditions | 15 |
| Total Lines | 90 |
| Code Lines | 53 |
| 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 | var buttonset = dialog.nextAll('.ui-dialog-buttonpane').find('.ui-dialog-buttonset'); |
||
| 95 | |||
| 96 | if ($('.datamanager2 .form_toolbar > *').length > 0) { |
||
| 97 | $('.datamanager2 .form_toolbar > *').each(function() { |
||
| 98 | var btn = $(this); |
||
| 99 | buttons.push({ |
||
| 100 | text: btn.val() || btn.text(), |
||
| 101 | click: function() { |
||
| 102 | if (btn.hasClass('cancel')) { |
||
| 103 | dialog.dialog('close'); |
||
| 104 | } else { |
||
| 105 | btn.closest('form').on('submit', function() { |
||
| 106 | buttonset |
||
| 107 | .find('.ui-button') |
||
| 108 | .addClass('ui-state-disabled'); |
||
| 109 | }); |
||
| 110 | btn.click(); |
||
| 111 | } |
||
| 112 | } |
||
| 113 | }); |
||
| 114 | }); |
||
| 115 | } |
||
| 116 | if (extra_buttons.length > 0) { |
||
| 117 | buttons = extra_buttons.concat(buttons); |
||
| 118 | } |
||
| 119 | |||
| 120 | // This doesn't work under certain circumstances when flexbox is used somewhere in the page: |
||
| 121 | // dialog.dialog('option', 'buttons', buttons); |
||
| 122 | // @todo: The root of the problem seems to be that jquery can't set the content element |
||
| 123 | // to a height of 0, so at some point this could be filed as a bug against their repo. Latest |
||
| 124 | // stable (3.4.1) is affected. For now, we just copy the relevant part from jqueryui's |
||
| 125 | // _createButtons method.. |
||
| 126 | |||
| 127 | buttonset.empty(); |
||
| 128 | |||
| 129 | $.each(buttons, function (name, props) { |
||
| 130 | var click, buttonOptions; |
||
| 131 | props = $.isFunction(props) ? {click: props, text: name} : props; |
||
| 132 | |||
| 133 | // Default to a non-submitting button |
||
| 134 | props = $.extend({type: "button"}, props); |
||
| 135 | |||
| 136 | // Change the context for the click callback to be the main element |
||
| 137 | click = props.click; |
||
| 138 | buttonOptions = { |
||
| 139 | icon: props.icon, |
||
| 140 | iconPosition: props.iconPosition, |
||
| 141 | label: props.text |
||
| 142 | }; |
||
| 143 | |||
| 144 | delete props.click; |
||
| 145 | delete props.icon; |
||
| 146 | delete props.iconPosition; |
||
| 147 | delete props.text; |
||
| 148 | |||
| 149 | $('<button></button>', props) |
||
| 150 | .button(buttonOptions) |
||
| 151 | .appendTo(buttonset) |
||
| 152 | .on('click', function() { |
||
| 153 | click.apply(dialog[0], arguments); |
||
| 154 | }); |
||
| 155 | }); |
||
| 156 | } |
||
| 157 | |||
| 192 |