Passed
Push — master ( 39edd6...47e6f5 )
by Pedro
01:57
created

public/js/hoverDot.js   B

Complexity

Conditions 1
Paths 2

Size

Total Lines 157
Code Lines 80

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 80
dl 0
loc 157
rs 7.6545
c 0
b 0
f 0
cc 1
nc 2
nop 1

2 Functions

Rating   Name   Duplication   Size   Complexity  
A hoverDot.js ➔ dot 0 14 1
B $.fn.dotHover 0 136 1

How to fix   Long Method   

Long Method

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:

1
/**
2
 * Created by (c) Sebastian Kaim, 2016
3
 *
4
 * This file is licensed under the MIT License.
5
 */
6
"use strict";
7
8
(function ($) {
9
10
    // dot helper class
11
    var dot = function (_x, _y, _text, _color) {
12
        this.x = _x;
13
        this.y = _y;
14
        this.text = _text;
15
        this.color = _color;
16
        this.radius = 24; // hover event radius
17
18
        this.render = function (context) {
19
            context.beginPath();
20
            context.arc(this.x, this.y, 4, 0, Math.PI * 2, true);
21
            context.fillStyle = this.color;
22
            context.fill();
23
        };
24
    };
25
26
    // register the plugin
27
    $.fn.dotHover = function (dots, options) {
28
29
        // settings
30
        var settings = $.extend({
31
            // defaults
32
33
            // this should be a valid image url which will be used as background
34
            img: "karte.png",
35
36
            // whether to allow the user to place dots on click
37
            setmode: false,
38
39
            // a callback for when an event is created
40
            setcallback: function (dot) {},
0 ignored issues
show
Unused Code introduced by
The parameter dot is not used and could be removed.

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.

Loading history...
41
42
            // default text for an unitialized dot
43
            defaulttext: "Example Text"
44
45
        }, options );
46
47
        // initialize dots
48
        settings.dots = [];
49
        $.each(dots, function (i, el) {
50
            settings.dots.push(new dot(el.x, el.y, el.text, el.color));
0 ignored issues
show
Coding Style Best Practice introduced by
By convention, constructors like dot should be capitalized.
Loading history...
51
        });
52
53
        //
54
        // create tooltip canvas
55
        //
56
        var tooltip = $('<div/>')
57
            .appendTo('body')
58
            .addClass('hoverDotTooltip')
59
            .css({
60
                backgroundColor: 'rgba(0,0,0,.8)',
61
                border: '1px solid #fff',
62
				padding: '5px',
0 ignored issues
show
Unused Code Bug introduced by
The key padding is used more than once in this object expression.
Loading history...
63
                position: 'absolute',
64
                padding: '5px',
65
                marginLeft: '-50px',
66
				color:'#fff'
67
            })
68
            .hide();
69
70
        var contexts = [];
71
72
        var showHideTooltip = function (show) {
73
            if(show)
74
            {
75
                tooltip.show();
76
                tooltip.get(0).style.marginLeft = ((-tooltip.width() / 2) + 3) + "px";
77
            }
78
            else
79
            {
80
                tooltip.hide();
81
            }
82
        };
83
84
        //
85
        // handle tooltip positioning on mouse move
86
        //
87
        var mouseMoveEvent = function(event, img, offsetX, offsetY) {
88
            var mouseX = parseInt(event.clientX - offsetX);
89
            var mouseY = parseInt(event.clientY - offsetY + $(window).scrollTop());
90
91
            var hit = false;
92
            for (var i = 0; i < settings.dots.length; i++) {
93
                var dot = settings.dots[i];
94
                var dx = mouseX - dot.x;
95
                var dy = mouseY - dot.y;
96
97
                if (dx * dx + dy * dy < dot.radius) {
98
                    tooltip.get(0).style.top = ((dot.y - 40) + offsetY) + "px";
99
                    tooltip.get(0).style.left = (dot.x + offsetX) + "px";
100
101
                    tooltip.html(dot.text);
102
                    hit = true;
103
                }
104
            }
105
106
            showHideTooltip(hit);
107
        };
108
109
        // re-renders all dots
110
        var render = function () {
111
            $.each(contexts, function (i, data) {
112
                $.each(settings.dots, function (j, dot) {
113
                    dot.render(data.ctx);
114
                });
115
            });
116
        };
117
118
        //
119
        // places a new dot
120
        //
121
        var placedot = function (event, element) {
122
            var ndot = new dot(event.clientX - element.offsetLeft, event.clientY - element.offsetTop + $(window).scrollTop(), settings.defaulttext); // -7,-7 for cursor offset
0 ignored issues
show
Coding Style Best Practice introduced by
By convention, constructors like dot should be capitalized.
Loading history...
123
124
            settings.dots.push(ndot);
125
            render();
126
            settings.setcallback(ndot);
127
        };
128
129
        //
130
        // removes a dot
131
        //
132
        this.removeDot = function(x, y)
133
        {
134
            settings.dots = settings.dots.filter(function(el, index) {
0 ignored issues
show
Unused Code introduced by
The parameter index is not used and could be removed.

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.

Loading history...
Unused Code introduced by
The parameter el is not used and could be removed.

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.

Loading history...
135
                return dot.x != x && dot.y != y;
136
            });
137
        };
138
139
        // init mouse move on each element
140
        this.each(function(i, el) {
141
            el.style= 'background: url(' + settings.img + ');' +
142
            'background-repeat: no-repeat; ' +
143
            'background-size: contain;' +
144
            'width: ' + settings.width + '; ';
145
            //'height: ' + settings.height + '; '
146
147
            contexts.push( { el: el, ctx: el.getContext('2d') });
148
149
            $(el).mousemove (function (event) {
150
                mouseMoveEvent(event, el, el.offsetLeft, el.offsetTop);
151
            });
152
153
            if(settings.setmode) $(el).click(function (e) {
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
154
                placedot(e, el);
155
            });
156
        });
157
158
        // render initial dots
159
        render();
160
161
        return this;
162
    };
163
164
})(jQuery);
165