// ---------------------------------------------------------- // ***************** 风声 JS 窗口 V1.00 ***************** // 作者:风声 // 版本:1.00 Preview // 网站:http://www.fonshen.com // 邮件:webmaster@fonshen.com // 版权:版权全体,源代码公开,各种用途均可免费使用 // ********************************************************** // ---------------------------------------------------------- var _window = new _Window("_window", "VISTA"); function _Window(codeName, className, width, height) { this.CodeName = codeName; this.ClassName = className; this.Version = "Ant JS Window V1.00.07.08.29 Preview" this.Width = width || 400; this.Height = height || 300; this.ActTimer = 10; this.Clocks = new Array(); this.zIndex = 0; this.wIndex = 0; this.Copy = null; this.onmousemove = document.onmousemove; this.onmouseup = document.onmouseup; this.onselectstart = document.onselectstart; } _Window.prototype.GetAttribute = function(source, name) { var reg = new RegExp("(^|,|\\s)"+ name +"\\s*=\\s*([^,]*)(\\s|,|$)", "i"); if (reg.test(source)) return RegExp.$2; return ""; } _Window.prototype.GetMaxNumber = function() { var num = 0; for(var i=0; i num) num = arguments[i]; return num; } _Window.prototype.Open = function(body, title, features) { var className = this.GetAttribute(features, "class") || this.ClassName; if(!className)return null; var width = parseInt(this.GetAttribute(features, "width")) || this.Width; var height = parseInt(this.GetAttribute(features, "height")) || this.Height; var left = this.GetAttribute(features, "left") || parseInt(Math.random() * 500); var top = this.GetAttribute(features, "top") || parseInt(Math.random() * 300); var resize = this.GetAttribute(features, "resize").toLowerCase() || "resize"; var move = this.GetAttribute(features, "move").toLowerCase() || "move"; var NewWin = document.createElement("div"); NewWin.setAttribute("id", "_W_" + (++this.wIndex)); NewWin.className = className; NewWin.style.position = "absolute"; NewWin.style.zIndex = ++this.zIndex; NewWin.style.left = left + "px"; NewWin.style.top = top + "px"; document.body.appendChild(NewWin); var styles = new Array("LEFT_TOP","TOP","RIGHT_TOP","RIGHT","RIGHT_BOTTOM","BOTTOM","LEFT_BOTTOM","LEFT","CENTER","CLOSE"); var sizes = new Array(); for(var i=0;i<10;i++) { var obj = document.createElement("div"); obj.className = styles[i]; obj.style.position = "absolute"; NewWin.appendChild(obj); sizes[i*2] = obj.offsetWidth; sizes[i*2+1] = obj.offsetHeight; if(i > 7 || move == "no") eval("obj.onmousedown = function(e){ " + this.CodeName + ".Show(this.parentNode); };"); else eval("obj.onmousedown = function(e){ " + this.CodeName + ".PreMove(this.parentNode, e ? e : window.event, move); };"); } NewWin.childNodes[8].style.width = width + "px"; NewWin.childNodes[8].setAttribute("NowWidth", width); height = this.Body(NewWin, body, height); NewWin.childNodes[8].style.height = height + "px"; NewWin.childNodes[8].setAttribute("NowHeight", height); NewWin.childNodes[8].style.left = (sizes[14] + NewWin.childNodes[8].offsetLeft) + "px"; NewWin.childNodes[8].style.top = (sizes[3] + NewWin.childNodes[8].offsetTop) + "px"; NewWin.style.width = (width + sizes[6] + sizes[14]) + "px"; NewWin.style.height = (height + sizes[3] + sizes[11]) + "px"; NewWin.setAttribute("MinWidth", this.GetMaxNumber(sizes[0] + sizes[4], sizes[6] + sizes[14] + sizes[18], sizes[8] + sizes[12]) + 20); NewWin.setAttribute("MinHeight", this.GetMaxNumber(sizes[1] + sizes[13], sizes[3] + sizes[11], sizes[5] + sizes[9]) + 2); NewWin.childNodes[1].style.width = (width + sizes[6] + sizes[14] - sizes[0] - sizes[4]) + "px"; NewWin.childNodes[3].style.height = (height + sizes[3] + sizes[11] - sizes[5] - sizes[9]) + "px"; NewWin.childNodes[5].style.width = (width + sizes[6] + sizes[14] - sizes[8] - sizes[12]) + "px"; NewWin.childNodes[7].style.height = (height + sizes[3] + sizes[11] - sizes[13] - sizes[1]) + "px"; NewWin.childNodes[0].style.left = "0px"; NewWin.childNodes[0].style.top = "0px"; NewWin.childNodes[1].style.left = sizes[0] + "px"; NewWin.childNodes[1].style.top = "0px"; NewWin.childNodes[2].style.right = "0px"; NewWin.childNodes[2].style.top = "0px"; NewWin.childNodes[3].style.right = "0px"; NewWin.childNodes[3].style.top = sizes[5] + "px"; NewWin.childNodes[4].style.right = "0px"; NewWin.childNodes[4].style.bottom = "0px"; NewWin.childNodes[5].style.left = sizes[12] + "px"; NewWin.childNodes[5].style.bottom = "0px"; NewWin.childNodes[6].style.left = "0px"; NewWin.childNodes[6].style.bottom = "0px"; NewWin.childNodes[7].style.left = "0px"; NewWin.childNodes[7].style.top = sizes[1] + "px"; if(resize != "no") { this.AppendResize(NewWin, "se-resize", "width:15px;height:15px;right:" + (sizes[6] - 13) + "px;bottom:" + (sizes[11] - 13) + "px;", resize, 0); this.AppendResize(NewWin, "sw-resize", "width:15px;height:15px;left:" + (sizes[14] - 13) + "px;bottom:" + (sizes[11] - 13) + "px;", resize, 1); if(resize != "resize-y") { this.AppendResize(NewWin, "w-resize", "width:5px;height:" + (height - 2) + "px;left:" + (sizes[14] - 5) + "px;bottom:" + (sizes[11] + 2) + "px;", "resize-x", 1); this.AppendResize(NewWin, "e-resize", "width:5px;height:" + (height - 2) + "px;right:" + (sizes[6] - 5) + "px;bottom:" + (sizes[11] + 2) + "px;", "resize-x", 0); } if(resize != "resize-x") this.AppendResize(NewWin, "s-resize", "width:" + (width - 4) + "px;height:5px;left:" + (sizes[14] + 2) + "px;bottom:" + (sizes[11] - 5) + "px;", "resize-y", 0); } this.Title(NewWin, title); eval("NewWin.childNodes[9].onclick = function(e){ " + this.CodeName + ".Close(this.parentNode); };"); return(NewWin); } _Window.prototype.Title = function(o, title) { o.childNodes[1].innerHTML = "
" + title + "
"; } _Window.prototype.Body = function(o, body, height) { if(body.slice(0, 5) == "[url]") { var str = (height < 0) ? "onload=\"" + this.CodeName + ".ResizeBy(this.parentNode.parentNode, 0, frames[frames.length-1].document.documentElement.scrollHeight)\"" : ""; o.childNodes[8].innerHTML = ""; return((height < 0) ? 0 : height); } else { o.childNodes[8].innerHTML = "
" + body + "
"; return((height < 0) ? o.childNodes[8].firstChild.offsetHeight + 1 : height); } } // ---- 添加 resize 区 ---- // _Window.prototype.AppendResize = function(o, cursor, shape, resize, origin) { var obj = document.createElement("DIV"); o.appendChild(obj); obj.style.cssText = "position:absolute;cursor:" + cursor + ";" + shape; eval("obj.onmousedown = function(e){ " + this.CodeName + ".PreResize(this.parentNode, e ? e : window.event, \"" + cursor + "\", \"" + resize + "\", " + origin + "); };"); } // ---- 复制 shadow 副本 ---- // _Window.prototype.Duplicate = function(o) { this.Copy = document.createElement("DIV"); document.body.appendChild(this.Copy); this.Copy.style.cssText = "position:absolute;left:" + (o.offsetLeft - 2) + "px;top:" + (o.offsetTop-2) + "px;width:" + o.offsetWidth + "px;height:" + o.offsetHeight + "px;border:2px dotted #000000;"; this.Copy.style.zIndex = this.zIndex + 1; } // ---- 注销事件 ---- // _Window.prototype.DetachEvent = function(o, e) { document.body.style.cursor = "default"; document.onmousemove = this.onmousemove; document.onmouseup = this.onmouseup; document.onselectstart = this.onselectstart; if(o.releaseCapture) o.releaseCapture(); } // ---- 注册事件 ---- // _Window.prototype.AttachEvent = function(o, e) { if(e.preventDefault) e.preventDefault(); else { document.onselectstart = function() { return false; }; if(o.setCapture) o.setCapture(); } } // ---- 显示窗口,调到最上层 ---- // _Window.prototype.Show = function(o) { if(o.style.zIndex < this.zIndex) o.style.zIndex = ++this.zIndex; } // ---- 关闭窗口 ---- // _Window.prototype.Close = function(o) { var oid = o.id; if (typeof(this.Clocks["M" + oid]) != "undefined") clearTimeout(this.Clocks["M" + oid]); if (typeof(this.Clocks["R" + oid]) != "undefined") clearTimeout(this.Clocks["R" + oid]); document.body.removeChild(o); } // ---- 准备调整窗口大小 ---- // _Window.prototype.PreResize = function(o, e, cursor, t, z) { if (typeof(this.Clocks["R" + o.id]) != "undefined") clearTimeout(this.Clocks["R" + o.id]); this.Show(o); this.Duplicate(o); var x, y = e.clientY - this.Copy.offsetHeight;; if(z) { o.style.right = (document.documentElement.scrollWidth - o.offsetWidth - o.offsetLeft) + "px"; o.style.left = null; this.Copy.style.right = (document.documentElement.scrollWidth - this.Copy.offsetWidth - this.Copy.offsetLeft) + "px"; this.Copy.style.left = null; x = e.clientX + this.Copy.offsetWidth; } else { o.style.left = o.offsetLeft + "px"; this.Copy.style.left = this.Copy.offsetLeft + "px"; x = e.clientX - this.Copy.offsetWidth; } document.body.style.cursor = cursor; eval("document.onmousemove = function(e) { " + this.CodeName + ".Resize(o, e ? e : window.event, x, y, t, z); };"); eval("document.onmouseup = function(e) { " + this.CodeName + ".Resized(o, e ? e : window.event, x, y, t, z); };"); this.AttachEvent(o, e); } // ---- 拖拽调整窗口(shadow 副本)大小 ---- // _Window.prototype.Resize = function(o, e, x, y, t, z) { if(t != "resize-y") { var min_w = parseInt(o.getAttribute("MinWidth")); var w = z ? x - e.clientX : e.clientX - x; if(w >= min_w) this.Copy.style.width = w + "px"; } if(t != "resize-x") { var min_h = parseInt(o.getAttribute("MinHeight")); var h = e.clientY - y; if(h >= min_h) this.Copy.style.height = h + "px"; } } // ---- 完成拖拽调整窗口大小 ---- // _Window.prototype.Resized = function(o, e, x, y, t, z) { this.DetachEvent(o, e); var dx = 0, dy = 0; if(t != "resize-y") { var min_w = parseInt(o.getAttribute("MinWidth")); var w = z ? x - e.clientX - 4 : e.clientX - x -4; w = (w > min_w) ? w : min_w; dx = w - o.offsetWidth; } if(t != "resize-x") { var min_h = parseInt(o.getAttribute("MinHeight")); var h = (e.clientY - y - 4 > min_h) ? (e.clientY - y - 4) : min_h; dy = h - o.offsetHeight; } if(this.ActTimer > 0) this.ActResizeBy(o.id, dx, dy, t); else this.ResizeBy(o, dx, dy, t); document.body.removeChild(this.Copy); this.Copy = null; } // ---- 调整窗口大小 ---- // _Window.prototype.ResizeBy = function(o, dx, dy, t) { if(dx) { o.style.width = (o.offsetWidth + dx) + "px"; o.childNodes[1].style.width = (o.childNodes[1].offsetWidth + dx) + "px"; o.childNodes[5].style.width = (o.childNodes[5].offsetWidth + dx) + "px"; if(o.childNodes[14]) o.childNodes[14].style.width = (o.childNodes[14].offsetWidth + dx) + "px"; var width = parseInt(o.childNodes[8].getAttribute("NowWidth")) + dx; o.childNodes[8].setAttribute("NowWidth", width); o.childNodes[8].style.width = width + "px"; } if(dy) { o.style.height = (o.offsetHeight + dy) + "px"; o.childNodes[3].style.height = (o.childNodes[3].offsetHeight + dy) + "px"; o.childNodes[7].style.height = (o.childNodes[7].offsetHeight + dy) + "px"; if(o.childNodes[13]) { o.childNodes[12].style.height = (o.childNodes[12].offsetHeight + dy) + "px"; o.childNodes[13].style.height = (o.childNodes[13].offsetHeight + dy) + "px"; } var height = parseInt(o.childNodes[8].getAttribute("NowHeight")) + dy; o.childNodes[8].setAttribute("NowHeight", height); o.childNodes[8].style.height = height + "px"; } } // ---- 动画调整窗口大小 ---- // _Window.prototype.ActResizeBy = function(oid, dx, dy, t) { if(dx || dy) { var x = dx/10; x = (x > 0) ? Math.ceil(x) : Math.floor(x); var y = dy/10; y = (y > 0) ? Math.ceil(y) : Math.floor(y); var o = document.getElementById(oid); this.ResizeBy(o, x, y, t); dx -= x; dy -= y; this.Clocks["R" + oid] = window.setTimeout(this.CodeName + ".ActResizeBy(\"" + oid + "\"," + dx + "," + dy + ",\"" + t + "\")", this.ActTimer); } } // ---- 准备移动窗口 ---- // _Window.prototype.PreMove = function(o, e, t) { if (typeof(this.Clocks["M" + o.id]) != "undefined") clearTimeout(this.Clocks["M" + o.id]); this.Show(o); this.Duplicate(o); var x = e.clientX - this.Copy.offsetLeft; var y = e.clientY - this.Copy.offsetTop; document.body.style.cursor = "move"; eval("document.onmousemove = function(e) { " + this.CodeName + ".Move(o, e ? e : window.event, x, y, t); };"); eval("document.onmouseup = function(e) { " + this.CodeName + ".Moved(o, e ? e : window.event, x, y, t); };"); this.AttachEvent(o, e); } // ---- 拖拽移动窗口(shadow 副本) ---- // _Window.prototype.Move = function(o, e, x, y, t) { if(t != "move-y") this.Copy.style.left = (e.clientX - x) + "px"; if(t != "move-x") this.Copy.style.top = (e.clientY - y) + "px"; } // ---- 完成拖拽移动窗口 ---- // _Window.prototype.Moved = function(o, e, x, y, t) { this.DetachEvent(o, e); var tx = (t == "move-y") ? null : (e.clientX - x + 2); var ty = (t == "move-x") ? null : (e.clientY - y + 2); if(this.ActTimer > 0) this.ActMoveTo(o.id, tx, ty, o.offsetLeft, o.offsetTop); else this.MoveTo(o, tx, ty); document.body.removeChild(this.Copy); this.Copy = null; } // ---- 移动窗口到 ---- // _Window.prototype.MoveTo = function(o, tx, ty) { if(tx != null) o.style.left = tx + "px"; if(ty != null) o.style.top = ty + "px"; } // ---- 动画移动窗口到 ---- // _Window.prototype.ActMoveTo = function(oid, tx, ty, ix, iy) { if((tx == null || tx == ix) && (ty == null || ty == iy)) return; if(tx != null) { var dx = (tx - ix)/10; dx = (dx > 0) ? Math.ceil(dx) : Math.floor(dx); ix += dx; } if(ty != null) { var dy = (ty - iy)/10; dy = (dy > 0) ? Math.ceil(dy) : Math.floor(dy); iy += dy; } var o = document.getElementById(oid); this.MoveTo(o, ix, iy); this.Clocks["M" + oid] = window.setTimeout(this.CodeName + ".ActMoveTo(\"" + oid + "\"," + tx + "," + ty + "," + ix + "," + iy + ")", this.ActTimer); }