From 09fa2be1fedb3acaf054ad91b2f6560ea5d89c24 Mon Sep 17 00:00:00 2001 From: enigmaquip Date: Sat, 25 Jan 2014 09:51:23 -0700 Subject: [PATCH] Draw Tools Update Updates to draw tools to add options menu (similar to bookmarks). Added import/export/reset. Also added ability to set color of drawn items and save color state --- plugins/draw-tools.user.js | 99 +++++++++++++++++++++++++++++++++++--- 1 file changed, 92 insertions(+), 7 deletions(-) diff --git a/plugins/draw-tools.user.js b/plugins/draw-tools.user.js index c254acc1..7d2c96fd 100644 --- a/plugins/draw-tools.user.js +++ b/plugins/draw-tools.user.js @@ -32,11 +32,11 @@ window.plugin.drawTools.loadExternals = function() { $('head').append(''); } -window.plugin.drawTools.setOptions = function() { +window.plugin.drawTools.setOptions = function(setcolor) { window.plugin.drawTools.lineOptions = { stroke: true, - color: '#a24ac3', + color: setcolor, weight: 4, opacity: 0.5, fill: false, @@ -62,7 +62,7 @@ window.plugin.drawTools.setOptions = function() { // renders the draw control buttons in the top left corner -window.plugin.drawTools.addDrawControl = function() { +window.plugin.drawTools.DrawControl = function() { var drawControl = new L.Control.Draw({ draw: { rectangle: false, @@ -124,8 +124,9 @@ window.plugin.drawTools.addDrawControl = function() { }, }); + return drawControl; + - map.addControl(drawControl); // plugin.drawTools.addCustomButtons(); } @@ -155,17 +156,21 @@ window.plugin.drawTools.save = function() { var data = []; window.plugin.drawTools.drawnItems.eachLayer( function(layer) { + console.log(layer); var item = {}; if (layer instanceof L.GeodesicCircle || layer instanceof L.Circle) { item.type = 'circle'; item.latLng = layer.getLatLng(); item.radius = layer.getRadius(); + item.color = layer.options.color; } else if (layer instanceof L.GeodesicPolygon || layer instanceof L.Polygon) { item.type = 'polygon'; item.latLngs = layer.getLatLngs(); + item.color = layer.options.color; } else if (layer instanceof L.GeodesicPolyline || layer instanceof L.Polyline) { item.type = 'polyline'; item.latLngs = layer.getLatLngs(); + item.color = layer.options.color; } else if (layer instanceof L.Marker) { item.type = 'marker'; item.latLng = layer.getLatLng(); @@ -191,12 +196,24 @@ window.plugin.drawTools.load = function() { var layer = null; switch(item.type) { case 'polyline': + if (typeof item.color === 'undefined'){ + window.plugin.drawTools.setOptions('#a24ac3'); + }else{ + window.plugin.drawTools.setOptions(item.color);} layer = L.geodesicPolyline(item.latLngs,window.plugin.drawTools.lineOptions); break; case 'polygon': + if (typeof item.color === 'undefined'){ + window.plugin.drawTools.setOptions('#a24ac3'); + }else{ + window.plugin.drawTools.setOptions(item.color);} layer = L.geodesicPolygon(item.latLngs,window.plugin.drawTools.polygonOptions); break; case 'circle': + if (typeof item.color === 'undefined'){ + window.plugin.drawTools.setOptions('#a24ac3'); + }else{ + window.plugin.drawTools.setOptions(item.color);} layer = L.geodesicCircle(item.latLng,item.radius,window.plugin.drawTools.polygonOptions); break; case 'marker': @@ -214,9 +231,65 @@ window.plugin.drawTools.load = function() { } +//Draw Tools Options + // Manual import, export and reset data + window.plugin.drawTools.manualOpt = function() { + dialog({ + html: plugin.drawTools.htmlSetbox, + dialogClass: 'ui-dialog-drawtoolsSet', + title: 'Draw Tools Options' + }); + } + + window.plugin.drawTools.optAlert = function(message) { + $('.ui-dialog-drawtoolsSet .ui-dialog-buttonset').prepend('

'+message+'

'); + $('.drawtools-alert').delay(2500).fadeOut(); + } + + window.plugin.drawTools.optCopy = function() { + dialog({ + html: '

Select all and press CTRL+C to copy it.

', + dialogClass: 'ui-dialog-drawtoolsSet-copy', + title: 'Draw Tools Export' + }); + } + + window.plugin.drawTools.optPaste = function() { + var promptAction = prompt('Press CTRL+V to paste it.', ''); + if(promptAction !== null && promptAction !== '') { + localStorage['plugin-draw-tools-layer'] = promptAction; + window.plugin.drawTools.drawnItems.clearLayers(); + window.plugin.drawTools.load(); + console.log('DRAWTOOLS: reset and imported drawn tiems'); + window.plugin.drawTools.optAlert('Import Successful. '); + } + } + + window.plugin.drawTools.optReset = function() { + var promptAction = prompt('All drawn items will be deleted. Are you sure? [Y/N]', ''); + if(promptAction !== null && (promptAction === 'Y' || promptAction === 'y')) { + delete localStorage['plugin-draw-tools-layer']; + window.plugin.drawTools.drawnItems.clearLayers(); + window.plugin.drawTools.load(); + console.log('DRAWTOOLS: reset all drawn items'); + window.plugin.drawTools.optAlert('Reset Successful. '); + } + } + + window.plugin.drawTools.optColor = function() { + var promptAction = prompt('Current Color is '+window.plugin.drawTools.lineOptions.color, ''); + if(promptAction !== null && promptAction !== '') { + plugin.drawTools.controller.removeFrom(map); + window.plugin.drawTools.setOptions(promptAction); +plugin.drawTools.controller = plugin.drawTools.DrawControl(); + plugin.drawTools.controller.addTo(map); + console.log('DRAWTOOLS: color changed'); + window.plugin.drawTools.optAlert('Color Changed. '); + } + } window.plugin.drawTools.boot = function() { - window.plugin.drawTools.setOptions(); + window.plugin.drawTools.setOptions('#a24ac3'); //create a leaflet FeatureGroup to hold drawn items window.plugin.drawTools.drawnItems = new L.FeatureGroup(); @@ -225,7 +298,8 @@ window.plugin.drawTools.boot = function() { plugin.drawTools.load(); //add the draw control - this references the above FeatureGroup for editing purposes - plugin.drawTools.addDrawControl(); + plugin.drawTools.controller = plugin.drawTools.DrawControl(); + plugin.drawTools.controller.addTo(map); //start off hidden. if the layer is enabled, the below addLayerGroup will add it, triggering a 'show' $('.leaflet-draw-section').hide(); @@ -262,7 +336,18 @@ window.plugin.drawTools.boot = function() { map.on('draw:edited', function(e) { window.plugin.drawTools.save(); }); - + //add options menu + $('#toolbox').append('DrawTools Opt'); + plugin.drawTools.htmlSetbox = '
' + +'Set Color' + +'Copy/Export Drawn Items' + +'Paste/Import Drawn Items' + +'Reset Drawn Items' + +'
'; + $('head').append(''); }