
    	e#                        d Z ddlmZ ddlZddlmZ ddlmZm	Z	m
Z
mZmZmZmZmZ ddlZ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 erddlmZ ddlmZ ddlm Z  ddl!m"Z"  G d de          ZdS )zL
Defines the Location  widget which allows changing the href of the window.
    )annotationsN)TYPE_CHECKINGAnyCallableClassVarDictListMappingOptional   )Location)Syncable)parse_query   )create_doc_if_none_exists)state)Document)Model)BokehSessionContext)Commc                      e Zd ZU dZ ej        dd          Z ej        dd          Z ej        dd          Z ej        dd	          Z	 ej        dd
          Z
 ej        dd          Z ej        dd          Z ej        dd          ZddiZded<    fdZ	 	 dBdCd Z	 	 dDdEd%ZdF fd)ZdGdH fd+ZdGdId.Zdd/dJd3ZedKd5            ZdLd8Z	 	 dMdNd?ZdGdOdAZ xZS )Pr   z
    The Location component can be made available in a server context
    to provide read and write access to the URL components in the
    browser.
    TzF
        The full url, e.g. 'https://localhost:80?color=blue#interact')readonlydocz=
        hostname in window.location e.g. 'panel.holoviz.org'z
^$|[\/].*$zE
        pathname in window.location e.g. '/user_guide/Interact.html')regexr   z=
        protocol in window.location e.g. 'http:' or 'https:'z*
        port in window.location e.g. '80'z^$|\?z5
        search in window.location e.g. '?color=blue'z^$|#z1
        hash in window.location e.g. '#interact'Fz
        Reload the page when the location is updated. For multipage
        apps this should be set to True, For single page apps this
        should be set to False)defaultr   nameNz"ClassVar[Mapping[str, str | None]]_renamec                     t                      j        di | g | _        d| _        | j                            | j        dg           d S )NFsearch )super__init___synced_syncingparamwatch_update_synced)selfparams	__class__s     1lib/python3.11/site-packages/panel/io/location.pyr"   zLocation.__init__@   sQ    ""6"""
,xj99999    r   
'Document'rootOptional['Model']parentcommOptional['Comm']return'Model'c                    t          di |                     |                                           }|p|}||f| j        |j        d         <   |                     || j        |||           |S )Nidr    )_BkLocation_process_param_change_init_params_modelsref_link_props_linked_properties)r(   r   r.   r0   r1   models         r+   
_get_modelzLocation._get_modelF   st     NNd889J9J9L9LMMNN}u(-vTXd^$ 7dDIIIr,   Optional[Document]Optional[Comm]
preprocessboolc                    t          |          }|                     ||          }|j        d         }| |||ft          j        |<   || j        |<   |S )N)r1   r6   )r   r?   r;   r   _views
_documents)r(   r   r1   rB   r.   r;   s         r+   get_rootzLocation.get_rootP   sW     (,,s..htn!4d3S#r,   session_contextr   Nonec                    | j         D ].\  }}}}	 |                     ||           # t          $ r Y +w xY wt                                          |           d S N)r#   unsync	Exceptionr!   _server_destroy)r(   rH   pps_r*   s        r+   rN   zLocation._server_destroy[   sv    < 	 	KAr1aAr""""   00000s   (
55Model | Nonec                    |r)|j         | j        v r| j        |j         = |j        d         }nd }t                                          |           |t
          j        v rt
          j        |= d S d S )Nr6   )documentrF   r;   r!   _cleanupr   rE   )r(   r.   r;   r*   s      r+   rU   zLocation._cleanupc   su     	}//ODM2(4.CCC%,S!!! r,   eventparam.parameterized.Eventc                   | j         rd S | j        }| j        D ]\  }}}}d |                                D             }i }|                                D ]s\  }	}
|	|vr
||	         }	 |j        |                             |
          }
n# t          $ r Y nw xY w	 |
t          ||          k    }n# t          $ r d}Y nw xY w|s|
||<   t	  |j        j        di | # t          $ r |r ||           Y w xY wd S )Nc                    i | ]\  }}||	S r    r    .0kvs      r+   
<dictcomp>z+Location._update_synced.<locals>.<dictcomp>s   s    ;;;1q!;;;r,   Fr    )	r$   query_paramsr#   itemsr%   deserializerM   getattrupdate)r(   rV   r_   rO   
parametersrQ   on_errormappingmappedr\   r]   pnameequals                r+   r'   zLocation._update_syncedn   s   = 	F(*., 	% 	%&Az1h;;
(8(8(:(:;;;GF$**,, & &1G##
22155AA    D"E!2!22EE  " " "!EEE" &$%F5M%(((((( % % % %HV$$$%'	% 	%s6   & B
BBB--B<;B<CC54C5queryeventsrk   Optional[Dict[str, Any]]c                  | j         rd S |pi }|D ]fd| j        D             }|sj        j        nj        }	 |j        j                                     j                  }n# t          $ r
 j        }Y nw xY wt          |t                    st          j        |          }|||d         j                 <   d| _         	  | j        di d |                                D              d| _         d S # d| _         w xY w)Nc                >    g | ]\  }}}}|j         j        fv |S r    )clsobj)r[   orP   rQ   es       r+   
<listcomp>z*Location._update_query.<locals>.<listcomp>   s3    QQQkaQQ15!%.=P=Pr=P=P=Pr,   r   Tc                    i | ]
\  }}|||S rK   r    rZ   s      r+   r^   z*Location._update_query.<locals>.<dictcomp>   s     V V V$!QAr,   Fr    )r$   r#   rq   rp   r%   r   	serializenewrM   
isinstancestrjsondumpsupdate_queryr`   )r(   rk   rl   
serializedmatchesownervalrs   s          @r+   _update_queryzLocation._update_query   sE    = 	F[b
 	1 	1AQQQQT\QQQG U]AEEEk!&)33AE::   ec3'' &joo-0Jwqz!&)**	"DWW V V*2B2B2D2D V V VWWW!DMMMEDM!!!!s   *A**A>=A>	)C; ;	DDict[str, Any]c                *    t          | j                  S rK   )r   r   )r(   s    r+   r_   zLocation.query_params   s    4;'''r,   kwargsMapping[str, Any]c                v    | j         }|                    |           dt          j        |          z   | _        d S )N?)r_   rc   urlparse	urlencoder   )r(   r   rk   s      r+   r|   zLocation.update_query   s7    !VH.u555r,   parameterizedparam.Parameterizedrd   $Optional[List[str] | Dict[str, str]]re   *Optional[Callable[[Dict[str, Any]], None]]c                   |pd |j         D             }t          |t                    st          t          ||                    }|j                             | j        t          |                    }| j                            ||||f           | 	                                 i }|
                                D ]x\  }}t          ||          }|	 |j         |                             |          }n# t          $ r Y nw xY wt          |t                    st          j        |          }|||<   y|                     |           dS )a"  
        Syncs the parameters of a Parameterized object with the query
        parameters in the URL. If no parameters are supplied all
        parameters except the name are synced.

        Arguments
        ---------
        parameterized (param.Parameterized):
          The Parameterized object to sync query parameters with
        parameters (list or dict):
          A list or dictionary specifying parameters to sync.
          If a dictionary is supplied it should define a mapping from
          the Parameterized's parameters to the names of the query
          parameters.
        on_error: (callable):
          Callback when syncing Parameterized with URL parameters
          fails. The callback is passed a dictionary of parameter
          values, which could not be applied.
        c                    g | ]
}|d k    |S )r   r    )r[   rO   s     r+   rt   z!Location.sync.<locals>.<listcomp>   s    #R#R#R!a6kkAkkkr,   Nrj   )r%   rx   dictzipr&   r   listr#   appendr'   r`   rb   rv   rM   ry   rz   r{   )	r(   r   rd   re   watcherrk   rO   r   r]   s	            r+   synczLocation.sync   sa   .  R#R#R}/B#R#R#R
*d++ 	;c*j99::J%++D,>Z@P@PQQ]JJKKK!'')) 
	 
	GAtq))Ay!'*44Q77   a%% "JqMME$KK'''''s    C55
DDOptional[List[str]]c                N   fd| j         D             }|s"t                    }t          d| d          g g c}| j         D ]\  }}}}	|ur|                    ||||	f           &j                            |           5                    t          |                                                     w                    fd|	                                D                        fd|	                                D             }
j        
                    |j        t          |
                    }|                    ||
||	f           || _         fd| j        	                                D             }|rdt          j        |          z   nd	| _        dS )
a  
        Unsyncs the parameters of the Parameterized with the query
        params in the URL. If no parameters are supplied all
        parameters except the name are unsynced.

        Arguments
        ---------
        parameterized (param.Parameterized):
          The Parameterized object to unsync query parameters with
        parameters (list):
          A list of parameters to unsync.
        c                (    g | ]}|d          u |S )r   r    )r[   sr   s     r+   rt   z#Location.unsync.<locals>.<listcomp>   s'    DDDadm.C.C1.C.C.Cr,   zCannot unsync z5 object since it was never synced in the first place.Nc                "    g | ]\  }}|v 	|S r    r    r[   rO   qrd   s      r+   rt   z#Location.unsync.<locals>.<listcomp>   s"     Q Q Qtq!jr,   c                $    i | ]\  }}|v	||S r    r    r   s      r+   r^   z#Location.unsync.<locals>.<dictcomp>   s3     6 6 6tq!!"*!4!4  !4!4!4r,   c                $    i | ]\  }}|v	||S r    r    )r[   r\   r]   unsynceds      r+   r^   z#Location.unsync.<locals>.<dictcomp>   s)    QQQ$!Qq?P?PA?P?P?Pr,   r    )r#   type
ValueErrorr   r%   unwatchextendr   valuesr`   r&   fnr_   r   r   r   )r(   r   rd   r~   ptypesyncedrO   r)   r   re   
new_paramsnew_watcherrk   r   s    ``          @r+   rL   zLocation.unsync   s    EDDDdlDDD 	E''E De D D D E E Er,0L 	F 	F(AvwA%%q&'8<===''000!V]]__ 5 56666 Q Q Q Qv||~~ Q Q QRRR6 6 6 6v||~~ 6 6 6
+177
DDTDTUUq*k8DEEEEQQQQ$"3"9"9";";QQQ9>FcH.u5555Br,   )NNN)
r   r-   r.   r/   r0   r/   r1   r2   r3   r4   )NNT)r   r@   r1   rA   rB   rC   r3   r4   )rH   r   r3   rI   rK   )r.   rR   r3   rI   )rV   rW   r3   rI   )rl   rW   rk   rm   r3   rI   )r3   r   )r   r   r3   rI   )NN)r   r   rd   r   re   r   r3   rI   )r   r   rd   r   r3   rI   )__name__
__module____qualname____doc__r%   Stringhrefhostnamepathnameprotocolportr   hashBooleanreloadr   __annotations__r"   r?   rG   rN   rU   r'   r   propertyr_   r|   r   rL   __classcell__)r*   s   @r+   r   r      s          5< ,I J J JD u|T 0@ A A AH u|- 6H I I IH u|T 0@ A A AH 5< ,- . . .D U\ /8 9 9 9F 5<g ,4 5 5 5D U]5 /" # # #F 4:4.G@@@@: : : : : :>CG     FJ	 	 	 	 	1 1 1 1 1 1	" 	" 	" 	" 	" 	" 	"% % % % %8 UY" " " " " "0 ( ( ( X(6 6 6 6 fj?C)( )( )( )( )(V"G "G "G "G "G "G "G "G "Gr,   r   )#r   
__future__r   rz   urllib.parseparser   typingr   r   r   r   r   r	   r
   r   r%   models.locationr   r7   reactiver   utilr   rT   r   r   bokeh.documentr   bokeh.modelr   bokeh.server.contextsr   pyviz_commsr   r    r,   r+   <module>r      s    # " " " " "                            5 5 5 5 5 5             / / / / / /       !''''''!!!!!!999999      [G [G [G [G [Gx [G [G [G [G [Gr,   