odoo-13.0/addons/web/static/tests/helpers/test_utils_dom.js
/**
 * simulate a drag and drop operation between 2 jquery nodes: $el and $to.
 * This is a crude simulation, with only the mousedown, mousemove and mouseup
 * events, but it is enough to help test drag and drop operations with jqueryUI
 * sortable.
 *
 * @todo: remove the withTrailingClick option when the jquery update branch is
 *   merged.  This is not the default as of now, because handlers are triggered
 *   synchronously, which is not the same as the 'reality'.
 *
 * @param {jqueryElement|HTMLElement} $el
 * @param {jqueryElement|HTMLElement} $to
 * @param {Object} [options]
 * @param {string|Object} [options.position='center'] target position:
 *   can either be one of {'top', 'bottom', 'left', 'right'} or
 *   an object with two attributes (top and left))
 * @param {boolean} [options.disableDrop=false] whether to trigger the drop action
 * @param {boolean} [options.continueMove=false] whether to trigger the
 *   mousedown action (will only work after another call of this function with
 *   without this option)
 * @param {boolean} [options.withTrailingClick=false] if true, this utility
 *   function will also trigger a click on the target after the mouseup event
 *   (this is actually what happens when a drag and drop operation is done)
 * @param {jQuery|HTMLElement} [options.mouseenterTarget=undefined] target of the mouseenter event
 * @param {jQuery|HTMLElement} [options.mousedownTarget=undefined] target of the mousedown event
 * @param {jQuery|HTMLElement} [options.mousemoveTarget=undefined] target of the mousemove event
 * @param {jQuery|HTMLElement} [options.mouseupTarget=undefined] target of the mouseup event
 * @param {jQuery|HTMLElement} [options.ctrlKey=undefined] if the ctrl key should be considered pressed at the time of mouseup
 * @returns {Promise}
 */
simulate a drag and drop operation between 2 jquery nodes: $el and $to.
This is a crude simulation, with only the mousedown, mousemove and mouseup
events, but it is enough to help test drag and drop operations with jqueryUI
sortable.

@todo: remove the withTrailingClick option when the jquery update branch is
merged.  This is not the default as of now, because handlers are triggered
synchronously, which is not the same as the 'reality'.


simulate a drag and drop operation between 2 jquery nodes : $el and $to . This is a crude simulation with only the mousedown mousemove and mouseup events but it is enough to help test drag and drop operations with jqueryUI sortable . @todo : remove the withTrailingClick option when the jquery update branch is merged . This is not the default as of now because handlers are triggered synchronously which is not the same as the ' reality ' .
function dragAndDrop($el, $to, options) {
    let el = null;
    if ($el instanceof EventTarget) {
        el = $el;
        $el = $(el);
    }
    if ($to instanceof EventTarget) {
        $to = $($to);
    }
    options = options || {};
    var position = options.position || 'center';
    var elementCenter = $el.offset();
    var toOffset = $to.offset();
    if (_.isObject(position)) {
        toOffset.top += position.top;
        toOffset.left += position.left;
    } else {
        toOffset.top += $to.outerHeight() / 2;
        toOffset.left += $to.outerWidth() / 2;
        var vertical_offset = (toOffset.top < elementCenter.top) ? -1 : 1;
        if (position === 'top') {
            toOffset.top -= $to.outerHeight() / 2 + vertical_offset;
        } else if (position === 'bottom') {
            toOffset.top += $to.outerHeight() / 2 - vertical_offset;
        } else if (position === 'left') {
            toOffset.left -= $to.outerWidth() / 2;
        } else if (position === 'right') {
            toOffset.left += $to.outerWidth() / 2;
        }
    }
    if ($to[0].ownerDocument !== document) {
        var bound = $('iframe')[0].getBoundingClientRect();
        toOffset.left += bound.left;
        toOffset.top += bound.top;
    }
    triggerEvent(options.mouseenterTarget || el || $el, 'mouseenter');
    if (!(options.continueMove)) {
        elementCenter.left += $el.outerWidth() / 2;
        elementCenter.top += $el.outerHeight() / 2;
        triggerEvent(options.mousedownTarget || el || $el, 'mousedown', {
            which: 1,
            pageX: elementCenter.left,
            pageY: elementCenter.top
        });
    }
    triggerEvent(options.mousemoveTarget || el || $el, 'mousemove', {
        which: 1,
        pageX: toOffset.left,
        pageY: toOffset.top
    });
    if (!options.disableDrop) {
        triggerEvent(options.mouseupTarget || el || $el, 'mouseup', {
            which: 1,
            pageX: toOffset.left,
            pageY: toOffset.top,
            ctrlKey: options.ctrlKey,
        });
        if (options.withTrailingClick) {
            triggerEvent(options.mouseupTarget || el || $el, 'click');
        }
    } else {
        $el.on('remove', function () {
            triggerEvent($el, 'mouseup');
        });
    }
    return concurrency.delay(0);
}
function dragAndDrop ( $el , $to , options ) { let el = null ; if ( $el instanceof EventTarget ) { el = $el ; $el = $ ( el ) ; } if ( $to instanceof EventTarget ) { $to = $ ( $to ) ; } options = options || { } ; var position = options . position || 'center' ; var elementCenter = $el . offset ( ) ; var toOffset = $to . offset ( ) ; if ( _ . isObject ( position ) ) { toOffset . top += position . top ; toOffset . left += position . left ; } else { toOffset . top += $to . outerHeight ( ) / 2 ; toOffset . left += $to . outerWidth ( ) / 2 ; var vertical_offset = ( toOffset . top < elementCenter . top ) ? - 1 : 1 ; if ( position === 'top' ) { toOffset . top -= $to . outerHeight ( ) / 2 + vertical_offset ; } else if ( position === 'bottom' ) { toOffset . top += $to . outerHeight ( ) / 2 - vertical_offset ; } else if ( position === 'left' ) { toOffset . left -= $to . outerWidth ( ) / 2 ; } else if ( position === 'right' ) { toOffset . left += $to . outerWidth ( ) / 2 ; } } if ( $to [ 0 ] . ownerDocument !== document ) { var bound = $ ( 'iframe' ) [ 0 ] . getBoundingClientRect ( ) ; toOffset . left += bound . left ; toOffset . top += bound . top ; } triggerEvent ( options . mouseenterTarget || el || $el , 'mouseenter' ) ; if ( ! ( options . continueMove ) ) { elementCenter . left += $el . outerWidth ( ) / 2 ; elementCenter . top += $el . outerHeight ( ) / 2 ; triggerEvent ( options . mousedownTarget || el || $el , 'mousedown' , { which : 1 , pageX : elementCenter . left , pageY : elementCenter . top } ) ; } triggerEvent ( options . mousemoveTarget || el || $el , 'mousemove' , { which : 1 , pageX : toOffset . left , pageY : toOffset . top } ) ; if ( ! options . disableDrop ) { triggerEvent ( options . mouseupTarget || el || $el , 'mouseup' , { which : 1 , pageX : toOffset . left , pageY : toOffset . top , ctrlKey : options . ctrlKey , } ) ; if ( options . withTrailingClick ) { triggerEvent ( options . mouseupTarget || el || $el , 'click' ) ; } } else { $el . on ( 'remove' , function ( ) { triggerEvent ( $el , 'mouseup' ) ; } ) ; } return concurrency . delay ( 0 ) ; }
simulate a drag and drop operation between 2 jquery nodes: $el and $to.
simulate a drag and drop operation between 2 jquery nodes : $el and $to .
// we are in an iframe // It's impossible to drag another element when one is already // being dragged. So it's necessary to finish the drop when the test is // over otherwise it's impossible for the next tests to drag and // drop elements.