
    Ben                        d dl mZ d dlmZ d dlmZmZmZ 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 d
dlmZ  G d dee          ZdS )    )annotations)OrderedDict)ClassVarListMappingN   )config)CDN_DISTbundled_files)ReactiveHTML)classproperty   )GridSpecc                  "   e Zd ZU dZ ej        dd          Z ej        dd          Z ej        d          Z	 ej
        d	          Z ej
        d	          Zd
ZdZddddddddZej         dej         dgZej         dgZd
ej         did
d id
dd iidZddddZded<   e d gZd!ed"<   ed#             Zed$             Zed%             Z ej        d&d'          d(             Z ej        dd'          d)             ZdS )*	GridStacka~  
    The `GridStack` layout allows arranging multiple Panel objects in a grid
    using a simple API to assign objects to individual grid cells or to a grid
    span.

    Other layout containers function like lists, but a `GridSpec` has an API
    similar to a 2D array, making it possible to use 2D assignment to populate,
    index, and slice the grid.

    Reference: https://panel.holoviz.org/reference/layouts/GridStack.html

    :Example:

    >>> pn.extension('gridstack')
    >>> gstack = GridStack(sizing_mode='stretch_both')
    >>> gstack[ : , 0: 3] = pn.Spacer(styles=dict(background='red'))
    >>> gstack[0:2, 3: 9] = pn.Spacer(styles=dict(background='green'))
    >>> gstack[2:4, 6:12] = pn.Spacer(styles=dict(background='orange'))
    >>> gstack[4:6, 3:12] = pn.Spacer(styles=dict(background='blue'))
    >>> gstack[0:2, 9:12] = pn.Spacer(styles=dict(background='purple'))
    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   	gridstacka  
    <div id="grid" class="grid-stack" style="width: 100%; height: 100%">
    {% 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
          const height = model.height || grid.offsetHeight;
          options.cellHeight = Math.floor(height/data.nrows);
        }
        const gridstack = GridStack.init(options, grid);
        function sync_state(load=false) {
          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) => {
          sync_state()
          view.invalidate_layout()
        })
        gridstack.on('dragstop', (event, el) => {
          sync_state()
        })
        sync_state()
        state.gridstack = gridstack
        state.init = false
        z
        self.nrows()
        if (!state.init) {
          state.init = true
          view.invalidate_layout()
        }
        state.gridstack.engine._notify()
        z+state.gridstack.enableMove(data.allow_drag)z/state.gridstack.enableResize(data.allow_resize)z"state.gridstack.column(data.ncols)a-  
        state.gridstack.opts.row = data.nrows
        if (data.nrows) {
          const height = model.height || grid.offsetHeight || model.min_height;
          state.gridstack.cellHeight(Math.floor(height/data.nrows))
        } else {
          state.gridstack.cellHeight('auto')
        }
        zstate.gridstack.destroy())renderafter_layout
allow_dragallow_resizencolsnrowsremovez'/gridstack@7.2.3/dist/gridstack.min.cssz-/gridstack@7.2.3/dist/gridstack-extra.min.cssz&/gridstack@7.2.3/dist/gridstack-all.jsz#/gridstack@7.2.3/dist/gridstack-allexports)pathsr   shimr   r   objects)r   r   r   z"ClassVar[Mapping[str, str | None]]_renamezcss/gridstack.csszClassVar[List[str]]_stylesheetsc                $    d| j         dd         iS )Nr   r   r   )__javascript__clss    6lib/python3.11/site-packages/panel/layout/gridstack.py__js_skip__zGridStack.__js_skip__   s     +AaC0
 	
    c                     t          |           S )Nr   r$   s    r&   r#   zGridStack.__javascript__   s    S!!!r(   c                "    t          | d          S )Ncssr*   r$   s    r&   __css__zGridStack.__css__   s    S%(((r(   state)watchc                6   t                      }d | D             }| j        D ]/}||d                  ||d         |d         |d         |d         f<   0| j                                         | j                            |           |                                  d S )Nc                H    i | ]}t          t          |                    | S  )strid).0objs     r&   
<dictcomp>z-GridStack._update_objects.<locals>.<dictcomp>   s&    888Cc"S''llC888r(   r4   y0x0y1x1)r   r.   r   clearupdate_update_sizing)selfr   
object_idsps       r&   _update_objectszGridStack._update_objects   s    --884888
 	P 	PA<Fqw<OGQtWagqw$899G$$$r(   c                   | j         r| j        r| j        | j         z  }nd}| j        r| j        r| j        | j        z  }nd}t	          | j                                                  D ]\  }\  \  }}}}|dn|}|| j         n|}|dn|}|| j        n|}||z
  ||z
  }	}i }
| j        dv r/|rt          |	|z            |
d<   |rt          ||z            |
d<   nK| j        |
d<   d| j        v r|rt          ||z            |
d<   n d| j        v r|rt          |	|z            |
d<    j	        j
        di fd|
                                D              d S )Nr   )fixedNwidthheightsizing_modec                @    i | ]\  }}j         |         j        ||S r2   )paramreadonly)r5   kvr6   s      r&   r7   z,GridStack._update_sizing.<locals>.<dictcomp>   s>          Ay|, 1     r(   r2   )r   rE   r   rF   	enumerater   itemsrG   intrI   r=   )r?   rE   rF   ir8   r9   r:   r;   hw
propertiesr6   s              @r&   r>   zGridStack._update_sizing   s   : 	$* 	Jtz)EEE: 	$+ 	[+FFF*3DL4F4F4H4H*I*I 	 	&A& RR#jbB!#$**BjbB!#$**Bb5"R%qAJ?22 7*-ag,,Jw' 9+.qx==Jx(,0,<
=)d...6.+.qx==Jx((!111e1*-ag,,Jw'CI          !+!1!1!3!3         '	 	r(   ) __name__
__module____qualname____doc__rI   Booleanr   r   r   r.   IntegerrE   rF   _extension_name	_template_scriptsr	   npm_cdn__css_raw____javascript_raw____js_require__r    __annotations__r
   r!   r   r'   r#   r-   dependsrB   r>   r2   r(   r&   r   r      sb         , !5= 4* + + +L t 2* + + +J EJ    E EM$'''EU]4(((F!OI> FI< .i5 5Hp >BBB>HHHK >AAA FNOOO
 
 ;
 N 7y3 3G    
 &&&)L     
 
 ]

 " " ]" ) ) ]) U]7$'''  (' U]9D)))! ! *)! ! !r(   r   )
__future__r   collectionsr   typingr   r   r   rI   r	   io.resourcesr
   r   reactiver   utilr   gridr   r   r2   r(   r&   <module>rj      s    " " " " " " # # # # # # * * * * * * * * * *        2 2 2 2 2 2 2 2 # # # # # #                  A A A A Ah A A A A Ar(   