13

如果我创建一个模式窗口:

Ext.define('myWindow', {
    extend: 'Ext.Container',
    alias: 'widget.myWindow',
    floating: true,
    modal: true,
    listeners:
        'onMaskClick???': { close the window }
    .....
}

我如何知道用户何时点击了窗口外的遮罩?在 Sench Touch 中,有一个配置 hideOnMaskTap 可以让我指定。extJS 的事件/配置是什么?

4

4 回答 4

17

Tramway 的案例(有点)适用于模态或非模态窗口。但如果子组件(如组合框的边界列表)浮动在窗口边界之外,则不会。

但是,如果您仍然使用模态窗口,您可以像这样在掩码上侦听单击事件。

Ext.define('myWindow', {
    extend: 'Ext.window.Window',
    alias: 'widget.myWindow',
    floating: true,
    modal: true,

    initComponent: function () {
        var me = this;
        me.callParent(arguments);
        me.mon(Ext.getBody(), 'click', function(el, e){
            me.close(me.closeAction);
        }, me, { delegate: '.x-mask' });
    }
});
于 2013-03-20T13:05:02.870 回答
11

可以监听所有点击事件,然后检查点击位置是否在窗口外

Ext.define('myWindow', {
    extend: 'Ext.Container',
    alias: 'widget.myWindow',
    floating: true,
    modal: true,

    initComponent: function () {
        this.initEvents();
        this.callParent();
    },

    initEvents: function () {
        //make sure your window is rendered and have sizes and position
        if(!this.rendered) {
            this.on('afterrender', this.initEvents, this, {single: true});
            return;
        }

        this.mon(Ext.getBody(), 'click', this._checkCloseClick, this);
    }

    _checkCloseClick: function (event) {
        var cx = event.getX(), cy = event.getY(),
            box = this.getBox();

        if (cx < box.x || cx > box.x + box.width || cy < box.y || cy > box.y + box.height) {
            //clean up listener listener
            this.mun(Ext.getBody(), 'click', this._checkCloseClick, this);
            this.close();
        }
    }
}
于 2012-09-15T08:41:54.553 回答
2

你也可以试试这个:

Ext.getBody().on('click', function(e, t){
    var el = win.getEl();

    if (!(el.dom === t || el.contains(t))) {
        win.close();
    }
});
于 2013-05-02T14:07:44.930 回答
0

我发现在 body 中添加一个监听器并检查 css 类对我来说有点生硬。您实际上可以覆盖(请参阅,完全不是hackey)_onMaskClick的私有方法。Ext.ZIndexManager它允许您将自己的配置参数(甚至事件)添加到所有窗口。

Ext.define('MyApp.ux.ZIndexManager_maskClick', {
  override: 'Ext.ZIndexManager',

  _onMaskClick: function ()
  {
    if (this.front)
    {
      var allowed = this.front.fireEvent('maskclick', this.front, this.mask);

      if (allowed !== false && this.front.closeOnMaskClick)
        this.front.close();
    }
    return this.callParent(arguments);
  },
});
于 2017-07-25T10:33:49.910 回答