| Conditions | 4 |
| Paths | 5 |
| Total Lines | 82 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| 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 | $(function () { |
||
| 2 | // Don't do anything if this isn't a Edit Counter page. |
||
| 3 | if ($("body.ec").length === 0) { |
||
| 4 | return; |
||
| 5 | } |
||
| 6 | |||
| 7 | // Set up charts. |
||
| 8 | $(".chart-wrapper").each(function () { |
||
| 9 | var chartType = $(this).data("chart-type"); |
||
| 10 | if ( chartType === undefined ) { |
||
| 11 | return false; |
||
| 12 | } |
||
| 13 | var data = $(this).data("chart-data"); |
||
| 14 | var labels = $(this).data("chart-labels"); |
||
| 15 | var $ctx = $("canvas", $(this)); |
||
| 16 | |||
| 17 | /** global: Chart */ |
||
| 18 | new Chart($ctx, { |
||
|
|
|||
| 19 | type: chartType, |
||
| 20 | data: { |
||
| 21 | labels: labels, |
||
| 22 | datasets: [ { data: data } ] |
||
| 23 | } |
||
| 24 | }); |
||
| 25 | |||
| 26 | return undefined; |
||
| 27 | }); |
||
| 28 | |||
| 29 | // Load top edits HTML via AJAX, to not slow down the initial page load. |
||
| 30 | // Only load if container is present, which is missing in subroutes, e.g. ec-namespacetotals, etc. |
||
| 31 | var $topEditsContainer = $("#topedits-container"); |
||
| 32 | if ($topEditsContainer[0]) { |
||
| 33 | /** global: xtBaseUrl */ |
||
| 34 | var url = xtBaseUrl + 'topedits/' |
||
| 35 | + $topEditsContainer.data('project') + '/' |
||
| 36 | + $topEditsContainer.data('username') + '/all?htmlonly=yes'; |
||
| 37 | $.ajax({ |
||
| 38 | url: url, |
||
| 39 | timeout: 30000 |
||
| 40 | }).done(function (data) { |
||
| 41 | $topEditsContainer.replaceWith(data); |
||
| 42 | }).fail(function (_xhr, _status, message) { |
||
| 43 | $topEditsContainer.replaceWith( |
||
| 44 | $.i18n('api-error', 'TopEdits API: <code>' + message + '</code>') |
||
| 45 | ); |
||
| 46 | }); |
||
| 47 | } |
||
| 48 | |||
| 49 | // Load recent global edits' HTML via AJAX, to not slow down the initial page load. |
||
| 50 | // Only load if container is present, which is missing in subroutes, e.g. ec-namespacetotals, etc. |
||
| 51 | var $latestGlobalContainer = $("#latestglobal-container"); |
||
| 52 | if ($latestGlobalContainer[0]) { |
||
| 53 | /** global: xtBaseUrl */ |
||
| 54 | var url = xtBaseUrl + 'ec-latestglobal/' |
||
| 55 | + $latestGlobalContainer.data('project') + '/' |
||
| 56 | + $latestGlobalContainer.data('username') + '?htmlonly=yes'; |
||
| 57 | $.ajax({ |
||
| 58 | url: url, |
||
| 59 | timeout: 30000 |
||
| 60 | }).done(function (data) { |
||
| 61 | $latestGlobalContainer.replaceWith(data); |
||
| 62 | }).fail(function (_xhr, _status, message) { |
||
| 63 | $latestGlobalContainer.replaceWith( |
||
| 64 | $.i18n('api-error', 'Global contributions API: <code>' + message + '</code>') |
||
| 65 | ); |
||
| 66 | }); |
||
| 67 | } |
||
| 68 | |||
| 69 | // Set up namespace toggle chart. |
||
| 70 | setupToggleTable(window.namespaceTotals, window.namespaceChart, null, function (newData) { |
||
| 71 | var total = 0; |
||
| 72 | Object.keys(newData).forEach(function (namespace) { |
||
| 73 | total += parseInt(newData[namespace], 10); |
||
| 74 | }); |
||
| 75 | var namespaceCount = Object.keys(newData).length; |
||
| 76 | $('.namespaces--namespaces').text( |
||
| 77 | namespaceCount.toLocaleString() + " " + |
||
| 78 | $.i18n('num-namespaces', namespaceCount) |
||
| 79 | ); |
||
| 80 | $('.namespaces--count').text(total.toLocaleString()); |
||
| 81 | }); |
||
| 82 | }); |
||
| 83 | |||
| 207 |