Skip to content

Commit f78a44d

Browse files
authored
Merge pull request #39 from aarranz/feature/add-a-setinitialcenter-button
Add a button for setting the initial map location
2 parents 76bd565 + 9cdb7f5 commit f78a44d

File tree

7 files changed

+84
-11
lines changed

7 files changed

+84
-11
lines changed

src/config.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version='1.0' encoding='UTF-8'?>
2-
<widget xmlns="http://wirecloud.conwet.fi.upm.es/ns/macdescription/1" vendor="CoNWeT" name="ol3-map" version="1.2.2b1">
2+
<widget xmlns="http://wirecloud.conwet.fi.upm.es/ns/macdescription/1" vendor="CoNWeT" name="ol3-map" version="1.2.2b2">
33
<details>
44
<title>OpenLayers Map</title>
55
<email>[email protected]</email>

src/css/styles.css

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@ body, html, .map {
55
margin: 0px;
66
}
77

8-
#button {
8+
#buttons {
99
position: absolute;
1010
top: 5px;
1111
right: 5px;
12+
display: flex;
13+
flex-direction: column;
14+
justify-content: center;
15+
align-items: center;
1216
}

src/doc/changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
- Use PoI id for the selection popup menu if the PoI does not provide a title
44
attribute.
5+
- Added a button to set the Initial Location setting using the center of the
6+
current displayed area.
57

68

79
## v1.2.1 (2020-03-07)

src/doc/userguide.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Map viewer widget using OpenLayers. It can receive Layers or Point of Interest d
66
## Settings
77

88

9-
- **Initial Location**: Decimal coordinates where map will be centered on load (e.g. `52, 5`). Leave this setting empty if you don't want to center the map at init. Remember to change the initial zoom level if you provide an initial location.
9+
- **Initial Location**: Decimal coordinates where map will be centered on load (e.g. `52, 5`). Leave this setting empty if you don't want to center the map at init. Remember to change the initial zoom level if you provide an initial location. This setting can be configured using the <i class="fas fa-crosshairs"></i> button that is displayed inside the widget when in editing mode.
1010
- **Initial Zoom Level**: Initial zoom level. From 1 to 22, where '1' represents the furthest level and '22' the maximum zoom level.
1111
- **Min Zoom**: Minimal zoom level.
1212
- **Layers Widget**: Widget to use for allowing user to switch between layers.

src/index.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99
</head>
1010
<body>
1111
<div id="map" class="map"></div>
12-
<div id="button" class="se-btn hidden"><span>Layers</span></div>
12+
<div id="buttons">
13+
<div id="button" class="se-btn fade fas fa-layer-group"></div>
14+
<div id="setcenter-button" class="se-btn hidden fas fa-crosshairs"></div>
15+
</div>
1316
</body>
1417
<script type="text/javascript" src="js/ol3-map-widget.js"></script>
1518
<script type="text/javascript" src="js/main.js"></script>

src/js/ol3-map-widget.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* Copyright (c) 2017 CoNWeT Lab., Universidad Politecnica de Madrid
3-
* Copyright (c) 2017-2019 Future Internet Consulting and Development Solutions S.L.
3+
* Copyright (c) 2017-2021 Future Internet Consulting and Development Solutions S.L.
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -261,11 +261,33 @@
261261
});
262262
let layers_widget_ref = MashupPlatform.prefs.get('layerswidget').trim();
263263
if (layers_widget_ref === "") {
264-
layers_button.classList.add('hidden');
264+
layers_button.classList.remove('in');
265265
} else {
266-
layers_button.classList.remove('hidden');
266+
layers_button.classList.add('in');
267267
}
268268

269+
// Set position button
270+
const setcenter_button = document.getElementById("setcenter-button");
271+
setcenter_button.addEventListener('click', (event) => {
272+
const currentCenter = this.map.getView().getCenter();
273+
const newValue = ol.proj.transform(currentCenter, 'EPSG:3857', 'EPSG:4326');
274+
MashupPlatform.prefs.set(
275+
"initialCenter",
276+
newValue.join(", ")
277+
);
278+
});
279+
const update_ui_buttons = (changes) => {
280+
// Use strict equality as changes can not contains changes on the
281+
// editing parameter
282+
if (changes.editing === true) {
283+
setcenter_button.classList.remove("hidden");
284+
} else if (changes.editing === false) {
285+
setcenter_button.classList.add("hidden");
286+
}
287+
};
288+
MashupPlatform.mashup.context.registerCallback(update_ui_buttons);
289+
update_ui_buttons({editing: MashupPlatform.mashup.context.get("editing")});
290+
269291
DEFAULT_MARKER = build_basic_style.call(this);
270292
this.base_layer = CORE_LAYERS.OSM;
271293
var initialCenter = MashupPlatform.prefs.get("initialCenter").split(",").map(Number);

tests/js/OpenlayersWidgetSpec.js

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* Copyright (c) 2017 CoNWeT Lab., Universidad Politecnica de Madrid
3-
* Copyright (c) 2017-2019 Future Internet Consulting and Development Solutions S.L.
3+
* Copyright (c) 2017-2021 Future Internet Consulting and Development Solutions S.L.
44
*/
55

66
/* global MashupPlatform, MockMP, ol, Widget */
@@ -10,7 +10,7 @@
1010
"use strict";
1111

1212
const HTML_FIXTURE = '<div id="map" class="map"></div>\n' +
13-
'<div id="button" class="se-btn"><span>Capas</span></div>';
13+
'<div id="button" class="se-btn fade"></div><div id="setcenter-button" class="se-btn"></div>';
1414

1515
const clearDocument = function clearDocument() {
1616
var elements = document.querySelectorAll('body > *:not(.jasmine_html-reporter)');
@@ -147,7 +147,7 @@
147147
widget.init();
148148

149149
let layers_button = document.getElementById('button');
150-
expect(layers_button.className).toBe('se-btn hidden');
150+
expect(layers_button.className).toBe('se-btn fade');
151151
});
152152

153153
it("widget ref", () => {
@@ -156,7 +156,7 @@
156156
widget.init();
157157

158158
let layers_button = document.getElementById('button');
159-
expect(layers_button.className).toBe('se-btn');
159+
expect(layers_button.className).toBe('se-btn fade in');
160160
});
161161

162162
it("widget ref (click)", () => {
@@ -219,6 +219,48 @@
219219

220220
});
221221

222+
describe("setcenter button", () => {
223+
224+
it("hidden if not started editing", () => {
225+
MashupPlatform.mashup.context.setContext({editing: false});
226+
227+
widget.init();
228+
229+
const setcenter_button = document.getElementById('setcenter-button');
230+
expect(setcenter_button.className).toBe('se-btn hidden');
231+
});
232+
233+
it("visible if started editing", () => {
234+
MashupPlatform.mashup.context.setContext({editing: true});
235+
236+
widget.init();
237+
238+
const setcenter_button = document.getElementById('setcenter-button');
239+
expect(setcenter_button.className).toBe('se-btn');
240+
});
241+
242+
it("should update dynamically", () => {
243+
MashupPlatform.mashup.context.setContext({editing: false});
244+
widget.init();
245+
246+
MashupPlatform.mashup.context.registerCallback.calls.argsFor(0)[0]({editing: true});
247+
248+
const setcenter_button = document.getElementById('setcenter-button');
249+
expect(setcenter_button.className).toBe('se-btn');
250+
});
251+
252+
it("should setup current center as the default value for the initialCenter setting", () => {
253+
MashupPlatform.mashup.context.setContext({editing: true});
254+
widget.init();
255+
const setcenter_button = document.getElementById('setcenter-button');
256+
257+
setcenter_button.click();
258+
259+
expect(MashupPlatform.prefs.set).toHaveBeenCalledWith("initialCenter", jasmine.any(String));
260+
});
261+
262+
});
263+
222264
describe("useclustering", () => {
223265

224266
it("should switch to use a cluster source for the main vector layer", () => {

0 commit comments

Comments
 (0)