dwilhelm89 / Leaflet.StyleEditor

Edit the style of features within Leaflet.
MIT License
174 stars 47 forks source link

Leaflet.StyleEditor

Build Status

The Leaflet StyleEditor allows to edit the style of any feature drawn within Leaflet. After activating the tool, the features can be edited by simply clicking them within the map.

Check out the Demo! And another demo with Leaflet.draw.

Usage

//Initialize the StyleEditor
map.addControl(L.control.styleEditor())

It's now possible to open the StyleEditor programmatically:

let styleEditor = L.control.styleEditor()
let marker = L.marker([51.5, -0.09])
marker.addTo(map)
styleEditor.enable(marker)

Settings

There are a bunch of settings you can define when initializing the styleeditor.

//Initialize the StyleEditor
let styleEditor = L.control.styleEditor({
    position: 'topleft',
    colorRamp: ['#1abc9c', '#2ecc71', '#3498db'],
    markers: ['circle-stroked', 'circle', 'square-stroked', 'square']
});
map.addControl(styleEditor)

Here is a list of all possible options.

Events

Events are prefixed with 'styleeditor:' unless defined differently in the settings styleEditorEventPrefix.

The following events exist:

event signification
visible The editor is visible and ready for user interaction.
hidden The editor is invisible.
changed An element has been styled. Element is given by the function.
editing A layer is being edited. The layer is given by the function.
marker A marker is being edited. The layer is given by the function.
Note: 'editing' will be called beforehand with the same layer.
geometry A geometry is being edited. The layer is given by the function.
Note:'editing' will be called beforehand with the same layer.
map.on('styleeditor:changed', function(element){
    console.log(element);
});

Packages

Bower

Leaflet.StyleEditor is also a registered package in Bower (based on nodejs). Integrate the source in your project with:

npm install -g bower
bower install Leaflet.StyleEditor

npm

Leaflet.StyleEditor is also a registered node module

npm install leaflet-styleeditor

or use the minified version if you do not use webpack or similar:

npm install leaflet-styleeditor-minified

Leaflet.Draw

When using Leaflet.draw most people will want to set useGrouping: false in the settings to prevent styling all added elements.

To let Leaflet.Draw directly show a styled marker set marker { icon: styleEditor.getDefaultIcon() } when initializing Leaflet.Draw.

Development

Marker

All Marker need to extend L.StyleEditor.marker.Marker.

At a minimum a new Marker implementation needs to provide these functions:

A new Marker implementation must define the markers that can be used in the options. Either as a list or a dictionary.

If a list is defined all colors will support the same icons.

If a dictionary is defined you may define supported icons for every color individually. 'default' is the fallback in the dictionary. I.e. if a color is not defined specifically the value for the key 'default' will be returned.

The markerForm can be individually set.

Forms

The StyleForm consists of different Forms, which consist of different FormElements.

Forms need to extend L.StyleEditor.forms.Form, every FormElement L.StyleEditor.formElements.FormElement.

Forms consist of FormElements defined in options.formElements as a dictionary mapping the "styleOption" (e.g. icon, color, dash,...) to the FormElement. A FormElement needs to implement createContent, where the select options are created.

style and lostFocus may be useful as well.

For a simple FormElement see DashElement, for a more complicated one see IconElement

Authors