Conditions | 2 |
Paths | 2 |
Total Lines | 79 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
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 | /** |
||
23 | function addTooltip(selectorOr$element, url, dataOrDataFunction, complete) { |
||
24 | 'use strict'; |
||
25 | |||
26 | const serverEndpoint = url || window.DOKU_BASE + 'lib/exe/ajax.php'; |
||
27 | const DELAY = 300; |
||
28 | const HOVER_DETECTION_DELAY = 100; |
||
29 | const TOOLTIP_PARENT_CLASS = 'hasTooltip'; |
||
30 | |||
31 | function hoverStart() { |
||
32 | const $element = jQuery(this); |
||
33 | $element.addClass('hover'); |
||
34 | if ($element.hasClass(TOOLTIP_PARENT_CLASS)) { |
||
35 | if ($element.data('MMtooltipID')) { |
||
36 | const $tooltipDiv = jQuery('#' + $element.data('MMtooltipID')); |
||
37 | $tooltipDiv.show().position({ |
||
38 | my: 'left top', |
||
39 | at: 'left bottom', |
||
40 | of: $element, |
||
41 | }).attr('aria-hidden', 'false'); |
||
42 | } |
||
43 | return; |
||
44 | } |
||
45 | const payload = typeof dataOrDataFunction === 'function' ? dataOrDataFunction($element) : dataOrDataFunction; |
||
46 | const timeOutReference = setTimeout(function getToolTip() { |
||
47 | const $div = jQuery('<div class="serverToolTip">') |
||
48 | .uniqueId() |
||
49 | .mouseleave(function hideTooltip() { |
||
50 | $div.removeClass('hover'); |
||
51 | $div.hide().attr('aria-hidden', 'true'); |
||
52 | }) |
||
53 | .mouseenter(function allowJSHoverDetection() { |
||
54 | $div.addClass('hover'); |
||
55 | }); |
||
56 | $element.addClass(TOOLTIP_PARENT_CLASS); |
||
57 | jQuery.get(serverEndpoint, payload).done(function injectTooltip(response) { |
||
58 | window.magicMatcherUtil.showAjaxMessages(response); |
||
59 | $div.html(response.data); |
||
60 | $div.appendTo(jQuery('body')); |
||
61 | $div.show().position({ |
||
62 | my: 'left top', |
||
63 | at: 'left bottom', |
||
64 | of: $element, |
||
65 | }); |
||
66 | if (!$element.hasClass('hover')) { |
||
67 | $div.hide(); |
||
68 | } |
||
69 | $element.data('MMtooltipID', $div.attr('id')); |
||
70 | if (typeof complete === 'function') { |
||
71 | complete($element, $div); |
||
72 | } |
||
73 | }).fail(function handleFailedState(jqXHR) { |
||
74 | window.magicMatcherUtil.showAjaxMessages(jqXHR.responseJSON); |
||
75 | $div.remove(); |
||
76 | }); |
||
77 | }, DELAY); |
||
78 | $element.data('timeOutReference', timeOutReference); |
||
79 | } |
||
80 | |||
81 | function hoverEnd() { |
||
82 | const $this = jQuery(this); |
||
83 | $this.removeClass('hover'); |
||
84 | clearTimeout($this.data('timeOutReference')); |
||
85 | if ($this.data('MMtooltipID')) { |
||
86 | setTimeout(function conditionalHideTooltip() { |
||
87 | const $tooltip = jQuery('#' + $this.data('MMtooltipID')); |
||
88 | if (!$tooltip.hasClass('hover')) { |
||
89 | $tooltip.hide().attr('aria-hidden', 'true'); |
||
90 | } |
||
91 | }, HOVER_DETECTION_DELAY); |
||
92 | } |
||
93 | } |
||
94 | |||
95 | if (typeof selectorOr$element === 'string') { |
||
96 | jQuery(document).on('mouseenter', selectorOr$element, hoverStart); |
||
97 | jQuery(document).on('mouseleave', selectorOr$element, hoverEnd); |
||
98 | } else { |
||
99 | selectorOr$element.hover(hoverStart, hoverEnd); |
||
100 | } |
||
101 | } |
||
102 | |||
127 |