easyui的window插件再次封装

easyui的window插件再次封装

说明:该插件弹出的弹出框在最顶层的页面中,而不是在当前页面所在的iframe中,它的可拖动范围是整个浏览器窗口;所以不能用JS的parent对象获取弹出它的父页面,这里用了一个笨方法,最多支持四级弹出框,即弹出框中再弹出弹出框的情况,笨方法也是方法,它很简单、很有效。

代码:

/**
* easyui的window插件再次封装
* 2014年11月10日
*/

SimpoWin = {
    showWin: function showWindow(title, url, width, height) {
        if (!top.SimpoWinId) top.SimpoWinId = 0;
        var divId = "simpoWin" + top.SimpoWinId;
        top.$("body").append('<div id="' + divId + '"></div>');

        top.$('#' + divId).window({
            modal: true,
            title: title,
             width,
            height: height,
            collapsible: false,
            minimizable: false,
            maximizable: false,
            content: function () {
                return '<iframe frameborder="0" src="' + url + '" style=" ' + (width - 14).toString() + 'px; height: ' + (height - 39).toString() + 'px; margin: 0;">';
            },
            onClose: function () {
                top.$('#' + divId).window('destroy');
                top.SimpoWinId--;
            }
        }).window('open');

        top.SimpoWinId++;
        switch (top.SimpoWinId) {
            case 1:
                top.SimpoWinParent1 = window;
                break;
            case 2:
                top.SimpoWinParent2 = window;
                break;
            case 3:
                top.SimpoWinParent3 = window;
                break;
            default:
                top.SimpoWinParent = window;
                break;
        }
    },

    closeWin: function () {
        var divId = "simpoWin" + (top.SimpoWinId - 1).toString();
        top.$('#' + divId).window('close');
    },

    GetWinParent: function () {
        switch (top.SimpoWinId) {
            case 1:
                return top.SimpoWinParent1;
            case 2:
                return top.SimpoWinParent2;
            case 3:
                return top.SimpoWinParent3;
            default:
                return top.SimpoWinParent;
        }
    }
}
View Code

带回调函数版,支持传入url或div容器id:

/**
* easyui的window插件再次封装
* 2014年11月10日
*/

SimpoWin = {
    showWin: function (title, url, width, height, okcallback, closecallback) {
        if (!top.SimpoWinId) top.SimpoWinId = 0;
        var divId = "simpoWin" + top.SimpoWinId;
        top.$("body").append('<div id="' + divId + '"></div>');

        top.$('#' + divId).window({
            modal: true,
            title: title,
             width,
            height: height,
            collapsible: false,
            minimizable: false,
            maximizable: false,
            content: function () {
                return '<iframe frameborder="0" src="' + url + '" style=" ' + (width - 14).toString() + 'px; height: ' + (height - 39).toString() + 'px; margin: 0;">';
            },
            onClose: function () {
                top.$('#' + divId).window('destroy');
                top.SimpoWinId--;
                if (closecallback) closecallback();
            }
        }).window('open');

        top.SimpoWinId++;
        switch (top.SimpoWinId) {
            case 1:
                top.SimpoWinParent1 = window;
                top.SimpoWinOKCallback1 = okcallback;
                break;
            case 2:
                top.SimpoWinParent2 = window;
                top.SimpoWinOKCallback2 = okcallback;
                break;
            case 3:
                top.SimpoWinParent3 = window;
                top.SimpoWinOKCallback3 = okcallback;
                break;
            default:
                top.SimpoWinParent = window;
                top.SimpoWinOKCallback = okcallback;
                break;
        }
    },

    closeWin: function () {
        var divId = "simpoWin" + (top.SimpoWinId - 1).toString();
        top.$('#' + divId).window('close');
    },

    OK: function (data) {
        switch (top.SimpoWinId) {
            case 1:
                top.SimpoWinOKCallback1(data);
            case 2:
                top.SimpoWinOKCallback2(data);
            case 3:
                top.SimpoWinOKCallback3(data);
            default:
                top.SimpoWinOKCallback(data);
        }
    },

    GetWinParent: function () {
        switch (top.SimpoWinId) {
            case 1:
                return top.SimpoWinParent1;
            case 2:
                return top.SimpoWinParent2;
            case 3:
                return top.SimpoWinParent3;
            default:
                return top.SimpoWinParent;
        }
    },

    showWin2: function (title, containerId, width, height, closecallback) {
        top.$('#' + containerId).css("display", "");
        var l = (top.$(top.window).width() - width) / 2;
        var t = (top.$(top.window).height() - height) / 2 + top.$(top.document).scrollTop();
        top.$('#' + containerId).window({
            modal: true,
            title: title,
             width,
            height: height,
            left: l,
            top: t,
            collapsible: false,
            minimizable: false,
            maximizable: false,
            onClose: function () {
                if (closecallback) closecallback();
            }
        }).window('open');
    },

    closeWin2: function (containerId) {
        top.$('#' + containerId).window('close');
    }
}
View Code

如何使用:

1、引用该插件的JS文件

2、父页面代码:

SimpoWin.showWin("选择素材", "TextMessageList.aspx", 880, 580);

 3、子页面代码:

var winParent = SimpoWin.GetWinParent();
winParent.setValue(val); //子页面操作父页面
SimpoWin.closeWin(); //关闭弹出框
原文地址:https://www.cnblogs.com/s0611163/p/4089346.html