Skip to content

Commit

Permalink
new approach for polyhighlight (ref #132)
Browse files Browse the repository at this point in the history
  • Loading branch information
atlefren committed Nov 27, 2015
1 parent c109be1 commit 33372bb
Show file tree
Hide file tree
Showing 11 changed files with 485 additions and 126 deletions.
2 changes: 1 addition & 1 deletion bower_components/KNreiseAPI/dist/KNreiseAPI.min.js

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions common/js/DatasetLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ KR.DatasetLoader = function (api, map, sidebar, errorCallback, useCommonCluster,
if (dataset.cluster) {
vectorLayer = new L.Knreise.MarkerClusterGroup({
dataset: dataset,
maxClusterRadius: maxClusterRadius
maxClusterRadius: maxClusterRadius,
unclusterThreshold: dataset.unclusterThreshold
}).addTo(map);
if (_addClusterClick) {
_addClusterClick(vectorLayer, dataset);
Expand Down Expand Up @@ -296,8 +297,6 @@ KR.DatasetLoader = function (api, map, sidebar, errorCallback, useCommonCluster,

var featurecollections = [];
var finished = _.after(toLoad.length, function () {
vectorLayer.isLoading = false;
vectorLayer.fire('dataloadend');

if (useCommonCluster) {
_resetDataGeoJson(vectorLayer, featurecollections);
Expand All @@ -308,6 +307,8 @@ KR.DatasetLoader = function (api, map, sidebar, errorCallback, useCommonCluster,
_resetDataGeoJson(vectorLayer, featurecollections);
}
}
vectorLayer.isLoading = false;
vectorLayer.fire('dataloadend');
if (callback) {
callback(featurecollections);
}
Expand Down
129 changes: 100 additions & 29 deletions common/js/KulturminneFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ KR.Config = KR.Config || {};
'use strict';

ns.getKulturminneFunctions = function (api) {
var _selectedPoly;
var _polyVisible = false;

var _vectorLayer;
var _map;
Expand All @@ -14,7 +16,7 @@ KR.Config = KR.Config || {};
var _enkeltMinneLayer;
var _loadedIds = [];
var _hideMarker = false;
var _showEnkeltminner = true;
var _showEnkeltminner = false;

var _hidePolygonLayer = function () {
map.removeLayer(_polygonLayer);
Expand All @@ -39,22 +41,6 @@ KR.Config = KR.Config || {};
}
};

var _checkThresholdPassed = function (map, threshold, callback) {
var prevZoom;
map.on('zoomstart', function (e) {
prevZoom = map.getZoom();
});
map.on('zoomend', function (e) {
var currentZoom = map.getZoom();
if (prevZoom > threshold && currentZoom <= threshold) {
callback('up');
}
if (prevZoom <= threshold && currentZoom > threshold) {
callback('down');
}
});
}

var _getMarkerForId = function (id) {
return _.find(_vectorLayer.getLayers(), function (layer) {
return (layer.feature.properties.id === id);
Expand Down Expand Up @@ -110,14 +96,7 @@ KR.Config = KR.Config || {};
});
};


var _markerClicked = function (e) {
_deselectPolygons();
var id = e.layer.feature.properties.id;
var poly = _getPolygonForId(id);
if (!poly) {
return;
}
var _highlightPolygon = function (poly) {
poly.setStyle({
weight: 1,
color: '#436978',
Expand All @@ -126,6 +105,20 @@ KR.Config = KR.Config || {};
opacity: 0.8,
fillOpacity: 0.4
});
}

var _markerClicked = function (e) {

_deselectPolygons();
var id = e.layer.feature.properties.id;

_selectedPoly = id;

var poly = _getPolygonForId(id);
if (!poly) {
return;
}
_highlightPolygon(poly);
if (_loadEnkeltminner) {
_loadEnkeltminner(e.layer.feature);
}
Expand Down Expand Up @@ -175,17 +168,92 @@ KR.Config = KR.Config || {};
};
}

var _highlightPolygonById = function (id) {
var poly = _getPolygonForId(id);
if (!poly) {
return;
}
_highlightPolygon(poly);
};

var _highlightMarkerById = function (id) {
var parent = _getMarkerForId(id);
if (parent) {
parent.fire('click');
}
};


var _polygonsLoaded = function (geoJson) {
_polygonLayer.clearLayers().addData(geoJson);
if (_selectedPoly) {
_highlightPolygonById(_selectedPoly);
_highlightMarkerById(_selectedPoly);
}
};

var _reloadPoly = function () {
if (!_polyVisible) {
return;
}
var bounds = _map.getBounds();

var ids = _.chain(_vectorLayer.getLayers())
.filter(function (layer) {
return bounds.contains(layer.getLatLng());
})
.map(function (layer) {
return layer.feature.properties.id;
})
.value();

if (ids.length) {
var q = {
api: 'kulturminnedataSparql',
type: 'lokalitetpoly',
lokalitet: ids
};
api.getData(q, _polygonsLoaded);
} else {
_polygonLayer.clearLayers();
}

};

var _togglePoly = function (direction) {

var shouldShow = (direction === 'down');

if (shouldShow) {
_polyVisible = true;
} else {
_polyVisible = false;
_polygonLayer.clearLayers();
}
};

var initKulturminnePoly = function (map, dataset, vectorLayer) {
_vectorLayer = vectorLayer;
_map = map;
_vectorLayer.on('hide', _hidePolygonLayer);
_vectorLayer.on('show', _showPolygonLayer);
_polygonLayer = _createPolygonLayer(dataset)

var showThreshold = 13;

if (map.getZoom() > showThreshold) {
_togglePoly('down');
}
_vectorLayer.on('dataloadend', _reloadPoly);
KR.Util.checkThresholdPassed(_map, showThreshold, _togglePoly);
_vectorLayer.on('click', _markerClicked);
_map.on('layerDeselect', _deselectPolygons);

/*
_checkThresholdPassed(_map, 13, _checkRemove);
_map.on('zoomend', _checkCluster);
_map.on('layerDeselect', _deselectPolygons);
_vectorLayer.on('click', _markerClicked);
if (_.has(dataset, 'showEnkeltminner')) {
_showEnkeltminner = dataset.showEnkeltminner;
Expand All @@ -194,8 +262,10 @@ KR.Config = KR.Config || {};
if (_showEnkeltminner) {
_setupEnkeltminner(dataset);
}
*/
};

/*
var _dataLoaded = function (geoJson) {
_polygonLayer.addData(geoJson);
var newIds = _.chain(geoJson.features)
Expand All @@ -215,7 +285,8 @@ KR.Config = KR.Config || {};
}
_loadedIds = _loadedIds.concat(newIds);
}

*/
/*
var loadKulturminnePoly = function (map, dataset, features) {
if (!features) {
return;
Expand All @@ -240,9 +311,9 @@ KR.Config = KR.Config || {};
};
api.getData(q, _dataLoaded);
};

*/
return {
loadKulturminnePoly: loadKulturminnePoly,
//loadKulturminnePoly: loadKulturminnePoly,
initKulturminnePoly: initKulturminnePoly
};
};
Expand Down
46 changes: 46 additions & 0 deletions common/js/L.Knreise.MarkerClusterGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,57 @@ L.Knreise.MarkerClusterGroup = L.MarkerClusterGroup.extend({

this._queue = [];
this.on('clusterclick', this._clusterClicked, this);
this.shouldUncluster = false;
},

onAdd: function (map) {
L.MarkerClusterGroup.prototype.onAdd.apply(this, arguments);
map.on('layerSelected', this._deselectAll, this);
this._map = map;
if (_.has(this.options, 'unclusterThreshold')) {
this._unclustred = L.featureGroup().addTo(map);
this._unclustred.on('click', _.bind(function (e) {
this.fire('click', e);
}, this));
var showThreshold = this.options.unclusterThreshold;
if (map.getZoom() > showThreshold) {
this._toggleCluster('down');
}
KR.Util.checkThresholdPassed(map, showThreshold, _.bind(this._toggleCluster, this));
}
},

addLayers: function (layers) {
if (this._unclustred) {
this._unclustred.clearLayers();
}

if (this.shouldUncluster) {
var bounds = this._map.getBounds();
_.chain(layers)
.filter(function (layer) {
return bounds.contains(layer.getLatLng());
})
.each(function (layer) {
this._unclustred.addLayer(layer);
}, this)
.value()

} else {
L.MarkerClusterGroup.prototype.addLayers.apply(this, arguments);
}
},

getLayers: function () {
if (this.shouldUncluster) {
return this._unclustred.getLayers();
} else {
L.MarkerClusterGroup.prototype.getLayers.apply(this, arguments);
}
},

_toggleCluster: function (direction) {
this.shouldUncluster = (direction === 'down');
},

_deselectAll: function () {
Expand Down
17 changes: 9 additions & 8 deletions common/js/datasets.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,11 @@ KR.Config = KR.Config || {};
template: KR.Util.getDatasetTemplate('ra_sparql'),
bbox: false,
isStatic: true,
init: kulturminneFunctions.initKulturminnePoly,
init: kulturminneFunctions.initKulturminnePoly/*,
loadWhenLessThan: {
count: 5,
callback: kulturminneFunctions.loadKulturminnePoly
}
}*/
}
],
description: 'Data fra Universitetsmuseene, Digitalt museum og Riksantikvaren'
Expand Down Expand Up @@ -186,11 +186,11 @@ KR.Config = KR.Config || {};
template: KR.Util.getDatasetTemplate('ra_sparql'),
bbox: false,
isStatic: true,
init: kulturminneFunctions.initKulturminnePoly,
init: kulturminneFunctions.initKulturminnePoly/*,
loadWhenLessThan: {
count: 5,
callback: kulturminneFunctions.loadKulturminnePoly
}
}*/
}
],
description: 'Arkeologidata fra Universitetsmuseene og Riksantikvaren'
Expand Down Expand Up @@ -218,11 +218,11 @@ KR.Config = KR.Config || {};
template: KR.Util.getDatasetTemplate('ra_sparql'),
bbox: false,
isStatic: true,
init: kulturminneFunctions.initKulturminnePoly,
init: kulturminneFunctions.initKulturminnePoly/*,
loadWhenLessThan: {
count: 5,
callback: kulturminneFunctions.loadKulturminnePoly
}
}*/
},
{
name: 'DiMu',
Expand Down Expand Up @@ -335,11 +335,12 @@ KR.Config = KR.Config || {};
template: KR.Util.getDatasetTemplate('ra_sparql'),
bbox: false,
isStatic: true,
init: kulturminneFunctions.initKulturminnePoly,
unclusterThreshold: 13,
init: kulturminneFunctions.initKulturminnePoly, /*
loadWhenLessThan: {
count: 10,
callback: kulturminneFunctions.loadKulturminnePoly
},
},*/
description: 'Data fra Riksantikvarens kulturminnesøk'
},
'brukerminner': {
Expand Down
16 changes: 16 additions & 0 deletions common/js/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -507,4 +507,20 @@ KR.Util = KR.Util || {};
}
};

ns.checkThresholdPassed = function (map, threshold, callback) {
var prevZoom;
map.on('zoomstart', function (e) {
prevZoom = map.getZoom();
});
map.on('zoomend', function (e) {
var currentZoom = map.getZoom();
if (prevZoom > threshold && currentZoom <= threshold) {
callback('up');
}
if (prevZoom <= threshold && currentZoom > threshold) {
callback('down');
}
});
};

}(KR.Util));
Loading

0 comments on commit 33372bb

Please sign in to comment.