| Conditions | 1 |
| Paths | 1 |
| Total Lines | 87 |
| 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 | /** |
||
| 4 | (function( $ ) { |
||
| 5 | 'use strict'; |
||
| 6 | |||
| 7 | /** |
||
| 8 | * Clones the hidden field to add another repeater. |
||
| 9 | */ |
||
| 10 | $('#add-repeater').on( 'click', function( e ) { |
||
| 11 | |||
| 12 | e.preventDefault(); |
||
| 13 | |||
| 14 | var clone = $('.repeater.hidden').clone(true); |
||
| 15 | |||
| 16 | clone.removeClass('hidden'); |
||
| 17 | clone.insertBefore('.repeater.hidden'); |
||
| 18 | |||
| 19 | return false; |
||
| 20 | |||
| 21 | }); |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Removes the selected repeater. |
||
| 25 | */ |
||
| 26 | $('.link-remove').on('click', function() { |
||
| 27 | |||
| 28 | var parents = $(this).parents('.repeater'); |
||
| 29 | |||
| 30 | if ( ! parents.hasClass( 'first' ) ) { |
||
| 31 | |||
| 32 | parents.remove(); |
||
| 33 | |||
| 34 | } |
||
| 35 | |||
| 36 | return false; |
||
| 37 | |||
| 38 | }); |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Shows/hides the selected repeater. |
||
| 42 | */ |
||
| 43 | $( '.btn-edit' ).on( 'click', function() { |
||
| 44 | |||
| 45 | var repeater = $(this).parents( '.repeater' ); |
||
| 46 | |||
| 47 | repeater.children( '.repeater-content' ).slideToggle( '150' ); |
||
| 48 | $(this).children( '.toggle-arrow' ).toggleClass( 'closed' ); |
||
| 49 | $(this).parents( '.handle' ).toggleClass( 'closed' ); |
||
| 50 | |||
| 51 | }); |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Changes the title of the repeater header as you type |
||
| 55 | */ |
||
| 56 | $(function(){ |
||
| 57 | |||
| 58 | $( '.repeater-title' ).on( 'keyup', function(){ |
||
| 59 | |||
| 60 | var repeater = $(this).parents( '.repeater' ); |
||
| 61 | var fieldval = $(this).val(); |
||
| 62 | |||
| 63 | if ( fieldval.length > 0 ) { |
||
| 64 | |||
| 65 | repeater.find( '.title-repeater' ).text( fieldval ); |
||
| 66 | |||
| 67 | } else { |
||
| 68 | |||
| 69 | repeater.find( '.title-repeater' ).text( nhdata.repeatertitle ); |
||
|
|
|||
| 70 | |||
| 71 | } |
||
| 72 | |||
| 73 | }); |
||
| 74 | |||
| 75 | }); |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Makes the repeaters sortable. |
||
| 79 | */ |
||
| 80 | $(function() { |
||
| 81 | |||
| 82 | $( '.repeaters' ).sortable({ |
||
| 83 | cursor: 'move', |
||
| 84 | handle: '.handle', |
||
| 85 | items: '.repeater', |
||
| 86 | opacity: 0.6 |
||
| 87 | }); |
||
| 88 | }); |
||
| 89 | |||
| 90 | })( jQuery ); |
||
| 91 |
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.