1 Star 0 Fork 179

fengybl / AdminLTE With Iframe

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
bootstrap-tab.js 13.81 KB
一键复制 编辑 原始数据 按行查看 历史
//保存页面id的field
var pageIdField = "data-pageId";
function getPageId(element) {
if (element instanceof jQuery) {
return element.attr(pageIdField);
} else {
return $(element).attr(pageIdField);
}
}
function findTabTitle(pageId) {
var $ele = null;
$(".page-tabs-content").find("a.menu_tab").each(function () {
var $a = $(this);
if ($a.attr(pageIdField) == pageId) {
$ele = $a;
return false;//退出循环
}
});
return $ele;
}
function findTabPanel(pageId) {
var $ele = null;
$("#tab-content").find("div.tab-pane").each(function () {
var $div = $(this);
if ($div.attr(pageIdField) == pageId) {
$ele = $div;
return false;//退出循环
}
});
return $ele;
}
function findIframeById(pageId) {
return findTabPanel(pageId).children("iframe");
}
function getActivePageId() {
var $a = $('.page-tabs-content').find('.active');
return getPageId($a);
}
function canRemoveTab(pageId) {
return findTabTitle(pageId).find('.fa-remove').size() > 0;
}
//添加tab
var addTabs = function (options) {
var defaultTabOptions = {
id: Math.random() * 200,
urlType: "relative",
title: "新页面"
};
options = $.extend(true, defaultTabOptions, options);
if (options.urlType === "relative") {
// var url = window.location.protocol + '//' + window.location.host + "/";
var basePath = window.location.pathname + "/../";
options.url = basePath + options.url;
}
var pageId = options.id;
//判断这个id的tab是否已经存在,不存在就新建一个
if (findTabPanel(pageId) === null) {
//创建新TAB的title
// title = '<a id="tab_' + pageId + '" data-id="' + pageId + '" class="menu_tab" >';
var $title = $('<a href="javascript:void(0);"></a>').attr(pageIdField, pageId).addClass("menu_tab");
var $text = $("<span class='page_tab_title'></span>").text(options.title).appendTo($title);
// title += '<span class="page_tab_title">' + options.title + '</span>';
//是否允许关闭
if (options.close) {
var $i = $("<i class='fa fa-remove page_tab_close' style='cursor: pointer' onclick='closeTab(this);'></i>").attr(pageIdField, pageId).appendTo($title);
// title += ' <i class="fa fa-remove page_tab_close" style="cursor: pointer;" data-id="' + pageId + '" onclick="closeTab(this)"></i>';
}
//加入TABS
$(".page-tabs-content").append($title);
var $tabPanel = $('<div role="tabpanel" class="tab-pane"></div>').attr(pageIdField, pageId);
if (options.content) {
//是否指定TAB内容
$tabPanel.append(options.content);
} else {
//没有内容,使用IFRAME打开链接
App.blockUI({
target: '#tab-content',
boxed: true,
message: '加载中......'//,
// animate: true
});
var $iframe = $("<iframe></iframe>").attr("src", options.url).css("width", "100%").attr("frameborder", "no").attr("id", "iframe_" + pageId).addClass("tab_iframe").attr(pageIdField, pageId);
//frameborder="no" border="0" marginwidth="0" marginheight="0" scrolling="yes" allowtransparency="yes"
//iframe 加载完成事件
$iframe.load(function () {
App.unblockUI('#tab-content');//解锁界面
App.fixIframeCotent();//修正高度
});
$tabPanel.append($iframe);
}
// $tab = $(content);
$("#tab-content").append($tabPanel);
//iframe 加载完成事件
/*$tab.find("iframe").load(function () {
App.fixIframeCotent();
});*/
}
activeTabByPageId(pageId);
};
//关闭tab
var closeTab = function (item) {
//item可以是a标签,也可以是i标签
//它们都有data-id属性,获取完成之后就没事了
var pageId = getPageId(item);
closeTabByPageId(pageId);
};
function closeTabByPageId(pageId) {
var $title = findTabTitle(pageId);//有tab的标题
var $tabPanel = findTabPanel(pageId);//装有iframe
if ($title.hasClass("active")) {
//要关闭的tab处于活动状态
//要把active class传递给其它tab
//优先传递给后面的tab,没有的话就传递给前一个
var $nextTitle = $title.next();
var activePageId;
if ($nextTitle.size() > 0) {
activePageId = getPageId($nextTitle);
} else {
activePageId = getPageId($title.prev());
}
setTimeout(function () {
//某种bug,需要延迟执行
activeTabByPageId(activePageId);
}, 100);
} else {
//要关闭的tab不处于活动状态
//直接移除就可以了,不用传active class
}
$title.remove();
$tabPanel.remove();
// scrollToTab($('.menu_tab.active')[0]);
}
function closeTabOnly(pageId) {
var $title = findTabTitle(pageId);//有tab的标题
var $tabPanel = findTabPanel(pageId);//装有iframe
$title.remove();
$tabPanel.remove();
}
var closeCurrentTab = function () {
var pageId = getActivePageId();
if (canRemoveTab(pageId)) {
closeTabByPageId(pageId);
}
};
function refreshTabById(pageId) {
var $iframe = findIframeById(pageId);
var url = $iframe.attr('src');
if (url.indexOf(top.document.domain) < 0) {
$iframe.attr("src", url);// 跨域状况下,重新设置url
} else {
$iframe[0].contentWindow.location.reload(true);//带参数刷新
}
App.blockUI({
target: '#tab-content',
boxed: true,
message: '加载中......'//,
// animate: true
});
}
var refreshTab = function () {
//刷新当前tab
var pageId = getActivePageId();
refreshTabById(pageId);
};
function getTabUrlById(pageId) {
var $iframe = findIframeById(pageId);
return $iframe[0].contentWindow.location.href;
}
function getTabUrl(element) {
var pageId = getPageId(element);
getTabUrlById(pageId);
}
/**
* 编辑tab的标题
* @param pageId
* @param title
*/
function editTabTitle(pageId, title) {
var $title = findTabTitle(pageId);//有tab的标题
var $span = $title.children("span.page_tab_title");
$span.text(title);
}
//计算多个jq对象的宽度和
var calSumWidth = function (element) {
var width = 0;
$(element).each(function () {
width += $(this).outerWidth(true);
});
return width;
};
//滚动到指定选项卡
var scrollToTab = function (element) {
//element是tab(a标签),装有标题那个
//div.content-tabs > div.page-tabs-content
var marginLeftVal = calSumWidth($(element).prevAll()),//前面所有tab的总宽度
marginRightVal = calSumWidth($(element).nextAll());//后面所有tab的总宽度
//一些按钮(向左,向右滑动)的总宽度
var tabOuterWidth = calSumWidth($(".content-tabs").children().not(".menuTabs"));
// tab(a标签)显示区域的总宽度
var visibleWidth = $(".content-tabs").outerWidth(true) - tabOuterWidth;
//将要滚动的长度
var scrollVal = 0;
if ($(".page-tabs-content").outerWidth() < visibleWidth) {
//所有的tab都可以显示的情况
scrollVal = 0;
} else if (marginRightVal <= (visibleWidth - $(element).outerWidth(true) - $(element).next().outerWidth(true))) {
//向右滚动
//marginRightVal(后面所有tab的总宽度)小于可视区域-(当前tab和下一个tab的宽度)
if ((visibleWidth - $(element).next().outerWidth(true)) > marginRightVal) {
scrollVal = marginLeftVal;
var tabElement = element;
while ((scrollVal - $(tabElement).outerWidth()) > ($(".page-tabs-content").outerWidth() - visibleWidth)) {
scrollVal -= $(tabElement).prev().outerWidth();
tabElement = $(tabElement).prev();
}
}
} else if (marginLeftVal > (visibleWidth - $(element).outerWidth(true) - $(element).prev().outerWidth(true))) {
//向左滚动
scrollVal = marginLeftVal - $(element).prev().outerWidth(true);
}
//执行动画
$('.page-tabs-content').animate({
marginLeft: 0 - scrollVal + 'px'
}, "fast");
};
//滚动条滚动到左边
var scrollTabLeft = function () {
var marginLeftVal = Math.abs(parseInt($('.page-tabs-content').css('margin-left')));
var tabOuterWidth = calSumWidth($(".content-tabs").children().not(".menuTabs"));
var visibleWidth = $(".content-tabs").outerWidth(true) - tabOuterWidth;
var scrollVal = 0;
if ($(".page-tabs-content").width() < visibleWidth) {
return false;
} else {
var tabElement = $(".menu_tab:first");
var offsetVal = 0;
while ((offsetVal + $(tabElement).outerWidth(true)) <= marginLeftVal) {
offsetVal += $(tabElement).outerWidth(true);
tabElement = $(tabElement).next();
}
offsetVal = 0;
if (calSumWidth($(tabElement).prevAll()) > visibleWidth) {
while ((offsetVal + $(tabElement).outerWidth(true)) < (visibleWidth) && tabElement.length > 0) {
offsetVal += $(tabElement).outerWidth(true);
tabElement = $(tabElement).prev();
}
scrollVal = calSumWidth($(tabElement).prevAll());
}
}
$('.page-tabs-content').animate({
marginLeft: 0 - scrollVal + 'px'
}, "fast");
};
//滚动条滚动到右边
var scrollTabRight = function () {
var marginLeftVal = Math.abs(parseInt($('.page-tabs-content').css('margin-left')));
var tabOuterWidth = calSumWidth($(".content-tabs").children().not(".menuTabs"));
var visibleWidth = $(".content-tabs").outerWidth(true) - tabOuterWidth;
var scrollVal = 0;
if ($(".page-tabs-content").width() < visibleWidth) {
return false;
} else {
var tabElement = $(".menu_tab:first");
var offsetVal = 0;
while ((offsetVal + $(tabElement).outerWidth(true)) <= marginLeftVal) {
offsetVal += $(tabElement).outerWidth(true);
tabElement = $(tabElement).next();
}
offsetVal = 0;
while ((offsetVal + $(tabElement).outerWidth(true)) < (visibleWidth) && tabElement.length > 0) {
offsetVal += $(tabElement).outerWidth(true);
tabElement = $(tabElement).next();
}
scrollVal = calSumWidth($(tabElement).prevAll());
if (scrollVal > 0) {
$('.page-tabs-content').animate({
marginLeft: 0 - scrollVal + 'px'
}, "fast");
}
}
};
//关闭其他选项卡
var closeOtherTabs = function (isAll) {
if (isAll) {
//关闭全部
$('.page-tabs-content').children("[" + pageIdField + "]").find('.fa-remove').parents('a').each(function () {
var $a = $(this);
var pageId = getPageId($a);
closeTabOnly(pageId);
// closeTab($a);
/*$('#' + $(this).data('id')).remove();
$(this).remove();*/
});
var firstChild = $(".page-tabs-content").children().eq(0); //选中那些删不掉的第一个菜单
if (firstChild) {
//激活这个选项卡
activeTabByPageId(getPageId(firstChild));
/*$('#' + firstChild.data('id')).addClass('active');
firstChild.addClass('active');*/
}
} else {
//除此之外全部删除
$('.page-tabs-content').children("[" + pageIdField + "]").find('.fa-remove').parents('a').not(".active").each(function () {
var $a = $(this);
var pageId = getPageId($a);
closeTabOnly(pageId);
// closeTab($a);
/*$('#' + $(this).data('id')).remove();
$(this).remove();*/
});
}
};
//激活Tab,通过id
function activeTabByPageId(pageId) {
$(".menu_tab").removeClass("active");
$("#tab-content").find(".active").removeClass("active");
//激活TAB
var $title = findTabTitle(pageId).addClass('active');
findTabPanel(pageId).addClass("active");
// scrollToTab($('.menu_tab.active'));
scrollToTab($title[0]);
}
$(function () {
var $tabs = $(".menuTabs");
//点击选项卡的时候就激活tab
$tabs.on("click", ".menu_tab", function () {
var pageId = getPageId(this);
activeTabByPageId(pageId);
});
//双击选项卡刷新页面
$tabs.on("dblclick", ".menu_tab", function () {
// console.log("dbclick");
var pageId = getPageId(this);
refreshTabById(pageId);
});
//选项卡右键菜单
function findTabElement(target) {
var $ele = $(target);
if (!$ele.is("a")) {
$ele = $ele.parents("a.menu_tab");
}
return $ele;
}
context.init({
preventDoubleContext: false,//不禁用原始右键菜单
compress: true//元素更少的padding
});
context.attach('.page-tabs-content', [
// {header: 'Options'},
{
text: '刷新',
action: function (e, $selector, rightClickEvent) {
//e是点击菜单的事件
//$selector就是$(".page-tabs-content")
//rightClickEvent就是右键打开菜单的事件
var pageId = getPageId(findTabElement(rightClickEvent.target));
refreshTabById(pageId);
}
},
{
text: "在新窗口打开",
action: function (e, $selector, rightClickEvent) {
var pageId = getPageId(findTabElement(rightClickEvent.target));
var url = getTabUrlById(pageId);
window.open(url);
}
}
// {text: 'Open in new Window', href: '#'},
// {divider: true},
// {text: 'Copy', href: '#'},
// {text: 'Dafuq!?', href: '#'}
]);
});
JavaScript
1
https://gitee.com/fengybl/AdminLTE-With-Iframe.git
git@gitee.com:fengybl/AdminLTE-With-Iframe.git
fengybl
AdminLTE-With-Iframe
AdminLTE With Iframe
master

搜索帮助