自定义信息提示框dialog.js
var owner;
function getOwner() {
if (owner)
return owner;
var owner = window;
try {
if (top.document.domain == window.document.domain) {
if(top.length > 1)
owner2=top[0];
else
owner = top;
}
} catch (ex) {
while (owner.parent) {
try {
if (owner.parent.document.domain == document.domain) {
owner = owner.parent;
}
} catch (e) {
break;
};
};
};
window.owner = owner;
return owner;
}
Dialog = mySoft.ui.Dialog = function Dialog(title, x, y, width, height) {// dialog为窗口名称
var object = this;
var hwnd = this.Hwnd = parseInt(Math.random() * 1000000000000);
var owner = getOwner();
this.Title = title ? title : "无标题";
this.Width = width ? width : 300;
this.Height = height;
this.X = x ? x : $getDocument().scrollLeft
+ ($getDocument().clientWidth - this.Width) / 2;
this.Y = y ? y : $getDocument().scrollTop
+ ($getDocument().clientHeight - this.Height) / 4;;
owner.window.zIndex = owner.window.zIndex ? owner.window.zIndex + 2 : 9999;
this.zIndex = owner.window.zIndex;
this.Show = function() {
if (document.getElementById("DialogMask" + hwnd)) {
document.getElementById("DialogMask" + hwnd).style.display = "block";
};
document.getElementById("Dialog" + hwnd).style.display = "block";
};
this.Hide = function() {
if (document.getElementById("DialogMask" + hwnd)) {
document.getElementById("DialogMask" + hwnd).style.display = "none";
};
document.getElementById("Dialog" + hwnd).style.display = "none";
document.getElementById("Dialog" + hwnd).oncontextmenu = function() {
return false
};
};
this.Dispose = function() {
try {
var dialog = document.getElementById("Dialog" + hwnd);
if (!dialog)
return;
var dialogMask = document.getElementById("DialogMask" + hwnd);
var dialogContent = document.getElementById("DialogContent" + hwnd);
if (dialogMask) {
dialogMask.parentNode.removeChild(dialogMask);
delete dialogMask;
dialogMask = null;
}
try {
var iframe = dialogContent.getElementsByTagName("iframe")[0];
if (iframe) {
iframe.contentWindow.document.open();
iframe.contentWindow.document.clear();
iframe.contentWindow.document.close();
iframe.parentNode.removeChild(iframe);
}
} catch (ex) {
};
dialog.innerHTML = "";
dialog.parentNode.removeChild(dialog);
delete dialog;
dialog = null;
delete object;
object = null;
if (window.CollectGarbage)
CollectGarbage();
} catch (ex) {
};
};
this.Close = function() {
this.Dispose();
};
this.getContentWindow = function() {
var dialogContent = document.getElementById("DialogContent" + hwnd);
var iframe = dialogContent.getElementsByTagName("iframe")[0];
return iframe.contentWindow;
};
this.showModelessDialog = function() {
var dialogMask = document.getElementById("DialogMask" + hwnd);
if (!dialogMask) {
dialogMask = document.createElement("div");
dialogMask.className = "MaskLayer";
dialogMask.id = "DialogMask" + hwnd;
document.body.appendChild(dialogMask);
dialogMask.style.zIndex = this.zIndex - 1;
dialogMask.style.display = "block";
dialogMask.onmousedown = function(e) {
if (e) {
event = e;
}
event.cancelBubble = true;
};
dialogMask.oncontextmenu = function() {
return false
};
};
dialogMask.style.display = "block";
delete dialogMask;
dialogMask = null;
this.Show();
};
this.AddContent = function(element) {
var dialogContent = document.getElementById("DialogContent" + hwnd);
dialogContent.appendChild(element);
};
this.GetContent = function() {
var dialogContent = document.getElementById("DialogContent" + hwnd);
return dialogContent;
};
this.SetSize = function(width, height) {
var dialog = document.getElementById("Dialog" + hwnd);
dialog.style.width = width + "px";
var dialogContent = document.getElementById("content" + hwnd);
dialogContent.style.height = height + "px";
}
this.setTitle = function(title) {
};
var dialog = document.createElement("div");
dialog.className = "Dialog";
dialog.id = "Dialog" + hwnd;
dialog.hwnd = hwnd;
dialog.style.zIndex = this.zIndex;
dialog.style.left = this.X + "px";
dialog.style.top = this.Y + "px";
dialog.style.width = this.Width + "px";
document.body.appendChild(dialog);
var dialogRange = document.createElement("div");// dialogRange 参数化
dialogRange.className = "DialogRange";
dialog.appendChild(dialogRange);
var header = document.createElement("div");
header.className = "DialogHeader";
header.id = "DialogHeader" + hwnd;
dialogRange.appendChild(header);
header.ondblclick = function(e) {
// object.Max();
};
var headerLeft = document.createElement("div");
headerLeft.className = "DialogHeaderLeft";
header.appendChild(headerLeft);
var expandImage = document.createElement("div");
headerLeft.appendChild(expandImage);
expandImage.className = "DialogHeaderExpand";
expandImage.setAttribute("State", "Expand");
expandImage.onmouseover = function() {
this.className = "DialogHeader" + this.getAttribute("State") + "Over";
};
expandImage.onmouseout = function() {
this.className = "DialogHeader" + this.getAttribute("State");
};
expandImage.onmousedown = function(e) {
var ev = window.event;
if (e) {
ev = e;
ev.srcElement = e.target;
};
ev.cancelBubble = true;
var dialogContent = document.getElementById("content" + hwnd);
if (this.getAttribute("State") == "Expand") {
this.title = "展开";
this.setAttribute("State", "Collapse");
dialogContent.style.display = "none";
} else {
this.title = "折叠";
this.setAttribute("State", "Expand");
dialogContent.style.display = "block";
};
delete dialogContent;
dialogContent = null;
};
var headerCenter = document.createElement("div");
headerCenter.className = "DialogHeaderCenter";
headerCenter.innerHTML = this.Title;
header.appendChild(headerCenter);
var headerRight = document.createElement("div");
headerRight.className = "DialogHeaderRight";
header.appendChild(headerRight);
var closeImage = document.createElement("div");
closeImage.className = "DialogHeaderClose";
closeImage.title = "关闭";
closeImage.onmouseover = function() {
this.className = "DialogHeaderCloseOver";
};
closeImage.onmouseout = function() {
this.className = "DialogHeaderClose";
};
closeImage.onmousedown = function(e) {
var ev = window.event;
if (e) {
ev = e;
ev.srcElement = e.target;
};
ev.cancelBubble = true;
try {
object.Close();
} catch (ex) {
}
};
headerRight.appendChild(closeImage);
var content = document.createElement("div");
content.id = "content" + hwnd;
content.className = "DialogContent";
dialogRange.appendChild(content);
var footer = document.createElement("div");
footer.className = "DialogFooter";
dialogRange.appendChild(footer);
if (this.Height) {
content.style.height = this.Height - header.offsetHeight
- footer.offsetHeight + "px";
content.style.overflow = "auto";
};
var contentMask = document.createElement("div");
contentMask.className = "DialogMask";
contentMask.id = "contentMask" + hwnd;
contentMask.style.display = "none";
content.appendChild(contentMask);
var dialogContent = document.createElement("div");
dialogContent.id = "DialogContent" + hwnd;
dialogContent.className = "DialogContent";
dialogContent.style.height = "100%";
// dialogContent.style.border="solid 1px #000000";
content.appendChild(dialogContent);
var iframe = document.createElement("iframe");
iframe.className = "MaskIframe";
dialog.appendChild(iframe);
iframe.style.height = dialog.offsetHeight - 1 + "px";
header.onmousedown = function(e) {
var ev = window.event;
if (e) {
ev = e;
ev.srcElement = e.target;
};
ev.cancelBubble = true;
var contentMask = document.getElementById("contentMask" + hwnd);
contentMask.style.display = "block";
var dialog = this.parentNode.parentNode;
var header = this;
this.style.cursor = "move";
var offsetX = ev.clientX - $GetLeft(dialog) - $getDocument().scrollLeft;
var offsetY = ev.clientY - $GetTop(dialog) - $getDocument().scrollTop;
document.onmousemove = function(e) {
var ev = window.event;
if (e) {
ev = e;
ev.srcElement = e.target;
};
window.getSelection
? window.getSelection().removeAllRanges()
: document.selection.empty();
ev.cancelBubble = true;
dialog.style.left = ev.clientX - offsetX + "px";
dialog.style.top = ev.clientY - offsetY + "px";
};
document.onmouseup = function(e) {
contentMask.style.display = "none";
header.style.cursor = "";
document.onmousemove = null;
document.onmouseup = null;
delete contentMask;
contentMask = null;
delete dialog;
dialog = null;
delete header;
header = null;
};
};
expandImage = null;
delete expandImage;
closeImage = null;
delete closeImage;
headerLeft = null;
delete headerLeft;
headerCenter = null;
delete headerCenter;
headerRight = null;
delete headerRight;
header = null;
delete header;
contentMask = null;
delete contentMask;
dialogContent = null;
delete dialogContent;
content = null;
delete content;
footer = null;
delete footer;
dialogRange = null;/* */
delete dialogRange;
dialog = null;/* */
delete dialog;
delete iframe;
iframe = null;
$addHandler(window, "unload", Function.createDelegate(this, this.Dispose));
};
$getWindow = mySoft.ui.Dialog.getWindow = function(name) {
var owner = getOwner();
return owner.window[name];
};
Dialog.showMsg = mySoft.ui.Dialog.showInfo = function(message, interval,
onClose, isMark) {
var interval = interval ? interval : 1000;
var hwnd = parseInt(Math.random() * 1000000000000);
var owner = getOwner();
var dialogMask = null;
if (isMark) {
dialogMask = document.createElement("div");
dialogMask.className = "MaskLayer";
document.body.appendChild(dialogMask);
}
var dialog = document.createElement("div");
dialog.className = "DialogMsg";
dialog.hwnd = hwnd;
dialog.innerHTML = message;
document.body.appendChild(dialog);
this.Dispose = function() {
if (dialogMask) {
dialogMask.parentNode.removeChild(dialogMask);
dialogMask = null;
delete dialogMask;
}
dialog.parentNode.removeChild(dialog);
dialog = null;
delete dialog;
if (onClose) {
onClose();
};
}
$addHandler(window, "unload", Function.createDelegate(this, this.Dispose));
window.setTimeout(Function.createDelegate(this, this.Dispose), interval);
}
function OpenDialog(url, title, x, y, width, height, win) {
return openDialog("dialog", url, title, x, y, width, height, win);
}
function openDialog(name, url, title, x, y, width, height, win) {
var owner = getOwner();
if (win)
owner = win;
var dialog = new owner.Dialog(title, x, y, width, height);
dialog.Close = function() {
this.Dispose();
if (owner.window[name]) {
owner.window[name] = null;
}
delete dialog;
dialog = null;
};
owner.window[name] = dialog;
dialog.showModelessDialog();
var dialogContent = dialog.GetContent();
dialogContent.innerHTML = '<iframe frameborder="0" width="100%" height="100%" src="'
+ url
+ '" onload="javascript:this.parentNode.childNodes[1].style.display=\'none\'"></iframe><div class="Loading"><div class="LoadingText">正在加载中...</div></div>';
delete dialogContent;
dialogContent = null;
$addHandler(window, "unload", Function.createDelegate(dialog,
dialog.Dispose));
return dialog;
}
function OpenAjaxDialog(url, title, x, y, width, height) {
var owner = getOwner();
var dialog = new owner.Dialog(title, x, y, width, height);
dialog.Close = function() {
var scripts = top.document.getElementsByTagName("head")[0]
.getElementsByTagName("script");
var length = scripts.length;
for (var s = length - 1; s > 0; s--) {
var script = scripts[s];
if ((script) && (script.getAttribute("id"))) {
script.parentNode.removeChild(script);
}
}
this.Dispose();
};
top.dialog = dialog;
dialog.showModelessDialog();
var dialogContent = dialog.GetContent();
dialogContent.innerHTML = '<div class="Loading"></div>';
var request = new $WebRequest(url, null, OnLoad, OnLoad);
request.Start();
$addHandler(window, "unload", function() {
dialog.Close()
});
function OnLoad(result) {
top.$Eval(dialogContent, result);
}
return dialog;
}
Dialog.Alert = mySoft.ui.Dialog.Alert = function(title, message, onOk, width,
height) {
var owner = getOwner();
var name = "AlertDialog";
// 如果存在则不再弹出
if (owner.window[name])
return;
var dialog = new owner.Dialog(title, null, null, width, null);
owner.window[name] = dialog;
dialog.showModelessDialog();
var dialogContent = dialog.GetContent();
var content = '<div class="DialogMessage"><div class="DialogMessageIcon"></div><div class="DialogMessageContent" align="center">'
+ message + '</div></div>';
content += '<div class="DialogActionBar" align="center"><input type="button" value=" 确定 " class="Button" onmouseover="this.className=\'ButtonOver\'" onmouseout=\"this.className=\'Button\'\" /></div>';
dialogContent.innerHTML = content;
// dialogContent.childNodes[0].style.height=dialogContent.clientHeight-dialogContent.childNodes[1].clientHeight+"px";
var button = dialogContent.childNodes[1].childNodes[0];
button.onclick = function() {
dialog.Close();
if (onOk) {
onOk();
}
};
dialog.Close = function() {
this.Dispose();
if (owner.window[name]) {
owner.window[name] = null;
}
delete dialog;
dialog = null;
};
button.focus();
delete content;
content = null;
delete dialogContent;
dialogContent = null;
$addHandler(owner, "unload", Function
.createDelegate(dialog, dialog.Dispose));
};
Dialog.Confirm = mySoft.ui.Dialog.Confirm = function(title, message, onOk,
onCancel, width, height) {
var owner = getOwner();
var name = "ConfirmDialog";
// 如果存在则不再弹出
if (owner.window[name])
return;
var dialog = new owner.Dialog(title, null, null, width, null);
dialog.Close = function() {
this.Dispose();
if (owner.window[name]) {
owner.window[name] = null;
}
delete dialog;
dialog = null;
};
owner.window[name] = dialog;
dialog.showModelessDialog();
var dialogContent = dialog.GetContent();
var dialogContent = dialog.GetContent();
var content = '<div class="DialogMessage" style="height:'
+ (dialogContent.clientHeight - 35)
+ 'px;"><div class="DialogMessageIcon"></div><div class="DialogMessageContent" align="center">'
+ message + '</div></div>';
content += '<div class="DialogActionBar" align="center"><input type="button" value=" 确定 " class="Button" onmouseover="this.className=\'ButtonOver\'" onmouseout=\"this.className=\'Button\'\" /><input type="button" value=" 取消 " class="Button" onmouseover="this.className=\'ButtonOver\'" onmouseout=\"this.className=\'Button\'\" /></div>';
dialogContent.innerHTML = content;
// dialogContent.childNodes[0].style.height=dialogContent.clientHeight-dialogContent.childNodes[1].clientHeight+"px";
var button = dialogContent.childNodes[1].childNodes[0];
button.onclick = function() {
dialog.Close();
if (onOk) {
onOk();
}
};
button.focus();
button = dialogContent.childNodes[1].childNodes[1];
button.onclick = function() {
dialog.Close();
if (onCancel) {
onCancel();
}
}
delete content;
content = null;
delete dialogContent;
dialogContent = null;
$addHandler(owner, "unload", Function
.createDelegate(dialog, dialog.Dispose));
};
本文来自博客园,作者:Marydon,转载请注明原文链接:https://www.cnblogs.com/Marydon20170307/p/17390490.html