
    Be)                        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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)edit_readonly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<   ed             Z fdZ	 	 dCdDd!Z	 	 dEdFd&ZdG fd*ZdHdI fd,ZdHdJd/Zdd0dKd4ZedLd6            ZdMd9Z	 	 dNdOd@ZdHdPdBZ xZS )Qr   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^$|[\/]|srcdoc$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                   	 ddl m} t          ||          r|j        
 |             S n# t          $ r  |             cY S w xY wi }d}|j        r|j         dx|d<   }|j        rD|d|j         z  }d|j        v r$|j                            d          \  |d<   |d<   n
|j        |d<   |j        rd x}}||j        z  }d	|j        v rBd
|j        v r9|j                            d	          \  |d<   }|                    d
          \  }}n^d	|j        v r!|j                            d	          \  |d<   }n4d
|j        v r!|j                            d
          \  |d<   }n
|j        |d<   |rd	| |d<   |rd
| |d<   ||d<    |             }t          |          5  |j
                            |           d d d            n# 1 swxY w Y   |S )Nr   )_RequestProxy :protocolz//hostnameport?#pathnamesearchhashhref)bokeh.server.contextsr    
isinstance_requestImportErrorr#   hostspliturir   paramupdate)	clsrequestr    paramsr+   r)   r*   querylocs	            1lib/python3.11/site-packages/panel/io/location.pyfrom_requestzLocation.from_request@   s   	;;;;;;g}55 9I9Qsuu :R 	 	 	355LLL	  	?+2+;(>(>(>>F:< 	2''''Dgl""5<\5G5G5L5L2z"F6NN%,\z"; 	,  FTGKDgk!!cW[&8&8,3K,=,=c,B,B)z"E${{3//##-4[->->s-C-C*z"FF##+2;+<+<S+A+A(z"DD%,[z" 0#/v<<x  ,!+Tvvcee3 	% 	%IV$$$	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	%
s!   &* A AG  GGc                     t                      j        di | g | _        d| _        | j                            | j        dg           d S )NFr)    )super__init___synced_syncingr3   watch_update_synced)selfr7   	__class__s     r:   r?   zLocation.__init__i   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)rD   r   rH   rJ   rK   models         r:   
_get_modelzLocation._get_modelo   st     NNd889J9J9L9LMMNN}u(-vTXd^$ 7dDIIIrF   Optional[Document]Optional[Comm]
preprocessboolc                    t          |          }|                     ||          }|j        d         }| |||ft          j        |<   || j        |<   |S )N)rK   rP   )r   rY   rU   r   _views
_documents)rD   r   rK   r\   rH   rU   s         r:   get_rootzLocation.get_rooty   sW     (,,s..htn!4d3S#rF   session_contextr   Nonec                    | j         D ].\  }}}}	 |                     ||           # t          $ r Y +w xY wt                                          |           d S N)r@   unsync	Exceptionr>   _server_destroy)rD   rb   pps_rE   s        r:   rh   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 )NrP   )documentr`   rU   r>   _cleanupr   r_   )rD   rH   rU   rE   s      r:   ro   zLocation._cleanup   su     	}//ODM2(4.CCC%,S!!! rF   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    ;;;1q!;;;rF   Fr=   )	rA   query_paramsr@   itemsr3   deserializerg   getattrr4   )rD   rp   ry   ri   
parametersrk   on_errormappingmappedrv   rw   pnameequals                r:   rC   zLocation._update_synced   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r8   eventsr8   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=   )r5   obj)ru   orj   rk   es       r:   
<listcomp>z*Location._update_query.<locals>.<listcomp>   s3    QQQkaQQ15!%.=P=Pr=P=P=PrF   r   Tc                    i | ]
\  }}|||S re   r=   rt   s      r:   rx   z*Location._update_query.<locals>.<dictcomp>   s     V V V$!QArF   Fr=   )rA   r@   r   r5   r3   r   	serializenewrg   r-   strjsondumpsupdate_queryrz   )rD   r8   r   
serializedmatchesownervalr   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 re   )r   r)   )rD   s    r:   ry   zLocation.query_params   s    4;'''rF   kwargsMapping[str, Any]c                v    | j         }|                    |           dt          j        |          z   | _        d S )Nr&   )ry   r4   urlparse	urlencoder)   )rD   r   r8   s      r:   r   zLocation.update_query   s7    !VH.u555rF   parameterizedparam.Parameterizedr}   $Optional[List[str] | Dict[str, str]]r~   *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=   )ru   ri   s     r:   r   z!Location.sync.<locals>.<listcomp>   s    #R#R#R!a6kkAkkkrF   Nr   )r3   r-   dictziprB   r   listr@   appendrC   rz   r|   r   rg   r   r   r   )	rD   r   r}   r~   watcherr8   ri   r   rw   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=   )ru   sr   s     r:   r   z#Location.unsync.<locals>.<listcomp>  s'    DDDadm.C.C1.C.C.CrF   zCannot unsync z5 object since it was never synced in the first place.Nc                "    g | ]\  }}|v 	|S r=   r=   ru   ri   qr}   s      r:   r   z#Location.unsync.<locals>.<listcomp>  s"     Q Q Qtq!jrF   c                $    i | ]\  }}|v	||S r=   r=   r   s      r:   rx   z#Location.unsync.<locals>.<dictcomp>  s3     6 6 6tq!!"*!4!4  !4!4!4rF   c                $    i | ]\  }}|v	||S r=   r=   )ru   rv   rw   unsynceds      r:   rx   z#Location.unsync.<locals>.<dictcomp>  s)    QQQ$!Qq?P?PA?P?P?PrF   r&   r!   )r@   type
ValueErrorr   r3   unwatchextendr   valuesrz   rB   fnry   r   r   r)   )rD   r   r}   r   ptypesyncedri   r7   r   r~   
new_paramsnew_watcherr8   r   s    ``          @r:   rf   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rF   )NNN)
r   rG   rH   rI   rJ   rI   rK   rL   rM   rN   )NNT)r   rZ   rK   r[   r\   r]   rM   rN   )rb   r   rM   rc   re   )rH   rl   rM   rc   )rp   rq   rM   rc   )r   rq   r8   r   rM   rc   )rM   r   )r   r   rM   rc   )NN)r   r   r}   r   r~   r   rM   rc   )r   r   r}   r   rM   rc   ) __name__
__module____qualname____doc__r3   Stringr+   r$   r(   r#   r%   r)   r*   Booleanreloadr   __annotations__classmethodr;   r?   rY   ra   rh   ro   rC   r   propertyry   r   r   rf   __classcell__)rE   s   @r:   r   r      s          5< ,I J J JD u|T 0@ A A AH u|"4 ;H 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@@@@& & [&P: : : : : :>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rF   r   )$r   
__future__r   r   urllib.parseparser   typingr   r   r   r   r   r	   r
   r   r3   models.locationr   rQ   reactiver   utilr   r   rn   r   r   bokeh.documentr   bokeh.modelr   r,   r   pyviz_commsr   r=   rF   r:   <module>r      s    # " " " " "                            5 5 5 5 5 5       - - - - - - - - / / / / / /       !''''''!!!!!!999999      DG DG DG DG DGx DG DG DG DG DGrF   