From 7169f0ee507f0bfab3fc694b1560c3d1004e3b5e Mon Sep 17 00:00:00 2001 From: Jack Reed Date: Tue, 5 Apr 2016 08:53:38 -0700 Subject: [PATCH] add the option to not automatically fit the map to the IIIF layer bounds --- README.md | 1 + leaflet-iiif.js | 28 ++++++++++++++++++---------- spec/LTileLayerIiifSpec.js | 21 +++++++++++++++++++++ 3 files changed, 40 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 1baf57e..3fd54da 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/leaflet-iiif.js b/leaflet-iiif.js index dce4d7f..d94b54a 100644 --- a/leaflet-iiif.js +++ b/leaflet-iiif.js @@ -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) { @@ -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) { @@ -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; diff --git a/spec/LTileLayerIiifSpec.js b/spec/LTileLayerIiifSpec.js index 0406bbf..fa9a5da 100644 --- a/spec/LTileLayerIiifSpec.js +++ b/spec/LTileLayerIiifSpec.js @@ -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); + }); + }); });