o
    Pfl                     @   sB   d dl mZmZmZ d dlmZ d dlmZ G dd deeZdS )    )ElementFigureMacroElement)Template)
JSCSSMixinc                       sN   e Zd ZdZedZdgZdgZ								d fd
d	Z fddZ	  Z
S )Drawa;  
    Vector drawing and editing plugin for Leaflet.

    Parameters
    ----------
    export : bool, default False
        Add a small button that exports the drawn shapes as a geojson file.
    filename : string, default 'data.geojson'
        Name of geojson file
    position : {'topleft', 'toprigth', 'bottomleft', 'bottomright'}
        Position of control.
        See https://leafletjs.com/reference.html#control
    show_geometry_on_click : bool, default True
        When True, opens an alert with the geometry description on click.
    draw_options : dict, optional
        The options used to configure the draw toolbar. See
        http://leaflet.github.io/Leaflet.draw/docs/leaflet-draw-latest.html#drawoptions
    edit_options : dict, optional
        The options used to configure the edit toolbar. See
        https://leaflet.github.io/Leaflet.draw/docs/leaflet-draw-latest.html#editpolyoptions

    Examples
    --------
    >>> m = folium.Map()
    >>> Draw(
    ...     export=True,
    ...     filename="my_data.geojson",
    ...     position="topleft",
    ...     draw_options={"polyline": {"allowIntersection": False}},
    ...     edit_options={"poly": {"allowIntersection": False}},
    ... ).add_to(m)

    For more info please check
    https://leaflet.github.io/Leaflet.draw/docs/leaflet-draw-latest.html

    a  
        {% macro script(this, kwargs) %}
            var options = {
              position: {{ this.position|tojson }},
              draw: {{ this.draw_options|tojson }},
              edit: {{ this.edit_options|tojson }},
            }
            // FeatureGroup is to store editable layers.
            var drawnItems_{{ this.get_name() }} = new L.featureGroup().addTo(
                {{ this._parent.get_name() }}
            );
            options.edit.featureGroup = drawnItems_{{ this.get_name() }};
            var {{ this.get_name() }} = new L.Control.Draw(
                options
            ).addTo( {{this._parent.get_name()}} );
            {{ this._parent.get_name() }}.on(L.Draw.Event.CREATED, function(e) {
                var layer = e.layer,
                    type = e.layerType;
                var coords = JSON.stringify(layer.toGeoJSON());
                {%- if this.show_geometry_on_click %}
                layer.on('click', function() {
                    alert(coords);
                    console.log(coords);
                });
                {%- endif %}
                drawnItems_{{ this.get_name() }}.addLayer(layer);
             });
            {{ this._parent.get_name() }}.on('draw:created', function(e) {
                drawnItems_{{ this.get_name() }}.addLayer(e.layer);
            });
            {% if this.export %}
            document.getElementById('export').onclick = function(e) {
                var data = drawnItems_{{ this.get_name() }}.toGeoJSON();
                var convertedData = 'text/json;charset=utf-8,'
                    + encodeURIComponent(JSON.stringify(data));
                document.getElementById('export').setAttribute(
                    'href', 'data:' + convertedData
                );
                document.getElementById('export').setAttribute(
                    'download', {{ this.filename|tojson }}
                );
            }
            {% endif %}
        {% endmacro %}
        )leaflet_draw_jszIhttps://cdnjs.cloudflare.com/ajax/libs/leaflet.draw/1.0.2/leaflet.draw.js)leaflet_draw_csszJhttps://cdnjs.cloudflare.com/ajax/libs/leaflet.draw/1.0.2/leaflet.draw.cssFdata.geojsontopleftTNc                    s@   t    d| _|| _|| _|| _|| _|pi | _|pi | _d S )NDrawControl)	super__init___nameexportfilenamepositionshow_geometry_on_clickdraw_optionsedit_options)selfr   r   r   r   r   r   	__class__ _/home/deployuser/azure_apps/autowriter/venv/lib/python3.10/site-packages/folium/plugins/draw.pyr   j   s   
	
zDraw.__init__c                    sj   t  jdi | |  }t|tsJ dd}d}| jr3|jjt|dd |j	jt|dd d S d S )Nz8You cannot render this Element if it is not in a Figure.aA  
            <style>
                #export {
                    position: absolute;
                    top: 5px;
                    right: 10px;
                    z-index: 999;
                    background: white;
                    color: black;
                    padding: 6px;
                    border-radius: 4px;
                    font-family: 'Helvetica Neue';
                    cursor: pointer;
                    font-size: 12px;
                    text-decoration: none;
                    top: 90px;
                }
            </style>
        z"<a href='#' id='export'>Export</a>r   )nameexport_buttonr   )
r   renderget_root
isinstancer   r   header	add_childr   html)r   kwargsfigureexport_styler   r   r   r   r   |   s   zDraw.render)Fr
   r   TNN)__name__
__module____qualname____doc__r   	_template
default_jsdefault_cssr   r   __classcell__r   r   r   r   r      s"    %1	r   N)	branca.elementr   r   r   jinja2r   folium.elementsr   r   r   r   r   r   <module>   s    