From 7fd88dd1e3968e03d91e23ecdef5688c81259d30 Mon Sep 17 00:00:00 2001 From: fkloft Date: Thu, 12 Feb 2015 16:06:20 +0100 Subject: [PATCH 01/14] Revert "search: draw a marker highlighting search results (rectangle for region, 10px circle for point)" This reverts commit 39e4608c4e1ce8f221af2c70da3d6396cc370262. --- code/geosearch.js | 73 +++++++++++++++++------------------------------ 1 file changed, 26 insertions(+), 47 deletions(-) diff --git a/code/geosearch.js b/code/geosearch.js index 27c8b124..14d9db50 100644 --- a/code/geosearch.js +++ b/code/geosearch.js @@ -4,7 +4,7 @@ window.setupGeosearch = function() { $('#geosearch').keypress(function(e) { if((e.keyCode ? e.keyCode : e.which) != 13) return; - + var search = $(this).val(); if ( window.search(search) ) return; @@ -15,57 +15,36 @@ window.setupGeosearch = function() { map.locate({setView : true, maxZoom: 13}); }); $('#geosearch').keyup(function(){$(this).removeClass('search_not_found')}); - - window.searchResultLayer = L.layerGroup(); - map.addLayer(window.searchResultLayer); } window.search = function(search) { - window.searchResultLayer.clearLayers(); - - var searchMarkerOptions = {color:'red', weight:3, opacity: 0.5, fill:false, dashArray:"5,5", clickable:false}; - - - if (!runHooks('geoSearch', search)) { - return true; - } - - if(search.split(",").length == 2) { - var ll = search.split(","); - if(!isNaN(ll[0]) && !isNaN(ll[1])) { - ll = [parseFloat(ll[0]), parseFloat(ll[1])]; - if(ll[0] >= -90 && ll[0] <= 90 && ll[1] >= -180 && ll[1] <= 180) { - var ll = L.latLng(ll); - window.map.setView(ll, 17); - - window.searchResultLayer.clearLayers(); - window.searchResultLayer.addLayer(L.circleMarker(ll,searchMarkerOptions)); - - if(window.isSmartphone()) window.show('map'); - return true; - } - } - } - - $.getJSON(NOMINATIM + encodeURIComponent(search), function(data) { - if(!data || !data[0]) { - $('#geosearch').addClass('search_not_found'); + if (!runHooks('geoSearch', search)) { return true; } - var b = data[0].boundingbox; - if(!b) return true; - var southWest = new L.LatLng(b[0], b[2]), - northEast = new L.LatLng(b[1], b[3]), - bounds = new L.LatLngBounds(southWest, northEast); - - window.searchResultLayer.clearLayers(); - if (southWest.equals(northEast)) { - window.searchResultLayer.addLayer(L.circleMarker(southWest,searchMarkerOptions)); - } else { - window.searchResultLayer.addLayer(L.rectangle(bounds,searchMarkerOptions)); + + if(search.split(",").length == 2) { + var ll = search.split(","); + if(!isNaN(ll[0]) && !isNaN(ll[1])) { + ll = [parseFloat(ll[0]), parseFloat(ll[1])]; + if(ll[0] >= -90 && ll[0] <= 90 && ll[1] >= -180 && ll[1] <= 180) { + window.map.setView(L.latLng(ll[0], ll[1]), 17); + if(window.isSmartphone()) window.show('map'); + return true; + } + } } - window.map.fitBounds(bounds, {maxZoom: 17}); - if(window.isSmartphone()) window.show('map'); - }); + $.getJSON(NOMINATIM + encodeURIComponent(search), function(data) { + if(!data || !data[0]) { + $('#geosearch').addClass('search_not_found'); + return true; + } + var b = data[0].boundingbox; + if(!b) return true; + var southWest = new L.LatLng(b[0], b[2]), + northEast = new L.LatLng(b[1], b[3]), + bounds = new L.LatLngBounds(southWest, northEast); + window.map.fitBounds(bounds, {maxZoom: 17}); + if(window.isSmartphone()) window.show('map'); + }); } From 4c7b8e0e70d84f1b3ebbacf37c2c4caafc7d7a7d Mon Sep 17 00:00:00 2001 From: fkloft Date: Thu, 12 Feb 2015 14:37:49 +0100 Subject: [PATCH 02/14] new search --- code/boot.js | 2 +- code/geosearch.js | 50 --------- code/hooks.js | 2 +- code/search.js | 267 ++++++++++++++++++++++++++++++++++++++++++++++ main.js | 8 +- style.css | 54 +++++++--- 6 files changed, 312 insertions(+), 71 deletions(-) delete mode 100644 code/geosearch.js create mode 100644 code/search.js diff --git a/code/boot.js b/code/boot.js index 5b1e86ce..1f75b734 100644 --- a/code/boot.js +++ b/code/boot.js @@ -591,7 +591,7 @@ function boot() { window.setupDialogs(); window.setupMap(); window.setupOMS(); - window.setupGeosearch(); + window.search.setup(); window.setupRedeem(); window.setupLargeImagePreview(); window.setupSidebarToggle(); diff --git a/code/geosearch.js b/code/geosearch.js deleted file mode 100644 index 14d9db50..00000000 --- a/code/geosearch.js +++ /dev/null @@ -1,50 +0,0 @@ - -// GEOSEARCH ///////////////////////////////////////////////////////// - -window.setupGeosearch = function() { - $('#geosearch').keypress(function(e) { - if((e.keyCode ? e.keyCode : e.which) != 13) return; - - var search = $(this).val(); - - if ( window.search(search) ) return; - - e.preventDefault(); - }); - $('#geosearchwrapper img').click(function(){ - map.locate({setView : true, maxZoom: 13}); - }); - $('#geosearch').keyup(function(){$(this).removeClass('search_not_found')}); -} - -window.search = function(search) { - if (!runHooks('geoSearch', search)) { - return true; - } - - if(search.split(",").length == 2) { - var ll = search.split(","); - if(!isNaN(ll[0]) && !isNaN(ll[1])) { - ll = [parseFloat(ll[0]), parseFloat(ll[1])]; - if(ll[0] >= -90 && ll[0] <= 90 && ll[1] >= -180 && ll[1] <= 180) { - window.map.setView(L.latLng(ll[0], ll[1]), 17); - if(window.isSmartphone()) window.show('map'); - return true; - } - } - } - - $.getJSON(NOMINATIM + encodeURIComponent(search), function(data) { - if(!data || !data[0]) { - $('#geosearch').addClass('search_not_found'); - return true; - } - var b = data[0].boundingbox; - if(!b) return true; - var southWest = new L.LatLng(b[0], b[2]), - northEast = new L.LatLng(b[1], b[3]), - bounds = new L.LatLngBounds(southWest, northEast); - window.map.fitBounds(bounds, {maxZoom: 17}); - if(window.isSmartphone()) window.show('map'); - }); -} diff --git a/code/hooks.js b/code/hooks.js index 8f05e5ff..2fd82477 100644 --- a/code/hooks.js +++ b/code/hooks.js @@ -59,7 +59,7 @@ window.VALID_HOOKS = [ 'portalAdded', 'linkAdded', 'fieldAdded', 'publicChatDataAvailable', 'factionChatDataAvailable', 'requestFinished', 'nicknameClicked', - 'geoSearch', 'iitcLoaded', + 'geoSearch', 'search', 'iitcLoaded', 'portalDetailLoaded', 'paneChanged']; window.runHooks = function(event, data) { diff --git a/code/search.js b/code/search.js new file mode 100644 index 00000000..77645994 --- /dev/null +++ b/code/search.js @@ -0,0 +1,267 @@ + +// SEARCH ///////////////////////////////////////////////////////// + +/* +you can implement your own result provider by listing to the search hook: +addHook('search', function(query) {}); + +`query` is an object with the following members: +- `term` is the term for which the user has searched +- `confirmed` is a boolean indicating if the user has pressed enter after searching. You should not search online or + heavy processing unless the user has confirmed the search term +- `addResult(result)` can be called to add a result to the query. + +`result` may have the following members (`title` is required, as well as one of `position` and `bounds`): +- `title`: the label for this result +- `description`: secondary information for this result +- `position`: a L.LatLng object describing the position of this result +- `bounds`: a L.LatLngBounds object describing the bounds of this result +- `layer`: a ILayer to be added to the map when the user selects this search result. Will be generated if not set. + Set to `null` to prevent the result from being added to the map. +- `onSelected(result, event)`: a handler to be called when the result is selected. May return `true` to prevent the map + from being repositioned. You may reposition the map yourself or do other work. +- `onRemove(result)`: a handler to be called when the result is removed from the map (because another result has been + selected or the search was cancelled by the user). +*/ + +window.search = { + lastSearch: null, +}; + +window.search.Query = function(term, confirmed) { + this.term = term; + this.confirmed = confirmed; + this.init(); +}; +window.search.Query.prototype.init = function() { + this.results = []; + + this.container = $('
'); + + this.header = $('

') + .text(this.confirmed + ? this.term + : ((this.term.length > 16 + ? this.term.substr(0,8) + "…" + this.term.substr(this.term.length-8,8) + : this.term) + + ' (Return to load more)')) + .appendTo(this.container); + + this.list = $('