
^Q\9,                 @   s   d  d l  Z  d  d l Z d  d l m Z d  d l m Z d d l m Z m Z m	 Z	 m
 Z
 m Z m Z e e d  s d d   Z n  d d	   Z Gd
 d   d e  Z d S)    N)ActionChains)WebDriverException   )	xpath_cls
xpath_item
is_integerPositionSizeElementRectcmpc             C   s   |  | k |  | k  S)N )abr   r   O/var/www/dbchiro/venv/build/django-nested-admin/nested_admin/tests/drag_drop.pyr      s    c             C   s   |  o d |  d k  S)Nr   r   )r   r   r   )xr   r   r   sign   s    r   c               @   s   e  Z d  Z d d   Z e d d    Z d d   Z e d d    Z e d	 d
    Z d d   Z	 d d   Z
 d d   Z d d   Z d d   Z d d   Z d d   Z e d d    Z d d d  Z d S)DragAndDropActionc             C   s  | |  _  | j |  _ t |  t |  k r9 d |  _ n	 d |  _ |  j  j | d d |  _ |  j  j | d |  j d d |  _ |  j  j | d |  j d d } t | d
  s | d d | d <n  |  j  j |  |  _	 t |  j d  r|  j d d g |  j d <n  d d   |  j D |  _ |  j  j
 d } x/ |  j d  d  D] \ } } | | d } qIWt |  |  _ |  j d  d  |  j d  d  k r|  j d d |  j d d k  r|  j d d d 7<qn  |  j  j t |  t |  d	  d  S)NTFZnamed_modelsZis_groupr   r   c             S   s   g  |  ] } t  |   q Sr   )tuple).0ir   r   r   
<listcomp>+   s   	 z.DragAndDropAction.__init__.<locals>.<listcomp>z+Depth of source and target must be the samer   r   r   r   r   r   r   r   r   r   r   r   )	test_caseseleniumlenZtarget_is_emptyZ_normalize_indexesfrom_indexes
to_indexesr   Zget_num_inlinestarget_num_itemsmodelstarget_num_inlinesZassertEqual)selfr   r   r   Znum_inlines_indexesZinline_modelsinline_index
item_indexr   r   r   __init__   s2    				 &"zDragAndDropAction.__init__c             C   s:   t  |  d  s3 |  j j d  } t |   |  _ n  |  j S)N_viewport_sizez
                return {
                    width:  (window.innerWidth ||  document.documentElement.clientWidth),
                    height: (window.innerHeight || document.documentElement.clientHeight)
                })hasattrr   execute_scriptr	   r$   )r    Z
dimensionsr   r   r   viewport_size9   s
    		zDragAndDropAction.viewport_sizec             C   s   |  j  j d  } t |   S)Nz
            return (function(m) {
                return m && {x: m.clientX, y: m.clientY};
            })(DJNesting.lastMouseMove))r   r&   r   )r    Zpos_dictr   r   r   get_mouse_positionG   s    		z$DragAndDropAction.get_mouse_positionc             C   s   t  |  d  s |  j j d |  j  } | j d k r? d } n |  j j r d j d t d  d t d  d t d	  d
 t d  g  } n6 d j d t d  d t d  d t d  g  } | j |  |  _	 n  |  j	 S)N_sourceindexesdivh3/z*[%s]zdjn-trzdjn-tdZtoolsz/*[%s]zdjn-drag-handlerzis-sortable)
r%   r   get_itemr   tag_nameZhas_grappellijoinr   find_element_by_xpathr)   )r    Zsource_itemZdrag_handler_xpathr   r   r   sourceN   s     	zDragAndDropAction.sourcec             C   s   t  |  d  s t |  j  d k rF |  j j |  j d  d
   } n	 |  j } d t d  |  j d d d t d  f } |  j |  j d d k r | d i t   d 6|  j d d d d	 67} n  | j	 |  |  _
 n  |  j
 S)N_targetr   z.//*[%s][%d]//*[%s]z	djn-groupr   z	djn-itemsz/*[%(item_pred)s][%(item_pos)d]Z	item_predZitem_posr   r   r   r   )r%   r   r   r   r.   r   r   r   r   r1   r3   )r    Ztarget_inline_parentZtarget_xpathr   r   r   targetd   s    "	+	
zDragAndDropAction.targetc             C   s+  |  j  } |  j j rH |  j j d k rH | j d k rH | j d  } n  y | j   Wn% t k
 r} |  j j	 d |  Yn X| j   t
 |  j  j | d d  j   j   t j d  t
 |  j  j d d
  j   t j d  t
 |  j  j d d  j   |  j j d	   } | SWd  QXd  S)NZchromer,   r   a)  
                var el = arguments[0],
                    top = el.getBoundingClientRect().top;
                if (top <= 15) {
                    document.documentElement.scrollTop += (top - 16);
                } else {
                    el.scrollIntoView();
                }
               g?r      z.ui-sortable-helperi)r2   r   Zhas_suitbrowserr/   find_element_by_css_selectorZclickr   r   r&   r   Zmove_to_element_with_offsetZclick_and_holdperformtimesleepmove_by_offsetZvisible_selector)r    r2   elr   r   r   initialize_dragu   s"    	-	
z!DragAndDropAction.initialize_dragc             C   s   | d k  r d n d } x9 t  d | |  D]% } t |  j  j d |  j   q+ W| | k r t |  j  j d | |  j   n  d  S)Nr   r6   i)ranger   r   r<   r9   )r    Zx_offsetZy_offset	incrementoffsetr   r   r   r<      s
    #z DragAndDropAction.move_by_offsetc             C   s   t  |  j  j   j   d  S)N)r   r   releaser9   )r    r   r   r   rB      s    zDragAndDropAction.releasec             C   s  |  j  d d  t |  j  } t d |  j d d k  oK |  j d k n o |  j d d |  j d k o t | d  d  |  j d  d   d k  } t |  } t | d i | r d n d d 6} |  j	   } |  j
 j } | j | j }	 t | j | j  }
 t d t | d d	 |
 d t |	  d	   } d
 } d } d  } d  } d } d } t d	 |
 d  } x| | k  r|  j } t | |  } | d k rPn  | d  k r| } n | | k rt | d  } n  | } | d  k r| } n  | j   | j   t | | d k r$d n d  } | | j }	 t |	  t |  | k rt |	  |
 k r| d 7} | d k rd } n2 | d k rd } n t t |	 d	  | |  } | d 9} n  | | } | | } |  j	   } | d k  r| j n
 | | j } | t |  k rT|  j j d | |  t |  j  j  d |  j   n t |  j  j  d |  j   | d 7} qvW| S)Nr   r   aliasesZbottomtopyr6         2      
   r5   z2document.documentElement.scrollTop += arguments[0]r   r   r   r   r   r   r   )r<   r   r   boolr   r   r   current_positionr
   r(   r'   heightrE   maxminabsZrefreshgetattrr   r   r&   r   r9   )r    Zhelper_elementZtarget_elementZdesired_posZtarget_bottomhelperr4   Z	mouse_posZviewport_heightZdyZinline_heightr@   Zmax_iterr   Zprev_pos_diffZ	directionZdirection_flipZ
flip_countZflip_multiplierZcurr_posZpos_diffZtarget_yincZmouse_edge_dyr   r   r   _match_helper_with_target   sr    1/1			

!.
		

"	z+DragAndDropAction._match_helper_with_targetc             C   sb   | j  d  j d  } d } x= | D]5 } | j | j k rA Pn  | |  r% | d 7} q% q% W| S)aS  
        For an unknown reason, evaluating XPath expressions of the form

            preceding-sibling::*[not(contains(@attr, 'value'))]

        Where 'value' is contained in at least one of the preceding siblings,
        is extraordinarily slow. So we just grab all siblings and iterate
        through the elements in python.
        z	parent::**r   r   )r1   Zfind_elements_by_xpathid)r    ctx	conditionZsiblingscountr=   r   r   r   _num_preceding_siblings   s    
z)DragAndDropAction._num_preceding_siblingsc             C   s   d d   } |  j  | d | S)Nc             S   s@   t  t j d |  j d    } | d h @o? | d d d h @S)Nz\s+classzdjn-itemzdjn-no-dragz	djn-theadzdjn-item-dragging)setresplitget_attribute)r=   Zclassesr   r   r   is_djn_item   s    !z?DragAndDropAction._num_preceding_djn_items.<locals>.is_djn_itemrX   )rZ   )r    rW   r`   r   r   r   _num_preceding_djn_items   s    z*DragAndDropAction._num_preceding_djn_itemsc             C   s   d d   } |  j  | d | S)Nc             S   s   d t  j d |  j d   k S)Nz	djn-groupz\s+r[   )r]   r^   r_   )r=   r   r   r   is_djn_group  s    zADragAndDropAction._num_preceding_djn_groups.<locals>.is_djn_grouprX   )rZ   )r    rW   rb   r   r   r   _num_preceding_djn_groups  s    z+DragAndDropAction._num_preceding_djn_groupsc             C   s   |  j  j d  } g  } d  } d t d  } x t d t |  j   D]w } | d  k rb | } n | j |  } |  j |  } | j d t d   } |  j |  } | j	 d | | f  qG Wt
 |  S)Nz.ui-sortable-placeholderzancestor::*[%s][1]zdjn-itemr   z	djn-group)r   r8   r   r?   r   r   r1   ra   rc   insertr   )r    placeholderposrW   Zancestor_xpathr   r"   r!   r   r   r   rL     s    			z"DragAndDropAction.current_positionFc             C   s`   |  j  } |  j   } | r@ d |  j j k r@ |  j j d  n  |  j | |  } |  j   d  S)NZ	phantomjsz	/dev/null)r4   r>   r   r7   r   Zsave_screenshotrT   rB   )r    Zscreenshot_hackr4   rR   r   r   r   move_to_target  s    	z DragAndDropAction.move_to_targetN)__name__
__module____qualname__r#   propertyr'   r(   r2   r4   r>   r<   rB   rT   rZ   ra   rc   rL   rg   r   r   r   r   r      s   #Hr   )r]   r:   Z'selenium.webdriver.common.action_chainsr   Zselenium.common.exceptionsr   utilsr   r   r   r   r	   r
   r%   __builtins__r   r   objectr   r   r   r   r   <module>   s   .