为 jQuery UI 构建匹配选项 Droppable 的相交公差

Build the matching option for jQuery UI Droppable's Intersect tolerance

我想将一个元素拖动到两个或更多可放置区域,但这些可放置区域需要完全包含在我的可拖动对象中。

问题是,jQuery UI's existing functionality for droppable tolerances 的 none 满足了这个需求。

理想情况下,我会使用 "intersect",但代码中可拖放对象的测量值是相反的。 (这个逻辑可以通过搜索$.ui.intersectjquery-ui.js中找到。)

我已经尝试通过 duck punching with jQuery 覆盖该函数并尝试将 tolerance 设置为这样的自定义函数:

tolerance: function(draggable, droppable) {
            if(!droppable.offset) return false;

            return ...logic check here...
        },
        drop: ...continues...

都没用。

这是一个 JSFiddle 来说明我的意思:https://jsfiddle.net/myingling/kgaqb0ay/5/

同样,一个人 //covered// 的所有项目都应该被分配。

修改$.ui.intersect 似乎是最好的方法。你有不同的选择。如果您不需要那么大的灵活性,您可以简单地添加一个 tolerance 类型,例如 'cover'。然后你只需要在intersect中检查公差类型的开关添加一个case,它可以恰好是'fit'的倒数。像这样:

  case 'fit':
    return (l <= x1 && x2 <= r && t <= y1 && y2 <= b);
    break;
  case 'cover':
    return (l >= x1 && x2 >= r && t >= y1 && y2 >= b);
    break;

参见:https://jsfiddle.net/6nyqja4a/4/

或者,如果您想要更大的灵活性,可以添加一个案例,其中 tolerance 是一个函数。然后你可以在选项中传递一个函数,它可以让你对不同的 droppable 有精确的容忍度。例如这样的事情: 在 interserct 函数中:

 if (toleranceMode instanceof Function) {

    return toleranceMode(draggable, droppable, x1, x2, y1, y2, l, r, t, b);

  } else {
    switch (toleranceMode) {
      case 'fit':
        return (l <= x1 && x2 <= r && t <= y1 && y2 <= b);
        break;
...

你这样称呼它:

$('.droppable').droppable({
  hoverClass: "yellow",
  tolerance: function(drag, drop, x1, x2, y1, y2, l, r, t, b) {
    return (l >= x1 && x2 >= r && t >= y1 && y2 >= b);
  },
  drop: function(event, ui) {
    $("#value_" + $(this).data("id")).val(ui.draggable.data("id"));
    console.log("Item " + $(this).data("id") + " taken by " + ui.draggable.data("id"));
  }
});

参见:https://jsfiddle.net/h4wm3r09/3/

From jquery 1.12 $.ui.intersect 函数被限定了作用域,因此之后不能直接修改。在$.ui.ddmanager中作为局部变量调用,所以即使修改$.ui.intersect,它不会被使用。自定义它有点复杂。你可以这样做,基本上你重新定义 intersect 然后重新定义 dragdrop 方法 $.ui.ddmanager 以便调用修改后的交集:

var intersect = $.ui.intersect = ( function() {
    function isOverAxis( x, reference, size ) {
        return ( x >= reference ) && ( x < ( reference + size ) );
    }

    return function( draggable, droppable, toleranceMode, event ) {

        if ( !droppable.offset ) {
            return false;
        }

        var x1 = ( draggable.positionAbs ||
                draggable.position.absolute ).left + draggable.margins.left,
            y1 = ( draggable.positionAbs ||
                draggable.position.absolute ).top + draggable.margins.top,
            x2 = x1 + draggable.helperProportions.width,
            y2 = y1 + draggable.helperProportions.height,
            l = droppable.offset.left,
            t = droppable.offset.top,
            r = l + droppable.proportions().width,
            b = t + droppable.proportions().height;
        if (toleranceMode instanceof Function) {

            return toleranceMode(draggable, droppable, x1, x2, y1, y2, l, r, t, b);

        } else {
            switch ( toleranceMode ) {
                case "fit":
                    return ( l <= x1 && x2 <= r && t <= y1 && y2 <= b );
                case "intersect":
                    return ( l < x1 + ( draggable.helperProportions.width / 2 ) && // Right Half
                x2 - ( draggable.helperProportions.width / 2 ) < r && // Left Half
                t < y1 + ( draggable.helperProportions.height / 2 ) && // Bottom Half
                y2 - ( draggable.helperProportions.height / 2 ) < b ); // Top Half
                case "pointer":
                    return isOverAxis( event.pageY, t, droppable.proportions().height ) &&
                isOverAxis( event.pageX, l, droppable.proportions().width );
                case "touch":
                    return (
                ( y1 >= t && y1 <= b ) || // Top edge touching
                ( y2 >= t && y2 <= b ) || // Bottom edge touching
                ( y1 < t && y2 > b ) // Surrounded vertically
            ) && (
                ( x1 >= l && x1 <= r ) || // Left edge touching
                ( x2 >= l && x2 <= r ) || // Right edge touching
                ( x1 < l && x2 > r ) // Surrounded horizontally
            );
                default:
                    return false;
            }
        }
    };
} )();

然后这个,你不做任何改变,你只需以完全相同的方式重新定义它们。

$.ui.ddmanager.drag = function( draggable, event ) {

    // If you have a highly dynamic page, you might try this option. It renders positions
    // every time you move the mouse.
    if ( draggable.options.refreshPositions ) {
        $.ui.ddmanager.prepareOffsets( draggable, event );
    }

    // Run through all droppables and check their positions based on specific tolerance options
    $.each( $.ui.ddmanager.droppables[ draggable.options.scope ] || [], function() {

        if ( this.options.disabled || this.greedyChild || !this.visible ) {
            return;
        }

        var parentInstance, scope, parent,
            intersects = intersect( draggable, this, this.options.tolerance, event ),
            c = !intersects && this.isover ?
                "isout" :
                ( intersects && !this.isover ? "isover" : null );
        if ( !c ) {
            return;
        }

        if ( this.options.greedy ) {

            // find droppable parents with same scope
            scope = this.options.scope;
            parent = this.element.parents( ":data(ui-droppable)" ).filter( function() {
                return $( this ).droppable( "instance" ).options.scope === scope;
            } );

            if ( parent.length ) {
                parentInstance = $( parent[ 0 ] ).droppable( "instance" );
                parentInstance.greedyChild = ( c === "isover" );
            }
        }

        // We just moved into a greedy child
        if ( parentInstance && c === "isover" ) {
            parentInstance.isover = false;
            parentInstance.isout = true;
            parentInstance._out.call( parentInstance, event );
        }

        this[ c ] = true;
        this[ c === "isout" ? "isover" : "isout" ] = false;
        this[ c === "isover" ? "_over" : "_out" ].call( this, event );

        // We just moved out of a greedy child
        if ( parentInstance && c === "isout" ) {
            parentInstance.isout = false;
            parentInstance.isover = true;
            parentInstance._over.call( parentInstance, event );
        }
    } );

}

$.ui.ddmanager.drop = function( draggable, event ) {

    var dropped = false;

    // Create a copy of the droppables in case the list changes during the drop (#9116)
    $.each( ( $.ui.ddmanager.droppables[ draggable.options.scope ] || [] ).slice(), function() {

        if ( !this.options ) {
            return;
        }
        if ( !this.options.disabled && this.visible &&
                intersect( draggable, this, this.options.tolerance, event ) ) {
            dropped = this._drop.call( this, event ) || dropped;
        }

        if ( !this.options.disabled && this.visible && this.accept.call( this.element[ 0 ],
                ( draggable.currentItem || draggable.element ) ) ) {
            this.isout = true;
            this.isover = false;
            this._deactivate.call( this, event );
        }

    } );
    return dropped;

}

https://jsfiddle.net/u6wfj8mj/1/

显然,这个更容易出错,可能有更好的方法来实现这一点。例如,通常您可以扩展小部件,这样会更干净。但是 intersectddmanagerdraggabledroppable[=52= 中都使用了] 并且不直接在这些小部件中。所以很难以干净的方式扩展。 您也可以将逻辑直接放在 draggables 和 droppables 的 drag event 和 drop event 中,但是由于有默认容差,不确定是否更好。

如何让jQuery UI的Droppable在左上角进入内部时接受Draggable? 我为 jQuery UI Droppable 1.9.2

创建了 jquery.ui.droppable-patch.js
(function ($, undefined) {
$.ui.intersect = function (draggable, droppable, toleranceMode) {
    if (!droppable.offset) return false;
    var x1 = (draggable.positionAbs || draggable.position.absolute).left, x2 = x1 + draggable.helperProportions.width,
        y1 = (draggable.positionAbs || draggable.position.absolute).top, y2 = y1 + draggable.helperProportions.height;
    var l = droppable.offset.left, r = l + droppable.proportions.width,
        t = droppable.offset.top, b = t + droppable.proportions.height;
    switch (toleranceMode) {
        case 'fit':
            return (l <= x1 && x2 <= r
            && t <= y1 && y2 <= b);
            break;
        case 'intersect':
            return (l < x1 + (draggable.helperProportions.width / 2) // Right Half
            && x2 - (draggable.helperProportions.width / 2) < r // Left Half
            && t < y1 + (draggable.helperProportions.height / 2) // Bottom Half
            && y2 - (draggable.helperProportions.height / 2) < b ); // Top Half
            break;
        case 'pointer':
            var draggableLeft = ((draggable.positionAbs || draggable.position.absolute).left + (draggable.clickOffset || draggable.offset.click).left),
                draggableTop = ((draggable.positionAbs || draggable.position.absolute).top + (draggable.clickOffset || draggable.offset.click).top),
                isOver = $.ui.isOver(draggableTop, draggableLeft, t, l, droppable.proportions.height, droppable.proportions.width);
            return isOver;
            break;
        case 'touch':
            return (
                    (y1 >= t && y1 <= b) || // Top edge touching
                    (y2 >= t && y2 <= b) || // Bottom edge touching
                    (y1 < t && y2 > b)      // Surrounded vertically
                ) && (
                    (x1 >= l && x1 <= r) || // Left edge touching
                    (x2 >= l && x2 <= r) || // Right edge touching
                    (x1 < l && x2 > r)      // Surrounded horizontally
                );
            break;

        case "top-left-touch":
            return ( y1 >= t && y1 <= b ) && ( x1 >= l && x1 <= r );
        case "top-right-touch":
            return ( y1 >= t && y1 <= b ) && ( x2 >= l && x2 <= r );
        case "bottom-left-touch":
            return ( y2 >= t && y2 <= b ) && ( x1 >= l && x1 <= r );
        case "bottom-right-touch":
            return ( y2 >= t && y2 <= b ) && ( x2 >= l && x2 <= r );
        default:
            return false;
            break;
    }
};

}(jQuery));