$.loading = function (bool, text) {
var $loadingpage = top.$("#loadingPage");
var $loadingtext = $loadingpage.find('.loading-content');
if (bool) {
$loadingpage.show();
} else {
if ($loadingtext.attr('istableloading') == undefined) {
$loadingpage.hide();
}
}
if (!!text) {
$loadingtext.html(text);
} else {
$loadingtext.html("数据加载中,请稍后…");
}
$loadingtext.css("left", (top.$('body').width() - $loadingtext.width()) / 2 - 50);
$loadingtext.css("top", (top.$('body').height() - $loadingtext.height()) / 2);
}
$.getLayerIframeId = function (layerId) {
var iframeId = top.$("#" + layerId).find("iframe").attr("id");
return iframeId;
}
$.currentWindow =function () {
var iframeId = top.$(".iframe:visible").attr("data-id");
var target = top.$('.iframe[data-id="' + iframeId + '"]');
return top.$(window.parent.document).contents().find(target)[0].contentWindow;
}
$.modalOpen = function (options) {
var defaults = {
id: null,
title: '系统窗口',
width: "100px",
height: "100px",
url: '',
shade: 0.3,
btn: ['确认', '关闭'],
btnclass: ['btn btn-primary', 'btn btn-danger'],
callBack: null
};
var options = $.extend(defaults, options);
var _width = top.$(window).width() > parseInt(options.width.replace('px', '')) ? options.width : top.$(window).width() + 'px';
var _height = top.$(window).height() > parseInt(options.height.replace('px', '')) ? options.height : top.$(window).height() + 'px';
top.layer.open({
id: options.id,
type: 2,
shade: options.shade,
title: options.title,
fix: false,
maxmin: true, //开启最大化最小化按钮
area: [_width, _height],
content: options.url,
tipsMore:options.tipsMore,
btn: options.btn,
yes: function (index,layero) {
$(window.parent.document).contents().find(layero.find('iframe')[0])[0].contentWindow.submitForm();
}, cancel: function () {
return true;
}
});
}
$.modalConfirm = function (content, callBack) {
top.layer.confirm(content, {
icon: "fa-exclamation-circle",
title: "系统提示",
btn: ['确认', '取消'],
btnclass: ['btn btn-primary', 'btn btn-danger'],
}, function () {
callBack(true);
}, function () {
callBack(false)
});
}
$.modalAlert = function (content, type) {
var icon = "";
if (type == '0') {
icon = 1;
}else if (type == '-100') {
icon = 2;
}else{
icon = 7;
}
top.layer.alert(content, {
icon: icon,
title: "系统提示",
skin: 'layui-layer-lan',
btn: ['确认'],
btnclass: ['btn btn-primary'],
});
}
$.modalMsg = function (content,type) {
if (type != undefined) {
var icon = "";
if (type == '0') {
icon = 1;
}else if (type == '-100') {
icon = 2;
}else{
icon = 7;
}
top.layer.msg(content, { icon: icon, time: 2000, shift: 5 });
top.$(".layui-layer-msg").find('i.' + icon).parents('.layui-layer-msg').addClass('layui-layer-msg-' + type);
} else {
top.layer.msg(content);
}
}
$.modalClose = function () {
var index = top.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
var $IsdialogClose = top.$("#layui-layer" + index).find('.layui-layer-btn').find("#IsdialogClose");
var IsClose = $IsdialogClose.is(":checked");
if ($IsdialogClose.length == 0) {
IsClose = true;
}
if (IsClose) {
top.layer.close(index);
} else {
location.reload();
}
}
$.submitForm = function (options) {
var defaults = {
url: "",
param: [],
loading: "正在提交数据...",
success: null,
close: true
};
var options = $.extend(defaults, options);
$.loading(true, options.loading);
window.setTimeout(function () {
$.ajax({
url: options.url,
data: options.param,
type: "post",
dataType: "json",
success: function (data) {
if (data.state == "0") {
options.success(data);
$.modalMsg(data.message, data.state);
if (options.close == true) {
$.modalClose();
}
} else {
$.modalMsg(data.message, data.state);
}
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
$.loading(false);
$.modalMsg(errorThrown, "error");
},
beforeSend: function () {
$.loading(true, options.loading);
},
complete: function () {
$.loading(false);
}
});
}, 500);
}
$.deleteForm = function (options) {
var defaults = {
prompt: "注:您确定要删除该项数据吗?",
url: "",
param: [],
loading: "正在删除数据...",
success: null,
close: true
};
var options = $.extend(defaults, options);
$.modalConfirm(options.prompt, function (r) {
if (r) {
$.loading(true, options.loading);
window.setTimeout(function () {
$.ajax({
url: options.url,
data: options.param,
type: "post",
dataType: "json",
success: function (data) {
if (data.state == "success") {
options.success(data);
$.modalAlert(data.message, data.state);
} else {
$.modalAlert(data.message, data.state);
}
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
$.loading(false);
$.modalMsg(errorThrown, "error");
},
beforeSend: function () {
$.loading(true, options.loading);
},
complete: function () {
$.loading(false);
}
});
}, 500);
}
});
}
layer 二次封装
最新推荐文章于 2023-10-07 15:13:33 发布