/** * 弹出层窗口对象 * * 注:本库依赖于jQuery和common两个js库 * 该对象依赖document.body对象,所以请在页面载入后再创建对象 * @param {} options * -title: 标题 * -content: 内容 * -colseIsHide: 点击关闭时是否隐藏窗体。true-隐藏;false-销毁[默认] * -width: 主体宽度[最小200px] * -height: 主体高度 * -padding: 主体边框与内容间距设置 * -showClose: 是否显示关闭按钮 默认true[显示] * -draggable: 是否打开拖动功能 默认false[否] * -autoScroll: 当内容超过窗口尺寸时,是否自动显示滚动条 默认true[是] * * @method setWidth(width:String|Integer) 设置窗体宽度 * @method setHeight(height:String|Integer) 设置窗体高度 * @method css(css:{}) 设置窗体css样式 * @method setTitle(title:String|DOM|jQueryElement) 设置窗体标题 * @method setContent(content:String|DOM|jQueryElement) 设置窗体内容 * @method getContent() 获得所有插入后的内容[以jQuery中的contents()方法形式返回] * @method appendContent(content:String|DOM|jQueryElement) 在原窗体内容后插入窗体内容 * @method prependContent(content:String|DOM|jQueryElement) 在原窗体内容前插入窗体内容 * @method show() 显示窗体内容 * @method hide() 隐藏窗体内容 * @method draggable(boolean) 是否支持拖动窗体 * @method destroy() 销毁窗体内容 * * @author Jesse Lu * @version 2010/04/02 */ function AutoDialog(options) { this.defaults = { title : "", content : "", showClose : true, closeIsHide : false, draggable: false, width : "auto", height : "auto", padding : "5px", autoScroll : true }; if (options) { for (var i in options) { this.defaults[i] = options[i]; } } this.longTime = new Date().getMilliseconds(); // 产生时间码 this.focusElement = null; // 打开Dialog前的焦点对象 // 窗体HTML代码 this.dialogBody = '' + '' + ''; this.dialog = null; // 弹出窗体的jQueryDOM对象 this.titleDOM = null; // 标题jQueryDOM对象 this.contentDOM = null; // 内容jQueryDOM对象 /* * 拖动事件属性 */ this.dragDOM = null; // 可拖动的区域jQueryDOM对象 this.dragIsDown = false; // 拖动事件移动判断参数 this._left = 0; // 窗体对于浏览器的左位移举例 this._top = 0; // 窗体对于浏览器的上位移举例 this._clientX = 0; // 记录拖动前鼠标定位X坐标 this._clientY = 0; // 记录拖动前鼠标定位Y坐标 // 拖动移动时方法 this.dragMoveHandle = (function(e) { if (this.dragIsDown && e.clientX > 0 && e.clientY > 0 && e.clientX < this.window.width() && e.clientY < this.window.height()) { this.dialog.css("left", (this._left - this._clientX + e.clientX) + "px"); this.dialog.css("top", (this._top - this._clientY + e.clientY) + "px"); } }).domain(this); if (!AutoDialog._initialized) { AutoDialog.prototype.documentBody = $(document.body); this.documentBody.append('
'); //loadScript('/ntsms/common/css/AutoDialog.css', 'css'); AutoDialog.prototype.window = $(window); AutoDialog.prototype.overlay = $('#overlay20100331'); AutoDialog.prototype.focusInput = this.overlay.children(".focus20100331"); // 初始化方法 AutoDialog.prototype.init = function() { this.documentBody.append(this.dialogBody); this.titleDOM = $("#dialogTitle20100331" + this.longTime); this.contentDOM = $("#boxContent20100331" + this.longTime); this.contentDOM.append(this.defaults.content); this.contentDOM.css({ "width" : this.defaults.width, "height" : this.defaults.height, "padding" : this.defaults.padding }); this.dialog = $('#dialog20100331' + this.longTime); this.dragDOM = this.dialog.find('.boxTit20100331'); this.dialog.css({ 'font-family' : '"宋体",Arial', 'font-size' : '12px', 'position' : 'absolute', 'z-index' : '90' }); $("#closeDialog20100331" + this.longTime).click((function() { this.defaults.closeIsHide ? this.hide() : this.destroy(); }).domain(this)); this.window.resize((function() { this.overlay.css({ 'height' : $(document).height(), 'width' : $(document).width() }); }).domain(this)); } AutoDialog.prototype.setWidth = function(w) { this.contentDOM.css("width", w); } AutoDialog.prototype.setHeight = function(h) { this.contentDOM.css("height", h); } AutoDialog.prototype.css = function(css) { this.contentDOM.css(css); } AutoDialog.prototype.setTitle = function(t) { this.titleDOM.empty(); this.titleDOM.append(t); } AutoDialog.prototype.setContent = function(c) { this.contentDOM.empty(); this.contentDOM.append(c); } AutoDialog.prototype.getContent = function() { return this.contentDOM.contents(); } AutoDialog.prototype.appendContent = function(c) { this.contentDOM.append(c); } AutoDialog.prototype.prependContent = function(c) { this.contentDOM.prepend(c); } AutoDialog.prototype.show = function() { this.focusElement = $(document.activeElement); this.focusInput.css({ 'top' : this.focusElement.offset().top, 'left' : this.focusElement.offset().left }); this.dialog.show(); if (this.defaults.autoScroll) { if (this.dialog.height() >= this.window.height()) { this.contentDOM.css({"height" : this.window.height() - 30}); } if (this.dialog.width() >= this.window.width()) { this.contentDOM.css({"width" : this.window.width() - 30}); } } this.dialog.center(); this.overlay.css({ 'height' : $(document).height(), 'width' : $(document).width() }); //this.overlay.hide(); // 偶尔会产生不显示遮罩层的bug,先隐藏再显示即可 this.overlay.show(); } // 隐藏窗体 AutoDialog.prototype.hide = function() { this.dialog.hide(); this.overlay.hide(); this.contentDOM.css({"height" : this.defaults.height, "width" : this.defaults.width}); } // 拖动事件 AutoDialog.prototype.draggable = function(bool) { if (bool) { this.dragDOM.css("cursor", "move"); var thisDialog = this; this.dragDOM.mousedown((function(e) { this._left = this.dialog.offset().left; this._top = this.dialog.offset().top; this._clientX = e.clientX; this._clientY = e.clientY; this.dragIsDown = true; try { this.dragDOM.get(0).setCapture(); } catch (e) { }; this.dialog.find(".box20100331").css("opacity", "0.5"); }).domain(thisDialog)); // 拖动时的事件绑定FF和IE if (document.addEventListener) { document.addEventListener("mousemove", this.dragMoveHandle, false); } else { this.dragDOM.mousemove(this.dragMoveHandle); } this.dragDOM.mouseup((function(e) { this.dragIsDown = false; try { this.dragDOM.get(0).releaseCapture(); } catch (e) { }; this.dialog.find(".box20100331").css("opacity", "1"); }).domain(thisDialog)); } else { this.dragDOM.css("cursor", "auto"); this.dragDOM.unbind(); if (document.removeEventListener) { document.removeEventListener("mousemove", this.dragMoveHandle, false); } } } // 销毁方法 AutoDialog.prototype.destroy = function() { this.focusInput.focus(); // 防止焦点丢失 this.focusElement.focus(); // 焦点还原 this.dialog.remove(); this.overlay.hide(); } AutoDialog._initialized = true; } this.init(); this.draggable(this.defaults.draggable); } $(function(){ loadScript('/ntsms/common/css/AutoDialog.css', 'css'); }); /** * 窗体居中方法 */ jQuery.prototype.center = function() { var $this = $(this); var $window = $(window); $this.css("position", "absolute"); if ($this.height() > $window.height()) { $this.css("top", $window.scrollTop()); } else { $this.css("top", $window.scrollTop() + ($window.height() - $this.height()) / 2); } if ($this.width() > $window.width()) { $this.css("left", $window.scrollLeft()); } else { $this.css("left", $window.scrollLeft() + ($window.width() - $this.width()) / 2); } }