
    h                     t    d dl Z d dlmZ d dlmZmZ d dlmZ d dlm	Z	m
Z
mZ d dlmZ dad Z G d d	e      Zy)
    N)apps)MediaWidget)flatatt)escape	mark_safestrip_spaces_between_tags)gettextc                  f    t        j                  d      rd} | S d } dj                   |        } | S )Ndjangocms_attributes_field c                     t         j                  j                  t         j                  j                  t                    } t        t         j                  j                  | d      dd      5 }|j                         }d d d        t        t         j                  j                  | d      dd      5 }|j                         }d d d        |fS # 1 sw Y   RxY w# 1 sw Y   fS xY w)Nz+static/djangocms_attributes_field/widget.jsrzutf-8)encodingz,static/djangocms_attributes_field/widget.css)ospathdirnameabspath__file__openjoinread)base_dirfjs_codecss_codes       R/home/dcms/DCMS/lib/python3.12/site-packages/djangocms_attributes_field/widgets.py_read_static_filesz-_read_inline_code.<locals>._read_static_files   s    wwrwwx'@AHbggll8-Z[]`krs #wx&&(#bggll8-[\^alst $xy668$W$$	# #$W$$s   -C2CCC'z$<style>{}</style><script>{}</script>)r   is_installedformat)_inline_coder   s     r   _read_inline_coder"      sC    56 	% E=DDFXFZ[    c                   L     e Zd ZdZ fdZed        Zd ZddZd Z	d Z
 xZS )	AttributesWidgetzp
    A widget that displays key/value pairs from JSON as a list of text input
    box pairs and back again.
    c                     |j                  di       | _        |j                  di       | _        |j                  dd      rt        nd | _        t	        |   |i | y)zO
        Supports additional kwargs: `key_attr`, `val_attr`, `sorted`.
        	key_attrs	val_attrssortedTc                     | S )N )xs    r   <lambda>z+AttributesWidget.__init__.<locals>.<lambda>0   s    ! r#   N)popr'   r(   r)   super__init__)selfargskwargs	__class__s      r   r0   zAttributesWidget.__init__*   sQ      K4K4 &

8T :f$)&)r#   c                 `    t         
t               a t         r
t               S t        ddid      S )z
        Returns the media required by this widget.
        If djangocms_attributes_field is installed, it will use the media class
        of the widget, otherwise it will inline the CSS and JS.
        all)z%djangocms_attributes_field/widget.css)z$djangocms_attributes_field/widget.js)cssjs)r!   r"   r   )r1   s    r   mediazAttributesWidget.media3   s9     ,.L7NE	 r#   c           
          dj                  t        |      t        |      |||t        d            }t        |j	                               S )a\  
        Renders to HTML a single key/value pair row.

        :param key: (str) key
        :param value: (str) value
        :param field_name: (str) String name of this field
        :param key_attrs: (dict) HTML attributes to be applied to the key input
        :param val_attrs: (dict) HTML attributes to be applied to the value input
        a3  
        <div class="form-row attributes-pair">
            <div class="field-box">
               <input type="text" class="attributes-key" name="attributes_key[{field_name}]" value="{key}" {key_attrs}>
            </div>
            <div class="field-box">
               <input type="text" class="attributes-value"
                      name="attributes_value[{field_name}]"
                      value="{value}" {val_attrs}>
                <a class="delete-attributes-pair deletelink" href="#" title="{remove}"></a>
            </div>
        </div>
        Remove)keyvalue
field_namer'   r(   remove)r    r   _r	   strip)r1   r<   r=   r>   r'   r(   templates          r   _render_rowzAttributesWidget._render_rowL   sO     Fs-!X;  
 	* ))9::r#   c                    |sd}|i }d}|rut        |t              ret        |      dkD  rW| j                  |      D ]C  }|| j	                  |||   |t        | j                        t        | j                              z  }E |dj                  | j	                  dd|t        | j                        t        | j                                    z  }|dj                  t        d            z  }|d	t         z  }t        |      S )
a~  
        Renders this field into an HTML string.

        :param name: (str) name of the field
        :param value: (str) a json string of a two-tuple list automatically passed in by django
        :param attrs: (dict) automatically passed in by django (unused by this function)
        :param renderer: (object) automatically passed in by django (unused by this function)
        z{}z(<div class="djangocms-attributes-field">r   z7
        <div class="template hidden">{}
        </div>r   z
        <div class="related-widget-wrapper">
            <a class="add-attributes-pair addlink" href="#" title="{title}"></a>
        </div>
        zAdd another key/value pair)titlez</div>)
isinstancedictlenr)   rC   r   r'   r(   r    r@   r!   r   )r1   namer=   attrsrendereroutputr<   s          r   renderzAttributesWidget.renderm   s    E=E;Zt,Ua{{5) t$**3c
D'$..BY[bcgcqcq[rsst 	 &))"b$8OQXY]YgYgQhij	k
 	  F01  
		
 	F<.))  r#   c                     d| d}d| d}||v rR||v rN|j                  |      }|j                  |      }t        t        ||      D cg c]  }|d   dk(  r| c}      S i S c c}w )a*  
        Returns the dict-representation of the key-value pairs
        sent in the POST parameters

        :param data: (dict) request.POST or request.GET parameters.
        :param files: (list) request.FILES
        :param name: (str) the name of the field associated with this widget.
        zattributes_key[]zattributes_value[r   r   )getlistrG   zip)	r1   datafilesrI   	key_field	val_fieldkeysvaluesitems	            r   value_from_datadictz$AttributesWidget.value_from_datadict   s}     &dV1-	'vQ/	d!2<<	*D\\),F#dF*;Q$47b=QRR	 Rs   
A%A%c                      y)NFr+   )r1   rR   rS   rI   s       r   value_omitted_from_dataz(AttributesWidget.value_omitted_from_data   s    r#   )NN)__name__
__module____qualname____doc__r0   propertyr9   rC   rM   rY   r[   __classcell__)r4   s   @r   r%   r%   #   s7    *  0;B"!H"r#   r%   )r   django.appsr   django.formsr   r   django.forms.utilsr   django.utils.htmlr   r   r	   django.utils.translationr
   r@   r!   r"   r%   r+   r#   r   <module>rg      s4    	  & & J J 1  @v @r#   