1
0
forked from MTSR/mapserver

improved search

This commit is contained in:
Thomas Rudin 2019-04-21 21:51:43 +02:00
parent 4e95a9490e
commit 2fb64772f4
8 changed files with 120 additions and 56 deletions

View File

@ -18,9 +18,9 @@ body {
#search-menu { #search-menu {
position: absolute; position: absolute;
top: 20%; top: 5%;
bottom: 20%; bottom: 5%;
left: 20%; left: 5%;
right: 20%; right: 5%;
z-index: 99999; z-index: 99999;
} }

View File

@ -1,18 +1,47 @@
/* exported LayerManager */ /* exported LayerManager */
/* globals RealtimeTileLayer: true */
function LayerManager(layers, map){ function LayerManager(wsChannel, layers, map){
this.listeners = []; this.listeners = [];
this.currentLayer = layers[0]; this.currentLayer = layers[0];
this.layers = layers; this.layers = layers;
this.map = map;
this.layerObjects = {};
var self = this; var self = this;
//All layers
layers.forEach(function(layer){
var tileLayer = new RealtimeTileLayer(wsChannel, layer.id, map);
self.layerObjects[layer.name] = tileLayer;
});
map.on('baselayerchange', function (e) { map.on('baselayerchange', function (e) {
self.setLayerId(e.layer.layerId); self.setLayerId(e.layer.layerId);
}); });
//current layer
var currentLayer = this.getCurrentLayer();
this.layerObjects[currentLayer.name].addTo(map);
} }
LayerManager.prototype.switchLayer = function(layerId){
var self = this;
Object.keys(this.layerObjects).forEach(function(key){
var layerObj = self.layerObjects[key];
if (self.map.hasLayer(layerObj)){
self.map.removeLayer(layerObj);
}
});
Object.keys(this.layerObjects).forEach(function(key){
var layerObj = self.layerObjects[key];
if (layerObj.layerId == layerId){
self.map.addLayer(layerObj);
}
});
};
LayerManager.prototype.setLayerId = function(layerId){ LayerManager.prototype.setLayerId = function(layerId){
var self = this; var self = this;
this.layers.forEach(function(layer){ this.layers.forEach(function(layer){

View File

@ -15,22 +15,11 @@ api.getConfig().then(function(cfg){
map.attributionControl.addAttribution('<a href="https://github.com/thomasrudin-mt/mapserver">Minetest Mapserver</a>'); map.attributionControl.addAttribution('<a href="https://github.com/thomasrudin-mt/mapserver">Minetest Mapserver</a>');
var layers = {};
var overlays = {}; var overlays = {};
window.layerMgr = new LayerManager(cfg.layers, map); window.layerMgr = new LayerManager(wsChannel, cfg.layers, map);
layerMgr.setLayerId( Hashroute.getLayerId() ); layerMgr.setLayerId( Hashroute.getLayerId() );
//All layers
cfg.layers.forEach(function(layer){
var tileLayer = new RealtimeTileLayer(wsChannel, layer.id, map);
layers[layer.name] = tileLayer;
});
//current layer
var currentLayer = layerMgr.getCurrentLayer();
layers[currentLayer.name].addTo(map);
//All overlays //All overlays
Overlaysetup(cfg, map, overlays, wsChannel, layerMgr); Overlaysetup(cfg, map, overlays, wsChannel, layerMgr);
@ -43,7 +32,7 @@ api.getConfig().then(function(cfg){
} }
//layer control //layer control
L.control.layers(layers, overlays, { position: "topright" }).addTo(map); L.control.layers(layerMgr.layerObjects, overlays, { position: "topright" }).addTo(map);
Hashroute.setup(map, layerMgr); Hashroute.setup(map, layerMgr);

View File

@ -5,21 +5,32 @@
var SearchInput = { var SearchInput = {
view: function(){ view: function(){
function handleInput(e){ function handleInput(e){
SearchService.search(e.target.value); SearchStore.query = e.target.value;
}
function handleKeyDown(e){
if (e.keyCode == 13){
SearchService.search();
}
}
function handleDoSearch(){
SearchService.search();
} }
return m("div", { class: "input-group mb-3" }, [ return m("div", { class: "input-group mb-3" }, [
m("div", { class: "input-group-prepend" }, [
m("span", { class: "input-group-text" }, [
m("i", { class: "fa fa-search"})
])
]),
m("input[type=text]", { m("input[type=text]", {
placeholder: "Search", placeholder: "Search",
class: "form-control", class: "form-control",
oninput: handleInput, oninput: handleInput,
onkeydown: handleKeyDown,
value: SearchStore.query value: SearchStore.query
}) }),
m("div", { class: "input-group-append", onclick: handleDoSearch }, [
m("span", { class: "input-group-text" }, [
m("i", { class: "fa fa-search"})
])
])
]); ]);
} }
}; };

View File

@ -8,7 +8,7 @@ var SearchMenu = {
var style = {}; var style = {};
if (!SearchStore.query) { if (!SearchStore.show) {
style.display = "none"; style.display = "none";
} }
@ -16,13 +16,21 @@ var SearchMenu = {
SearchService.clear(); SearchService.clear();
} }
function getContent(){
if (SearchStore.busy){
return m("div", m("i", { class: "fa fa-spinner"}));
} else {
return m(SearchResult, { map: vnode.attrs.map });
}
}
return m("div", { class: "card", id: "search-menu", style: style }, [ return m("div", { class: "card", id: "search-menu", style: style }, [
m("div", { class: "card-header" }, [ m("div", { class: "card-header" }, [
m("i", { class: "fa fa-search"}), m("i", { class: "fa fa-search"}),
"Search", "Search",
m("i", { class: "fa fa-times float-right", onclick: close }), m("i", { class: "fa fa-times float-right", onclick: close }),
]), ]),
m("div", { class: "card-body", style: {overflow: "auto"} }, m(SearchResult, { map: vnode.attrs.map })) m("div", { class: "card-body", style: {overflow: "auto"} }, getContent())
]); ]);
} }
}; };

View File

@ -12,7 +12,6 @@ var SearchResult = {
} }
function getPos(obj){ function getPos(obj){
var layer = layerMgr.getLayerByY(obj.y);
var text = obj.x + "/" + obj.y + "/" + obj.z; var text = obj.x + "/" + obj.y + "/" + obj.z;
return m("span", {class:"badge badge-success"}, text); return m("span", {class:"badge badge-success"}, text);
@ -24,6 +23,21 @@ var SearchResult = {
var description = obj.type; var description = obj.type;
var type = obj.type; var type = obj.type;
if (obj.type == "train"){
description = [
m("span", obj.attributes.station),
" ",
m("span", {class:"badge badge-info"}, obj.attributes.line)
];
type = m("i", { class: "fa fa-subway" });
}
if (obj.type == "travelnet"){
description = m("span", obj.attributes.station_name);
type = m("img", { src: "pics/travelnet_inv.png" });
}
if (obj.type == "poi"){ if (obj.type == "poi"){
description = m("span", obj.attributes.name); description = m("span", obj.attributes.name);
type = m("img", { src: "css/images/marker-icon.png" }); type = m("img", { src: "css/images/marker-icon.png" });
@ -50,7 +64,12 @@ var SearchResult = {
} }
function onclick(){ function onclick(){
var layer = layerMgr.getLayerByY(obj.y);
layerMgr.switchLayer(layer.id);
map.setView([obj.z, obj.x], 12); map.setView([obj.z, obj.x], 12);
SearchStore.show = false;
} }
return m("tr", {"class": row_classes}, [ return m("tr", {"class": row_classes}, [

View File

@ -3,49 +3,55 @@
var SearchService = { var SearchService = {
search: function(q){ search: function(){
SearchStore.query = q; SearchStore.show = true;
this.fetchData(); this.fetchData();
}, },
fetchData: debounce(function(){ fetchData: function(){
SearchStore.result = []; SearchStore.result = [];
if (!SearchStore.query){ if (!SearchStore.query){
return; return;
} }
function searchFor(q){ SearchStore.busy = true;
q.pos1 = { x:-2048, y:-2048, z:-2048 };
q.pos2 = { x:2048, y:2048, z:2048 }; function searchFor(type, key, valuelike){
return api.getMapObjects(q); return api.getMapObjects({
pos1: { x:-2048, y:-2048, z:-2048 },
pos2: { x:2048, y:2048, z:2048 },
type: type,
attributelike: {
key: key,
value: "%" + valuelike +"%"
}
});
} }
var shop_prom = searchFor({ var prom_list = [];
type: "shop",
attributelike: { prom_list.push(searchFor("shop", "out_item", SearchStore.query));
key: "out_item", prom_list.push(searchFor("poi", "name", SearchStore.query));
value: "%" + SearchStore.query + "%" prom_list.push(searchFor("train", "station", SearchStore.query));
} prom_list.push(searchFor("travelnet", "station_name", SearchStore.query));
Promise.all(prom_list)
.then(function(results){
var arr = [];
results.forEach(function(r) {
arr = arr.concat(r);
});
SearchStore.result = arr;
SearchStore.busy = false;
}); });
var poi_prom = searchFor({ },
type: "poi",
attributelike: {
key: "name",
value: "%" + SearchStore.query + "%"
}
});
Promise.all([shop_prom, poi_prom]).then(function(results){
SearchStore.result = results[1].concat(results[0]);
});
}, 400),
clear: function(){ clear: function(){
SearchStore.query = "";
SearchStore.result = []; SearchStore.result = [];
SearchStore.show = false;
} }
}; };

View File

@ -2,5 +2,7 @@
var SearchStore = { var SearchStore = {
query: "", query: "",
show: false,
busy: false,
result: [] result: []
}; };