| Conditions | 14 |
| Total Lines | 59 |
| Code Lines | 37 |
| 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 subform.js ➔ init_subform 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 init_subform(id, sortable, allow_add, allow_delete) { |
||
| 2 | var container = $('#' + id), |
||
| 3 | delete_button = $('<a class="button remove-item">-</a>'), |
||
| 4 | add_button = $('<a class="button add-item">+</a>') |
||
| 5 | .on('click', function(e) { |
||
| 6 | e.preventDefault(); |
||
| 7 | add_form(container, add_button, delete_button, sortable, allow_delete); |
||
| 8 | }), |
||
| 9 | index = 0; |
||
| 10 | |||
| 11 | container.on('click', 'a.remove-item', function(e) { |
||
| 12 | e.preventDefault(); |
||
| 13 | $(this).parent().remove(); |
||
| 14 | if ( container.data('max-count') > 0 |
||
| 15 | && container.data('max-count') >= container.find('fieldset').length |
||
| 16 | && container.find('.add-item').length === 0) { |
||
| 17 | if(allow_add === true) { |
||
| 18 | container.append(add_button); |
||
| 19 | } |
||
| 20 | } |
||
| 21 | }); |
||
| 22 | |||
| 23 | container.children().each(function() { |
||
| 24 | if(allow_delete === true) { |
||
| 25 | $(this).prepend(delete_button.clone()); |
||
| 26 | } |
||
| 27 | index++; |
||
| 28 | }); |
||
| 29 | |||
| 30 | container.data('index', index); |
||
| 31 | if ( (container.data('max-count') === 0 |
||
| 32 | || container.data('max-count') > index) |
||
| 33 | && allow_add === true) { |
||
| 34 | container.append(add_button); |
||
| 35 | } |
||
| 36 | |||
| 37 | if (sortable === true) { |
||
| 38 | container |
||
| 39 | .sortable({items: '> :not(a.add-item)'}) |
||
| 40 | .on('sortupdate', function() { |
||
| 41 | $($(this).find('> .ui-sortable-handle').get().reverse()).each(function(index, element) { |
||
| 42 | let id = element.id |
||
| 43 | if (!id) { |
||
| 44 | id = $('> .input > *:first-child', element).attr('id'); |
||
| 45 | } |
||
| 46 | $('#' + id + '_score').val(index); |
||
| 47 | }); |
||
| 48 | }); |
||
| 49 | } |
||
| 50 | |||
| 51 | add_button.on('click', function() { |
||
| 52 | // If there is exactly one file selector, we're probably in some sort of attachment list, |
||
| 53 | // so let's assume the user wants to add a file |
||
| 54 | // (at some point this should probably be made configurable) |
||
| 55 | if ($(this).prev().find('input[type="file"]').length === 1) { |
||
| 56 | $(this).prev().find('input[type="file"]').click(); |
||
| 57 | } |
||
| 58 | }); |
||
| 59 | } |
||
| 60 | |||
| 81 |