/** * jQuery webUI 1.1.0 * * Author * */ if (typeof (WebUIManagers) == "undefined") WebUIManagers = {}; (function ($) { /// $.fn.webGetLayoutManager = function () { return WebUIManagers[this[0].id + "_Layout"]; }; $.fn.webRemoveLayoutManager = function () { return this.each(function () { WebUIManagers[this.id + "_Layout"] = null; }); }; $.webDefaults = $.webDefaults || {}; $.webDefaults.Layout = { topHeight: 60, bottomHeight: 50, leftWidth: 110, centerWidth: 300, rightWidth: 170, InWindow : true, //是否以窗口的高度为准 height设置为百分比时可用 heightDiff : 0, //高度补差 height:'100%', //高度 onHeightChanged: null, isLeftCollapse: false, //初始化时 左边是否隐藏 isRightCollapse: false, //初始化时 右边是否隐藏 allowLeftCollapse: true, //是否允许 左边可以隐藏 allowRightCollapse: true, //是否允许 右边可以隐藏 allowLeftResize: true, //是否允许 左边可以调整大小 allowRightResize: true, //是否允许 右边可以调整大小 allowTopResize: true, //是否允许 头部可以调整大小 allowBottomResize: true, //是否允许 底部可以调整大小 space: 3 //间隔 }; $.fn.webLayout = function (p) { this.each(function () { p = $.extend({ }, $.webDefaults.Layout, p || {}); if (this.usedLayout) return; var g = { init: function () { $("> .n-layout-left .n-layout-header,> .n-layout-right .n-layout-header", g.layout).hover(function () { $(this).addClass("n-layout-header-over"); }, function () { $(this).removeClass("n-layout-header-over"); }); $(".n-layout-header-toggle", g.layout).hover(function () { $(this).addClass("n-layout-header-toggle-over"); }, function () { $(this).removeClass("n-layout-header-toggle-over"); }); $(".n-layout-header-toggle", g.left).click(function () { g.setLeftCollapse(true); }); $(".n-layout-header-toggle", g.right).click(function () { g.setRightCollapse(true); }); //set top g.middleTop = 0; if (g.top) { g.middleTop += g.top.height(); g.middleTop += parseInt(g.top.css('borderTopWidth')); g.middleTop += parseInt(g.top.css('borderBottomWidth')); g.middleTop += p.space; } if (g.left) { g.left.css({ top: g.middleTop }); g.leftCollapse.css({ top: g.middleTop }); } if (g.center) g.center.css({ top: g.middleTop }); if (g.right) { g.right.css({ top: g.middleTop }); g.rightCollapse.css({ top: g.middleTop }); } //set left if (g.left) g.left.css({ left: 0 }); g.onResize(); g.onResize(); }, setCollapse: function () { g.leftCollapse.hover(function () { $(this).addClass("n-layout-collapse-left-over"); }, function () { $(this).removeClass("n-layout-collapse-left-over"); }); g.leftCollapse.toggle.hover(function () { $(this).addClass("n-layout-collapse-left-toggle-over"); }, function () { $(this).removeClass("n-layout-collapse-left-toggle-over"); }); g.rightCollapse.hover(function () { $(this).addClass("n-layout-collapse-right-over"); }, function () { $(this).removeClass("n-layout-collapse-right-over"); }); g.rightCollapse.toggle.hover(function () { $(this).addClass("n-layout-collapse-right-toggle-over"); }, function () { $(this).removeClass("n-layout-collapse-right-toggle-over"); }); g.leftCollapse.toggle.click(function () { g.setLeftCollapse(false); }); g.rightCollapse.toggle.click(function () { g.setRightCollapse(false); }); if (g.left && g.isLeftCollapse) { g.leftCollapse.show(); g.leftDropHandle && g.leftDropHandle.hide(); g.left.hide(); } if (g.right && g.isRightCollapse) { g.rightCollapse.show(); g.rightDropHandle && g.rightDropHandle.hide(); g.right.hide(); } }, setLeftCollapse: function (isCollapse) { if (!g.left) return false; g.isLeftCollapse = isCollapse; if (g.isLeftCollapse) { g.leftCollapse.show(); g.leftDropHandle && g.leftDropHandle.hide(); g.left.hide(); } else { g.leftCollapse.hide(); g.leftDropHandle && g.leftDropHandle.show(); g.left.show(); } g.onResize(); }, setRightCollapse: function (isCollapse) { if (!g.right) return false; g.isRightCollapse = isCollapse; g.onResize(); if (g.isRightCollapse) { g.rightCollapse.show(); g.rightDropHandle && g.rightDropHandle.hide(); g.right.hide(); } else { g.rightCollapse.hide(); g.rightDropHandle && g.rightDropHandle.show(); g.right.show(); } g.onResize(); }, addDropHandle: function () { if (g.left && p.allowLeftResize) { g.leftDropHandle = $("
"); g.layout.append(g.leftDropHandle); g.leftDropHandle && g.leftDropHandle.show(); g.leftDropHandle.mousedown(function (e) { g.start('leftresize', e); }); } if (g.right && p.allowRightResize) { g.rightDropHandle = $("
"); g.layout.append(g.rightDropHandle); g.rightDropHandle && g.rightDropHandle.show(); g.rightDropHandle.mousedown(function (e) { g.start('rightresize', e); }); } if (g.top && p.allowTopResize) { g.topDropHandle = $("
"); g.layout.append(g.topDropHandle); g.topDropHandle.show(); g.topDropHandle.mousedown(function (e) { g.start('topresize', e); }); } if (g.bottom && p.allowBottomResize) { g.bottomDropHandle = $("
"); g.layout.append(g.bottomDropHandle); g.bottomDropHandle.show(); g.bottomDropHandle.mousedown(function (e) { g.start('bottomresize', e); }); } g.draggingxline = $("
"); g.draggingyline = $("
"); g.layout.append(g.draggingxline).append(g.draggingyline); }, setDropHandlePosition: function () { if (g.leftDropHandle) { g.leftDropHandle.css({ left: g.left.width() + parseInt(g.left.css('left')), height: g.middleHeight, top: g.middleTop }); } if (g.rightDropHandle) { g.rightDropHandle.css({ left: parseInt(g.right.css('left')) - p.space, height: g.middleHeight, top: g.middleTop }); } if (g.topDropHandle) { g.topDropHandle.css({ top: g.top.height() + parseInt(g.top.css('top')), width: g.top.width() }); } if (g.bottomDropHandle) { g.bottomDropHandle.css({ top: parseInt(g.bottom.css('top')) - p.space, width: g.bottom.width() }); } }, onResize: function () { var oldheight = g.layout.height(); //set layout height var h = 0; var windowHeight = $(window).height(); var parentHeight = null; if (typeof(p.height) == "string" && p.height.indexOf('%') > 0) { var layoutparent = g.layout.parent(); if (p.InWindow || layoutparent[0].tagName.toLowerCase() == "body") { parentHeight = windowHeight; parentHeight -= parseInt($('body').css('paddingTop')); parentHeight -= parseInt($('body').css('paddingBottom')); } else{ parentHeight = layoutparent.height(); } h = parentHeight * parseFloat(p.height) * 0.01; if(p.InWindow || layoutparent[0].tagName.toLowerCase() == "body") h -= (g.layout.offset().top - parseInt($('body').css('paddingTop'))); } else { h = parseInt(p.height); } h += p.heightDiff; g.layout.height(h); g.layoutHeight = g.layout.height(); g.middleWidth = g.layout.width(); g.middleHeight = g.layout.height(); if (g.top) { g.middleHeight -= g.top.height(); g.middleHeight -= parseInt(g.top.css('borderTopWidth')); g.middleHeight -= parseInt(g.top.css('borderBottomWidth')); g.middleHeight -= p.space; } if (g.bottom) { g.middleHeight -= g.bottom.height(); g.middleHeight -= parseInt(g.bottom.css('borderTopWidth')); g.middleHeight -= parseInt(g.bottom.css('borderBottomWidth')); g.middleHeight -= p.space; } //specific g.middleHeight -= 2; if (p.onHeightChanged && g.layoutHeight != oldheight) { p.onHeightChanged({ layoutHeight: g.layoutHeight, diff: g.layoutHeight - oldheight, middleHeight: g.middleHeight }); } if (g.center) { g.centerWidth = g.middleWidth; if (g.left) { if (g.isLeftCollapse) { g.centerWidth -= g.leftCollapse.width(); g.centerWidth -= parseInt(g.leftCollapse.css('borderLeftWidth')); g.centerWidth -= parseInt(g.leftCollapse.css('borderRightWidth')); g.centerWidth -= parseInt(g.leftCollapse.css('left')); g.centerWidth -= p.space; } else { g.centerWidth -= g.leftWidth; g.centerWidth -= parseInt(g.left.css('borderLeftWidth')); g.centerWidth -= parseInt(g.left.css('borderRightWidth')); g.centerWidth -= parseInt(g.left.css('left')); g.centerWidth -= p.space; } } if (g.right) { if (g.isRightCollapse) { g.centerWidth -= g.rightCollapse.width(); g.centerWidth -= parseInt(g.rightCollapse.css('borderLeftWidth')); g.centerWidth -= parseInt(g.rightCollapse.css('borderRightWidth')); g.centerWidth -= parseInt(g.rightCollapse.css('right')); g.centerWidth -= p.space; } else { g.centerWidth -= g.rightWidth; g.centerWidth -= parseInt(g.right.css('borderLeftWidth')); g.centerWidth -= parseInt(g.right.css('borderRightWidth')); g.centerWidth -= p.space; } } g.centerLeft = 0; if (g.left) { if (g.isLeftCollapse) { g.centerLeft += g.leftCollapse.width(); g.centerLeft += parseInt(g.leftCollapse.css('borderLeftWidth')); g.centerLeft += parseInt(g.leftCollapse.css('borderRightWidth')); g.centerLeft += parseInt(g.leftCollapse.css('left')); g.centerLeft += p.space; } else { g.centerLeft += g.left.width(); g.centerLeft += parseInt(g.left.css('borderLeftWidth')); g.centerLeft += parseInt(g.left.css('borderRightWidth')); g.centerLeft += p.space; } } g.center.css({ left: g.centerLeft }); g.center.width(g.centerWidth); g.center.height(g.middleHeight); var contentHeight = g.middleHeight; if(g.center.header) contentHeight-= g.center.header.height(); g.center.content.height(contentHeight); } if (g.left) { g.leftCollapse.height(g.middleHeight); g.left.height(g.middleHeight); } if (g.right) { g.rightCollapse.height(g.middleHeight); g.right.height(g.middleHeight); //set left g.rightLeft = 0; if (g.left) { if (g.isLeftCollapse) { g.rightLeft += g.leftCollapse.width(); g.rightLeft += parseInt(g.leftCollapse.css('borderLeftWidth')); g.rightLeft += parseInt(g.leftCollapse.css('borderRightWidth')); g.rightLeft += p.space; } else { g.rightLeft += g.left.width(); g.rightLeft += parseInt(g.left.css('borderLeftWidth')); g.rightLeft += parseInt(g.left.css('borderRightWidth')); g.rightLeft += parseInt(g.left.css('left')); g.rightLeft += p.space; } } if (g.center) { g.rightLeft += g.center.width(); g.rightLeft += parseInt(g.center.css('borderLeftWidth')); g.rightLeft += parseInt(g.center.css('borderRightWidth')); g.rightLeft += p.space; } g.right.css({ left: g.rightLeft }); } if (g.bottom) { g.bottomTop = g.layoutHeight - g.bottom.height() - 2; g.bottom.css({ top: g.bottomTop }); } g.setDropHandlePosition(); }, start: function (dragtype, e) { g.dragtype = dragtype; if (dragtype == 'leftresize' || dragtype == 'rightresize') { g.xresize = { startX: e.pageX }; g.draggingyline.css({ left: e.pageX - g.layout.offset().left, height: g.middleHeight, top: g.middleTop }).show(); $('body').css('cursor', 'col-resize'); } else if (dragtype == 'topresize' || dragtype == 'bottomresize') { g.yresize = { startY: e.pageY }; g.draggingxline.css({ top: e.pageY - g.layout.offset().top, width: g.layout.width() }).show(); $('body').css('cursor', 'row-resize'); } else { return; } g.layout.lock.width(g.layout.width()); g.layout.lock.height(g.layout.height()); g.layout.lock.show(); if ($.browser.msie || $.browser.safari) $('body').bind('selectstart', function () { return false; }); // 不能选择 $(document).bind('mouseup', g.stop); $(document).bind('mousemove', g.drag); }, drag: function (e) { if (g.xresize) { g.xresize.diff = e.pageX - g.xresize.startX; g.draggingyline.css({ left: e.pageX - g.layout.offset().left }); $('body').css('cursor', 'col-resize'); } else if (g.yresize) { g.yresize.diff = e.pageY - g.yresize.startY; g.draggingxline.css({ top: e.pageY - g.layout.offset().top }); $('body').css('cursor', 'row-resize'); } }, stop: function (e) { if (g.xresize && g.xresize.diff != undefined) { if (g.dragtype == 'leftresize') { if(typeof(g.leftWidth) == "string"){ g.leftWidth = Number(g.leftWidth); } g.leftWidth += g.xresize.diff; g.left.width(g.leftWidth); if (g.center) g.center.width(g.center.width() - g.xresize.diff).css({ left: parseInt(g.center.css('left')) + g.xresize.diff }); else if (g.right) g.right.width(g.left.width() - g.xresize.diff).css({ left: parseInt(g.right.css('left')) + g.xresize.diff }); } else if (g.dragtype == 'rightresize') { g.rightWidth -= g.xresize.diff; g.right.width(g.rightWidth).css({ left: parseInt(g.right.css('left')) + g.xresize.diff }); if (g.center) g.center.width(g.center.width() + g.xresize.diff); else if (g.left) g.left.width(g.left.width() + g.xresize.diff); } } else if (g.yresize && g.yresize.diff != undefined) { if (g.dragtype == 'topresize') { g.top.height(g.top.height() + g.yresize.diff); g.middleTop += g.yresize.diff; g.middleHeight -= g.yresize.diff; if (g.left) { g.left.css({ top: g.middleTop }).height(g.middleHeight); g.leftCollapse.css({ top: g.middleTop }).height(g.middleHeight); } if (g.center) g.center.css({ top: g.middleTop }).height(g.middleHeight); if (g.right) { g.right.css({ top: g.middleTop }).height(g.middleHeight); g.rightCollapse.css({ top: g.middleTop }).height(g.middleHeight); } } else if (g.dragtype == 'bottomresize') { g.bottom.height(g.bottom.height() - g.yresize.diff); g.middleHeight += g.yresize.diff; g.bottomTop += g.yresize.diff; g.bottom.css({ top: g.bottomTop }); if (g.left) { g.left.height(g.middleHeight); g.leftCollapse.height(g.middleHeight); } if (g.center) g.center.height(g.middleHeight); if (g.right) { g.right.height(g.middleHeight); g.rightCollapse.height(g.middleHeight); } } } g.setDropHandlePosition(); g.draggingxline.hide(); g.draggingyline.hide(); g.xresize = g.yresize = g.dragtype = false; g.layout.lock.hide(); if ($.browser.msie || $.browser.safari) $('body').unbind('selectstart'); $(document).unbind('mousemove', g.drag); $(document).unbind('mouseup', g.stop); $('body').css('cursor', ''); } }; g.layout = $(this); if (!g.layout.hasClass("n-layout")) g.layout.addClass("n-layout"); g.width = g.layout.width(); //top if ($("> div[position=top]", g.layout).length > 0) { g.top = $("> div[position=top]", g.layout).wrap('
').parent(); g.top.content = $("> div[position=top]", g.top); if (!g.top.content.hasClass("n-layout-content")) g.top.content.addClass("n-layout-content"); g.topHeight = p.topHeight; if (g.topHeight) { g.top.height(g.topHeight); } } //bottom if ($("> div[position=bottom]", g.layout).length > 0) { g.bottom = $("> div[position=bottom]", g.layout).wrap('
').parent(); g.bottom.content = $("> div[position=bottom]", g.top); if (!g.bottom.content.hasClass("n-layout-content")) g.bottom.content.addClass("n-layout-content"); g.bottomHeight = p.bottomHeight; if (g.bottomHeight) { g.bottom.height(g.bottomHeight); } } //left if ($("> div[position=left]", g.layout).length > 0) { g.left = $("> div[position=left]", g.layout).wrap('
').parent(); g.left.header = $('
'); g.left.prepend(g.left.header); g.left.header.toggle = $(".n-layout-header-toggle", g.left.header); g.left.content = $("> div[position=left]", g.left); if (!g.left.content.hasClass("n-layout-content")) g.left.content.addClass("n-layout-content"); if(!p.allowLeftCollapse) $(".n-layout-header-toggle", g.left.header).remove(); //set title var lefttitle = g.left.content.attr("title"); if (lefttitle) { g.left.content.attr("title", ""); $(".n-layout-header-inner", g.left.header).html(lefttitle); } //set width g.leftWidth = p.leftWidth; if (g.leftWidth) g.left.width(g.leftWidth); } //center if ($("> div[position=center]", g.layout).length > 0) { g.center = $("> div[position=center]", g.layout).wrap('
').parent(); g.center.content = $("> div[position=center]", g.center); g.center.content.addClass("n-layout-content"); //set title var centertitle = g.center.content.attr("title"); if (centertitle) { g.center.content.attr("title", ""); g.center.header = $('
'); g.center.prepend(g.center.header); g.center.header.html(centertitle); } //set width g.centerWidth = p.centerWidth; if (g.centerWidth) g.center.width(g.centerWidth); } //right if ($("> div[position=right]", g.layout).length > 0) { g.right = $("> div[position=right]", g.layout).wrap('
').parent(); g.right.header = $('
'); g.right.prepend(g.right.header); g.right.header.toggle = $(".n-layout-header-toggle", g.right.header); if(!p.allowRightCollapse) $(".n-layout-header-toggle", g.right.header).remove(); g.right.content = $("> div[position=right]", g.right); if (!g.right.content.hasClass("n-layout-content")) g.right.content.addClass("n-layout-content"); //set title var righttitle = g.right.content.attr("title"); if (righttitle) { g.right.content.attr("title", ""); $(".n-layout-header-inner", g.right.header).html(righttitle); } //set width g.rightWidth = p.rightWidth; if (g.rightWidth) g.right.width(g.rightWidth); } //lock g.layout.lock = $("
"); g.layout.append(g.layout.lock); //DropHandle g.addDropHandle(); //Collapse g.isLeftCollapse = p.isLeftCollapse; g.isRightCollapse = p.isRightCollapse; g.leftCollapse = $(''); g.rightCollapse = $(''); g.layout.append(g.leftCollapse).append(g.rightCollapse); g.leftCollapse.toggle = $("> .n-layout-collapse-left-toggle", g.leftCollapse); g.rightCollapse.toggle = $("> .n-layout-collapse-right-toggle", g.rightCollapse); g.setCollapse(); //init g.init(); $(window).resize(function () { g.onResize(); }); if (this.id == undefined) this.id = "WebUI_" + new Date().getTime(); WebUIManagers[this.id + "_Layout"] = g; this.usedLayout = true; }); if (this.length == 0) return null; if (this.length == 1) return WebUIManagers[this[0].id + "_Layout"]; var managers = []; this.each(function() { managers.push(WebUIManagers[this.id + "_Layout"]); }); return managers; }; })(jQuery);