');
$('.drawtools-alert').delay(2500).fadeOut();
}
window.plugin.drawTools.optCopy = function() {
if (typeof android !== 'undefined' && android && android.shareString) {
android.shareString(localStorage['plugin-drawTools-data']);
} else {
var stockWarnings = {};
var stockLinks = [];
window.plugin.drawTools.drawnItems.eachLayer( function(layer) {
if (layer instanceof L.GeodesicCircle || layer instanceof L.Circle) {
stockWarnings.noCircle = true;
return; //.eachLayer 'continue'
} else if (layer instanceof L.GeodesicPolygon || layer instanceof L.Polygon) {
stockWarnings.polyAsLine = true;
// but we can export them
} else if (layer instanceof L.GeodesicPolyline || layer instanceof L.Polyline) {
// polylines are fine
} else if (layer instanceof L.Marker) {
stockWarnings.noMarker = true;
return; //.eachLayer 'continue'
} else {
stockWarnings.unknown = true; //should never happen
return; //.eachLayer 'continue'
}
// only polygons and polylines make it through to here
var latLngs = layer.getLatLngs();
// stock only handles one line segment at a time
for (var i=0; i40) stockWarnTexts.push('Warning: Stock intel may not work with more than 40 line segments - there are '+stockLinks.length);
if (stockWarnings.noCircle) stockWarnTexts.push('Warning: Circles cannot be exported to stock intel');
if (stockWarnings.noMarker) stockWarnTexts.push('Warning: Markers cannot be exported to stock intel');
if (stockWarnings.unknown) stockWarnTexts.push('Warning: UNKNOWN ITEM TYPE');
var html = '
or, export as a link for the standard intel map (for non IITC users)
'
+'';
if (stockWarnTexts.length>0) {
html += '
'+stockWarnTexts.join('
')+'
';
}
dialog({
html: html,
width: 600,
dialogClass: 'ui-dialog-drawtoolsSet-copy',
title: 'Draw Tools Export'
});
}
}
window.plugin.drawTools.optExport = function() {
if(typeof android !== 'undefined' && android && android.saveFile) {
android.saveFile('IITC-drawn-items.json', 'application/json', localStorage['plugin-drawTools-data']);
}
}
window.plugin.drawTools.optPaste = function() {
var promptAction = prompt('Press CTRL+V to paste (draw-tools data or stock intel URL).', '');
if(promptAction !== null && promptAction !== '') {
try {
// first see if it looks like a URL-format stock intel link, and if so, try and parse out any stock drawn items
// from the pls parameter
if (promptAction.match(new RegExp("^(https?://)?(www\\.)?ingress\\.com/intel.*[?&]pls="))) {
//looks like a ingress URL that has drawn items...
var items = promptAction.split(/[?&]/);
var foundAt = -1;
for (var i=0; iImport failed');
}
}
}
window.plugin.drawTools.optImport = function() {
if (window.requestFile === undefined) return;
window.requestFile(function(filename, content) {
try {
var data = JSON.parse(content);
window.plugin.drawTools.resetDrawings();
window.plugin.drawTools.importMapping(data);
console.log('DRAWTOOLS: reset and imported drawn items');
window.plugin.drawTools.optAlert('Import Successful.');
// to write back the data to localStorage
window.plugin.drawTools.save();
} catch(e) {
console.warn('DRAWTOOLS: failed to import data: '+e);
window.plugin.drawTools.optAlert('Import failed');
}
});
}
window.plugin.drawTools.optReset = function() {
var promptAction = confirm('All drawn items will be deleted. Are you sure?', '');
if(promptAction) {
window.plugin.drawTools.resetDrawings();
window.plugin.drawTools.optAlert('Reset Successful. ');
runHooks('pluginDrawTools', {event: 'clear'});
}
}
window.plugin.drawTools.snapToPortals = function() {
var dataParams = getMapZoomTileParameters(getDataZoomForMapZoom(map.getZoom()));
if (dataParams.level > 0) {
if (!confirm('Not all portals are visible on the map. Snap to portals may move valid points to the wrong place. Continue?')) {
return;
}
}
if (mapDataRequest.status.short != 'done') {
if (!confirm('Map data has not completely loaded, so some portals may be missing. Do you want to continue?')) {
return;
}
}
var visibleBounds = map.getBounds();
// let's do all the distance calculations in screen space. 2d is much easier, should be faster, and is more than good enough
// we'll pre-project all the on-screen portals too, to save repeatedly doing it
var visiblePortals = {};
$.each(window.portals, function(guid,portal) {
var ll = portal.getLatLng();
if (visibleBounds.contains(ll)) {
visiblePortals[guid] = map.project(ll);
}
});
if (Object.keys(visiblePortals).length == 0) {
alert('Error: No portals visible in this view - nothing to snap points to!');
return;
}
var findClosestPortalLatLng = function(latlng) {
var testpoint = map.project(latlng);
var minDistSquared = undefined;
var minGuid = undefined;
for (var guid in visiblePortals) {
var p = visiblePortals[guid];
var distSquared = (testpoint.x-p.x)*(testpoint.x-p.x) + (testpoint.y-p.y)*(testpoint.y-p.y);
if (minDistSquared == undefined || minDistSquared > distSquared) {
minDistSquared = distSquared;
minGuid = guid;
}
}
return minGuid ? portals[minGuid].getLatLng() : undefined; //should never hit 'undefined' case - as we abort when the list is empty
};
var changedCount = 0;
var testCount = 0;
window.plugin.drawTools.drawnItems.eachLayer(function(layer) {
if (layer.getLatLng) {
//circles and markers - a single point to snap
var ll = layer.getLatLng();
if (visibleBounds.contains(ll)) {
testCount++;
var newll = findClosestPortalLatLng(ll);
if (!newll.equals(ll)) {
layer.setLatLng(new L.LatLng(newll.lat,newll.lng));
changedCount++;
}
}
} else if (layer.getLatLngs) {
var lls = layer.getLatLngs();
var layerChanged = false;
for (var i=0; i 0) {
runHooks('pluginDrawTools',{event:'layersSnappedToPortals'}); //or should we send 'layersEdited'? as that's effectively what's happened...
}
alert('Tested '+testCount+' points, and moved '+changedCount+' onto portal coordinates');
window.plugin.drawTools.save();
}
window.plugin.drawTools.boot = function() {
// add a custom hook for draw tools to share it's activity with other plugins
pluginCreateHook('pluginDrawTools');
window.addHook('iitcLoaded', window.plugin.drawTools.registerFieldForSyncing);
window.plugin.drawTools.currentMarker = window.plugin.drawTools.getMarkerIcon(window.plugin.drawTools.currentColor);
window.plugin.drawTools.setOptions();
//create a leaflet FeatureGroup to hold drawn items
window.plugin.drawTools.drawnItems = new L.FeatureGroup();
//load any previously saved items
plugin.drawTools.load();
//add the draw control - this references the above FeatureGroup for editing purposes
plugin.drawTools.addDrawControl();
//start off hidden. if the layer is enabled, the below addLayerGroup will add it, triggering a 'show'
$('.leaflet-draw-section').hide();
//hide the draw tools when the 'drawn items' layer is off, show it when on
map.on('layeradd', function(obj) {
if(obj.layer === window.plugin.drawTools.drawnItems) {
$('.leaflet-draw-section').show();
}
});
map.on('layerremove', function(obj) {
if(obj.layer === window.plugin.drawTools.drawnItems) {
$('.leaflet-draw-section').hide();
}
});
//add the layer
window.addLayerGroup('Drawn Items', window.plugin.drawTools.drawnItems, true);
//place created items into the specific layer
map.on('draw:created', function(e) {
var type=e.layerType;
var layer=e.layer;
window.plugin.drawTools.drawnItems.addLayer(layer);
window.plugin.drawTools.save();
if(layer instanceof L.Marker) {
window.registerMarkerForOMS(layer);
}
runHooks('pluginDrawTools',{event:'layerCreated',layer:layer});
});
map.on('draw:deleted', function(e) {
window.plugin.drawTools.save();
runHooks('pluginDrawTools',{event:'layersDeleted'});
});
map.on('draw:edited', function(e) {
window.plugin.drawTools.save();
runHooks('pluginDrawTools',{event:'layersEdited'});
});
//add options menu
$('#toolbox').append('DrawTools Opt');
$('head').append('');
}
var setup = window.plugin.drawTools.loadExternals;
// PLUGIN END //////////////////////////////////////////////////////////
@@PLUGINEND@@