| Conditions | 1 |
| Paths | 4 |
| Total Lines | 72 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
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 | /** |
||
| 11 | $.fn.sidebar = function(options) { |
||
| 12 | var $sidebar = this; |
||
| 13 | var $tabs = $sidebar.find('ul.sidebar-tabs, .sidebar-tabs > ul'); |
||
| 14 | var $container = $sidebar.children('.sidebar-content').first(); |
||
| 15 | |||
| 16 | options = $.extend({ |
||
| 17 | position: 'left' |
||
| 18 | }, options || {}); |
||
| 19 | |||
| 20 | $sidebar.addClass('sidebar-' + options.position); |
||
| 21 | |||
| 22 | $tabs.children('li').children('a').on('click', function(e) { |
||
| 23 | e.preventDefault(); |
||
| 24 | var $tab = $(this).closest('li'); |
||
| 25 | |||
| 26 | if ($tab.hasClass('active')) |
||
| 27 | $sidebar.close(); |
||
| 28 | else if (!$tab.hasClass('disabled') && this.hash.slice(1) != '') |
||
| 29 | $sidebar.open(this.hash.slice(1), $tab); |
||
| 30 | }); |
||
| 31 | |||
| 32 | $sidebar.find('.sidebar-close').on('click', function() { |
||
| 33 | $sidebar.close(); |
||
| 34 | }); |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Open sidebar (if necessary) and show the specified tab. |
||
| 38 | * |
||
| 39 | * @param {string} id - The id of the tab to show (without the # character) |
||
| 40 | * @param {jQuery} [$tab] - The jQuery object representing the tab node (used internally for efficiency) |
||
| 41 | */ |
||
| 42 | $sidebar.open = function(id, $tab) { |
||
| 43 | if (typeof $tab === 'undefined') |
||
| 44 | $tab = $tabs.find('li > a[href="#' + id + '"]').parent(); |
||
| 45 | |||
| 46 | // hide old active contents |
||
| 47 | $container.children('.sidebar-pane.active').removeClass('active'); |
||
| 48 | |||
| 49 | // show new content |
||
| 50 | $container.children('#' + id).addClass('active'); |
||
| 51 | |||
| 52 | // remove old active highlights |
||
| 53 | $tabs.children('li.active').removeClass('active'); |
||
| 54 | |||
| 55 | // set new highlight |
||
| 56 | $tab.addClass('active'); |
||
| 57 | |||
| 58 | $sidebar.trigger('content', { 'id': id }); |
||
| 59 | |||
| 60 | if ($sidebar.hasClass('collapsed')) { |
||
| 61 | // open sidebar |
||
| 62 | $sidebar.trigger('opening'); |
||
| 63 | $sidebar.removeClass('collapsed'); |
||
| 64 | } |
||
| 65 | }; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Close the sidebar (if necessary). |
||
| 69 | */ |
||
| 70 | $sidebar.close = function() { |
||
| 71 | // remove old active highlights |
||
| 72 | $tabs.children('li.active').removeClass('active'); |
||
| 73 | |||
| 74 | if (!$sidebar.hasClass('collapsed')) { |
||
| 75 | // close sidebar |
||
| 76 | $sidebar.trigger('closing'); |
||
| 77 | $sidebar.addClass('collapsed'); |
||
| 78 | } |
||
| 79 | }; |
||
| 80 | |||
| 81 | return $sidebar; |
||
| 82 | }; |
||
| 83 |