Skip to content

Commit

Permalink
SEARCH with scanner link or GUID (#690)
Browse files Browse the repository at this point in the history
Modification to allow pasting of a scanner link into IITC's search.
('%2C' substitution)

---------

Co-authored-by: lejeu <40754851+lejeu@users.noreply.github.com>
Co-authored-by: xscreach <xscreach@users.noreply.github.com>
  • Loading branch information
3 people authored Jan 22, 2024
1 parent 8a48377 commit f9e9f72
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 29 deletions.
79 changes: 50 additions & 29 deletions core/code/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

/*
you can implement your own result provider by listing to the search hook:
addHook('search', function(query) {});
window.addHook('search', function(query) {});
`query` is an object with the following members:
- `term` is the term for which the user has searched
Expand All @@ -25,6 +25,8 @@ addHook('search', function(query) {});
selected or the search was cancelled by the user).
*/

/* global L -- eslint */

window.search = {
lastSearch: null,
};
Expand Down Expand Up @@ -116,7 +118,6 @@ window.search.Query.prototype.addResult = function(result) {
.append($('<em>')
.append(result.description));
}

};

window.search.Query.prototype.resultLayer = function(result) {
Expand Down Expand Up @@ -259,45 +260,50 @@ window.search.setup = function() {
});
};

window.search.addSearchResult = function (query, data, guid) {
var team = window.teamStringToId(data.team);
var color = team === window.TEAM_NONE ? '#CCC' : window.COLORS[team];
var latLng = L.latLng(data.latE6 / 1e6, data.lngE6 / 1e6);
query.addResult({
title: data.title,
description: window.TEAM_SHORTNAMES[team] + ', L' + data.level + ', ' + data.health + '%, ' + data.resCount + ' Resonators',
position: latLng,
icon: 'data:image/svg+xml;base64,' + btoa('@include_string:images/icon-portal.svg@'.replace(/%COLOR%/g, color)),
onSelected: function (result, event) {
if (event.type === 'dblclick') {
window.zoomToAndShowPortal(guid, latLng);
} else if (window.portals[guid]) {
if (!window.map.getBounds().contains(result.position)) {
window.map.setView(result.position);
}
window.renderPortalDetails(guid);
} else {
window.selectPortalByLatLng(latLng);
}
return true; // prevent default behavior
},
});
};

// search for portals
addHook('search', function(query) {
window.addHook('search', function (query) {
var term = query.term.toLowerCase();
var teams = ['NEU','RES','ENL'];

$.each(portals, function(guid, portal) {
var data = portal.options.data;
if(!data.title) return;

if(data.title.toLowerCase().indexOf(term) !== -1) {
var team = portal.options.team;
var color = team==TEAM_NONE ? '#CCC' : COLORS[team];
query.addResult({
title: data.title,
description: teams[team] + ', L' + data.level + ', ' + data.health + '%, ' + data.resCount + ' Resonators',
position: portal.getLatLng(),
icon: 'data:image/svg+xml;base64,'+btoa('@include_string:images/icon-portal.svg@'.replace(/%COLOR%/g, color)),
onSelected: function(result, event) {
if(event.type == 'dblclick') {
zoomToAndShowPortal(guid, portal.getLatLng());
} else if(window.portals[guid]) {
if(!map.getBounds().contains(result.position)) map.setView(result.position);
renderPortalDetails(guid);
} else {
window.selectPortalByLatLng(portal.getLatLng());
}
return true; // prevent default behavior
},
});
window.search.addSearchResult(query, data, guid);
}
});
});


// search for locations
// TODO: recognize 50°31'03.8"N 7°59'05.3"E and similar formats
addHook('search', function(query) {
var locations = query.term.match(/[+-]?\d+\.\d+, ?[+-]?\d+\.\d+/g);
window.addHook('search', function (query) {
var locations = query.term.replaceAll(/%2C/gi, ',').match(/[+-]?\d+\.\d+, ?[+-]?\d+\.\d+/g);
var added = {};
if(!locations) return;
locations.forEach(function(location) {
Expand Down Expand Up @@ -330,7 +336,7 @@ addHook('search', function(query) {


// search on OpenStreetMap
addHook('search', function(query) {
window.addHook('search', function (query) {
if(!query.confirmed) return;

// Viewbox search orders results so they're closer to the viewbox
Expand Down Expand Up @@ -359,7 +365,7 @@ addHook('search', function(query) {
data.forEach(function(item) {
if(resultMap[item.place_id]) { return; } // duplicate
resultMap[item.place_id] = true;

var result = {
title: item.display_name,
description: 'Type: ' + item.type,
Expand Down Expand Up @@ -393,11 +399,26 @@ addHook('search', function(query) {
query.addResult(result);
});
}

// Bounded search allows amenity-only searches (e.g. "amenity=toilet") via special phrases
// http://wiki.openstreetmap.org/wiki/Nominatim/Special_Phrases/EN
var bounded = '&bounded=1';

$.getJSON(NOMINATIM + encodeURIComponent(query.term) + viewbox + bounded, onQueryResult.bind(null, true));
});

// search on guid
window.addHook('search', function (query) {
const guid_re = /[0-9a-f]{32}\.[0-9a-f]{2}/;
const res = query.term.match(guid_re);
if (res) {
const guid = res[0];
const data = window.portalDetail.get(guid);
if (data) window.search.addSearchResult(query, data, guid);
else {
window.portalDetail.request(guid).then(function (data) {
window.search.addSearchResult(query, data, guid);
});
}
}
});
1 change: 1 addition & 0 deletions core/total-conversion-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ window.TEAM_TO_CSS = ['none', 'res', 'enl', 'mac'];
window.TEAM_NAMES = ['Neutral', 'Resistance', 'Enlightened', '__MACHINA__'];
window.TEAM_CODES = ['N', 'R', 'E', 'M'];
window.TEAM_CODENAMES = ['NEUTRAL', 'RESISTANCE', 'ENLIGHTENED', 'MACHINA'];
window.TEAM_SHORTNAMES = ['NEU', 'RES', 'ENL', 'MAC'];

window.TEAM_NAME_NONE = window.TEAM_NAMES[window.TEAM_NONE];
window.TEAM_NAME_RES = window.TEAM_NAMES[window.TEAM_RES];
Expand Down

0 comments on commit f9e9f72

Please sign in to comment.