Passed
Push — master ( ac8ada...90f624 )
by Tomasz
08:26
created

app.Drag.handleDragEvent   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 1
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 2
1
/* 
2
 * this code is copied from https://openlayers.org/en/latest/examples/custom-interactions.html
3
 * with minimal changes
4
 */
5
6
7
window.app = {};
8
var app = window.app;
9
app.Drag = function() {
10
  ol.interaction.Pointer.call(this, {
0 ignored issues
show
Bug introduced by
The variable ol seems to be never declared. If this is a global, consider adding a /** global: ol */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
11
    handleDownEvent: app.Drag.prototype.handleDownEvent,
12
    handleDragEvent: app.Drag.prototype.handleDragEvent,
13
    handleMoveEvent: app.Drag.prototype.handleMoveEvent,
14
    handleUpEvent: app.Drag.prototype.handleUpEvent
15
  });
16
17
  this.coordinate_ = null;
18
  this.cursor_ = 'pointer';
19
  this.feature_ = null;
20
  this.previousCursor_ = undefined;
21
};
22
23
ol.inherits(app.Drag, ol.interaction.Pointer);
0 ignored issues
show
Bug introduced by
The variable ol seems to be never declared. If this is a global, consider adding a /** global: ol */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
24
25
app.Drag.prototype.handleDownEvent = function(evt) {
26
  var map = evt.map;
27
  var feature = map.forEachFeatureAtPixel(evt.pixel,
28
      function(feature, layer) {
0 ignored issues
show
Unused Code introduced by
The parameter layer 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...
29
        return feature;
30
      },
31
      {layerFilter: app.Drag.prototype.layerFilter}
32
  );
33
34
  if (feature) {
35
    this.coordinate_ = evt.coordinate;
36
    this.feature_ = feature;
37
  }
38
  return !!feature;
39
};
40
41
app.Drag.prototype.handleDragEvent = function(evt) {
42
  var map = evt.map;
43
44
  var feature = map.forEachFeatureAtPixel(evt.pixel,
0 ignored issues
show
Unused Code introduced by
The assignment to variable feature seems to be never used. Consider removing it.
Loading history...
45
      function(feature, layer) {
0 ignored issues
show
Unused Code introduced by
The parameter layer 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...
46
        return feature;
47
      },
48
      {layerFilter: app.Drag.prototype.layerFilter});
49
50
  var deltaX = evt.coordinate[0] - this.coordinate_[0];
51
  var deltaY = evt.coordinate[1] - this.coordinate_[1];
52
53
  var geometry = /** @type {ol.geom.SimpleGeometry} */
54
      (this.feature_.getGeometry());
55
  geometry.translate(deltaX, deltaY);
56
57
  this.coordinate_[0] = evt.coordinate[0];
58
  this.coordinate_[1] = evt.coordinate[1];
59
};
60
61
app.Drag.prototype.handleMoveEvent = function(evt) {
62
  if (this.cursor_) {
63
    var map = evt.map;
64
    var feature = map.forEachFeatureAtPixel(evt.pixel,
65
        function(feature, layer) {
0 ignored issues
show
Unused Code introduced by
The parameter layer 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...
66
          return feature;
67
        },
68
        {layerFilter: app.Drag.prototype.layerFilter});
69
    var element = evt.map.getTargetElement();
70
    if (feature) {
71
      if (element.style.cursor != this.cursor_) {
72
        this.previousCursor_ = element.style.cursor;
73
        element.style.cursor = this.cursor_;
74
      }
75
    } else if (this.previousCursor_ !== undefined) {
76
      element.style.cursor = this.previousCursor_;
77
      this.previousCursor_ = undefined;
78
    }
79
  }
80
};
81
82
app.Drag.prototype.handleUpEvent = function(evt) {
83
  setTmpPointer(evt.coordinate);
84
  this.coordinate_ = null;
85
  this.feature_ = null;
86
  return false;
87
};
88
89
app.Drag.prototype.layerFilter = function(layer) {
90
    if (layer === tmpLayer)
0 ignored issues
show
Bug introduced by
The variable tmpLayer seems to be never declared. If this is a global, consider adding a /** global: tmpLayer */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
91
              return(true);
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...
92
          else
93
              return(false);
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
94
}
95
96
97