js封装openDialog方法

function openDialog(args, cb) {
    console.log("args", args);
    //var rootFontSize = parseInt($("html").css('fontSize'));

    // debugger
    var height = 500;
    var width = 660;
    if (args.height) {
        if (args.height.toString().indexOf('%') === -1) {
            height = args.height;
        } else {
            //百分比调整
            height = parseInt(args.height.toString().replace('%', '')) / 100 * window.innerHeight;
        }
    } else if (args.full) {
        height = window.innerHeight;
    }

    if (args.width) {
        if (args.width.toString().indexOf('%') === -1) {
            width = args.width;
        } else {
            //百分比调整
            width = parseInt(args.width.toString().replace('%', '')) / 100 * window.innerWidth;
        }
    } else if (args.full) {
        width = window.innerWidth;
    }

    var zoom = $("body").css("zoom") || 1;
    console.log("zoom", zoom);
    height = height * zoom;
    width = width * zoom;
    //height = (height / window.devicePixelRatio);
    //width = (width / window.devicePixelRatio);

    height = Math.min(height, $(window).height() - 20);
    width = Math.min(width, $(window).width() - 20);
    //height = Math.min(height, window.innerHeight - 20);
    //width = Math.min(width, window.innerWidth - 20);

    height = height + "px";
    width = width + "px";


    var closeBtn = 1;
    if (args.closeBtn === 0) {
        closeBtn = 0;
    }

    //右上角关闭时候是否提示
    var isConfirm = 0;
    if (args.isConfirm === 1) {
        isConfirm = 1;
    }

    var maxmin = false;
    if (args.maxmin === true) {
        maxmin = true;
    }

    var moveEl = ".layui-layer-title";
    if (args.move == false) {
        moveEl = false;
    }

    var resize = true;
    if (args.resize == false) {
        resize = false;
    }

    // 格式化Url
    try {
        // 将..替换成全路径
        if (getRootUrl && typeof (getRootUrl) == "function") {
            args.url = args.url.replace("../", getRootUrl());
        }
    } catch (e) {

    }

    var type = typeof (args.url) === "string" ? 2 : 1;
    var index = clayer.open({
        type: type,
        title: args.title || "",
        maxmin: maxmin,
        moveType: 1,        //拖拽风格,0是默认,1是传统拖动
        move: moveEl,        //是否允许拖动
        resize: resize,      //是否允许拉伸
        shift: 0,           //0-6的动画形式,-1不开启
        shadeClose: false,  //是否点击遮罩关闭
        area: [width, height],
        content: args.url || "",
        closeBtn: closeBtn,
        end:args.end,
        success: function (layero, index) {
            // 关闭loading...
            hideLoad();

        },
        cancel: function (index) {
            // 维护dialogArray中数据
            if (isConfirm == 1) {
                layer.confirm('确定关闭吗?', function (index2) {
                    console.log("关闭");
                    layer.close(index2);

                    if (args.cancel) {
                        args.cancel();
                    }
                    var start = -1;
                    $.each(dialogArray, function (i, value) {
                        if (value.index == index) {
                            start = i;
                        }
                    });
                    // 如果搜索出数据,则进行删除
                    if (start != -1) {
                        dialogArray.splice(start, 1);
                    }

                    layer.close(index);
                });
                return false;
            } else {
                if (args.cancel) {
                    args.cancel();
                }
                var start = -1;
                $.each(dialogArray, function (i, value) {
                    if (value.index == index) {
                        start = i;
                    }
                });
                // 如果搜索出数据,则进行删除
                if (start != -1) {
                    dialogArray.splice(start, 1);
                }

            }
        }
    });

    dialogArray.push({
        index: index,
        callback: cb
    });
    //console.log("dialogArray", dialogArray);
    // 显示loading...
    //showLoad();
    //if (type != 1) {
    //    clayer.load();
    //}
}

js封装openDialog方法_第1张图片

你可能感兴趣的:(vue.js,前端,javascript,elementui)