| Conditions | 1 |
| Paths | 1 |
| Total Lines | 88 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 | define(['map/clientlayer', 'map/labellayer', 'leaflet', 'moment', 'map/locationmarker'], |
||
| 66 | return function (config, map, router, buttons) { |
||
| 67 | var userLocation; |
||
| 68 | |||
| 69 | var locateUserButton = new LocateButton(function (d) { |
||
| 70 | if (d) { |
||
| 71 | enableTracking(); |
||
| 72 | } else { |
||
| 73 | self.disableTracking(); |
||
| 74 | } |
||
| 75 | }); |
||
| 76 | |||
| 77 | var mybuttons = []; |
||
| 78 | |||
| 79 | function addButton(button) { |
||
| 80 | var el = button.onAdd(); |
||
| 81 | mybuttons.push(el); |
||
| 82 | buttons.appendChild(el); |
||
| 83 | } |
||
| 84 | |||
| 85 | self.clearButtons = function clearButtons() { |
||
| 86 | mybuttons.forEach(function (d) { |
||
| 87 | buttons.removeChild(d); |
||
| 88 | }); |
||
| 89 | }; |
||
| 90 | |||
| 91 | var showCoordsPickerButton = new CoordsPickerButton(function (d) { |
||
| 92 | if (d) { |
||
| 93 | enableCoords(); |
||
| 94 | } else { |
||
| 95 | disableCoords(); |
||
| 96 | } |
||
| 97 | }); |
||
| 98 | |||
| 99 | function enableTracking() { |
||
| 100 | map.locate({ |
||
| 101 | watch: true, |
||
| 102 | enableHighAccuracy: true, |
||
| 103 | setView: true |
||
| 104 | }); |
||
| 105 | locateUserButton.set(true); |
||
| 106 | } |
||
| 107 | |||
| 108 | self.disableTracking = function disableTracking() { |
||
| 109 | map.stopLocate(); |
||
| 110 | self.locationError(); |
||
| 111 | locateUserButton.set(false); |
||
| 112 | }; |
||
| 113 | |||
| 114 | function enableCoords() { |
||
| 115 | map.getContainer().classList.add('pick-coordinates'); |
||
| 116 | map.on('click', showCoordinates); |
||
| 117 | showCoordsPickerButton.set(true); |
||
| 118 | } |
||
| 119 | |||
| 120 | function disableCoords() { |
||
| 121 | map.getContainer().classList.remove('pick-coordinates'); |
||
| 122 | map.off('click', showCoordinates); |
||
| 123 | showCoordsPickerButton.set(false); |
||
| 124 | } |
||
| 125 | |||
| 126 | function showCoordinates(e) { |
||
| 127 | router.fullUrl({ zoom: map.getZoom(), lat: e.latlng.lat, lng: e.latlng.lng }); |
||
| 128 | disableCoords(); |
||
| 129 | } |
||
| 130 | |||
| 131 | self.locationFound = function locationFound(e) { |
||
| 132 | if (!userLocation) { |
||
| 133 | userLocation = new LocationMarker(e.latlng).addTo(map); |
||
| 134 | } |
||
| 135 | |||
| 136 | userLocation.setLatLng(e.latlng); |
||
| 137 | userLocation.setAccuracy(e.accuracy); |
||
| 138 | }; |
||
| 139 | |||
| 140 | self.locationError = function locationError() { |
||
| 141 | if (userLocation) { |
||
| 142 | map.removeLayer(userLocation); |
||
| 143 | userLocation = null; |
||
| 144 | } |
||
| 145 | }; |
||
| 146 | |||
| 147 | self.init = function init() { |
||
| 148 | addButton(locateUserButton); |
||
| 149 | addButton(showCoordsPickerButton); |
||
| 150 | }; |
||
| 151 | |||
| 152 | return self; |
||
| 153 | }; |
||
| 154 | }); |
||
| 155 |