From a8d736ac7ba0aa6ba9caadfb40d5a04a766d3c31 Mon Sep 17 00:00:00 2001
From: Matthias Schiffer <mschiffer@universe-factory.net>
Date: Sun, 19 Aug 2018 17:42:48 +0200
Subject: [PATCH] gluon-config-mode-geo-location-osm: add OSM-based location
 picker

gluon-config-mode-geo-location-osm extends the
gluon-config-mode-geo-location with a location picker based on
OpenStreetMaps.

Based-on-patch-by: Jan-Tarek Butt <tarek@ring0.de>
---
 docs/user/site.rst                            | 15 +++++++++++
 .../Makefile                                  | 20 ++++++++++++++
 .../check_site.lua                            |  4 +++
 .../i18n/de.po                                | 17 ++++++++++++
 .../gluon-config-mode-geo-location-osm.pot    |  7 +++++
 .../gluon/config-mode/geo-location-osm.lua    | 26 +++++++++++++++++++
 .../gluon-config-mode-geo-location/i18n/de.po | 12 ++++-----
 .../gluon-config-mode-geo-location/i18n/fr.po | 10 +++----
 .../i18n/gluon-config-mode-geo-location.pot   |  6 ++---
 .../config-mode/wizard/0400-geo-location.lua  | 16 +++++++++++-
 10 files changed, 117 insertions(+), 16 deletions(-)
 create mode 100644 package/gluon-config-mode-geo-location-osm/Makefile
 create mode 100644 package/gluon-config-mode-geo-location-osm/check_site.lua
 create mode 100644 package/gluon-config-mode-geo-location-osm/i18n/de.po
 create mode 100644 package/gluon-config-mode-geo-location-osm/i18n/gluon-config-mode-geo-location-osm.pot
 create mode 100644 package/gluon-config-mode-geo-location-osm/luasrc/usr/lib/lua/gluon/config-mode/geo-location-osm.lua

diff --git a/docs/user/site.rst b/docs/user/site.rst
index 85d69cbdd..3f24970d6 100644
--- a/docs/user/site.rst
+++ b/docs/user/site.rst
@@ -407,6 +407,13 @@ config_mode \: optional
     package. Set *geo_location.show_altitude* to *true* if you want the altitude
     field to be visible.
 
+    The *geo_location.osm* section is only relevant when the *gluon-config-mode-geo-location-osm*
+    package is used. The *center.lon* and *center.lat* values are mandatory in this case and
+    define the default center of the map when no position has been picked yet. The *zoom* level
+    defaults to 12 in this case. *openlayers_url* allows to override the base URL of the
+    *build/ol.js* and *css/ol.css* files (the default is
+    ``https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.2.0``).
+
     The remote login page only shows SSH key configuration by default. A
     password form can be displayed by setting *remote_login.show_password_form*
     to true; in this case, *remote_login.min_password_length* defines the
@@ -419,6 +426,14 @@ config_mode \: optional
           },
           geo_location = {
             show_altitude = true,
+            osm = {
+              center = {
+                lon = 52.951947558,
+                lat = 7.844238281,
+              },
+              zoom = 13,
+              openlayers_url = 'http://ffac/ol',
+            },
           },
           remote_login = {
             show_password_form = true,
diff --git a/package/gluon-config-mode-geo-location-osm/Makefile b/package/gluon-config-mode-geo-location-osm/Makefile
new file mode 100644
index 000000000..4dbaf9d00
--- /dev/null
+++ b/package/gluon-config-mode-geo-location-osm/Makefile
@@ -0,0 +1,20 @@
+include $(TOPDIR)/rules.mk
+
+PKG_NAME:=gluon-config-mode-geo-location-osm
+PKG_VERSION:=1
+
+include ../gluon.mk
+
+define Package/gluon-config-mode-geo-location-osm
+  TITLE:=Set geographic location of a node (map support)
+  DEPENDS:=+gluon-config-mode-geo-location +gluon-web-osm
+endef
+
+define Package/gluon-config-mode-geo-location-osm/install
+	$(Gluon/Build/Install)
+
+	$(INSTALL_DIR) $(1)/lib/gluon/config-mode/www/static/
+	$(LN) /lib/gluon/web/www/static/gluon-web-osm.js $(1)/lib/gluon/config-mode/www/static/
+endef
+
+$(eval $(call BuildPackageGluon,gluon-config-mode-geo-location-osm))
diff --git a/package/gluon-config-mode-geo-location-osm/check_site.lua b/package/gluon-config-mode-geo-location-osm/check_site.lua
new file mode 100644
index 000000000..8e69ba0a1
--- /dev/null
+++ b/package/gluon-config-mode-geo-location-osm/check_site.lua
@@ -0,0 +1,4 @@
+need_number(in_site({'config_mode', 'geo_location', 'osm', 'center', 'lon'}))
+need_number(in_site({'config_mode', 'geo_location', 'osm', 'center', 'lat'}))
+need_number(in_site({'config_mode', 'geo_location', 'osm', 'zoom'}), false)
+need_string(in_site({'config_mode', 'geo_location', 'osm', 'openlayers_url'}), false)
diff --git a/package/gluon-config-mode-geo-location-osm/i18n/de.po b/package/gluon-config-mode-geo-location-osm/i18n/de.po
new file mode 100644
index 000000000..dc0e23dfd
--- /dev/null
+++ b/package/gluon-config-mode-geo-location-osm/i18n/de.po
@@ -0,0 +1,17 @@
+msgid ""
+msgstr ""
+"Content-Type: text/plain; charset=UTF-8\n"
+"Project-Id-Version: gluon-config-mode-geo-location\n"
+"Last-Translator: Matthias Schiffer <mschiffer@universe-factory.net>\n"
+"Language-Team: German\n"
+"Language: de\n"
+"MIME-Version: 1.0\n"
+"Content-Transfer-Encoding: 8bit\n"
+"Plural-Forms: nplurals=2; plural=(n != 1);\n"
+
+msgid ""
+"You may also select the position on the map displayed below if your computer "
+"is connected to the internet at the moment."
+msgstr ""
+"Wenn dein Computer aktuell mit dem Internet verbunden ist, kann die Position "
+"auch auf der hier angezeigten Karte ausgewählt werden."
diff --git a/package/gluon-config-mode-geo-location-osm/i18n/gluon-config-mode-geo-location-osm.pot b/package/gluon-config-mode-geo-location-osm/i18n/gluon-config-mode-geo-location-osm.pot
new file mode 100644
index 000000000..0a987630f
--- /dev/null
+++ b/package/gluon-config-mode-geo-location-osm/i18n/gluon-config-mode-geo-location-osm.pot
@@ -0,0 +1,7 @@
+msgid ""
+msgstr "Content-Type: text/plain; charset=UTF-8"
+
+msgid ""
+"You may also select the position on the map displayed below if your computer "
+"is connected to the internet at the moment."
+msgstr ""
diff --git a/package/gluon-config-mode-geo-location-osm/luasrc/usr/lib/lua/gluon/config-mode/geo-location-osm.lua b/package/gluon-config-mode-geo-location-osm/luasrc/usr/lib/lua/gluon/config-mode/geo-location-osm.lua
new file mode 100644
index 000000000..4969f1ac8
--- /dev/null
+++ b/package/gluon-config-mode-geo-location-osm/luasrc/usr/lib/lua/gluon/config-mode/geo-location-osm.lua
@@ -0,0 +1,26 @@
+local osm = require 'gluon.web.model.osm'
+local site = require 'gluon.site'
+
+local tonumber = tonumber
+
+
+module 'gluon.config-mode.geo-location-osm'
+
+MapValue = osm.MapValue
+
+function help(i18n)
+	local pkg_i18n = i18n 'gluon-config-mode-geo-location-osm'
+	return pkg_i18n.translate(
+		'You may also select the position on the map displayed below if your computer is connected to the internet at the moment.'
+	)
+end
+
+function options()
+	local config = site.config_mode.geo_location.osm
+
+	return {
+		openlayers_url = config.openlayers_url(),
+		zoom = config.zoom(12),
+		pos = config.center(),
+	}
+end
diff --git a/package/gluon-config-mode-geo-location/i18n/de.po b/package/gluon-config-mode-geo-location/i18n/de.po
index 1ef10a2b0..0ff3d4339 100644
--- a/package/gluon-config-mode-geo-location/i18n/de.po
+++ b/package/gluon-config-mode-geo-location/i18n/de.po
@@ -14,11 +14,11 @@ msgid "Altitude"
 msgstr "Höhe"
 
 msgid ""
-"If you want the location of your node to be displayed on the map, you can "
-"enter its coordinates here."
+"If you want the location of your node to be displayed on public maps, you "
+"can enter its coordinates here."
 msgstr ""
-"Um deinen Knoten auf der Karte anzeigen zu können, benötigen wir seine "
-"Koordinaten. Hier hast du die Möglichkeit, diese zu hinterlegen."
+"Um deinen Knoten auf öffentlichen Karten anzeigen zu können, benötigen wir "
+"seine Koordinaten. Hier hast du die Möglichkeit, diese zu hinterlegen."
 
 msgid "Latitude"
 msgstr "Breitengrad"
@@ -26,8 +26,8 @@ msgstr "Breitengrad"
 msgid "Longitude"
 msgstr "Längengrad"
 
-msgid "Show node on the map"
-msgstr "Knoten auf der Karte anzeigen"
+msgid "Advertise node position"
+msgstr "Knotenposition veröffentlichen"
 
 msgid ""
 "Specifying the altitude is optional; it should only be filled in if an "
diff --git a/package/gluon-config-mode-geo-location/i18n/fr.po b/package/gluon-config-mode-geo-location/i18n/fr.po
index b66aacb21..5a725cb68 100644
--- a/package/gluon-config-mode-geo-location/i18n/fr.po
+++ b/package/gluon-config-mode-geo-location/i18n/fr.po
@@ -14,11 +14,9 @@ msgid "Altitude"
 msgstr "Hauteur"
 
 msgid ""
-"If you want the location of your node to be displayed on the map, you can "
-"enter its coordinates here."
+"If you want the location of your node to be displayed on public maps, you "
+"can enter its coordinates here."
 msgstr ""
-"Pour Afficher votre nœud sur la Carte nous avons besoin de ses coordonnées. "
-"Ici vous pouvez entrer sa position."
 
 msgid "Latitude"
 msgstr "Latitude"
@@ -26,8 +24,8 @@ msgstr "Latitude"
 msgid "Longitude"
 msgstr "Longitude"
 
-msgid "Show node on the map"
-msgstr "Afficher le nœud sur la carte"
+msgid "Advertise node position"
+msgstr ""
 
 msgid ""
 "Specifying the altitude is optional; it should only be filled in if an "
diff --git a/package/gluon-config-mode-geo-location/i18n/gluon-config-mode-geo-location.pot b/package/gluon-config-mode-geo-location/i18n/gluon-config-mode-geo-location.pot
index 04bbd3648..27ab905d6 100644
--- a/package/gluon-config-mode-geo-location/i18n/gluon-config-mode-geo-location.pot
+++ b/package/gluon-config-mode-geo-location/i18n/gluon-config-mode-geo-location.pot
@@ -5,8 +5,8 @@ msgid "Altitude"
 msgstr ""
 
 msgid ""
-"If you want the location of your node to be displayed on the map, you can "
-"enter its coordinates here."
+"If you want the location of your node to be displayed on public maps, you "
+"can enter its coordinates here."
 msgstr ""
 
 msgid "Latitude"
@@ -15,7 +15,7 @@ msgstr ""
 msgid "Longitude"
 msgstr ""
 
-msgid "Show node on the map"
+msgid "Advertise node position"
 msgstr ""
 
 msgid ""
diff --git a/package/gluon-config-mode-geo-location/luasrc/lib/gluon/config-mode/wizard/0400-geo-location.lua b/package/gluon-config-mode-geo-location/luasrc/lib/gluon/config-mode/wizard/0400-geo-location.lua
index 2e5111bd0..6a211b643 100644
--- a/package/gluon-config-mode-geo-location/luasrc/lib/gluon/config-mode/wizard/0400-geo-location.lua
+++ b/package/gluon-config-mode-geo-location/luasrc/lib/gluon/config-mode/wizard/0400-geo-location.lua
@@ -4,6 +4,9 @@ return function(form, uci)
 
 	local site = require 'gluon.site'
 
+	local osm
+	pcall(function() osm = require 'gluon.config-mode.geo-location-osm' end)
+
 	local location = uci:get_first("gluon-node-info", "location")
 
 	local show_altitude = site.config_mode.geo_location.show_altitude(false)
@@ -14,6 +17,9 @@ return function(form, uci)
 			'If you want the location of your node to ' ..
 			'be displayed on the map, you can enter its coordinates here.'
 		)
+		if osm then
+			text = text .. ' ' .. osm.help(i18n)
+		end
 		if show_altitude then
 			text = text .. ' ' .. pkg_i18n.translate(
 				'Specifying the altitude is optional; it should only be filled in if an accurate ' ..
@@ -26,7 +32,7 @@ return function(form, uci)
 
 	local o
 
-	local share_location = s:option(Flag, "location", pkg_i18n.translate("Show node on the map"))
+	local share_location = s:option(Flag, "location", pkg_i18n.translate("Advertise node position"))
 	share_location.default = uci:get_bool("gluon-node-info", location, "share_location")
 	function share_location:write(data)
 		uci:set("gluon-node-info", location, "share_location", data)
@@ -37,6 +43,12 @@ return function(form, uci)
 		end
 	end
 
+	local map = {}
+	if osm then
+		map = s:option(osm.MapValue, "map", osm.options())
+		map:depends(share_location, true)
+	end
+
 	o = s:option(Value, "latitude", pkg_i18n.translate("Latitude"), pkg_i18n.translatef("e.g. %s", "53.873621"))
 	o.default = uci:get("gluon-node-info", location, "latitude")
 	o:depends(share_location, true)
@@ -44,6 +56,7 @@ return function(form, uci)
 	function o:write(data)
 		uci:set("gluon-node-info", location, "latitude", data)
 	end
+	map.lat = o
 
 	o = s:option(Value, "longitude", pkg_i18n.translate("Longitude"), pkg_i18n.translatef("e.g. %s", "10.689901"))
 	o.default = uci:get("gluon-node-info", location, "longitude")
@@ -52,6 +65,7 @@ return function(form, uci)
 	function o:write(data)
 		uci:set("gluon-node-info", location, "longitude", data)
 	end
+	map.lon = o
 
 	if show_altitude then
 		o = s:option(Value, "altitude",
-- 
GitLab