contabs.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. $(function () {
  2. //计算元素集合的总宽度
  3. function calSumWidth(elements) {
  4. var width = 0;
  5. $(elements).each(function () {
  6. width += $(this).outerWidth(true);
  7. });
  8. return width;
  9. }
  10. //滚动到指定选项卡
  11. function scrollToTab(element) {
  12. var marginLeftVal = calSumWidth($(element).prevAll()), marginRightVal = calSumWidth($(element).nextAll());
  13. // 可视区域非tab宽度
  14. var tabOuterWidth = calSumWidth($(".content-tabs").children().not(".J_menuTabs"));
  15. //可视区域tab宽度
  16. var visibleWidth = $(".content-tabs").outerWidth(true) - tabOuterWidth;
  17. //实际滚动宽度
  18. var scrollVal = 0;
  19. if ($(".page-tabs-content").outerWidth() < visibleWidth) {
  20. scrollVal = 0;
  21. } else if (marginRightVal <= (visibleWidth - $(element).outerWidth(true) - $(element).next().outerWidth(true))) {
  22. if ((visibleWidth - $(element).next().outerWidth(true)) > marginRightVal) {
  23. scrollVal = marginLeftVal;
  24. var tabElement = element;
  25. while ((scrollVal - $(tabElement).outerWidth()) > ($(".page-tabs-content").outerWidth() - visibleWidth)) {
  26. scrollVal -= $(tabElement).prev().outerWidth();
  27. tabElement = $(tabElement).prev();
  28. }
  29. }
  30. } else if (marginLeftVal > (visibleWidth - $(element).outerWidth(true) - $(element).prev().outerWidth(true))) {
  31. scrollVal = marginLeftVal - $(element).prev().outerWidth(true);
  32. }
  33. $('.page-tabs-content').animate({
  34. marginLeft: 0 - scrollVal + 'px'
  35. }, "fast");
  36. }
  37. //查看左侧隐藏的选项卡
  38. function scrollTabLeft() {
  39. var marginLeftVal = Math.abs(parseInt($('.page-tabs-content').css('margin-left')));
  40. // 可视区域非tab宽度
  41. var tabOuterWidth = calSumWidth($(".content-tabs").children().not(".J_menuTabs"));
  42. //可视区域tab宽度
  43. var visibleWidth = $(".content-tabs").outerWidth(true) - tabOuterWidth;
  44. //实际滚动宽度
  45. var scrollVal = 0;
  46. if ($(".page-tabs-content").width() < visibleWidth) {
  47. return false;
  48. } else {
  49. var tabElement = $(".J_menuTab:first");
  50. var offsetVal = 0;
  51. while ((offsetVal + $(tabElement).outerWidth(true)) <= marginLeftVal) {//找到离当前tab最近的元素
  52. offsetVal += $(tabElement).outerWidth(true);
  53. tabElement = $(tabElement).next();
  54. }
  55. offsetVal = 0;
  56. if (calSumWidth($(tabElement).prevAll()) > visibleWidth) {
  57. while ((offsetVal + $(tabElement).outerWidth(true)) < (visibleWidth) && tabElement.length > 0) {
  58. offsetVal += $(tabElement).outerWidth(true);
  59. tabElement = $(tabElement).prev();
  60. }
  61. scrollVal = calSumWidth($(tabElement).prevAll());
  62. }
  63. }
  64. $('.page-tabs-content').animate({
  65. marginLeft: 0 - scrollVal + 'px'
  66. }, "fast");
  67. }
  68. //查看右侧隐藏的选项卡
  69. function scrollTabRight() {
  70. var marginLeftVal = Math.abs(parseInt($('.page-tabs-content').css('margin-left')));
  71. // 可视区域非tab宽度
  72. var tabOuterWidth = calSumWidth($(".content-tabs").children().not(".J_menuTabs"));
  73. //可视区域tab宽度
  74. var visibleWidth = $(".content-tabs").outerWidth(true) - tabOuterWidth;
  75. //实际滚动宽度
  76. var scrollVal = 0;
  77. if ($(".page-tabs-content").width() < visibleWidth) {
  78. return false;
  79. } else {
  80. var tabElement = $(".J_menuTab:first");
  81. var offsetVal = 0;
  82. while ((offsetVal + $(tabElement).outerWidth(true)) <= marginLeftVal) {//找到离当前tab最近的元素
  83. offsetVal += $(tabElement).outerWidth(true);
  84. tabElement = $(tabElement).next();
  85. }
  86. offsetVal = 0;
  87. while ((offsetVal + $(tabElement).outerWidth(true)) < (visibleWidth) && tabElement.length > 0) {
  88. offsetVal += $(tabElement).outerWidth(true);
  89. tabElement = $(tabElement).next();
  90. }
  91. scrollVal = calSumWidth($(tabElement).prevAll());
  92. if (scrollVal > 0) {
  93. $('.page-tabs-content').animate({
  94. marginLeft: 0 - scrollVal + 'px'
  95. }, "fast");
  96. }
  97. }
  98. }
  99. //通过遍历给菜单项加上data-index属性
  100. $(".J_menuItem").each(function (index) {
  101. if (!$(this).attr('data-index')) {
  102. $(this).attr('data-index', index);
  103. }
  104. });
  105. //update-begin--Author:zhoujf Date:20170707 for:TASK #2003 【UI改进】列表加载慢的时候会出现白板--------------------
  106. function hiddenloading(){
  107. $("#panelloadingDiv").hide();
  108. }
  109. function showloading(){
  110. $("#panelloadingDiv").show();
  111. }
  112. //update-end--Author:zhoujf Date:20170707 for:TASK #2003 【UI改进】列表加载慢的时候会出现白板--------------------
  113. function menuItem() {
  114. // 获取标识数据
  115. var dataUrl = $(this).attr('href'),
  116. dataIndex = $(this).data('index'),
  117. menuName = $.trim($(this).text()),
  118. flag = true;
  119. if (dataUrl == undefined || $.trim(dataUrl).length == 0)return false;
  120. // 选项卡菜单已存在
  121. $('.J_menuTab').each(function () {
  122. if ($(this).data('id') == dataUrl) {
  123. if (!$(this).hasClass('active')) {
  124. $(this).addClass('active').siblings('.J_menuTab').removeClass('active');
  125. scrollToTab(this);
  126. // 显示tab对应的内容区
  127. $('.J_mainContent .J_iframe').each(function () {
  128. if ($(this).data('id') == dataUrl) {
  129. $(this).show().siblings('.J_iframe').hide();
  130. return false;
  131. }
  132. });
  133. }
  134. flag = false;
  135. return false;
  136. }
  137. });
  138. // 选项卡菜单不存在
  139. if (flag) {
  140. var str = '<a href="javascript:;" class="active J_menuTab" data-id="' + dataUrl + '">' + menuName + ' <i class="fa fa-times-circle"></i><!--&nbsp;<i class="fa fa-refresh"></i>--></a>';
  141. $('.J_menuTab').removeClass('active');
  142. // 添加选项卡对应的iframe
  143. //update-begin--Author:zhoujf Date:20170707 for:TASK #2003 【UI改进】列表加载慢的时候会出现白板--------------------
  144. var str1 = '<iframe class="J_iframe" onreadystatechange="hiddenloading();" onload="hiddenloading();" name="iframe' + dataIndex + '" width="100%" height="100%" src="' + dataUrl + '" frameborder="0" data-id="' + dataUrl + '" seamless></iframe>';
  145. //update-end--Author:zhoujf Date:20170707 for:TASK #2003 【UI改进】列表加载慢的时候会出现白板--------------------
  146. $('.J_mainContent').find('iframe.J_iframe').hide().parents('.J_mainContent').append(str1);
  147. //显示loading提示
  148. // var loading = layer.load();
  149. //
  150. // $('.J_mainContent iframe:visible').load(function () {
  151. // //iframe加载完成后隐藏loading提示
  152. // layer.close(loading);
  153. // });
  154. // 添加选项卡
  155. $('.J_menuTabs .page-tabs-content').append(str);
  156. //update-begin--Author:zhoujf Date:20170707 for:TASK #2003 【UI改进】列表加载慢的时候会出现白板--------------------
  157. showloading();
  158. //update-end--Author:zhoujf Date:20170707 for:TASK #2003 【UI改进】列表加载慢的时候会出现白板--------------------
  159. scrollToTab($('.J_menuTab.active'));
  160. }
  161. //var last = $("a[data-id='"+dataUrl+"']");
  162. var imageMenuData = [
  163. [{
  164. text: "刷新缓存",
  165. func: function() {
  166. var target = $('.J_iframe[data-id="' + dataUrl + '"]');
  167. var url = target.attr('src');
  168. //显示loading提示
  169. var loading = layer.load();
  170. target.attr('src', url).load(function () {
  171. //关闭loading提示
  172. layer.close(loading);
  173. });
  174. }
  175. }]
  176. ];
  177. try{
  178. $("a[data-id='"+dataUrl+"']").smartMenu(imageMenuData, {
  179. name:"name"+uuid()
  180. });
  181. }catch(e){
  182. console.log(e);
  183. }
  184. return false;
  185. }
  186. $('.J_menuItem').on('click', menuItem);
  187. // 关闭选项卡菜单
  188. function closeTab() {
  189. var closeTabId = $(this).parents('.J_menuTab').data('id');
  190. var currentWidth = $(this).parents('.J_menuTab').width();
  191. //update-begin--Author:zhoujf Date:20170707 for:TASK #2003 【UI改进】列表加载慢的时候会出现白板--------------------
  192. hiddenloading();
  193. //update-end--Author:zhoujf Date:20170707 for:TASK #2003 【UI改进】列表加载慢的时候会出现白板--------------------
  194. // 当前元素处于活动状态
  195. if ($(this).parents('.J_menuTab').hasClass('active')) {
  196. // 当前元素后面有同辈元素,使后面的一个元素处于活动状态
  197. if ($(this).parents('.J_menuTab').next('.J_menuTab').size()) {
  198. var activeId = $(this).parents('.J_menuTab').next('.J_menuTab:eq(0)').data('id');
  199. $(this).parents('.J_menuTab').next('.J_menuTab:eq(0)').addClass('active');
  200. $('.J_mainContent .J_iframe').each(function () {
  201. if ($(this).data('id') == activeId) {
  202. $(this).show().siblings('.J_iframe').hide();
  203. return false;
  204. }
  205. });
  206. var marginLeftVal = parseInt($('.page-tabs-content').css('margin-left'));
  207. if (marginLeftVal < 0) {
  208. $('.page-tabs-content').animate({
  209. marginLeft: (marginLeftVal + currentWidth) + 'px'
  210. }, "fast");
  211. }
  212. // 移除当前选项卡
  213. $(this).parents('.J_menuTab').remove();
  214. // 移除tab对应的内容区
  215. $('.J_mainContent .J_iframe').each(function () {
  216. if ($(this).data('id') == closeTabId) {
  217. $(this).remove();
  218. return false;
  219. }
  220. });
  221. }
  222. // 当前元素后面没有同辈元素,使当前元素的上一个元素处于活动状态
  223. if ($(this).parents('.J_menuTab').prev('.J_menuTab').size()) {
  224. var activeId = $(this).parents('.J_menuTab').prev('.J_menuTab:last').data('id');
  225. $(this).parents('.J_menuTab').prev('.J_menuTab:last').addClass('active');
  226. $('.J_mainContent .J_iframe').each(function () {
  227. if ($(this).data('id') == activeId) {
  228. $(this).show().siblings('.J_iframe').hide();
  229. return false;
  230. }
  231. });
  232. var marginLeftVal = parseInt($('.page-tabs-content').css('margin-left'));
  233. if (marginLeftVal < 0) {
  234. $('.page-tabs-content').animate({
  235. marginLeft: (marginLeftVal + currentWidth) + 'px'
  236. }, "fast");
  237. }
  238. // 移除当前选项卡
  239. $(this).parents('.J_menuTab').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. $(this).parents('.J_menuTab').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. $('.J_menuTabs').on('click', "i[class='fa fa-times-circle']", closeTab);
  265. $('.J_menuTabs').on('click', "i[class='fa fa-refresh']", refreshTab);
  266. //关闭其他选项卡
  267. function closeOtherTabs(){
  268. $('.page-tabs-content').children("[data-id]").not(":first").not(".active").each(function () {
  269. $('.J_iframe[data-id="' + $(this).data('id') + '"]').remove();
  270. $(this).remove();
  271. });
  272. $('.page-tabs-content').css("margin-left", "0");
  273. //update-begin--Author:zhoujf Date:20170707 for:TASK #2003 【UI改进】列表加载慢的时候会出现白板--------------------
  274. hiddenloading();
  275. //update-end--Author:zhoujf Date:20170707 for:TASK #2003 【UI改进】列表加载慢的时候会出现白板--------------------
  276. }
  277. $('.J_tabCloseOther').on('click', closeOtherTabs);
  278. //滚动到已激活的选项卡
  279. function showActiveTab(){
  280. scrollToTab($('.J_menuTab.active'));
  281. }
  282. $('.J_tabShowActive').on('click', showActiveTab);
  283. // 点击选项卡菜单
  284. function activeTab() {
  285. if (!$(this).hasClass('active')) {
  286. var currentId = $(this).data('id');
  287. // 显示tab对应的内容区
  288. $('.J_mainContent .J_iframe').each(function () {
  289. if ($(this).data('id') == currentId) {
  290. $(this).show().siblings('.J_iframe').hide();
  291. return false;
  292. }
  293. });
  294. $(this).addClass('active').siblings('.J_menuTab').removeClass('active');
  295. scrollToTab(this);
  296. }
  297. }
  298. $('.J_menuTabs').on('click', '.J_menuTab', activeTab);
  299. //刷新iframe
  300. function refreshTab() {
  301. var target = $('.J_iframe[data-id="' + $(this).data('id') + '"]');
  302. if(target==undefined){
  303. target = $('.J_iframe[data-id="' + $(this).parent().data('id') + '"]');
  304. }
  305. var url = target.attr('src');
  306. //显示loading提示
  307. var loading = layer.load();
  308. target.attr('src', url).load(function () {
  309. //关闭loading提示
  310. layer.close(loading);
  311. });
  312. }
  313. $('.J_menuTabs').on('dblclick', '.J_menuTab', refreshTab);
  314. // 左移按扭
  315. $('.J_tabLeft').on('click', scrollTabLeft);
  316. // 右移按扭
  317. $('.J_tabRight').on('click', scrollTabRight);
  318. // 关闭全部
  319. $('.J_tabCloseAll').on('click', function () {
  320. $('.page-tabs-content').children("[data-id]").not(":first").each(function () {
  321. $('.J_iframe[data-id="' + $(this).data('id') + '"]').remove();
  322. $(this).remove();
  323. });
  324. $('.page-tabs-content').children("[data-id]:first").each(function () {
  325. $('.J_iframe[data-id="' + $(this).data('id') + '"]').show();
  326. $(this).addClass("active");
  327. });
  328. $('.page-tabs-content').css("margin-left", "0");
  329. });
  330. function uuid() {
  331. var s = [];
  332. var hexDigits = "0123456789abcdef";
  333. for (var i = 0; i < 36; i++) {
  334. s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
  335. }
  336. s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
  337. s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
  338. s[8] = s[13] = s[18] = s[23] = "-";
  339. var uuid = s.join("");
  340. return uuid;
  341. }
  342. });