Skip to content

Commit 3991e94

Browse files
committed
Simple array position based implementation of cookies for layer preference
1 parent d274cb3 commit 3991e94

File tree

2 files changed

+40
-12
lines changed

2 files changed

+40
-12
lines changed

assets/javascripts/app.js

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,31 @@ var App = (function ($, publ) {
6161
// through the template causes encoding problems
6262
publ.updateForm(features);
6363

64-
contents.layers.forEach(function(layer) {
64+
contents.layers.forEach(function(layer,idx) {
6565
var s = layer.type.split(".");
66-
layerArr.push(
67-
new ol.layer.Tile({
68-
title: layer.name,
69-
baseLayer: true,
70-
visible: false,
71-
source: new ol[s[1]][s[2]](layer.options)
72-
})
73-
);
66+
var l = new ol.layer.Tile({
67+
index: idx,
68+
title: layer.name,
69+
baseLayer: true,
70+
visible: false,
71+
source: new ol[s[1]][s[2]](layer.options)
72+
});
73+
l.on("change:visible", function(e) {
74+
if (e.target.getVisible()) {
75+
document.cookie = "_redmine_gtt_basemap=" + e.target.get("index") + ";path=/";
76+
}
77+
});
78+
layerArr.push(l);
7479
});
75-
layerArr[0].setVisible(true);
80+
81+
// Decide which baselayer to show
82+
var idx = getCookie("_redmine_gtt_basemap") || 0;
83+
if (layerArr.length > idx) {
84+
layerArr[idx].setVisible(true);
85+
}
86+
else {
87+
layerArr[0].setVisible(true);
88+
}
7689

7790
// Layer for vector features
7891
vector = new ol.layer.Vector({
@@ -186,7 +199,7 @@ var App = (function ($, publ) {
186199
});
187200

188201
// Add LayerSwitcher Image Toolbar
189-
map.addControl(new ol.control.LayerPopup());
202+
map.addControl(new ol.control.LayerPopup());
190203
};
191204

192205
publ.getColor = function (feature) {
@@ -584,6 +597,21 @@ var App = (function ($, publ) {
584597
$("#geom").val(JSON.stringify(geojson.features[0]));
585598
};
586599

600+
function getCookie(cname) {
601+
var name = cname + "=";
602+
var ca = document.cookie.split(';');
603+
for(var i = 0; i < ca.length; i++) {
604+
var c = ca[i];
605+
while (c.charAt(0) == ' ') {
606+
c = c.substring(1);
607+
}
608+
if (c.indexOf(name) == 0) {
609+
return c.substring(name.length, c.length);
610+
}
611+
}
612+
return "";
613+
}
614+
587615
/**
588616
* Return public objects
589617
*/

lib/redmine_gtt/hooks/view_layouts_base_html_head_hook.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def view_layouts_base_html_head(context={})
1212
tags << stylesheet_link_tag("ol3-ext.min.css", :plugin => "redmine_gtt", :media => "all")
1313
tags << stylesheet_link_tag("app.css", :plugin => "redmine_gtt", :media => "all")
1414

15-
tags << javascript_include_tag('ol.js', :plugin => 'redmine_gtt')
15+
tags << javascript_include_tag('ol-debug.js', :plugin => 'redmine_gtt')
1616
tags << javascript_include_tag('ol3-ext.min.js', :plugin => 'redmine_gtt')
1717
tags << javascript_include_tag('app.js', :plugin => 'redmine_gtt')
1818
return tags.join("\n")

0 commit comments

Comments
 (0)