contabs.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. /**
  2. * author:liugf
  3. * copyright:www.jeeplus.org
  4. */
  5. var _this;
  6. function hide(){
  7. layer.closeAll('tips');
  8. }
  9. //关闭全部选项卡
  10. function closeAllTabs(){
  11. $('.page-tabs-content').children("[data-id]").not(":first").each(function () {
  12. $('.J_iframe[data-id="' + $(this).data('id') + '"]').remove();
  13. $(this).remove();
  14. });
  15. $('.page-tabs-content').children("[data-id]:first").each(function () {
  16. $('.J_iframe[data-id="' + $(this).data('id') + '"]').show();
  17. $(this).addClass("active");
  18. });
  19. $('.page-tabs-content').css("margin-left", "0");
  20. }
  21. //关闭左侧选项卡
  22. function closeLeftTabs(){
  23. var othis = $(_this);
  24. othis.prevAll('.J_menuTab').not(":last").each(function () {
  25. if($(this).hasClass('active')){
  26. setActiveTab(_this);
  27. }
  28. $('.J_iframe[data-id="' + $(this).data('id') + '"]').remove();
  29. $(this).remove();
  30. });
  31. $('.page-tabs-content').css("margin-left", "0");
  32. }
  33. //关闭右侧选项卡
  34. function closeRightTabs(){
  35. var othis = $(_this);
  36. othis.nextAll('.J_menuTab').each(function () {
  37. if($(this).hasClass('active')){
  38. setActiveTab(_this);
  39. }
  40. $('.J_iframe[data-id="' + $(this).data('id') + '"]').remove();
  41. $(this).remove();
  42. });
  43. }
  44. //计算元素集合的总宽度
  45. function calSumWidth(elements) {
  46. var width = 0;
  47. $(elements).each(function () {
  48. width += $(this).outerWidth(true);
  49. });
  50. return width;
  51. }
  52. //滚动到指定选项卡
  53. function scrollToTab(element) {
  54. var marginLeftVal = calSumWidth($(element).prevAll()), marginRightVal = calSumWidth($(element).nextAll());
  55. // 可视区域非tab宽度
  56. var tabOuterWidth = calSumWidth($(".content-tabs").children().not(".J_menuTabs"));
  57. //可视区域tab宽度
  58. var visibleWidth = $(".content-tabs").outerWidth(true) - tabOuterWidth;
  59. //实际滚动宽度
  60. var scrollVal = 0;
  61. if ($(".page-tabs-content").outerWidth() < visibleWidth) {
  62. scrollVal = 0;
  63. } else if (marginRightVal <= (visibleWidth - $(element).outerWidth(true) - $(element).next().outerWidth(true))) {
  64. if ((visibleWidth - $(element).next().outerWidth(true)) > marginRightVal) {
  65. scrollVal = marginLeftVal;
  66. var tabElement = element;
  67. while ((scrollVal - $(tabElement).outerWidth()) > ($(".page-tabs-content").outerWidth() - visibleWidth)) {
  68. scrollVal -= $(tabElement).prev().outerWidth();
  69. tabElement = $(tabElement).prev();
  70. }
  71. }
  72. } else if (marginLeftVal > (visibleWidth - $(element).outerWidth(true) - $(element).prev().outerWidth(true))) {
  73. scrollVal = marginLeftVal - $(element).prev().outerWidth(true);
  74. }
  75. $('.page-tabs-content').animate({
  76. marginLeft: 0 - scrollVal + 'px'
  77. }, "fast");
  78. }
  79. //查看左侧隐藏的选项卡
  80. function scrollTabLeft() {
  81. var marginLeftVal = Math.abs(parseInt($('.page-tabs-content').css('margin-left')));
  82. // 可视区域非tab宽度
  83. var tabOuterWidth = calSumWidth($(".content-tabs").children().not(".J_menuTabs"));
  84. //可视区域tab宽度
  85. var visibleWidth = $(".content-tabs").outerWidth(true) - tabOuterWidth;
  86. //实际滚动宽度
  87. var scrollVal = 0;
  88. if ($(".page-tabs-content").width() < visibleWidth) {
  89. return false;
  90. } else {
  91. var tabElement = $(".J_menuTab:first");
  92. var offsetVal = 0;
  93. while ((offsetVal + $(tabElement).outerWidth(true)) <= marginLeftVal) {//找到离当前tab最近的元素
  94. offsetVal += $(tabElement).outerWidth(true);
  95. tabElement = $(tabElement).next();
  96. }
  97. offsetVal = 0;
  98. if (calSumWidth($(tabElement).prevAll()) > visibleWidth) {
  99. while ((offsetVal + $(tabElement).outerWidth(true)) < (visibleWidth) && tabElement.length > 0) {
  100. offsetVal += $(tabElement).outerWidth(true);
  101. tabElement = $(tabElement).prev();
  102. }
  103. scrollVal = calSumWidth($(tabElement).prevAll());
  104. }
  105. }
  106. $('.page-tabs-content').animate({
  107. marginLeft: 0 - scrollVal + 'px'
  108. }, "fast");
  109. }
  110. //查看右侧隐藏的选项卡
  111. function scrollTabRight() {
  112. var marginLeftVal = Math.abs(parseInt($('.page-tabs-content').css('margin-left')));
  113. // 可视区域非tab宽度
  114. var tabOuterWidth = calSumWidth($(".content-tabs").children().not(".J_menuTabs"));
  115. //可视区域tab宽度
  116. var visibleWidth = $(".content-tabs").outerWidth(true) - tabOuterWidth;
  117. //实际滚动宽度
  118. var scrollVal = 0;
  119. if ($(".page-tabs-content").width() < visibleWidth) {
  120. return false;
  121. } else {
  122. var tabElement = $(".J_menuTab:first");
  123. var offsetVal = 0;
  124. while ((offsetVal + $(tabElement).outerWidth(true)) <= marginLeftVal) {//找到离当前tab最近的元素
  125. offsetVal += $(tabElement).outerWidth(true);
  126. tabElement = $(tabElement).next();
  127. }
  128. offsetVal = 0;
  129. while ((offsetVal + $(tabElement).outerWidth(true)) < (visibleWidth) && tabElement.length > 0) {
  130. offsetVal += $(tabElement).outerWidth(true);
  131. tabElement = $(tabElement).next();
  132. }
  133. scrollVal = calSumWidth($(tabElement).prevAll());
  134. if (scrollVal > 0) {
  135. $('.page-tabs-content').animate({
  136. marginLeft: 0 - scrollVal + 'px'
  137. }, "fast");
  138. }
  139. }
  140. }
  141. function menuItem() {
  142. // 获取标识数据
  143. var dataUrl = $(this).attr('href'),
  144. dataIndex = $(this).data('index'),
  145. menuName = $.trim($(this).text()),
  146. target = $(this).attr('target'),
  147. flag = true;
  148. if (dataUrl == undefined || $.trim(dataUrl).length == 0)return false;
  149. // 选项卡菜单已存在
  150. $('.J_menuTab').each(function () {
  151. if ($(this).data('id') == dataUrl) {
  152. if (!$(this).hasClass('active')) {
  153. $(this).addClass('active').siblings('.J_menuTab').removeClass('active');
  154. scrollToTab(this);
  155. // 显示tab对应的内容区
  156. $('.J_mainContent .J_iframe').each(function () {
  157. if ($(this).data('id') == dataUrl) {
  158. $(this).show().siblings('.J_iframe').hide();
  159. return false;
  160. }
  161. });
  162. }
  163. flag = false;
  164. return false;
  165. }
  166. });
  167. // 选项卡菜单不存在
  168. if (flag) {
  169. if(target == '_blank'){
  170. Window.location.href=dataUrl;
  171. return false;
  172. }
  173. var str = '<a href="javascript:;" class="active J_menuTab" data-id="' + dataUrl + '">' + menuName + ' <i class="fa fa-times-circle"></i></a>';
  174. $('.J_menuTab').removeClass('active');
  175. // 添加选项卡对应的iframe
  176. var str1 = '<iframe class="J_iframe" name="iframe' + dataIndex + '" width="100%" height="100%" src="' + dataUrl + '" frameborder="0" data-id="' + dataUrl + '" seamless></iframe>';
  177. $('.J_mainContent').find('iframe.J_iframe').hide().parents('.J_mainContent').append(str1);
  178. //显示loading提示
  179. var index = layer.load(1, {
  180. shade: [0.1,'#fff'] //0.1透明度的白色背景
  181. });
  182. $('.J_mainContent iframe:visible').load(function () {
  183. //iframe加载完成后隐藏loading提示
  184. layer.close(index);
  185. });
  186. // 添加选项卡
  187. $('.J_menuTabs .page-tabs-content').append(str);
  188. scrollToTab($('.J_menuTab.active'));
  189. }
  190. return false;
  191. }
  192. //关闭当前选项卡菜单
  193. function closeCurrentTab(){
  194. var othis = $(_this);
  195. var closeTabId = othis.data('id');
  196. var currentWidth = othis.width();
  197. if($('.page-tabs-content').children("[data-id]:first").data('id') == closeTabId){
  198. return;
  199. }
  200. // 当前元素处于活动状态
  201. if (othis.hasClass('active')) {
  202. // 当前元素后面有同辈元素,使后面的一个元素处于活动状态
  203. if (othis.next('.J_menuTab').size()) {
  204. var activeId = othis.next('.J_menuTab:eq(0)').data('id');
  205. othis.next('.J_menuTab:eq(0)').addClass('active');
  206. $('.J_mainContent .J_iframe').each(function () {
  207. if ($(this).data('id') == activeId) {
  208. $(this).show().siblings('.J_iframe').hide();
  209. return false;
  210. }
  211. });
  212. var marginLeftVal = parseInt($('.page-tabs-content').css('margin-left'));
  213. if (marginLeftVal < 0) {
  214. $('.page-tabs-content').animate({
  215. marginLeft: (marginLeftVal + currentWidth) + 'px'
  216. }, "fast");
  217. }
  218. // 移除当前选项卡
  219. othis.remove();
  220. // 移除tab对应的内容区
  221. $('.J_mainContent .J_iframe').each(function () {
  222. if ($(this).data('id') == closeTabId) {
  223. $(this).remove();
  224. return false;
  225. }
  226. });
  227. }
  228. // 当前元素后面没有同辈元素,使当前元素的上一个元素处于活动状态
  229. if (othis.prev('.J_menuTab').size()) {
  230. var activeId = othis.prev('.J_menuTab:last').data('id');
  231. othis.prev('.J_menuTab:last').addClass('active');
  232. $('.J_mainContent .J_iframe').each(function () {
  233. if ($(this).data('id') == activeId) {
  234. $(this).show().siblings('.J_iframe').hide();
  235. return false;
  236. }
  237. });
  238. // 移除当前选项卡
  239. othis.remove();
  240. // 移除tab对应的内容区
  241. $('.J_mainContent .J_iframe').each(function () {
  242. if ($(this).data('id') == closeTabId) {
  243. $(this).remove();
  244. return false;
  245. }
  246. });
  247. }
  248. }
  249. // 当前元素不处于活动状态
  250. else {
  251. // 移除当前选项卡
  252. othis.remove();
  253. // 移除相应tab对应的内容区
  254. $('.J_mainContent .J_iframe').each(function () {
  255. if ($(this).data('id') == closeTabId) {
  256. $(this).remove();
  257. return false;
  258. }
  259. });
  260. scrollToTab($('.J_menuTab.active'));
  261. }
  262. return false;
  263. }
  264. // 关闭选项卡菜单
  265. function closeTab() {
  266. var closeTabId = $(this).parents('.J_menuTab').data('id');
  267. var currentWidth = $(this).parents('.J_menuTab').width();
  268. // 当前元素处于活动状态
  269. if ($(this).parents('.J_menuTab').hasClass('active')) {
  270. // 当前元素后面有同辈元素,使后面的一个元素处于活动状态
  271. if ($(this).parents('.J_menuTab').next('.J_menuTab').size()) {
  272. var activeId = $(this).parents('.J_menuTab').next('.J_menuTab:eq(0)').data('id');
  273. $(this).parents('.J_menuTab').next('.J_menuTab:eq(0)').addClass('active');
  274. $('.J_mainContent .J_iframe').each(function () {
  275. if ($(this).data('id') == activeId) {
  276. $(this).show().siblings('.J_iframe').hide();
  277. return false;
  278. }
  279. });
  280. var marginLeftVal = parseInt($('.page-tabs-content').css('margin-left'));
  281. if (marginLeftVal < 0) {
  282. $('.page-tabs-content').animate({
  283. marginLeft: (marginLeftVal + currentWidth) + 'px'
  284. }, "fast");
  285. }
  286. // 移除当前选项卡
  287. $(this).parents('.J_menuTab').remove();
  288. // 移除tab对应的内容区
  289. $('.J_mainContent .J_iframe').each(function () {
  290. if ($(this).data('id') == closeTabId) {
  291. $(this).remove();
  292. return false;
  293. }
  294. });
  295. }
  296. // 当前元素后面没有同辈元素,使当前元素的上一个元素处于活动状态
  297. if ($(this).parents('.J_menuTab').prev('.J_menuTab').size()) {
  298. var activeId = $(this).parents('.J_menuTab').prev('.J_menuTab:last').data('id');
  299. $(this).parents('.J_menuTab').prev('.J_menuTab:last').addClass('active');
  300. $('.J_mainContent .J_iframe').each(function () {
  301. if ($(this).data('id') == activeId) {
  302. $(this).show().siblings('.J_iframe').hide();
  303. return false;
  304. }
  305. });
  306. // 移除当前选项卡
  307. $(this).parents('.J_menuTab').remove();
  308. // 移除tab对应的内容区
  309. $('.J_mainContent .J_iframe').each(function () {
  310. if ($(this).data('id') == closeTabId) {
  311. $(this).remove();
  312. return false;
  313. }
  314. });
  315. }
  316. }
  317. // 当前元素不处于活动状态
  318. else {
  319. // 移除当前选项卡
  320. $(this).parents('.J_menuTab').remove();
  321. // 移除相应tab对应的内容区
  322. $('.J_mainContent .J_iframe').each(function () {
  323. if ($(this).data('id') == closeTabId) {
  324. $(this).remove();
  325. return false;
  326. }
  327. });
  328. scrollToTab($('.J_menuTab.active'));
  329. }
  330. return false;
  331. }
  332. //关闭其他选项卡
  333. function closeOtherTabs(){
  334. var othis = $(_this);
  335. setActiveTab(_this);
  336. $('.page-tabs-content').children("[data-id]").not(":first").not(".active").each(function () {
  337. $('.J_iframe[data-id="' + $(this).data('id') + '"]').remove();
  338. $(this).remove();
  339. });
  340. $('.page-tabs-content').css("margin-left", "0");
  341. }
  342. //滚动到已激活的选项卡
  343. function showActiveTab(){
  344. scrollToTab($('.J_menuTab.active'));
  345. }
  346. function setActiveTab(element){
  347. if (!$(element).hasClass('active')) {
  348. var currentId = $(element).data('id');
  349. // 显示tab对应的内容区
  350. $('.J_mainContent .J_iframe').each(function () {
  351. if ($(this).data('id') == currentId) {
  352. $(this).show().siblings('.J_iframe').hide();
  353. }
  354. });
  355. $(element).addClass('active').siblings('.J_menuTab').removeClass('active');
  356. scrollToTab(element);
  357. }
  358. }
  359. // 点击选项卡菜单
  360. function activeTab() {
  361. if (!$(this).hasClass('active')) {
  362. var currentId = $(this).data('id');
  363. // 显示tab对应的内容区
  364. $('.J_mainContent .J_iframe').each(function () {
  365. if ($(this).data('id') == currentId) {
  366. $(this).show().siblings('.J_iframe').hide();
  367. return false;
  368. }
  369. });
  370. $(this).addClass('active').siblings('.J_menuTab').removeClass('active');
  371. scrollToTab(this);
  372. }
  373. }
  374. //刷新iframe
  375. function refreshTab() {
  376. var target = $('.J_iframe[data-id="' + $(this).data('id') + '"]');
  377. var url = target.attr('src');
  378. //显示loading提示
  379. var index = layer.load(1, {
  380. shade: [0.1,'#fff'] //0.1透明度的白色背景
  381. });
  382. target.attr('src', url).load(function () {
  383. //关闭loading提示
  384. layer.close(index);
  385. });
  386. }
  387. function refreshCurrentTab() {
  388. var othis = $(_this);
  389. setActiveTab(othis);
  390. var target = $('.J_iframe[data-id="' + othis.data('id') + '"]');
  391. var url = target.attr('src');
  392. //显示loading提示
  393. var index = layer.load(1, {
  394. shade: [0.1,'#fff'] //0.1透明度的白色背景
  395. });
  396. target.attr('src', url).load(function () {
  397. //关闭loading提示
  398. layer.close(index);
  399. });
  400. }
  401. function getActiveTab(){
  402. return $(".J_iframe:visible");
  403. }
  404. //打开选项卡菜单
  405. function openTab(url,title, isNew){//isNew 为true时,打开一个新的选项卡;为false时,如果选项卡不存在,打开一个新的选项卡,如果已经存在,使已经存在的选项卡变为活跃状态。
  406. // 获取标识数据
  407. var dataUrl = url,
  408. dataIndex ,
  409. menuName = title,
  410. flag = true;
  411. if (dataUrl == undefined || top.$.trim(dataUrl).length == 0)return false;
  412. // //设置dataIndex
  413. // $(".J_menuItem").each(function (index) {
  414. // if (!$(this).attr('data-index')) {
  415. // $(this).attr('data-index', index);
  416. // }
  417. // });
  418. if(!isNew){
  419. top.$('.J_menuTab').each(function () {
  420. if (top.$(this).data('id') == dataUrl) {// 选项卡已存在,激活。
  421. if (!top.$(this).hasClass('active')) {
  422. top.$(this).addClass('active').siblings('.J_menuTab').removeClass('active');
  423. scrollToTab(top.$(this));
  424. // 显示tab对应的内容区
  425. top.$('.J_mainContent .J_iframe').each(function () {
  426. if (top.$(this).data('id') == dataUrl) {
  427. top.$(this).show().siblings('.J_iframe').hide();
  428. return false;
  429. }
  430. });
  431. }
  432. flag = false;
  433. return false;
  434. }
  435. });
  436. }
  437. if(isNew || flag){//isNew为true,打开一个新的选项卡; flag为true,选项卡不存在,打开一个新的选项卡。
  438. var str = '<a href="javascript:;" class="active J_menuTab" data-id="' + dataUrl + '">' + menuName + ' <i class="fa fa-times-circle"></i></a>';
  439. top.$('.J_menuTab').removeClass('active');
  440. // 添加选项卡对应的iframe
  441. var str1 = '<iframe class="J_iframe" name="iframe' + dataIndex + '" width="100%" height="100%" src="' + dataUrl + '" frameborder="0" data-id="' + dataUrl + '" seamless></iframe>';
  442. top.$('.J_mainContent').find('iframe.J_iframe').hide().parents('.J_mainContent').append(str1);
  443. //显示loading提示
  444. var index = layer.load(1, {
  445. shade: [0.1,'#fff'] //0.1透明度的白色背景
  446. });
  447. top.$('.J_mainContent iframe:visible').load(function () {
  448. //iframe加载完成后隐藏loading提示
  449. layer.close(index);
  450. });
  451. // 添加选项卡
  452. top.$('.J_menuTabs .page-tabs-content').append(str);
  453. scrollToTab(top.$('.J_menuTab.active'));
  454. }
  455. return false;
  456. }
  457. $(function () {
  458. //通过遍历给菜单项加上data-index属性
  459. $(".J_menuItem").each(function (index) {
  460. if (!$(this).attr('data-index')) {
  461. $(this).attr('data-index', index);
  462. }
  463. });
  464. $('.J_menuItem').on('click', menuItem);
  465. $('.J_menuTabs').on('click', '.J_menuTab i', closeTab);
  466. $('.J_tabCloseOther').on('click', closeOtherTabs);
  467. $('.J_tabShowActive').on('click', showActiveTab);
  468. $('.J_menuTabs').on('click', '.J_menuTab', activeTab);
  469. $('.J_menuTabs').on('dblclick', '.J_menuTab', refreshTab);
  470. $.contextMenu({
  471. selector: '.J_menuTab',
  472. callback: function(key, options) {
  473. _this = this;
  474. if(key === "closeTab"){
  475. closeCurrentTab();
  476. }else if(key ==="closeOtherTabs" ){
  477. closeOtherTabs();
  478. }else if(key === "refresh"){
  479. refreshCurrentTab();
  480. }else if(key ==="closeLeftTabs" ){
  481. closeLeftTabs();
  482. }else if(key === "closeRightTabs"){
  483. closeRightTabs();
  484. }else if(key === "closeAllTabs"){
  485. closeAllTabs()
  486. }
  487. },
  488. items: {
  489. "closeTab": {name: "关闭标签", icon: "fa-times "},
  490. "closeOtherTabs": {name: "关闭其他标签"},
  491. "refresh": {name: "刷新", icon: "fa-refresh"},
  492. "sep1": "---------",
  493. "closeLeftTabs": {name: "关闭左侧标签"},
  494. "closeRightTabs": {name: "关闭右侧标签"},
  495. "closeAllTabs": {name: "关闭全部标签", icon: "fa-times-circle "}
  496. }
  497. });
  498. // 左移按扭
  499. $('.J_tabLeft').on('click', scrollTabLeft);
  500. // 右移按扭
  501. $('.J_tabRight').on('click', scrollTabRight);
  502. // 关闭全部
  503. $('.J_tabCloseAll').on('click', closeAllTabs());
  504. });