Skip to content

Commit 8df7752

Browse files
committed
Preserves extent when location filter is on
1 parent 169a1d8 commit 8df7752

File tree

1 file changed

+39
-7
lines changed

1 file changed

+39
-7
lines changed

assets/javascripts/app.js

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ var App = (function ($, publ) {
99
var map, vector, bounds, contents, toolbar, geolocation = null;
1010
var features = [];
1111
var layerArr = [];
12+
var filters = {
13+
location: false,
14+
distance: false
15+
};
1216

1317
// Quick hack
1418
var quick_hack = {
@@ -149,15 +153,14 @@ var App = (function ($, publ) {
149153
this.setView();
150154
this.setGeolocation();
151155
this.setGeocoding();
152-
this.zoomToExtent();
153156
this.parseHistory();
154157

155158
// Control button
156159
var maximizeCtrl = new ol.control.Button({
157160
html: '<i class="icon-maximize" ></i>',
158161
title: "Maximize",
159162
handleClick: function () {
160-
publ.zoomToExtent();
163+
publ.zoomToExtent(true);
161164
}
162165
});
163166
toolbar.addControl(maximizeCtrl);
@@ -194,8 +197,19 @@ var App = (function ($, publ) {
194197
// Add LayerSwitcher Image Toolbar
195198
map.addControl(new ol.control.LayerPopup());
196199

197-
// Add map events
198-
map.on('moveend', publ.updateFilter);
200+
// Because Redmine filter functions are applied later, the Window onload
201+
// event provides a workaround to have filters loaded before executing
202+
// the following code
203+
$(window).bind('load', function() {
204+
if ($("tr#tr_bbox").length > 0) {
205+
filters.location = true;
206+
}
207+
if ($("tr#tr_distance").length > 0) {
208+
filters.distance = true;
209+
}
210+
publ.zoomToExtent();
211+
map.on('moveend', publ.updateFilter);
212+
});
199213
};
200214

201215
/**
@@ -326,8 +340,18 @@ var App = (function ($, publ) {
326340
/**
327341
*
328342
*/
329-
publ.zoomToExtent = function () {
330-
if (vector.getSource().getFeatures().length > 0) {
343+
publ.zoomToExtent = function (force) {
344+
if (!force && (filters.distance || filters.location)) {
345+
// Do not zoom to extent but show the previous extent stored as cookie
346+
var parts = (getCookie("_redmine_gtt_permalink")).split("/");
347+
map.getView().setZoom(parseInt(parts[0], 10));
348+
map.getView().setCenter(ol.proj.transform([
349+
parseFloat(parts[1]),
350+
parseFloat(parts[2])
351+
],'EPSG:4326','EPSG:3857'));
352+
map.getView().setRotation(parseFloat(parts[3]));
353+
}
354+
else if (vector.getSource().getFeatures().length > 0) {
331355
var extent = ol.extent.createEmpty();
332356
// Because the vector layer is set to "useSpatialIndex": false, we cannot
333357
// make use of "vector.getSource().getExtent()"
@@ -359,6 +383,15 @@ var App = (function ($, publ) {
359383
$('#tr_distance #values_distance_3').val(center[0]);
360384
$('#tr_distance #values_distance_4').val(center[1]);
361385

386+
// Set Permalink as Cookie
387+
var cookie = [];
388+
var hash = map.getView().getZoom() + '/' +
389+
Math.round(center[0] * 1000000) / 1000000 + '/' +
390+
Math.round(center[1] * 1000000) / 1000000 + '/' +
391+
map.getView().getRotation();
392+
cookie.push("_redmine_gtt_permalink=" + hash);
393+
cookie.push("path=" + window.location.pathname);
394+
document.cookie = cookie.join(";");
362395

363396
extent = ol.proj.transformExtent(extent,'EPSG:3857','EPSG:4326').join('|');
364397
console.log("Map Extent (WGS84): ",extent);
@@ -741,4 +774,3 @@ function buildDistanceFilterRow(operator, values){
741774
$('#values_'+fieldId+'_3').val(x);
742775
$('#values_'+fieldId+'_4').val(y);
743776
}
744-

0 commit comments

Comments
 (0)