1 Star 0 Fork 0

oforz / Du.js.plugin.wBox

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
wBox.js 8.69 KB
一键复制 编辑 原始数据 按行查看 历史
huye 提交于 2013-09-06 15:54 . 修改 callback 添加异常处理
/**
* wBox with Dujs
* Create By Huye QQ 214526735
* Date: 2013-07-13T03:01:00.000Z
**/~
function($, undefined){
$.plugin.wBox =
$.fn.wBox =
function(style, self){
if(!self && parent){
return parent(style)
}
var i, ins, win, html, config;
config = typeof style === 'string'
? $.parse(style) : style ? style : {};
$.extend(config, DefaultConfig);
html = '<div class="' + config.style + '">';
$.each(xBoxClass, function(item, main){
if(main){
html += '<div class="' + main + '">';
i = 0;
while(++i < 9){
html += '<div class="c' + i + '"></div>'
}
html += '</div>'
}else{
$.each(item.split('|'), function(cls){
html += '<div class="' + cls + '"></div>'
})
}
});
ins = $(html + '</div>');
Blanket.before(ins);
win = new xWindow(ins, config);
config.resize || ins.find('.resize').hide();
config.status ? config.status = 1 : ins.hide();
return $.isa(this) ? win.content(this) : win
}
function xWindow(board, config){
var _window = this;
_window.tmp = {};
_window.config = config;
_window.dom = {
board : board,
close : board.find('>.close'),
title : board.find('>.title'),
content : board.find('>.content'),
block : board.find('.border,.resize').add(board),
height : board.find('.resize .c3,.resize .c7'),
width : board.find('.resize .c1,.resize .c5')
};
xBoxWindows[config.id = ++xBoxIndex] = _window;
_window.resize(config.width, config.height);
_window.center(config.left, config.top);
if(config.title){
_window.title(config.title)
}
if(config.loadUrl){
_window.content('<iframe style="width:100%;height:100%"'
+ ' scrolling="auto" frameborder="0" src=""></iframe>');
board.find('iframe').attr('src', config.loadUrl)
}
board.click(function(){_window.focus()});
_window.dom.close.html('X').mouseout(function(){
$(this).removeClass('close_hover')
}).mouseover(function(){
$(this).addClass('close_hover')
}).click(function(e){
_window.close(e);
return false
});
_window.dom.title.mousedown(function(e){
if(config.move && mousedown(e)){
Blanket.css('cursor', 'move');
return false
}
}).on('selectstart', function(){return false});
board.find('.resize>div').mousedown(function(e){
if(config.resize && mousedown(e)){
var n = e.target.className.slice(1);
n = [0, 1, 3, 2, 6, 4, 12, 8, 9][n];
$.extend(config.event, {
H : board.height(),
W : board.width(),
Z : n
});
return false
}
});
config.modal ? _window.modal(true) : _window.focus();
function mousedown(e){
if(e.which === 1){
_window.focus();
var offset = board.offset(),
cursor = $(e.target).css('cursor');
Blanket.css({
zIndex : xBoxIndex,
left : ClientStatus.L,
top : ClientStatus.T,
width : ClientStatus.W,
height : ClientStatus.H,
cursor : cursor
}).show();
config.event = {
X : e.screenX,
Y : e.screenY,
L : offset.left,
T : offset.top
};
return true
}
}
}
xWindow.prototype = {
show : function(){
var _this = this;
_this.dom.board.show();
return _this.config.modal ?
_this.modal(true) : _this.focus()
},
hide : function(){
var _this = this;
_this.dom.board.hide();
updateStack(_this);
return _this
},
close : function(e){
var _this = this;
if($.isFunc(e)){
_this.dom.$_close = e
}else if(_this = _this.dom){
try{
if(_this.$_close &&
_this.$_close(e) === false){
return false
}
}catch(e){}
_this.board.hide().remove();
updateStack(this)
}
},
title : function(title){
var text = $('<div class="text"></div>');
this.dom.title.empty().append(text);
text.text(title);
return this
},
content : function(content){
this.dom.content.empty().append(content);
return this
},
focus : function(){
var _this = this;
if(_this === CurrentWindow){
return _this
}
updateStack(_this, CurrentWindow);
CurrentWindow = _this;
_this.dom.board.css('z-index', ++xBoxIndex);
return _this.config.modal ? _this.modal(true) : _this
},
modal : function(modal){
var config = this.config;
if(config.modal = modal !== false){
this.focus();
Modal.css('z-index', xBoxIndex).show()
}else{
Modal.hide()
}
return this
},
opacity : function(opacity){
this.dom.board.opacity(opacity);
return this
},
center : function(left, top){
var _this = this,
config = _this.config,
board = _this.dom.board;
if(_this.tmp.ct){
clearTimeout(_this.tmp.ct)
}
if(BodyLoad){
config.center = 0;
if(!$.isNumeric(left)){
if(left === undefined || left === true)config.center = 1;
left = config.center ? ClientStatus.L + (ClientStatus.W - board.width()) / 2 : board.css('left')
}
if(!$.isNumeric(top)){
if(top === undefined || top === true)config.center += 2;
top = config.center & 2 ? ClientStatus.T + (ClientStatus.H - board.height()) / 2 : board.css('top')
}
_this.moveTo(left, top)
}else{
_this.tmp.ct = setTimeout(function(){
_this.center(left, top)
}, 13)
}
return _this
},
resize : function(width, height){
var offset, _this = this;
width && _this.dom.block.width(width);
height && _this.dom.block.height(height);
if(BodyLoad){
offset = _this.dom.width.css('left');
height && _this.dom.height.height(height - offset * 2);
width && _this.dom.width.width(width - offset * 2)
}else{
setTimeout(function(){
_this.resize(width, height)
}, 13)
}
return _this
},
moveTo : function(left, top){
var _this = this;
if(_this.tmp.ct){
clearTimeout(_this.tmp.ct);
_this.tmp.ct = null
}
_this.dom.board.css({
left : left,
top : top
});
return _this
}
};
function windowEvent(){
var f, k, client = {
W : Window.width(),
H : Window.height(),
L : Window.scrollLeft(),
T : Window.scrollTop()
};
for(k in client)if(client[k] !==
ClientStatus[k]){f = true;break}
if(f){
Modal.css({left : client.L, top : client.T})
.width(client.W).height(client.H);
ClientStatus = client
}
}
function updateStack(_this, insert){
var stack, prev, next;
if(stack = _this.zStack){
prev = stack.prev;
if(_this === CurrentWindow){
if(prev){
prev.focus();
updateStack(_this)
}else{
CurrentWindow = undefined
}
_this.config.modal && _this.modal(false)
}else{
next = stack.next;
if(prev)prev.zStack.next = next;
if(next)next.zStack.prev = prev
}
}
if(insert){
_this.zStack = {
prev : insert
};
insert.zStack.next = _this
}else{
_this.zStack = {}
}
}
var ClientStatus = {},
parent = window.parent,
xBoxIndex = 999, xBoxWindows = {},
BodyLoad, Blanket = $('<div></div>'),
Modal = Blanket.clone().addClass('modal'),
CurrentWindow, Root = Blanket.clone().addClass('wBox'),
xBoxClass = {
/* border block */
'border' : 8,
/* resize_position */
'resize' : 8,
/* resize_position */
'' : 'title|content|close'
},
DefaultConfig = {
width : 640,
height : 320,
minWidth : 126,
minHeight : 76,
style : 'default',
close : 1,
move : 1,
modal : 0,
resize : 1,
status : 1,
opacity : 1
},
Window = $(window),
timer = setInterval(function(){
if(document.body){
clearInterval(timer);
Root.prependTo('body');
$('head').append(link);
setInterval(windowEvent, 53);
windowEvent();
BodyLoad = true
}
}, 15);
var link = document.createElement('link');
link.rel = "stylesheet"; link.type = "text/css";
link.href = (window.XWBOX_DIR || $.dir()) + "wbox.css";
parent = parent != window && parent.Dujs
? parent.Dujs.fn.wBox : null;
Root.append(Modal).append(Blanket);
Blanket.css('background-color', '#000')
.opacity(0).mousemove(function(e){
var config = CurrentWindow.config,
board = CurrentWindow.dom.board,
tmp = config.event;
config.center = 0;
if(tmp.Z){
//resize
var x, y;
if(tmp.Z & 2){
x = e.screenX - tmp.X + tmp.W;
if(x < config.minWidth)x = config.minWidth
}else if(tmp.Z & 8){
x = tmp.X - e.screenX + tmp.W;
if(x < config.minWidth)x = config.minWidth;
board.css('left', tmp.W - x + tmp.L)
}
if(tmp.Z & 1){
y = tmp.Y - e.screenY + tmp.H;
if(y < config.minHeight)y = config.minHeight;
board.css('top', tmp.H - y + tmp.T)
}else if(tmp.Z & 4){
y = e.screenY - tmp.Y + tmp.H;
if(y < config.minHeight)y = config.minHeight
}
CurrentWindow.resize(x, y)
}else{
//move
board.css({
left : e.screenX - tmp.X + tmp.L,
top : e.screenY - tmp.Y + tmp.T
})
}
return false
}).mouseup(function(){
Blanket.hide()
}).add(Modal).css({
position : 'absolute',
display : 'none',
left : '0',
top : '0'
})
}(Dujs);
JavaScript
1
https://gitee.com/scus/du-js-plugin-wbox.git
git@gitee.com:scus/du-js-plugin-wbox.git
scus
du-js-plugin-wbox
Du.js.plugin.wBox
master

搜索帮助