// 扩展 $.browser
function uaMatch(a){a=a.toLowerCase();var b=rwebkit.exec(a)||ropera.exec(a)||rmsie.exec(a)||a.indexOf("compatible")<0&&rmozilla.exec(a)||[];return{browser:b[1]||"",version:b[2]||"0"}}jQuery.extend({browser:function(){var e={},f=window.navigator.userAgent,g=uaMatch(f);return g.browser&&(e[g.browser]=!0,e.version=g.version),{browser:e}}});
(function($) {
var cachedata = {};
var arrweebox = new Array();
var weebox = function(content,options) {
var self = this;
this.dh = null;
this.mh = null;
this.dc = null;
this.dt = null;
this.db = null;
this.selector = null;
this.ajaxurl = null;
this.options = null;
this._dragging = false;
this._content = content || '';
this._options = options || {};
this.ajaxresult = null;
this._defaults = {
boxid: null,
boxclass: null,
cache: false,
type: 'dialog',
title: '',
width: 0,
height: 0,
timeout: 0,
draggable: true,
modal: true,
focus: null,
blur: null,
position: 'center',
topheight:120,
overlay: 30,
showTitle: true,
showClose: true,
showButton: true,
showCancel: true,
showHeader: true,
showOk: true,
isFull:true,
isCloseToHide:false,
okBtnName: '确定',
cancelBtnName: '取消',
contentType: 'text',
contentChange: false,
clickClose: false,
zIndex: 999,
animate: '',
showAnimate:'',
hideAnimate:'',
onclose: null,
onopen: null,
onready:null,
oncancel: null,
onok: null,
suggest:{url:'',tele:'',vele:'',fn:null},
ajaxdata:null,
select:{url:'',type:'radio', tele:'',vele:'',width:120,search:false,fn:null}
};
//初始化选项
this.initOptions = function() {
self._options = self._options || {};
self._options.animate = self._options.animate || '';
self._options.showAnimate = self._options.showAnimate || self._options.animate;
self._options.hideAnimate = self._options.hideAnimate || self._options.animate;
self._options.type = self._options.type || 'dialog';
self._options.title = self._options.title || '';
self._options.boxclass = self._options.boxclass || 'wee'+self._options.type;
self._options.contentType = self._options.contentType || "";
if (self._options.contentType == "") {
self._options.contentType = (self._content.substr(0,1) == '#') ? 'selector' : 'text';
}
self.options = $.extend({}, self._defaults, self._options);
self._options = null;
self._defaults = null;
};
//初始化弹窗Box
this.initBox = function() {
var html = '';
switch(self.options.type) {
case 'alert':
case 'select':
case 'dialog':
html = '
';
break;
case 'custom':
case 'suggest':
html = '';
break;
}
self.dh = $(html).appendTo('body').hide().css({
position: 'absolute',
overflow: 'hidden',
zIndex: self.options.zIndex
});
self.dc = self.find('.dialog-content');
self.dt = self.find('.dialog-title');
self.db = self.find('.dialog-button');
self.dhh = self.find('.dialog-header');
if (self.options.boxid) {
self.dh.attr('id', self.options.boxid);
}
if (self.options.boxclass) {
self.dh.addClass(self.options.boxclass);
}
if (self.options.height>0) {
self.dc.css('height', self.options.height);
}
if (self.options.width>0) {
self.dh.css('width', self.options.width);
}
self.dh.bgiframe();
}
//初始化遮照
this.initMask = function() {
if (self.options.modal) {
if ($.browser.msie) {
h= document.compatMode == "CSS1Compat" ? document.documentElement.clientHeight : document.body.clientHeight;
w= document.compatMode == "CSS1Compat" ? document.documentElement.clientWidth : document.body.clientWidth;
} else {
h= self.bheight();
w= self.bwidth();
}
self.mh = $("")
.appendTo('body').hide().css({
width: w,
height: h,
zIndex: self.options.zIndex-1
}).bgiframe();
}
}
//初始化弹窗内容
this.initContent = function(content) {
self.dh.find(".dialog-ok").val(self.options.okBtnName);
self.dh.find(".dialog-cancel").val(self.options.cancelBtnName);
if (self.options.title == '') {
//self.dt.hide();
//self.dt.html(self._titles[self._options.type] || '');
} else {
self.dt.html(self.options.title);
}
if (!self.options.showTitle) {
self.dt.hide();
}
if(self.options.draggable){
self.dt.css({"cursor":"move"});
}
if(!self.options.showHeader){
self.dhh.remove();
}
if(!self.options.showClose){
self.dhh.find('.dialog-close').hide();
}
if (!self.options.showCancel) {
self.dhh.find('.dialog-cancel').hide();
}
if (!self.options.showButton) {
self.dh.find('.dialog-button').hide();
}
if (!self.options.showOk) {
self.dh.find(".dialog-ok").hide();
}
if (self.options.type == 'suggest') {//例外处理
self.hide();
//self.options.clickClose = true;
$(self.options.suggest.tele).unbind('keyup').keyup(function(){
var val = $.trim(this.value);
var data = null;
for(key in cachedata) {
if (key == val) {
data = cachedata[key];
}
}
if (data === null) {
$.ajax({
type: "GET",
data:{q:val},
url: self.options.suggest.url || self._content,
success: function(res){data = res;},
dataType:'json',
async: false
});
}
cachedata[val] = data;
var html = '';
for(key in data) {
html += ''+data[key].name+'';
}
self.show();
self.setContent(html);
self.find('li').click(function(){
var i = self.find('li').index(this);
$(self.options.suggest.tele).val(data[i].name);
$(self.options.suggest.vele).val(data[i].id);
if (typeof self.options.suggest.fn == 'function') {
fn(data[i]);
}
self.hide();
});
});
} else if(self.options.type == 'select') {
var type = self.options.select.type || 'radio';
var url = self.options.select.url || self._content || '';
var search = self.options.select.search || false;
$.ajax({
type: "GET",
url: url,
success: function(data){
var html = '';
if (data === null) {
html = '没有数据';
} else {
if (search) {
html += '';
}
var ovals = $.trim($(self.options.select.vele).val()).split(',');//原值
html += '';
for(key in data) {
var checked = ($.inArray(data[key].id, ovals)==-1)?'':'checked="checked"';
html += '
';
}
html += '';
}
self.show();
self.setContent(html);
self.find('li').width(self.options.select.width);
self.find('.wsearch input').keyup(function(){
var v = $.trim(this.value);
self.find('li').hide();
for(i in data) {
if (data[i].id==v || data[i].name.indexOf(v)!=-1) {
self.find('li:eq('+i+')').show();
}
}
});
self.setOnok(function(){
if (type=='radio') {
if (self.find(':checked').length == 0) {
$(self.options.select.tele).val('');
$(self.options.select.vele).val('');
} else {
var i = self.find(':radio[name=wchoose]').index(self.find(':checked')[0]);
$(self.options.select.tele).val(data[i].name);
$(self.options.select.vele).val(data[i].id);
if (typeof self.options.select.fn == 'function') fn(data[i]);
}
} else {
if (self.find(':checked').length == 0) {
$(self.options.select.tele).val('');
$(self.options.select.vele).val('');
} else {
var temps=[],ids=[],names=[];
self.find(':checked').each(function(){
var i = self.find(':checkbox[name=wchoose]').index(this);
temps.push(data[i]);
ids.push(data[i].id);
names.push(data[i].name);
});
$(self.options.select.tele).val(names.join(","));
$(self.options.select.vele).val(ids.join(","));
if (typeof self.options.select.fn == 'function') fn(temps);
}
}
self.close();
});
},
dataType:'json'
});
} else {
if (self.options.contentType == "selector") {
self.show();
self.selector = self._content;
self._content = $(self.selector).html();
self.setContent(self._content);
//if have checkbox do
var cs = $(self.selector).find(':checkbox');
self.dc.find(':checkbox').each(function(i){
this.checked = cs[i].checked;
});
$(self.selector).empty();
self.focus();
self.onopen();
} else if (self.options.contentType == "ajax") {
self.ajaxurl = self._content;
self.show();
self.setLoading();
self.dh.find(".dialog-button").hide();
if (self.options.cache == false) {
if (self.ajaxurl.indexOf('?') == -1) {
self.ajaxurl += "?_t="+Math.random();
} else {
self.ajaxurl += "&_t="+Math.random();
}
}
if(self.options.ajaxdata == null)
{
$.get(self.ajaxurl,function(data){
self._content = data;
self.show();
self.setContent(self._content);
self.focus();
self.onopen();
//计算高度
self.setTopPosition();
});
}
else
{
$.post(self.ajaxurl,self.options.ajaxdata,function(result) {
self._content = result.html;
delete result.html;
self.ajaxresult = result;
self.show();
self.setContent(self._content);
self.focus();
self.onopen();
},"json");
}
} else if (self.options.contentType == "iframe") { /*加入iframe使程序可以直接引用其它页面 by ePim*/
var html = '');
html += ('');
self.show();
self.setContent(html);
self.onopen();
} else {
self.show();
self.setContent(self._content);
self.focus();
self.onopen();
}
}
self.setCenterPosition();
}
//初始化弹窗事件
this.initEvent = function() {
self.dh.find(".dialog-close,.dialog-cancel, .dialog-ok").unbind('click').click(function(){self.close()});
if (typeof(self.options.onok) == "function") {
self.dh.find(".dialog-ok").unbind('click').click(function(){self.options.onok(self)});
}
if (typeof(self.options.oncancel) == "function") {
self.dh.find(".dialog-cancel").unbind('click').click(function(){self.options.oncancel(self)});
}
if (self.options.timeout>0) {
window.setTimeout(self.close, (self.options.timeout * 1000));
}
this.drag();
}
//设置onok事件
this.setOnok = function(fn) {
self.dh.find(".dialog-ok").unbind('click');
if (typeof(fn)=="function") self.dh.find(".dialog-ok").click(function(){fn(self)});
}
//设置onOncancel事件
this.setOncancel = function(fn) {
self.dh.find(".dialog-cancel").unbind('click');
if (typeof(fn)=="function") self.dh.find(".dialog-cancel").click(function(){fn(self)});
}
//设置onOnclose事件
this.setOnclose = function(fn) {
self.options.onclose = fn;
}
//弹窗拖拽
this.drag = function() {
if (self.options.draggable && self.options.showTitle) {
self.dh.find('.dialog-header').mousedown(function(event){
var h = this;
var o = document;
var ox = self.dh.position().left;
var oy = self.dh.position().top;
var mx = event.clientX;
var my = event.clientY;
var width = self.dh.width();
var height = self.dh.height();
var bwidth = self.bwidth();
var bheight = self.bheight();
if(h.setCapture) {
h.setCapture();
}
$(document).mousemove(function(event){
if (window.getSelection) {
window.getSelection().removeAllRanges();
} else {
document.selection.empty();
}
//TODO
var left = Math.max(ox+event.clientX-mx, 0);
var top = Math.max(oy+event.clientY-my, 0);
var left = Math.min(left, bwidth-width);
var top = Math.min(top, bheight-height);
self.dh.css({left: left, top: top});
}).mouseup(function(){
if(h.releaseCapture) {
h.releaseCapture();
}
$(document).unbind('mousemove');
$(document).unbind('mouseup');
});
});
}
}
//打开前的回弹函数
this.onopen = function() {
if (typeof(self.options.onopen) == "function") {
self.options.onopen(self);
}
}
//增加一个按钮
this.addButton = function(opt) {
opt = opt || {};
opt.title = opt.title || 'OK';
opt.bclass = opt.bclass || 'dialog-btn1';
opt.fn = opt.fn || null;
opt.index = opt.index || 0;
var btn = $('').click(function(){
if (typeof opt.fn == "function") opt.fn(self);
});
if (opt.index < self.db.find('input').length) {
self.db.find('input:eq('+opt.index+')').before(btn);
} else {
self.db.append(opt);
}
}
//显示弹窗
this.show = function() {
if (self.options.showButton) {
self.dh.find('.dialog-button').show();
}
if (self.options.position == 'center') {
self.setCenterPosition();
}
else if(self.options.position == 'top') {
self.setTopPosition();
} else {
self.setElementPosition();
}
if (typeof self.options.showAnimate == "string") {
if (self.options.animate) {
self.dh.show(self.options.animate);
} else {
self.dh.show();
}
} else {
self.dh.animate(self.options.showAnimate.animate, self.options.showAnimate.speed);
}
if (self.mh) {
self.mh.show();
}
}
this.hide = function(fn) {
if (typeof self.options.hideAnimate == "string") {
self.dh.hide(self.options.animate, fn);
} else {
self.dh.animate(self.options.hideAnimate.animate, self.options.hideAnimate.speed, "", fn);
}
}
//设置弹窗焦点
this.focus = function() {
if (self.options.focus) {
self.dh.find(self.options.focus).focus();//TODO IE中要两次
self.dh.find(self.options.focus).focus();
} else {
self.dh.find('.dialog-cancel').focus();
}
}
//在弹窗内查找元素
this.find = function(selector) {
return self.dh.find(selector);
}
//设置加载加状态
this.setLoading = function() {
self.dh.find(".dialog-button").hide();
self.setContent('',true);
}
this.setWidth = function(width) {
self.dh.width(width);
}
//设置标题
this.setTitle = function(title) {
self.dt.html(title);
}
//取得标题
this.getTitle = function() {
return self.dt.html();
}
//设置内容
this.setContent = function(content,isLoading) {
self.dc.html(content);
if (self.options.height>0) {
self.dc.css('height', self.options.height);
} else {
self.dc.css('height','auto');
}
if (self.options.width>0) {
self.dh.css('width', self.options.width);
} else {
self.dh.css('width','');
}
if (self.options.showButton) {
self.dh.find(".dialog-button").show();
}
if (self.dc.height() < 60) {
self.dc.height(60);
}
if(isLoading !== true)
{
self.dc.ready(function(){
if(self.options.isFull)
self.dc.height(self.dc.get(0).scrollHeight);
if (self.options.onready != null)
self.options.onready(self);
});
}
}
//取得内容
this.getContent = function() {
return self.dc.html();
}
//使能按钮
this.disabledButton = function(btname, state) {
self.dh.find('.dialog-'+btname).attr("disabled", state);
}
//隐藏按钮
this.hideButton = function(btname) {
self.dh.find('.dialog-'+btname).hide();
}
//显示按钮
this.showButton = function(btname) {
self.dh.find('.dialog-'+btname).show();
}
//显示关闭
this.showClose = function(btname) {
self.dhh.find('.dialog-'+btname).show();
}
//设置按钮标题
this.setButtonTitle = function(btname, title) {
self.dh.find('.dialog-'+btname).val(title);
}
//操作完成
this.next = function(opt) {
opt = opt || {};
opt.title = opt.title || self.getTitle();
opt.content = opt.content || "";
opt.okname = opt.okname || "确定";
opt.width = opt.width || 260;
opt.onok = opt.onok || self.close;
opt.onclose = opt.onclose || null;
opt.oncancel = opt.oncancel || null;
opt.hideCancel = opt.hideCancel || true;
self.setTitle(opt.title);
self.setButtonTitle("ok", okname);
self.setWidth(width);
self.setOnok(opt.onok);
self.show();
if (opt.content != "") self.setContent(opt.content);
if (opt.hideCancel) self.hideButton("cancel");
if (typeof(opt.onclose) == "function") self.setOnclose(opt.onclose);
if (typeof(opt.oncancel) == "function") self.setOncancel(opt.oncancel);
}
//关闭弹窗
this.close = function(n) {
if(self.options.isCloseToHide)
{
self.hide();
self.mh.hide();
return;
}
if (typeof(self.options.onclose) == "function") {
self.options.onclose(self);
}
if (self.options.contentType == 'selector') {
if (self.options.contentChange) {
//if have checkbox do
var cs = self.find(':checkbox');
$(self.selector).html(self.getContent());
if (cs.length > 0) {
$(self.selector).find(':checkbox').each(function(i){
this.checked = cs[i].checked;
});
}
} else {
$(self.selector).html(self._content);
}
} else if(self.options.contentType == "iframe") {
var iframe = self.dh.find(".dialogIframe");
iframe.removeAttr("src");
}
//设置关闭后的焦点
if (self.options.blur) {
$(self.options.blur).focus();
}
//从数组中删除
for(i=0;i docRight) {
left = docRight - self.dh.width() - 1;
}
if (adjust && top + self.dh.height() > docBottom) {
top = docBottom - self.dh.height() - 1;
}
left = Math.max(left+refleft, 0);
top = Math.max(top+reftop, 0);
self.dh.css({top: top, left: left});
}
this.initOptions();
this.initMask();
this.initBox();
this.initContent();
this.initEvent();
}
var weeboxs = function() {
var self = this;
this._onbox = false;
this._opening = false;
this.zIndex = 999;
this.length = function() {
return arrweebox.length;
}
this.open = function(content, options) {
self._opening = true;
if (typeof(options) == "undefined") {
options = {};
}
if (options.boxid) {
for(var i=0; i0) {
return arrweebox[arrweebox.length-1];
} else {
return false;
}
}
$(window).scroll(function() {
if (arrweebox.length > 0) {
for(i=0;i 0) {
var box = self.getTopBox();
if (box.options.position == "center") {
box.setCenterPosition();
}
else if(box.options.position == 'top') {
box.setTopPosition();
}
if (box.mh) {
box.mh.css({
width: box.bwidth(),
height: box.bheight()
});
}
}
});
$(document).click(function(event) {
if (event.button==2) return true;
if (arrweebox.length>0) {
var box = self.getTopBox();
if(!self._opening && !self._onbox && box.options.clickClose) {
box.close();
}
}
self._opening = false;
self._onbox = false;
});
}
$.extend({weeboxs: new weeboxs()});
})(jQuery);