o
    Pf                     @   sr   d dl mZ d dlmZ d dlmZ d dlmZ 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 )    )MacroElement)Template)Map)
JSCSSMixin)FeatureGroupGeoJsonTopoJson)MarkerCluster)parse_optionsc                       sV   e Zd ZdZedZdgZdgZ					 	d fd	d
	Zdd Z	 fddZ
  ZS )Searcha{  
    Adds a search tool to your map.

    Parameters
    ----------
    layer: GeoJson, TopoJson, FeatureGroup, MarkerCluster class object.
        The map layer to index in the Search view.
    search_label: str, optional
        'properties' key in layer to index Search, if layer is GeoJson/TopoJson.
    search_zoom: int, optional
        Zoom level to set the map to on match.
        By default zooms to Polygon/Line bounds and points
        on their natural extent.
    geom_type: str, default 'Point'
        Feature geometry type. "Point", "Line" or "Polygon"
    position: str, default 'topleft'
        Change the position of the search bar, can be:
        'topleft', 'topright', 'bottomright' or 'bottomleft',
    placeholder: str, default 'Search'
        Placeholder text inside the Search box if nothing is entered.
    collapsed: boolean, default False
        Whether the Search box should be collapsed or not.
    **kwargs.
        Assorted style options to change feature styling on match.
        Use the same way as vector layer arguments.

    See https://github.com/stefanocudini/leaflet-search for more information.

    a  
        {% macro script(this, kwargs) %}
            var {{this.layer.get_name()}}searchControl = new L.Control.Search({
                layer: {{this.layer.get_name()}},
                {% if this.search_label %}
                propertyName: '{{this.search_label}}',
                {% endif %}
                collapsed: {{this.collapsed|tojson|safe}},
                textPlaceholder: '{{this.placeholder}}',
                position:'{{this.position}}',
            {% if this.geom_type == 'Point' %}
                initial: false,
                {% if this.search_zoom %}
                zoom: {{this.search_zoom}},
                {% endif %}
                hideMarkerOnCollapse: true
            {% else %}
                marker: false,
                moveToLocation: function(latlng, title, map) {
                var zoom = {% if this.search_zoom %} {{ this.search_zoom }} {% else %} map.getBoundsZoom(latlng.layer.getBounds()) {% endif %}
                    map.flyTo(latlng, zoom); // access the zoom
                }
            {% endif %}
                });
                {{this.layer.get_name()}}searchControl.on('search:locationfound', function(e) {
                    {{this.layer.get_name()}}.setStyle(function(feature){
                        return feature.properties.style
                    })
                    {% if this.options %}
                    e.layer.setStyle({{ this.options|tojson }});
                    {% endif %}
                    if(e.layer._popup)
                        e.layer.openPopup();
                })
                {{this.layer.get_name()}}searchControl.on('search:collapsed', function(e) {
                        {{this.layer.get_name()}}.setStyle(function(feature){
                            return feature.properties.style
                    });
                });
            {{this._parent.get_name()}}.addControl( {{this.layer.get_name()}}searchControl );

        {% endmacro %}
        )zLeaflet.Search.jszLhttps://cdn.jsdelivr.net/npm/leaflet-search@2.9.7/dist/leaflet-search.min.js)zLeaflet.Search.csszMhttps://cdn.jsdelivr.net/npm/leaflet-search@2.9.7/dist/leaflet-search.min.cssNPointtopleftFc           	         sb   t    t|ttttfsJ d|| _|| _|| _	|| _
|| _|| _|| _tdi || _d S )Nz]Search can only index FeatureGroup, MarkerCluster, GeoJson, and TopoJson layers at this time. )super__init__
isinstancer   r	   r   r   layersearch_labelsearch_zoom	geom_typepositionplaceholder	collapsedr
   options)	selfr   r   r   r   r   r   r   kwargs	__class__r   a/home/deployuser/azure_apps/autowriter/venv/lib/python3.10/site-packages/folium/plugins/search.pyr   e   s   
zSearch.__init__c                 C   sJ   |d ur| j d ur| j |v sJ d| j  d| t| jts#J dd S )NzThe label 'z' was not available in z/Search can only be added to folium Map objects.)r   r   _parentr   )r   keysr   r   r   test_params   s   zSearch.test_paramsc                    s   t | jtrt| jjd d d  }n$t | jtr7| jjdd }t| jjd | d d d  }nd }| j	|d t
 jd	i | d S )
Nfeaturesr   
properties.objects
geometries)r    r   )r   r   r   tupledatar    r   object_pathsplitr!   r   render)r   r   r    obj_namer   r   r   r,      s   zSearch.render)NNr   r   r   F)__name__
__module____qualname____doc__r   	_template
default_jsdefault_cssr   r!   r,   __classcell__r   r   r   r   r      s$    /
	r   N)branca.elementr   jinja2r   foliumr   folium.elementsr   folium.featuresr   r   r   folium.pluginsr	   folium.utilitiesr
   r   r   r   r   r   <module>   s    