【问题标题】:Constrain position of Dojo FloatingPane约束 Dojo FloatingPane 的位置
【发布时间】:2012-02-25 16:40:53
【问题描述】:

我有一个 dojox.layout.FloatingPane(作为自定义 dijit),它可以放置在其封闭 div 内的任何位置。我的问题是用户可能会将 FloatingPane 完全拖到其容器之外并且无法检索它。是否有任何简单的方法可以强制 FloatingPane 始终保持完全可见?

这是我的代码:

dojo.provide("ne.trim.dijits.SearchDialog");

dojo.require("dijit._Widget");
dojo.require("dijit._Templated");
dojo.require("dojox.layout.FloatingPane");

dojo.declare("ne.trim.dijits.SearchDialog", [dijit._Widget, dijit._Templated], {

templateString: dojo.cache("ne.trim.dijits", "templates/SearchDialog.html"),
initialised:false,
floatingPane: null,

postCreate: function() {
    this.init();
},

init: function() {
    console.debug("SearchDialog.init()", "start");
    if ( this.initialised === false ) {
        this.createSearchDialog();
    }
    //ne.trim.AppGlobals.searchDialog = this;
    console.debug("SearchDialog.init()", "end");        
},

createSearchDialog: function() {
    var node = dojo.byId('searchbox');  
    floatingPane = new dojox.layout.FloatingPane({
         title: "A floating pane",
         resizable: true, dockable: true, constrainToContainer: true,
         style:   "position:absolute;top:100;right:100;width:400px;height:300px;z-index:100",
    }, node );

    this.initialised=true;

    floatingPane.startup();
}

});

【问题讨论】:

    标签: dojo


    【解决方案1】:

    首先,请参阅 jsFiddle 上的工作示例:http://jsfiddle.net/phusick/3vTXW/

    现在进行一些解释;) FloatingPane 的 DnD 功能是通过在窗格的 postCreate 方法中实例化的 dojo.dnd.Moveable 类实现的。要限制 FloatingPane 的移动,您应该使用以下可移动对象之一:

    • dojo.dnd.parentConstainedMoveable - 受 DOM 节点约束
    • dojo.dnd.boxConstrainedMoveable - 通过坐标约束:{l: 10, t: 10, w: 100, h: 100}
    • dojo.dnd.constrainedMoveable - 通过在提供的函数中计算的坐标进行约束

    更多详情请见上述jsFiddle

    根据文档,您应该在Moveable 实例上调用destroy() 以将其删除,但由于FloatingPane 的原始Moveable 未分配给任何对象属性,因此我不销毁它,我只是实例化一个这三个moveables 在子类中的同一个 DOM 节点上:

    var ConstrainedFloatingPane = dojo.declare(dojox.layout.FloatingPane, {
    
        postCreate: function() {
            this.inherited(arguments);
            this.moveable = new dojo.dnd.move.constrainedMoveable(this.domNode, {
                handle: this.focusNode,
                constraints: function() {
                    return dojo.coords(dojo.body());
                }
            });
        }
    
    });
    

    现在您可以使用ConstainedFloatingPane 代替dojox.layout.FloatingPane

    var floatingPane = new ConstrainedFloatingPane({
        title: "A Constrained Floating Pane",
        resizable: true
    }, searchboxNode);
    

    【讨论】:

    • 这是一个很好的响应 phusick,正是我所需要的。非常感谢!
    • 也是一个好问题!它终于让我深入挖掘了 Dojo 的 DnD。
    • 这有一个问题,因为您可以调整 ConstrainedFloatingPane 的大小,使其大于实际的约束区域。有没有办法解决这个问题?
    猜你喜欢
    • 1970-01-01
    • 2017-10-14
    • 1970-01-01
    • 1970-01-01
    • 2015-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-28
    相关资源
    最近更新 更多