Skip to content

Commit 2877d2e

Browse files
author
Per Liedman
committed
Refactor files and how L.Routing namespace is constructed
1 parent 76fca10 commit 2877d2e

17 files changed

+148
-139
lines changed

Gruntfile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module.exports = function(grunt) {
33
pkg: grunt.file.readJSON('package.json'),
44
browserify: {
55
control: {
6-
src: ['src/L.Routing.Control.js'],
6+
src: ['src/index.js'],
77
dest: 'dist/leaflet-routing-machine.js',
88
options: {
99
browserifyOptions: {

src/L.Routing.Waypoint.js

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/L.Routing.Autocomplete.js renamed to src/autocomplete.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
(function() {
22
'use strict';
33

4-
L.Routing = L.Routing || {};
4+
var L = require('leaflet');
55

6-
L.Routing.Autocomplete = L.Class.extend({
6+
module.exports = L.Class.extend({
77
options: {
88
timeout: 500,
99
blurTimeout: 100,

src/L.Routing.Control.js renamed to src/control.js

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,15 @@
33

44
var L = require('leaflet');
55

6-
L.Routing = L.Routing || {};
7-
L.extend(L.Routing, require('./L.Routing.Itinerary'));
8-
L.extend(L.Routing, require('./L.Routing.Line'));
9-
L.extend(L.Routing, require('./L.Routing.Plan'));
10-
L.extend(L.Routing, require('./L.Routing.OSRMv1'));
11-
L.extend(L.Routing, require('./L.Routing.Mapbox'));
12-
L.extend(L.Routing, require('./L.Routing.ErrorControl'));
13-
14-
L.Routing.Control = L.Routing.Itinerary.extend({
6+
var Itinerary = require('./itinerary');
7+
var Line = require('./line');
8+
var Plan = require('./plan');
9+
var OSRMv1 = require('./osrm-v1');
10+
11+
module.exports = Itinerary.extend({
1512
options: {
1613
fitSelectedRoutes: 'smart',
17-
routeLine: function(route, options) { return L.Routing.line(route, options); },
14+
routeLine: function(route, options) { return new Line(route, options); },
1815
autoRoute: true,
1916
routeWhileDragging: false,
2017
routeDragInterval: 500,
@@ -28,11 +25,11 @@
2825
initialize: function(options) {
2926
L.Util.setOptions(this, options);
3027

31-
this._router = this.options.router || new L.Routing.OSRMv1(options);
32-
this._plan = this.options.plan || L.Routing.plan(this.options.waypoints, options);
28+
this._router = this.options.router || new OSRMv1(options);
29+
this._plan = this.options.plan || new Plan(this.options.waypoints, options);
3330
this._requestCount = 0;
3431

35-
L.Routing.Itinerary.prototype.initialize.call(this, options);
32+
Itinerary.prototype.initialize.call(this, options);
3633

3734
this.on('routeselected', this._routeSelected, this);
3835
if (this.options.defaultErrorHandler) {
@@ -75,7 +72,7 @@
7572
},
7673

7774
onAdd: function(map) {
78-
var container = L.Routing.Itinerary.prototype.onAdd.call(this, map);
75+
var container = Itinerary.prototype.onAdd.call(this, map);
7976

8077
this._map = map;
8178
this._map.addLayer(this._plan);
@@ -100,7 +97,7 @@
10097
map.removeLayer(this._alternatives[i]);
10198
}
10299
}
103-
return L.Routing.Itinerary.prototype.onRemove.call(this, map);
100+
return Itinerary.prototype.onRemove.call(this, map);
104101
},
105102

106103
getWaypoints: function() {
@@ -347,10 +344,4 @@
347344
}
348345
}
349346
});
350-
351-
L.Routing.control = function(options) {
352-
return new L.Routing.Control(options);
353-
};
354-
355-
module.exports = L.Routing;
356347
})();

src/L.Routing.ErrorControl.js renamed to src/error-control.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
(function() {
22
'use strict';
33

4-
L.Routing = L.Routing || {};
4+
var L = require('leaflet');
55

6-
L.Routing.ErrorControl = L.Control.extend({
6+
module.exports = L.Control.extend({
77
options: {
88
header: 'Routing error',
99
formatMessage: function(error) {
@@ -52,8 +52,4 @@
5252
delete this._element;
5353
}
5454
});
55-
56-
L.Routing.errorControl = function(routingControl, options) {
57-
return new L.Routing.ErrorControl(routingControl, options);
58-
};
5955
})();

src/L.Routing.Formatter.js renamed to src/formatter.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@
33

44
var L = require('leaflet');
55

6-
L.Routing = L.Routing || {};
6+
var Localization = require('./localization');
77

8-
L.extend(L.Routing, require('./L.Routing.Localization'));
9-
10-
L.Routing.Formatter = L.Class.extend({
8+
module.exports = L.Class.extend({
119
options: {
1210
units: 'metric',
1311
unitNames: null,
@@ -22,7 +20,7 @@
2220
var langs = L.Util.isArray(this.options.language) ?
2321
this.options.language :
2422
[this.options.language, 'en'];
25-
this._localization = new L.Routing.Localization(langs);
23+
this._localization = new Localization(langs);
2624
},
2725

2826
formatDistance: function(d /* Number (meters) */, sensitivity) {
@@ -158,7 +156,4 @@
158156
return strings[0] + (strings.length > 1 && instr.road ? strings[1] : '');
159157
}
160158
});
161-
162-
module.exports = L.Routing;
163159
})();
164-

src/L.Routing.GeocoderElement.js renamed to src/geocoder-element.js

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
'use strict';
33

44
var L = require('leaflet');
5-
L.Routing = L.Routing || {};
6-
L.extend(L.Routing, require('./L.Routing.Autocomplete'));
5+
var Autocomplete = require('./autocomplete');
6+
var Localization = require('./localization');
77

88
function selectInputText(input) {
99
if (input.setSelectionRange) {
@@ -15,7 +15,7 @@
1515
}
1616
}
1717

18-
L.Routing.GeocoderElement = L.Class.extend({
18+
module.exports = L.Class.extend({
1919
includes: L.Mixin.Events,
2020

2121
options: {
@@ -33,7 +33,7 @@
3333
};
3434
},
3535
geocoderPlaceholder: function(i, numberWaypoints, geocoderElement) {
36-
var l = new L.Routing.Localization(geocoderElement.options.language).localize('ui');
36+
var l = new Localization(geocoderElement.options.language).localize('ui');
3737
return i === 0 ?
3838
l.startPlaceholder :
3939
(i < numberWaypoints - 1 ?
@@ -85,7 +85,7 @@
8585
}, this);
8686
}
8787

88-
new L.Routing.Autocomplete(geocoderInput, function(r) {
88+
new Autocomplete(geocoderInput, function(r) {
8989
geocoderInput.value = r.name;
9090
wp.name = r.name;
9191
wp.latLng = r.center;
@@ -143,10 +143,4 @@
143143
this.fire('reversegeocoded', {waypoint: wp, value: value});
144144
}
145145
});
146-
147-
L.Routing.geocoderElement = function(wp, i, nWps, plan) {
148-
return new L.Routing.GeocoderElement(wp, i, nWps, plan);
149-
};
150-
151-
module.exports = L.Routing;
152146
})();

src/index.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
var L = require('leaflet'),
2+
Control = require('./control'),
3+
Itinerary = require('./itinerary'),
4+
Line = require('./line'),
5+
OSRMv1 = require('./osrm-v1'),
6+
Plan = require('./plan'),
7+
Waypoint = require('./waypoint'),
8+
Autocomplete = require('./autocomplete'),
9+
Formatter = require('./formatter'),
10+
GeocoderElement = require('./geocoder-element'),
11+
Localization = require('./localization'),
12+
ItineraryBuilder = require('./itinerary-builder'),
13+
Mapbox = require('./mapbox'),
14+
ErrorControl = require('./error-control');
15+
16+
L.routing = {
17+
control: function(options) { return new Control(options); },
18+
itinerary: function(options) {
19+
return Itinerary(options);
20+
},
21+
line: function(route, options) {
22+
return new Line(route, options);
23+
},
24+
plan: function(waypoints, options) {
25+
return new Plan(waypoints, options);
26+
},
27+
waypoint: function(latLng, name, options) {
28+
return new Waypoint(latLng, name, options);
29+
},
30+
osrmv1: function(options) {
31+
return new OSRMv1(options);
32+
},
33+
localization: function(options) {
34+
return new Localization(options);
35+
},
36+
formatter: function(options) {
37+
return new Formatter(options);
38+
},
39+
geocoderElement: function(wp, i, nWps, plan) {
40+
return new L.Routing.GeocoderElement(wp, i, nWps, plan);
41+
},
42+
itineraryBuilder: function(options) {
43+
return new ItineraryBuilder(options);
44+
},
45+
mapbox: function(accessToken, options) {
46+
return new Mapbox(accessToken, options);
47+
},
48+
errorControl: function(routingControl, options) {
49+
return new ErrorControl(routingControl, options);
50+
},
51+
autocomplete: function(elem, callback, context, options) {
52+
return new Autocomplete(elem, callback, context, options);
53+
}
54+
};
55+
56+
module.exports = L.Routing = {
57+
Control: Control,
58+
Itinerary: Itinerary,
59+
Line: Line,
60+
OSRMv1: OSRMv1,
61+
Plan: Plan,
62+
Waypoint: Waypoint,
63+
Autocomplete: Autocomplete,
64+
Formatter: Formatter,
65+
GeocoderElement: GeocoderElement,
66+
Localization: Localization,
67+
Formatter: Formatter,
68+
ItineraryBuilder: ItineraryBuilder,
69+
70+
// Legacy; remove these in next major release
71+
control: L.routing.control,
72+
itinerary: L.routing.itinerary,
73+
line: L.routing.line,
74+
plan: L.routing.plan,
75+
waypoint: L.routing.waypoint,
76+
osrmv1: L.routing.osrmv1,
77+
geocoderElement: L.routing.geocoderElement,
78+
mapbox: L.routing.mapbox,
79+
errorControl: L.routing.errorControl,
80+
};

src/L.Routing.ItineraryBuilder.js renamed to src/itinerary-builder.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22
'use strict';
33

44
var L = require('leaflet');
5-
L.Routing = L.Routing || {};
65

7-
L.Routing.ItineraryBuilder = L.Class.extend({
6+
module.exports = L.Class.extend({
87
options: {
98
containerClassName: ''
109
},
@@ -42,6 +41,4 @@
4241
return row;
4342
}
4443
});
45-
46-
module.exports = L.Routing;
4744
})();

src/L.Routing.Itinerary.js renamed to src/itinerary.js

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22
'use strict';
33

44
var L = require('leaflet');
5+
var Formatter = require('./formatter');
6+
var ItineraryBuilder = require('./itinerary-builder');
57

6-
L.Routing = L.Routing || {};
7-
L.extend(L.Routing, require('./L.Routing.Formatter'));
8-
L.extend(L.Routing, require('./L.Routing.ItineraryBuilder'));
9-
10-
L.Routing.Itinerary = L.Control.extend({
8+
module.exports = L.Control.extend({
119
includes: L.Mixin.Events,
1210

1311
options: {
@@ -37,8 +35,8 @@
3735

3836
initialize: function(options) {
3937
L.setOptions(this, options);
40-
this._formatter = this.options.formatter || new L.Routing.Formatter(this.options);
41-
this._itineraryBuilder = this.options.itineraryBuilder || new L.Routing.ItineraryBuilder({
38+
this._formatter = this.options.formatter || new Formatter(this.options);
39+
this._itineraryBuilder = this.options.itineraryBuilder || new ItineraryBuilder({
4240
containerClassName: this.options.itineraryClassName
4341
});
4442
},
@@ -224,10 +222,4 @@
224222
this.fire('routeselected', routes);
225223
}
226224
});
227-
228-
L.Routing.itinerary = function(options) {
229-
return new L.Routing.Itinerary(options);
230-
};
231-
232-
module.exports = L.Routing;
233225
})();

0 commit comments

Comments
 (0)