Refactor services

This commit is contained in:
Bryan Housel
2016-02-08 10:31:34 -05:00
parent a9389b6773
commit 3e65156754
14 changed files with 65 additions and 48 deletions
+3 -3
View File
@@ -137,7 +137,7 @@ iD can use external presets exclusively or along with the default OpenStreetMap
var iD = iD()
.presets(customPresets)
.taginfo(iD.taginfo())
.taginfo(iD.services.taginfo())
.imagery(iD.data.imagery);
```
@@ -152,7 +152,7 @@ Just like Presets, Imagery can be configured using the `iD().imagery` accessor.
var iD = iD()
.presets(customPresets)
.taginfo(iD.taginfo())
.taginfo(iD.services.taginfo())
.imagery(customImagery);
```
@@ -168,7 +168,7 @@ The Imagery object should follow the structure defined by [editor-imagery-index]
var iD = iD()
.presets(customPresets)
.taginfo(iD.taginfo().endpoint('url'))
.taginfo(iD.services.taginfo().endpoint('url'))
.imagery(customImagery);
```
+1
View File
@@ -63,6 +63,7 @@ dist/iD.js: \
js/lib/marked.js \
js/id/start.js \
js/id/id.js \
js/id/services.js \
js/id/services/*.js \
js/id/util.js \
js/id/util/*.js \
+3 -2
View File
@@ -40,7 +40,8 @@
<script src='js/id/util/session_mutex.js'></script>
<script src='js/id/util/suggest_names.js'></script>
<script src='js/id/services/countrycode.js'></script>
<script src='js/id/services.js'></script>
<script src='js/id/services/nominatum.js'></script>
<script src='js/id/services/taginfo.js'></script>
<script src='js/id/services/wikipedia.js'></script>
@@ -251,7 +252,7 @@
id = iD()
.presets(iD.data.presets)
.imagery(iD.data.imagery)
.taginfo(iD.taginfo())
.taginfo(iD.services.taginfo())
.assetPath('dist/');
d3.select('#id-container')
+1
View File
@@ -0,0 +1 @@
iD.services = {};
@@ -1,15 +1,11 @@
iD.countryCode = function() {
var countryCode = {},
iD.services.nominatum = function() {
var nominatum = {},
endpoint = 'https://nominatim.openstreetmap.org/reverse?';
if (!iD.countryCode.cache) {
iD.countryCode.cache = rbush();
}
var cache = iD.countryCode.cache;
countryCode.search = function(location, callback) {
var countryCodes = cache.search([location[0], location[1], location[0], location[1]]);
nominatum.countryCode = function(location, callback) {
var cache = iD.services.nominatum.cache,
countryCodes = cache.search([location[0], location[1], location[0], location[1]]);
if (countryCodes.length > 0)
return callback(null, countryCodes[0][4]);
@@ -34,5 +30,15 @@ iD.countryCode = function() {
});
};
return countryCode;
nominatum.reset = function() {
iD.services.nominatum.cache = rbush();
return nominatum;
};
if (!iD.services.nominatum.cache) {
nominatum.reset();
}
return nominatum;
};
+13 -6
View File
@@ -1,4 +1,4 @@
iD.taginfo = function() {
iD.services.taginfo = function() {
var taginfo = {},
endpoint = 'https://taginfo.openstreetmap.org/api/4/',
tag_sorts = {
@@ -14,11 +14,6 @@ iD.taginfo = function() {
line: 'ways'
};
if (!iD.taginfo.cache) {
iD.taginfo.cache = {};
}
var cache = iD.taginfo.cache;
function sets(parameters, n, o) {
if (parameters.geometry && o[parameters.geometry]) {
@@ -68,6 +63,8 @@ iD.taginfo = function() {
var debounced = _.debounce(d3.json, 100, true);
function request(url, debounce, callback) {
var cache = iD.services.taginfo.cache;
if (cache[url]) {
callback(null, cache[url]);
} else if (debounce) {
@@ -132,5 +129,15 @@ iD.taginfo = function() {
return taginfo;
};
taginfo.reset = function() {
iD.services.taginfo.cache = {};
return taginfo;
};
if (!iD.services.taginfo.cache) {
taginfo.reset();
}
return taginfo;
};
+1 -1
View File
@@ -1,4 +1,4 @@
iD.wikipedia = function() {
iD.services.wikipedia = function() {
var wiki = {},
endpoint = 'https://en.wikipedia.org/w/api.php?';
+1 -1
View File
@@ -109,7 +109,7 @@ iD.ui.preset.address = function(field, context) {
var center = entity.extent(context.graph()).center(),
addressFormat;
iD.countryCode().search(center, function (err, countryCode) {
iD.services.nominatum().countryCode(center, function (err, countryCode) {
addressFormat = _.find(iD.data.addressFormats, function (a) {
return a && a.countryCodes && _.contains(a.countryCodes, countryCode);
}) || _.first(iD.data.addressFormats);
+1 -1
View File
@@ -1,6 +1,6 @@
iD.ui.preset.localized = function(field, context) {
var dispatch = d3.dispatch('change', 'input'),
wikipedia = iD.wikipedia(),
wikipedia = iD.services.wikipedia(),
input, localizedInputs, wikiTitles,
entity;
+1 -1
View File
@@ -1,6 +1,6 @@
iD.ui.preset.wikipedia = function(field, context) {
var dispatch = d3.dispatch('change'),
wikipedia = iD.wikipedia(),
wikipedia = iD.services.wikipedia(),
link, entity, lang, title;
function i(selection) {
+5 -4
View File
@@ -38,7 +38,8 @@
<script src='../js/id/id.js'></script>
<script src='../js/id/util.js'></script>
<script src='../js/id/services/countrycode.js'></script>
<script src='../js/id/services.js'></script>
<script src='../js/id/services/nominatum.js'></script>
<script src='../js/id/services/taginfo.js'></script>
<script src='../js/id/services/wikipedia.js'></script>
@@ -262,6 +263,7 @@
<script src="spec/actions/split.js"></script>
<script src="spec/actions/unrestrict_turn.js"></script>
<script src="spec/geo.js"></script>
<script src="spec/geo/extent.js"></script>
<script src="spec/geo/intersection.js"></script>
<script src="spec/geo/multipolygon.js"></script>
@@ -301,9 +303,8 @@
<script src="spec/ui/preset/localized.js"></script>
<script src="spec/ui/preset/wikipedia.js"></script>
<script src="spec/countrycode.js"></script>
<script src="spec/geo.js"></script>
<script src="spec/taginfo.js"></script>
<script src="spec/services/nominatum.js"></script>
<script src="spec/services/taginfo.js"></script>
<script src="spec/util.js"></script>
<script src='spec/util/session_mutex.js'></script>
+3 -3
View File
@@ -59,6 +59,7 @@
<script src="spec/actions/straighten.js"></script>
<script src="spec/actions/unrestrict_turn.js"></script>
<script src="spec/geo.js"></script>
<script src="spec/geo/extent.js"></script>
<script src="spec/geo/intersection.js"></script>
<script src="spec/geo/multipolygon.js"></script>
@@ -98,9 +99,8 @@
<script src="spec/ui/preset/localized.js"></script>
<script src="spec/ui/preset/wikipedia.js"></script>
<script src="spec/countrycode.js"></script>
<script src="spec/geo.js"></script>
<script src="spec/taginfo.js"></script>
<script src="spec/services/nominatum.js"></script>
<script src="spec/services/taginfo.js"></script>
<script src="spec/util.js"></script>
<script src='spec/util/session_mutex.js'></script>
@@ -1,10 +1,10 @@
describe("iD.countryCode", function() {
var server, countryCode;
describe("iD.services.nominatum", function() {
var server, nominatum;
beforeEach(function() {
server = sinon.fakeServer.create();
iD.countryCode.cache = null;
countryCode = iD.countryCode();
nominatum = iD.services.nominatum();
nominatum.reset();
});
afterEach(function() {
@@ -15,10 +15,10 @@ describe("iD.countryCode", function() {
return iD.util.stringQs(url.substring(url.indexOf('?') + 1));
}
describe("#search", function() {
it("calls the given callback with the results of the search query", function() {
describe("#countryCode", function() {
it("calls the given callback with the results of the country code query", function() {
var callback = sinon.spy();
countryCode.search([16, 48], callback);
nominatum.countryCode([16, 48], callback);
server.respondWith("GET", "https://nominatim.openstreetmap.org/reverse?addressdetails=1&format=json&lat=48&lon=16",
[200, { "Content-Type": "application/json" },
@@ -29,9 +29,9 @@ describe("iD.countryCode", function() {
{format: "json", addressdetails: "1", lat: "48", lon: "16"});
expect(callback).to.have.been.calledWith(null, "at");
});
it("should not cache the first search result", function() {
it("should not cache the first country code result", function() {
var callback = sinon.spy();
countryCode.search([16, 48], callback);
nominatum.countryCode([16, 48], callback);
server.respondWith("GET", "https://nominatim.openstreetmap.org/reverse?addressdetails=1&format=json&lat=48&lon=16",
[200, { "Content-Type": "application/json" },
@@ -45,7 +45,7 @@ describe("iD.countryCode", function() {
server.restore();
server = sinon.fakeServer.create();
countryCode.search([17, 49], callback);
nominatum.countryCode([17, 49], callback);
server.respondWith("GET", "https://nominatim.openstreetmap.org/reverse?addressdetails=1&format=json&lat=49&lon=17",
[200, { "Content-Type": "application/json" },
@@ -56,9 +56,9 @@ describe("iD.countryCode", function() {
{format: "json", addressdetails: "1", lat: "49", lon: "17"});
expect(callback).to.have.been.calledWith(null, "cz");
});
it("should cache the first search result", function() {
it("should cache the first country code result", function() {
var callback = sinon.spy();
countryCode.search([16, 48], callback);
nominatum.countryCode([16, 48], callback);
server.respondWith("GET", "https://nominatim.openstreetmap.org/reverse?addressdetails=1&format=json&lat=48&lon=16",
[200, { "Content-Type": "application/json" },
@@ -72,7 +72,7 @@ describe("iD.countryCode", function() {
server.restore();
server = sinon.fakeServer.create();
countryCode.search([16.01, 48.01], callback);
nominatum.countryCode([16.01, 48.01], callback);
server.respondWith("GET", "https://nominatim.openstreetmap.org/reverse?addressdetails=1&format=json&lat=48.01&lon=16.01",
[200, { "Content-Type": "application/json" },
@@ -83,7 +83,7 @@ describe("iD.countryCode", function() {
});
it("calls the given callback with an error", function() {
var callback = sinon.spy();
countryCode.search([1000, 1000], callback);
nominatum.countryCode([1000, 1000], callback);
server.respondWith("GET", "https://nominatim.openstreetmap.org/reverse?addressdetails=1&format=json&lat=1000&lon=1000",
[200, { "Content-Type": "application/json" },
@@ -1,9 +1,9 @@
describe("iD.taginfo", function() {
describe("iD.services.taginfo", function() {
var server, taginfo;
beforeEach(function() {
server = sinon.fakeServer.create();
taginfo = iD.taginfo();
taginfo = iD.services.taginfo();
});
afterEach(function() {