Remove expandUrl duplicated code from createFromTemplate
and make use of expandUrl function instead.
This commit is contained in:
@@ -49,7 +49,8 @@ ol.source.XYZ = function(xyzOptions) {
|
|||||||
} else if (goog.isDef(xyzOptions.urls)) {
|
} else if (goog.isDef(xyzOptions.urls)) {
|
||||||
tileUrlFunction = ol.TileUrlFunction.createFromTemplates(xyzOptions.urls);
|
tileUrlFunction = ol.TileUrlFunction.createFromTemplates(xyzOptions.urls);
|
||||||
} else if (goog.isDef(xyzOptions.url)) {
|
} else if (goog.isDef(xyzOptions.url)) {
|
||||||
tileUrlFunction = ol.TileUrlFunction.createFromTemplate(xyzOptions.url);
|
tileUrlFunction = ol.TileUrlFunction.createFromTemplates(
|
||||||
|
ol.TileUrlFunction.expandUrl(xyzOptions.url));
|
||||||
}
|
}
|
||||||
|
|
||||||
var tileGrid = new ol.tilegrid.XYZ({
|
var tileGrid = new ol.tilegrid.XYZ({
|
||||||
|
|||||||
@@ -20,28 +20,15 @@ ol.TileUrlFunctionType;
|
|||||||
* @return {ol.TileUrlFunctionType} Tile URL function.
|
* @return {ol.TileUrlFunctionType} Tile URL function.
|
||||||
*/
|
*/
|
||||||
ol.TileUrlFunction.createFromTemplate = function(template) {
|
ol.TileUrlFunction.createFromTemplate = function(template) {
|
||||||
var match =
|
return function(tileCoord) {
|
||||||
/\{(\d)-(\d)\}/.exec(template) || /\{([a-z])-([a-z])\}/.exec(template);
|
if (goog.isNull(tileCoord)) {
|
||||||
if (match) {
|
return undefined;
|
||||||
var templates = [];
|
} else {
|
||||||
var startCharCode = match[1].charCodeAt(0);
|
return template.replace('{z}', tileCoord.z)
|
||||||
var stopCharCode = match[2].charCodeAt(0);
|
.replace('{x}', tileCoord.x)
|
||||||
var charCode;
|
.replace('{y}', tileCoord.y);
|
||||||
for (charCode = startCharCode; charCode <= stopCharCode; ++charCode) {
|
|
||||||
templates.push(template.replace(match[0], String.fromCharCode(charCode)));
|
|
||||||
}
|
}
|
||||||
return ol.TileUrlFunction.createFromTemplates(templates);
|
};
|
||||||
} else {
|
|
||||||
return function(tileCoord) {
|
|
||||||
if (goog.isNull(tileCoord)) {
|
|
||||||
return undefined;
|
|
||||||
} else {
|
|
||||||
return template.replace('{z}', tileCoord.z)
|
|
||||||
.replace('{x}', tileCoord.x)
|
|
||||||
.replace('{y}', tileCoord.y);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ describe('ol.TileUrlFunction', function() {
|
|||||||
it('creates expected URLs', function() {
|
it('creates expected URLs', function() {
|
||||||
var template = 'http://tile-{1-3}/{z}/{x}/{y}';
|
var template = 'http://tile-{1-3}/{z}/{x}/{y}';
|
||||||
var urls = ol.TileUrlFunction.expandUrl(template);
|
var urls = ol.TileUrlFunction.expandUrl(template);
|
||||||
expect(urls).toEqual([
|
expect(urls).to.eql([
|
||||||
'http://tile-1/{z}/{x}/{y}',
|
'http://tile-1/{z}/{x}/{y}',
|
||||||
'http://tile-2/{z}/{x}/{y}',
|
'http://tile-2/{z}/{x}/{y}',
|
||||||
'http://tile-3/{z}/{x}/{y}'
|
'http://tile-3/{z}/{x}/{y}'
|
||||||
@@ -18,7 +18,7 @@ describe('ol.TileUrlFunction', function() {
|
|||||||
it('creates expected URLs', function() {
|
it('creates expected URLs', function() {
|
||||||
var template = 'http://tile-{c-e}/{z}/{x}/{y}';
|
var template = 'http://tile-{c-e}/{z}/{x}/{y}';
|
||||||
var urls = ol.TileUrlFunction.expandUrl(template);
|
var urls = ol.TileUrlFunction.expandUrl(template);
|
||||||
expect(urls).toEqual([
|
expect(urls).to.eql([
|
||||||
'http://tile-c/{z}/{x}/{y}',
|
'http://tile-c/{z}/{x}/{y}',
|
||||||
'http://tile-d/{z}/{x}/{y}',
|
'http://tile-d/{z}/{x}/{y}',
|
||||||
'http://tile-e/{z}/{x}/{y}'
|
'http://tile-e/{z}/{x}/{y}'
|
||||||
@@ -33,31 +33,23 @@ describe('ol.TileUrlFunction', function() {
|
|||||||
expect(tileUrl(new ol.TileCoord(3, 2, 1))).to.eql('3/2/1');
|
expect(tileUrl(new ol.TileCoord(3, 2, 1))).to.eql('3/2/1');
|
||||||
expect(tileUrl(null)).to.be(undefined);
|
expect(tileUrl(null)).to.be(undefined);
|
||||||
});
|
});
|
||||||
describe('with number range', function() {
|
});
|
||||||
it('creates expected URL', function() {
|
|
||||||
var template = 'http://tile-{1-3}/{z}/{x}/{y}';
|
describe('createFromTemplates', function() {
|
||||||
var tileUrlFunction = ol.TileUrlFunction.createFromTemplate(template);
|
it('creates expected URL', function() {
|
||||||
var tileCoord = new ol.TileCoord(3, 2, 1);
|
var templates = [
|
||||||
tileCoord.hash = function() { return 3; };
|
'http://tile-1/{z}/{x}/{y}',
|
||||||
expect(tileUrlFunction(tileCoord)).to.eql('http://tile-1/3/2/1');
|
'http://tile-2/{z}/{x}/{y}',
|
||||||
tileCoord.hash = function() { return 2; };
|
'http://tile-3/{z}/{x}/{y}'
|
||||||
expect(tileUrlFunction(tileCoord)).to.eql('http://tile-3/3/2/1');
|
];
|
||||||
tileCoord.hash = function() { return 1; };
|
var tileUrlFunction = ol.TileUrlFunction.createFromTemplates(templates);
|
||||||
expect(tileUrlFunction(tileCoord)).to.eql('http://tile-2/3/2/1');
|
var tileCoord = new ol.TileCoord(3, 2, 1);
|
||||||
});
|
tileCoord.hash = function() { return 3; };
|
||||||
});
|
expect(tileUrlFunction(tileCoord)).to.eql('http://tile-1/3/2/1');
|
||||||
describe('with character range', function() {
|
tileCoord.hash = function() { return 2; };
|
||||||
it('creates expected URL', function() {
|
expect(tileUrlFunction(tileCoord)).to.eql('http://tile-3/3/2/1');
|
||||||
var template = 'http://tile-{c-e}/{z}/{x}/{y}';
|
tileCoord.hash = function() { return 1; };
|
||||||
var tileUrlFunction = ol.TileUrlFunction.createFromTemplate(template);
|
expect(tileUrlFunction(tileCoord)).to.eql('http://tile-2/3/2/1');
|
||||||
var tileCoord = new ol.TileCoord(3, 2, 1);
|
|
||||||
tileCoord.hash = function() { return 3; };
|
|
||||||
expect(tileUrlFunction(tileCoord)).to.eql('http://tile-c/3/2/1');
|
|
||||||
tileCoord.hash = function() { return 2; };
|
|
||||||
expect(tileUrlFunction(tileCoord)).to.eql('http://tile-e/3/2/1');
|
|
||||||
tileCoord.hash = function() { return 1; };
|
|
||||||
expect(tileUrlFunction(tileCoord)).to.eql('http://tile-d/3/2/1');
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user