Skip to content

Commit

Permalink
add the option to not automatically fit the map to the IIIF layer bounds
Browse files Browse the repository at this point in the history
  • Loading branch information
mejackreed committed Apr 5, 2016
1 parent aa3dfd6 commit 7169f0e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 10 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Option | Type | Default | Description
------ | ---- | ------- | -----------
`tileFormat` | `String` | `'jpg'` | The [format](http://iiif.io/api/image/2.0/#format) of the returned image.
`tileSize` | Number | 256 | Tile size (width and height in pixels, assuming tiles are square).
`fitBounds` | Boolean | true | Automatically center and fit the maps bounds to the added IIIF layer

### Development

Expand Down
28 changes: 18 additions & 10 deletions leaflet-iiif.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ L.TileLayer.Iiif = L.TileLayer.extend({
continuousWorld: true,
tileSize: 256,
updateWhenIdle: true,
tileFormat: 'jpg'
tileFormat: 'jpg',
fitBounds: true
},

initialize: function(url, options) {
Expand Down Expand Up @@ -73,21 +74,16 @@ L.TileLayer.Iiif = L.TileLayer.extend({
// Wait for deferred to complete
$.when(_this._infoDeferred).done(function() {

// Find best zoom level and center map
var initialZoom = _this._getInitialZoom(map.getSize()),
imageSize = _this._imageSizes[initialZoom],
sw = map.options.crs.pointToLatLng(L.point(0, imageSize.y), initialZoom),
ne = map.options.crs.pointToLatLng(L.point(imageSize.x, 0), initialZoom),
bounds = L.latLngBounds(sw, ne);

map.fitBounds(bounds, true);

// Set maxZoom for map
map._layersMaxZoom = _this.maxZoom;

// Call add TileLayer
L.TileLayer.prototype.onAdd.call(_this, map);

if (_this.options.fitBounds) {
_this._fitBounds();
}

// Reset tile sizes to handle non 256x256 IIIF tiles
_this.on('tileload', function(tile, url) {

Expand All @@ -103,6 +99,18 @@ L.TileLayer.Iiif = L.TileLayer.extend({
});
});
},
_fitBounds: function() {
var _this = this;

// Find best zoom level and center map
var initialZoom = _this._getInitialZoom(map.getSize());
var imageSize = _this._imageSizes[initialZoom];
var sw = map.options.crs.pointToLatLng(L.point(0, imageSize.y), initialZoom);
var ne = map.options.crs.pointToLatLng(L.point(imageSize.x, 0), initialZoom);
var bounds = L.latLngBounds(sw, ne);

map.fitBounds(bounds, true);
},
_getInfo: function() {
var _this = this;

Expand Down
21 changes: 21 additions & 0 deletions spec/LTileLayerIiifSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,28 @@ describe('L.TileLayer.Iiif', function() {
map = L.map(div);
});

afterEach(function() {
document.body.removeChild(div);
});

function iiifLayerFactory(options) {
return L.tileLayer.iiif('http://localhost:9876/base/fixtures/mlk_info.json', options || {});
}

it('initializes the map', function(){
expect(typeof (map)).toEqual('object');
});

describe('fitBounds', function() {
var iiifLayer;

beforeEach(function() {
iiifLayer = iiifLayerFactory();
});

it('by default is on', function() {
expect(iiifLayer.options.fitBounds).toBe(true);
map.addLayer(iiifLayer);
});
});
});

0 comments on commit 7169f0e

Please sign in to comment.