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 {
position: absolute;
top: 20%;
bottom: 20%;
left: 20%;
right: 20%;
top: 5%;
bottom: 5%;
left: 5%;
right: 5%;
z-index: 99999;
}

View File

@ -1,18 +1,47 @@
/* exported LayerManager */
/* globals RealtimeTileLayer: true */
function LayerManager(layers, map){
function LayerManager(wsChannel, layers, map){
this.listeners = [];
this.currentLayer = layers[0];
this.layers = layers;
this.map = map;
this.layerObjects = {};
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) {
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){
var self = this;
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>');
var layers = {};
var overlays = {};
window.layerMgr = new LayerManager(cfg.layers, map);
window.layerMgr = new LayerManager(wsChannel, cfg.layers, map);
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
Overlaysetup(cfg, map, overlays, wsChannel, layerMgr);
@ -43,7 +32,7 @@ api.getConfig().then(function(cfg){
}
//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);

View File

@ -5,21 +5,32 @@
var SearchInput = {
view: function(){
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" }, [
m("div", { class: "input-group-prepend" }, [
m("span", { class: "input-group-text" }, [
m("i", { class: "fa fa-search"})
])
]),
m("input[type=text]", {
placeholder: "Search",
class: "form-control",
oninput: handleInput,
onkeydown: handleKeyDown,
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 = {};
if (!SearchStore.query) {
if (!SearchStore.show) {
style.display = "none";
}
@ -16,13 +16,21 @@ var SearchMenu = {
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 }, [
m("div", { class: "card-header" }, [
m("i", { class: "fa fa-search"}),
"Search",
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){
var layer = layerMgr.getLayerByY(obj.y);
var text = obj.x + "/" + obj.y + "/" + obj.z;
return m("span", {class:"badge badge-success"}, text);
@ -24,6 +23,21 @@ var SearchResult = {
var description = 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"){
description = m("span", obj.attributes.name);
type = m("img", { src: "css/images/marker-icon.png" });
@ -50,7 +64,12 @@ var SearchResult = {
}
function onclick(){
var layer = layerMgr.getLayerByY(obj.y);
layerMgr.switchLayer(layer.id);
map.setView([obj.z, obj.x], 12);
SearchStore.show = false;
}
return m("tr", {"class": row_classes}, [

View File

@ -3,49 +3,55 @@
var SearchService = {
search: function(q){
SearchStore.query = q;
search: function(){
SearchStore.show = true;
this.fetchData();
},
fetchData: debounce(function(){
fetchData: function(){
SearchStore.result = [];
if (!SearchStore.query){
return;
}
function searchFor(q){
q.pos1 = { x:-2048, y:-2048, z:-2048 };
q.pos2 = { x:2048, y:2048, z:2048 };
return api.getMapObjects(q);
SearchStore.busy = true;
function searchFor(type, key, valuelike){
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({
type: "shop",
attributelike: {
key: "out_item",
value: "%" + SearchStore.query + "%"
}
var prom_list = [];
prom_list.push(searchFor("shop", "out_item", SearchStore.query));
prom_list.push(searchFor("poi", "name", 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(){
SearchStore.query = "";
SearchStore.result = [];
SearchStore.show = false;
}
};

View File

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