a
    bbD                     @   sZ   d dl mZ d dlZddlmZ ddlmZ ddlmZ ddl	m
Z
 G d	d
 d
ee
ZdS )    )OrderedDictN   bundled_files)ReactiveHTML)classproperty   )GridSpecc                   @   s   e Zd ZdZejdddZejdddZejddZ	ej
dd	Zej
dd	Zd
ZdZdgdgdgdgdgdZddgZdgZd
did
d id
dd iidZedd Zi Zedd Zedd Zejdddd d! Zejd"ddd#d$ ZdS )%	GridStackz
    The GridStack layout builds on the GridSpec component and
    gridstack.js to allow resizing and dragging items in the grid.
    Tz'
        Allow resizing the grid cells.)defaultdocz'
        Allow dragging the grid cells.zV
        Current state of the grid (updated as items are resized and
        dragged).)r   N)r   Z	gridstacka  
    <div id="grid" class="grid-stack">
    {% for key, obj in objects.items() %}
      <div data-id="{{ id(obj) }}" class="grid-stack-item" gs-h="{{ (key[2] or nrows)-(key[0] or 0) }}" gs-w="{{ (key[3] or ncols)-(key[1] or 0) }}" gs-y="{{ (key[0] or 0) }}" gs-x="{{ (key[1] or 0) }}">
        <div id="content" class="grid-stack-item-content">${obj}</div>
      </div>
    {% endfor %}
    </div>
    a  
        const options = {
          column: data.ncols,
          disableResize: !data.allow_resize,
          disableDrag: !data.allow_drag,
          margin: 0
        }
        if (data.nrows)
          options.row = data.nrows
          if (model.height)
            options.cellHeight = Math.floor(model.height/data.nrows)
        const gridstack = GridStack.init(options, grid);
        function sync_state() {
          const items = []
          for (const node of gridstack.engine.nodes) {
            items.push({id: node.el.getAttribute('data-id'), x0: node.x, y0: node.y, x1: node.x+node.w, y1: node.y+node.h})
          }
          data.state = items
        }
        gridstack.on('resizestop', (event, el) => {
          window.dispatchEvent(new Event("resize"));
          sync_state()
        })
        gridstack.on('dragstop', (event, el) => {
          sync_state()
        })
        sync_state()
        state.gridstack = gridstack
        z+state.gridstack.enableMove(data.allow_drag)z/state.gridstack.enableResize(data.allow_resize)z"state.gridstack.column(data.ncols)z
          state.gristack.opts.row = data.nrows
          if (data.nrows && model.height)
            state.gridstack.cellHeight(Math.floor(model.height/data.nrows))
          else
            state.gridstack.cellHeight('auto')
        )Zrender
allow_dragallow_resizencolsnrowszChttps://cdn.jsdelivr.net/npm/gridstack@4.2.5/dist/gridstack.min.csszIhttps://cdn.jsdelivr.net/npm/gridstack@4.2.5/dist/gridstack-extra.min.csszAhttps://cdn.jsdelivr.net/npm/gridstack@4.2.5/dist/gridstack-h5.jsz>https://cdn.jsdelivr.net/npm/gridstack@4.2.5/dist/gridstack-h5exports)pathsr   shimc                 C   s   d| j dd iS )Nr
   r   r   )__javascript__cls r   5lib/python3.9/site-packages/panel/layout/gridstack.py__js_skip__l   s    zGridStack.__js_skip__c                 C   s   t | S )Nr   r   r   r   r   r   t   s    zGridStack.__javascript__c                 C   s
   t | dS )NZcssr   r   r   r   r   __css__x   s    zGridStack.__css__state)Zwatchc                 C   sj   t  }dd | D }| jD ],}||d  ||d |d |d |d f< q| j  | j| |   d S )Nc                 S   s   i | ]}t t||qS r   )strid).0objr   r   r   
<dictcomp>       z-GridStack._update_objects.<locals>.<dictcomp>r   y0x0y1x1)r   r   objectsclearupdate_update_sizing)selfr&   Z
object_idspr   r   r   _update_objects|   s    
*
zGridStack._update_objectsr&   c                    s:  | j rtt| j| j  }nd}| jr<tt| j| j }nd}t| j D ]\}\\}}}} |d u rndn|}|d u r| j n|}|d u rdn|}|d u r| jn|}|| ||  }}	| j	dv r|	| || d}
n:d| j	i}
d| j	v r|| |
d< nd| j	v r|	| |
d<  j
jf i  fdd|
 D  qNd S )	Nr   )ZfixedN)widthheightsizing_moder-   r.   c                    s"   i | ]\}} j | js||qS r   )paramreadonly)r   kvr   r   r   r       s   z,GridStack._update_sizing.<locals>.<dictcomp>)r   intfloatr-   r   r.   	enumerater&   itemsr/   r0   r(   )r*   r-   r.   ir"   r#   r$   r%   hwZ
propertiesr   r4   r   r)      s,    "


zGridStack._update_sizing)__name__
__module____qualname____doc__r0   ZBooleanr   r   Listr   ZIntegerr-   r.   Z_extension_nameZ	_templateZ_scriptsZ__css_raw__Z__javascript_raw__Z__js_require__r   r   Z_renamer   r   Zdependsr,   r)   r   r   r   r   r
      sN   +



	r
   )collectionsr   r0   Zio.resourcesr   Zreactiver   utilr   Zgridr	   r
   r   r   r   r   <module>   s   