U
    }Ha                     @   s:   d dl mZ d dlmZ d dlmZmZ G dd dZdS )    )	Container)DynamicError)Fieldset
MultiFieldc                   @   sV   e Zd ZeeefZdd Zdd Zdd Z	dd Z
d	d
 Zdd Zdd Zdd ZdS )LayoutSlicec                 C   s.   || _ t|tr$t||d d| _n|| _d S )N   )layout
isinstanceintslice)selfr   key r   =/tmp/pip-unpacked-wheel-rp2i33ek/crispy_forms/layout_slice.py__init__
   s    
zLayoutSlice.__init__c                 O   sh   |rDt |trt|}n|f}|| jkr2|| }n|| }|||S t |trX|||S ||f|S dS )z|
        Returns a layout object of type `LayoutClass` with `args` and `kwargs` that
        wraps `fields` inside.
        N)r	   listtuple
args_first)r   LayoutClassfieldsargskwargs	argumentsr   r   r   wrapped_object   s    






zLayoutSlice.wrapped_objectc              	   C   s   t | jtr8t| jt| jj D ]}|| j| q$nt | jtr| jD ]}|d }t|dkrt|| j|d  qJ| jj|d  }|dd D ]}|j| }qz|||d  W qJ tk
r   t	dY qJX qJdS )z
        Iterates over layout objects pointed in `self.slice` executing `function` on them.
        It passes `function` penultimate layout object and the position where to find last one
        r   r   zqTrying to wrap a field within an already wrapped field,                             recheck your filter or layoutN)
r	   r   rangeindiceslenr   r   r   
IndexErrorr   )r   functionipointerpositionlayout_objectr   r   r   pre_map(   s"    
zLayoutSlice.pre_mapc                    s     fdd} | dS )z
        Wraps every layout object pointed in `self.slice` under a `LayoutClass` instance with
        `args` and `kwargs` passed.
        c                    s$   j  | j| f| j|< d S N)r   r   r#   jr   r   r   r   r   r   wrap_objectN   s    z%LayoutSlice.wrap.<locals>.wrap_objectNr$   )r   r   r   r   r)   r   r(   r   wrapH   s    zLayoutSlice.wrapc                    s     fdd} | dS )z
        Wraps every layout object pointed in `self.slice` under a `LayoutClass` instance with
        `args` and `kwargs` passed, unless layout object's parent is already a subclass of
        `LayoutClass`.
        c                    s.   t |  s*j | j| f| j|< d S r%   )r	   r   r   r&   r(   r   r   wrap_object_onceZ   s    
z/LayoutSlice.wrap_once.<locals>.wrap_object_onceNr*   )r   r   r   r   r,   r   r(   r   	wrap_onceS   s    zLayoutSlice.wrap_oncec                 O   s   t | jtr| jjdk	r | jjnd}| j|| jj| j f||| jj|< tt| jt	| jj D ]}||krf| jj|= qfnt | jt
rtddS )z
        Wraps all layout objects pointed in `self.slice` together under a `LayoutClass`
        instance with `args` and `kwargs` passed.
        Nr   z=wrap_together doesn't work with filter, only with [] operator)r	   r   startr   r   r   reversedr   r   r   r   r   )r   r   r   r   r.   r    r   r   r   wrap_together`   s      zLayoutSlice.wrap_togetherc                 C   s   t | jtr<t| jt| jj D ]}|| jj|  q$nvt | jtr| jD ]b}|d }| jj|d  }|dd D ]}|}|j| }qv|jdkrt |t	r|| qN|| qNdS )z
        Iterates over layout objects pointed in `self.slice` executing `function` on them
        It passes `function` last layout object
        r   r   Nupdate_attrs)
r	   r   r   r   r   r   r   r   __name__str)r   r   r    r!   r"   r#   Zprevious_layout_objectr   r   r   mapt   s    

zLayoutSlice.mapc                    s    fdd}|  | dS )z`
        Updates attributes of every layout object pointed in `self.slice` using kwargs
        c                    sb      }t| dr^d|krRd| jkrB| jd  d|d 7  < n|d| jd< | j| d S )NattrsZ	css_classclassz %s)copyhasattrr5   popupdate)r#   r   original_kwargsr   r   r1      s    

z3LayoutSlice.update_attributes.<locals>.update_attrsN)r4   )r   r<   r1   r   r;   r   update_attributes   s    
zLayoutSlice.update_attributesN)r2   
__module____qualname__r   r   r   r   r   r   r$   r+   r-   r0   r4   r=   r   r   r   r   r      s   
 r   N)Zcrispy_forms.bootstrapr   Zcrispy_forms.exceptionsr   Zcrispy_forms.layoutr   r   r   r   r   r   r   <module>   s   