B
    CVvgÑ  ã               @   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   úJ/tmp/pip-install-o3oxmrkh/django-crispy-forms/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ƒr<xÎt| j t| jjƒ¡Ž D ]}|| j|ƒ q&W n t | jtƒrÜx’| jD ]ˆ}|j}t|ƒdkrx|| j|d ƒ qP| jj|d  }x|dd… D ]}|j| }q–W y|||d ƒ W qP t	k
rÖ   t
dƒ‚Y qPX qPW d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   Ú	positionsÚ
IndexErrorr   )r   ÚfunctionÚiÚpointerr   Ú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   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 )N)r	   r   r   )r#   r%   )r   r   r   r   r   r   Úwrap_object_onceZ   s    
z/LayoutSlice.wrap_once.<locals>.wrap_object_onceN)r$   )r   r   r   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|< xLtt| j t	| jjƒ¡Ž ƒD ]}||krh| jj|= qhW nt | jt
ƒr˜t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@x®t| j t| jjƒ¡Ž D ]}|| jj| ƒ q&W n|t | jtƒr¼xn| jD ]d}|j}| jj|d  }x"|dd… D ]}|}|j| }q|W |j	dkr°t |t
ƒr°||ƒ qT||ƒ qTW d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   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Úhasattrr1   ÚpopÚupdate)r#   r   )Úoriginal_kwargsr   r   r-   ’   s    

z3LayoutSlice.update_attributes.<locals>.update_attrsN)r0   )r   r7   r-   r   )r7   r   Úupdate_attributes�   s    
zLayoutSlice.update_attributesN)r.   Ú
__module__Ú__qualname__r   r   r   r   r   r   r$   r'   r)   r,   r0   r8   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   