diff --git a/API.md b/API.md index b88592cae..e0981f8e0 100644 --- a/API.md +++ b/API.md @@ -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); ``` diff --git a/Makefile b/Makefile index 4ad6861eb..65a4f96a5 100644 --- a/Makefile +++ b/Makefile @@ -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 \ diff --git a/index.html b/index.html index 1b832bc3c..1567bd893 100644 --- a/index.html +++ b/index.html @@ -40,7 +40,8 @@ - + + @@ -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') diff --git a/js/id/services.js b/js/id/services.js new file mode 100644 index 000000000..c11dc788f --- /dev/null +++ b/js/id/services.js @@ -0,0 +1 @@ +iD.services = {}; diff --git a/js/id/services/countrycode.js b/js/id/services/nominatum.js similarity index 66% rename from js/id/services/countrycode.js rename to js/id/services/nominatum.js index c324cf64e..37aa8e2c1 100644 --- a/js/id/services/countrycode.js +++ b/js/id/services/nominatum.js @@ -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; }; diff --git a/js/id/services/taginfo.js b/js/id/services/taginfo.js index ade77ada7..b71661d25 100644 --- a/js/id/services/taginfo.js +++ b/js/id/services/taginfo.js @@ -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; }; diff --git a/js/id/services/wikipedia.js b/js/id/services/wikipedia.js index eb121f57a..a33143a07 100644 --- a/js/id/services/wikipedia.js +++ b/js/id/services/wikipedia.js @@ -1,4 +1,4 @@ -iD.wikipedia = function() { +iD.services.wikipedia = function() { var wiki = {}, endpoint = 'https://en.wikipedia.org/w/api.php?'; diff --git a/js/id/ui/preset/address.js b/js/id/ui/preset/address.js index d19fe9213..6457d0044 100644 --- a/js/id/ui/preset/address.js +++ b/js/id/ui/preset/address.js @@ -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); diff --git a/js/id/ui/preset/localized.js b/js/id/ui/preset/localized.js index 35e6307c7..18d27ff70 100644 --- a/js/id/ui/preset/localized.js +++ b/js/id/ui/preset/localized.js @@ -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; diff --git a/js/id/ui/preset/wikipedia.js b/js/id/ui/preset/wikipedia.js index 4b069cc2b..5ac04cef3 100644 --- a/js/id/ui/preset/wikipedia.js +++ b/js/id/ui/preset/wikipedia.js @@ -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) { diff --git a/test/index.html b/test/index.html index bc932e87a..439242d07 100644 --- a/test/index.html +++ b/test/index.html @@ -38,7 +38,8 @@ - + + @@ -262,6 +263,7 @@ + @@ -301,9 +303,8 @@ - - - + + diff --git a/test/index_packaged.html b/test/index_packaged.html index 5af0b5d99..9cd68a17e 100644 --- a/test/index_packaged.html +++ b/test/index_packaged.html @@ -59,6 +59,7 @@ + @@ -98,9 +99,8 @@ - - - + + diff --git a/test/spec/countrycode.js b/test/spec/services/nominatum.js similarity index 81% rename from test/spec/countrycode.js rename to test/spec/services/nominatum.js index 1b519d3f4..b280e8741 100644 --- a/test/spec/countrycode.js +++ b/test/spec/services/nominatum.js @@ -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" }, diff --git a/test/spec/taginfo.js b/test/spec/services/taginfo.js similarity index 98% rename from test/spec/taginfo.js rename to test/spec/services/taginfo.js index 7d91d2f5b..df66510c6 100644 --- a/test/spec/taginfo.js +++ b/test/spec/services/taginfo.js @@ -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() {