| Conditions | 1 |
| Paths | 1 |
| Total Lines | 115 |
| 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:
| 1 | /** |
||
| 5 | (function($, Symphony) { |
||
| 6 | 'use strict'; |
||
| 7 | |||
| 8 | // holds up all instances |
||
| 9 | var instances = $(); |
||
| 10 | // last scroll position |
||
| 11 | var scrollTop = 0; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * Create affix elements. |
||
| 15 | * Affix elements follow the scroll and are constrain by their container. |
||
| 16 | * |
||
| 17 | * @since Symphony 2.5 |
||
| 18 | * |
||
| 19 | * @name $.symphonyAffix |
||
| 20 | * @class |
||
| 21 | * |
||
| 22 | * @param {Object} options An object specifying containing the attributes specified below |
||
| 23 | * @param {String} [options.container=$(this).parent()] Selector to find affix container |
||
| 24 | * |
||
| 25 | * @example |
||
| 26 | |||
| 27 | var affix = $('#affix').symphonyAffix({ |
||
| 28 | container: '.parent', |
||
| 29 | }); |
||
| 30 | */ |
||
| 31 | $.fn.symphonyAffix = function(options) { |
||
| 32 | var objects = $(this), |
||
| 33 | settings = { |
||
| 34 | // public |
||
| 35 | container: null, |
||
| 36 | // private |
||
| 37 | top: 0, |
||
| 38 | freespace: 0, |
||
| 39 | bottom: 0, |
||
| 40 | height: 0, |
||
| 41 | maxtop: 0 |
||
| 42 | }; |
||
| 43 | |||
| 44 | $.extend(settings, options); |
||
| 45 | |||
| 46 | |||
| 47 | /*--------------------------------------------------------------------- |
||
| 48 | Initialisation |
||
| 49 | ---------------------------------------------------------------------*/ |
||
| 50 | |||
| 51 | objects.each(function createOneAffix() { |
||
| 52 | var itemSettings = $.extend({}, settings); |
||
| 53 | var item = $(this); |
||
| 54 | var updateItemSettings = function () { |
||
| 55 | itemSettings.top = itemSettings.container.offset().top; |
||
| 56 | itemSettings.freespace = itemSettings.container.height(); |
||
| 57 | itemSettings.bottom = itemSettings.top + itemSettings.freespace; |
||
| 58 | itemSettings.height = item.height(); |
||
| 59 | itemSettings.maxtop = (itemSettings.freespace - itemSettings.height) + 'px'; |
||
| 60 | }; |
||
| 61 | |||
| 62 | // use parent as default container |
||
| 63 | if (!itemSettings.container) { |
||
| 64 | itemSettings.container = item.parent(); |
||
| 65 | } |
||
| 66 | |||
| 67 | // resolve jQuery object |
||
| 68 | else { |
||
| 69 | itemSettings.container = $(itemSettings.container); |
||
| 70 | } |
||
| 71 | |||
| 72 | // cache cssom values |
||
| 73 | updateItemSettings(); |
||
| 74 | item.on('updatesettings.affix', updateItemSettings); |
||
| 75 | |||
| 76 | item.addClass('js-affix'); |
||
| 77 | |||
| 78 | // save instance settings |
||
| 79 | item.data('affix-settings', itemSettings); |
||
| 80 | |||
| 81 | // register instance |
||
| 82 | instances = instances.add(item); |
||
| 83 | }); |
||
| 84 | |||
| 85 | // Init |
||
| 86 | $(window).triggerHandler('scroll'); |
||
| 87 | |||
| 88 | return objects; |
||
| 89 | }; |
||
| 90 | |||
| 91 | /*-----------------------------------------------------------------------*/ |
||
| 92 | |||
| 93 | // One listener for all instances |
||
| 94 | $(window).scroll(function affixScroll(e) { |
||
|
|
|||
| 95 | scrollTop = $(this).scrollTop(); |
||
| 96 | Symphony.Utilities.requestAnimationFrame(function affixScrollRaf() { |
||
| 97 | instances.each(function affixScrollOne() { |
||
| 98 | var item = $(this); |
||
| 99 | var settings = item.data('affix-settings'); |
||
| 100 | var cssClass = 'js-affix-scroll'; |
||
| 101 | var top = ''; |
||
| 102 | if (scrollTop < settings.top) { |
||
| 103 | cssClass = 'js-affix-top'; |
||
| 104 | } else if (scrollTop > settings.bottom) { |
||
| 105 | cssClass = 'js-affix-bottom'; |
||
| 106 | top = settings.maxtop; |
||
| 107 | } |
||
| 108 | // Do changes only if state changes |
||
| 109 | if (!item.hasClass(cssClass)) { |
||
| 110 | item |
||
| 111 | .removeClass('js-affix-scroll js-affix-top js-affix-bottom') |
||
| 112 | .addClass(cssClass) |
||
| 113 | .css({top: top}); |
||
| 114 | } |
||
| 115 | }); |
||
| 116 | }); |
||
| 117 | }); |
||
| 118 | |||
| 119 | })(window.jQuery, window.Symphony); |
||
| 120 |
This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.