
    vg:                         d Z ddlmZ ddlmZmZ ddlmZ  G d dej                        Z	 G d dej                        Z
 G d	 d
ej                        Zy)z=
Custom form widgets for conditional visibility and styling.
    )forms)SafeText	mark_safe)settingsc                       e Zd ZdZddZy)AllowanceConditionalVisibilityz
    A custom widget that loads conditional js to the form.

    Example:
    class MyForm(forms.Form):
        my_field = forms.CharField(widget=AllowanceConditionalVisibility, required=False)

    Nc                 t    dt         j                   d}d| d| d| d}|xs i }d|d<   t        ||z         S )	N<script src="/z&build/js/allowanceWidget.js"></script>
        <script id="QScript">
            $(document).ready(function () {
                $("[for='id_"']").remove()
                $("#<Script").remove()
            });
        </script>
        Frequiredr   
STATIC_URLr   selfnamevalueattrsrendererrendered_scriptadditional_scripts          5/var/www/horilla/payroll/widgets/component_widgets.pyrenderz%AllowanceConditionalVisibility.render   n     X0011WX 	!F !F #F 		 !j+<<==    NN)__name__
__module____qualname____doc__r    r   r   r   r      s    >r   r   c                       e Zd ZdZdefdZy)DeductionConditionalVisibilityz
    A custom widget that loads conditional js to the form.

    Example:
    class MyForm(forms.Form):
        my_field = forms.CharField(widget=DeductionConditionalVisibility, required=False)

    returnc                 t    dt         j                   d}d| d| d| d}|xs i }d|d<   t        ||z         S )	Nr
   z&build/js/deductionWidget.js"></script>r   r   r   r   Fr   r   r   s          r   r   z%DeductionConditionalVisibility.render1   r   r   N)r   r    r!   r"   r   r   r#   r   r   r%   r%   '   s    >h >r   r%   c                   0     e Zd ZdZdd fd
ZddZ xZS )StyleWidgetaj  
    A custom widget that enhances the styling and functionality of elements.

    Example:
    class MyForm(forms.Form):
        my_field = forms.CharField(widget=styleWidget, required=False)

        def __init__(self, *args, **kwargs):
            super().__init__(*args, **kwargs)
            self.fields['style'].widget = widget.styleWidget(form=self)

    N)formc                    |J|j                   j                         D ]-  \  }}|j                  j                  j	                  ddd       / t        |   |i | y )Nzstyle-widget)zdata-widgetclass)fieldsitemswidgetr   updatesuper__init__)r   r*   argskwargs_field	__class__s         r   r2   zStyleWidget.__init__Q   s]     KK--/ 5""))$2^L 	$)&)r   c           	          dt         j                   d}d| d| d| dt         j                   d	}|xs i }d|d	<   t        ||z         S )
a  
        Renders the widget as HTML, including the necessary scripts and styles for select2.

        Args:
            name (str): The name of the form field.
            value (Any): The current value of the form field.
            attrs (dict, optional): Additional HTML attributes for the widget.
            renderer: A custom renderer to use, if applicable.

        Returns:
            str: The rendered HTML representation of the widget.
        r
   z"build/js/styleWidget.js"></script>r   r   r   a  Script").remove()
                // Select all select elements with select2 initialized
                var selects = $("select[data-widget='style-widget']").select2();
                function toggleSelect2() {
                    selects.each(function() {
                        var select = $(this);
                        var select2Container = select.nextAll(".select2.select2-container").first();
                        if (select.is(":hidden")) {
                        select2Container.hide();
                        } else {
                            select2Container.show();
                        }
                    });
                }
                $("select, [type='checkbox'], [type='radio']").change(function (e) {
                    e.preventDefault();
                    toggleSelect2();
                });
                toggleSelect2();
            });
        </script>
        <link rel="stylesheet" type="text/css" href="/z$build/css/styleWidget.css">
        Fr   r   r   s          r   r   zStyleWidget.renderY   s     X0011ST 	!F !F #F 7* 8@7J7J6K L	36 !j+<<==r   r   )r   r    r!   r"   r2   r   __classcell__)r7   s   @r   r)   r)   C   s     $( *->r   r)   N)r"   djangor   django.utils.safestringr   r   horillar   Widgetr   r%   r)   r#   r   r   <module>r>      sG     7 >U\\ >8>U\\ >8C>%,, C>r   