diff --git a/src/c3nav/editor/static/editor/css/editor.css b/src/c3nav/editor/static/editor/css/editor.css index f8b29227..2269cf01 100644 --- a/src/c3nav/editor/static/editor/css/editor.css +++ b/src/c3nav/editor/static/editor/css/editor.css @@ -13,12 +13,40 @@ top:2px; } -.leaflet-editbar a { - font-size: 14px; +.leaflet-drawbar { + display:none; +} +.leaflet-drawbar a, .leaflet-drawbar a:hover { + font-size: 13px; + width:auto; + padding:0 6px; +} + +.leaflet-editbar a, .leaflet-editbar a:hover { + font-size: 13px; + width:auto; + min-width:50px; + text-align:left; + padding:0 6px; + display:none; } .leaflet-editbar a.current { - background-color:#FFFFDD; + display:block; + font-weight:bold; } +.leaflet-editbar.usable:hover a { + width:80px; + display:block; +} +.leaflet-editbar:not(.usable) a.current, .leaflet-editbar:not(:hover) a.current, .leaflet-drawbar a { + border-radius:4px; + border:0; +} +.leaflet-editbar:not(.usable) a.current:hover { + background-color:#FFFFFF; + cursor:default; +} + .leaflet-levels a { font-size:16px; color:#333333; diff --git a/src/c3nav/editor/static/editor/js/editor.js b/src/c3nav/editor/static/editor/js/editor.js new file mode 100644 index 00000000..8b650175 --- /dev/null +++ b/src/c3nav/editor/static/editor/js/editor.js @@ -0,0 +1,219 @@ +editor = { + feature_types: { + bounds: { + type: 'polygon', + color: '#FFFFFF', + }, + building: { + type: 'polygon', + color: '#CCCCCC', + }, + wall: { + type: 'polygon', + color: '#333333', + }, + obstacle: { + type: 'polygon', + color: '#999999', + }, + door: { + type: 'polygon', + color: '#FF00FF', + }, + step: { + type: 'polyline', + color: '#FF0000', + }, + elevator: { + type: 'polygon', + color: '#99CC00', + }, + }, + + init: function() { + // Init Map + editor.map = L.map('mapeditor', { + zoom: 2, + maxZoom: 10, + minZoom: 1, + crs: L.CRS.Simple, + editable: true, + closePopupOnClick: false, + }); + + L.control.scale({imperial: false}).addTo(editor.map); + + editor.get_packages(); + editor.init_features(); + }, + + init_features: function() { + editor._feature_type = null; + L.FeatureLayerControl = L.Control.extend({ + options: { + position: 'topleft' + }, + onAdd: function (map) { + var container = L.DomUtil.create('div', 'leaflet-control leaflet-bar leaflet-editbar usable'); + $('').appendTo(container).html('').attr({ + href: '#', + title: 'disable editing', + name: 'null' + }).on('click', function() { + editor.set_feature_type(null); + }); + var plural; + for (var feature_type in editor.feature_types) { + $('').appendTo(container).text(feature_type+((feature_type.substr(-1) != 's')?'s':'')).attr({ + href: '#', + title: 'edit '+feature_type+' layer', + name: feature_type + }).on('click', editor._layer_button_click); + } + return container; + } + }); + editor.map.addControl(new L.FeatureLayerControl()); + + // Add drawing new features + editor._drawing = null; + editor._adding = null; + + L.DrawControl = L.Control.extend({ + options: { + position: 'topleft' + }, + onAdd: function (map) { + var container = L.DomUtil.create('div', 'leaflet-control leaflet-bar leaflet-drawbar'); + $('').appendTo(container).text('start drawing').attr({ + href: '#', + title: 'start drawing', + name: '' + }).on('click', function(e) { + e.preventDefault(); + editor.start_drawing(); + }); + + $('').appendTo(container).text('cancel').attr({ + href: '#', + title: 'cancel drawing', + name: '' + }).on('click', function(e) { + e.preventDefault(); + editor.cancel_drawing(); + }); + return container; + } + }); + editor.map.addControl(new L.DrawControl()); + $('#drawcancel').hide(); + + editor.map.on('editable:drawing:commit', function (e) { + editor._drawing = null; + editor._adding = e.layer; + + e.layer.disableEdit(); + L.popup({ + closeButton: false, + autoClose: false, + }).setContent('').setLatLng(e.layer.getCenter()).openOn(editor.map); + console.log(e.layer.toGeoJSON()); + }).on('editable:drawing:cancel', function (e) { + if (editor._drawing !== null && editor._adding === null) { + e.layer.remove(); + } + }); + }, + + _layer_button_click: function(e) { + e.preventDefault(); + editor.set_feature_type($(this).attr('name')); + }, + + set_feature_type: function(feature_type) { + if (editor._drawing !== null || editor._adding !== null) return; + + $('.leaflet-editbar .current').removeClass('current'); + $('.leaflet-editbar [name='+feature_type+']').addClass('current'); + editor._feature_type = feature_type; + + $('.leaflet-drawbar').toggle(feature_type !== null); + $('#drawstart').text('add '+feature_type); + }, + start_drawing: function() { + if (editor._feature_type === null) return; + + editor._drawing = editor._feature_type; + var options = editor.feature_types[editor._drawing]; + if (options.type == 'polygon') { + editor.map.editTools.startPolygon(null, options); + } else if (options.type == 'polyline') { + editor.map.editTools.startPolyline(null, options); + } + $('.leaflet-editbar').toggleClass('usable', false); + $('#drawstart').hide(); + $('#drawcancel').show(); + }, + cancel_drawing: function() { + if (editor._drawing === null || editor._adding !== null) return; + editor.map.editTools.stopDrawing(); + editor._drawing = null; + $('.leaflet-editbar').toggleClass('usable', true); + $('#drawcancel').hide(); + $('#drawstart').show(); + }, + + get_packages: function() { + $.getJSON('/api/v1/packages/', function(packages) { + var bounds = [[0, 0], [0, 0]]; + var pkg; + for(var i=0;i').appendTo(container).text(icon).attr({ - href: '#', - title: 'add '+name, - name: type - }).on('click', function(e) { - e.preventDefault(); - - // If we are currently adding a feature, don't start drawing another. - if (currently_adding !== null) { - return; - } - - // If we are currently drawing a feature, don't start drawing another. - if (currently_drawing !== null) { - // If it is a feature of the same type, cancel it. - if (currently_drawing === type) { - $('.leaflet-editbar .current').removeClass('current'); - currently_drawing = null; - map.editTools.stopDrawing(); - } - return; - } - - currently_drawing = type; - console.log(type); - $('.leaflet-editbar .current').removeClass('current'); - $('.leaflet-editbar [name='+type+']').addClass('current'); - options = feature_types[type]; - if (options.type == 'polygon') { - map.editTools.startPolygon(null, options); - } else if (options.type == 'polyline') { - map.editTools.startPolyline(null, options); - } - }); -} - -if ($('#mapeditor').length) { - // Init Map - var map = L.map('mapeditor', { - zoom: 2, - maxZoom: 10, - minZoom: 1, - crs: L.CRS.Simple, - editable: true, - closePopupOnClick: false, - }); - - $.getJSON('/api/v1/packages/', function(packages) { - var bounds = [[0, 0], [0, 0]]; - var pkg; - for(var i=0;i').setLatLng(e.layer.getCenter()).openOn(map); - console.log(e.layer.toGeoJSON()); - }).on('editable:drawing:cancel', function (e) { - if (currently_drawing === null && currently_adding === null) { - e.layer.remove(); - } - }); - - L.control.scale({imperial: false}).addTo(map); -} diff --git a/src/c3nav/editor/templates/editor/base.html b/src/c3nav/editor/templates/editor/base.html index 61b0f5c0..3c4b4555 100644 --- a/src/c3nav/editor/templates/editor/base.html +++ b/src/c3nav/editor/templates/editor/base.html @@ -51,7 +51,7 @@ - + {% endcompress %}