0
0

tabmenu.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. /*
  2. * @Author: Paco
  3. * @Date: 2017-01-31
  4. * @lastModify 2017-05-08
  5. * +----------------------------------------------------------------------
  6. * | jqadmin [ jq酷打造的一款懒人后台模板 ]
  7. * | Copyright (c) 2017 http://jqadmin.jqcool.net All rights reserved.
  8. * | Licensed ( http://jqadmin.jqcool.net/licenses/ )
  9. * | Author: Paco <admin@jqcool.net>
  10. * +----------------------------------------------------------------------
  11. */
  12. layui.define(['jquery', 'element'], function(exports) {
  13. var $ = layui.jquery,
  14. element = layui.element,
  15. device = layui.device(),
  16. tabMenu = function() {
  17. this.config = {
  18. item: '#WeTabTip',
  19. closed: true
  20. };
  21. },
  22. objTab = {};
  23. /**
  24. * [参数设置 options]
  25. */
  26. tabMenu.prototype.set = function(options) {
  27. if (typeof(options) == 'string' && options != "") {
  28. this.config.item = options;
  29. } else if (typeof(options) == 'object') {
  30. $.extend(true, this.config, options);
  31. }
  32. return this;
  33. };
  34. /**
  35. *@todo 初始化对象
  36. *@return 返回对象参数初始化结果
  37. */
  38. tabMenu.prototype.init = function(options) {
  39. var _this = this,
  40. config = _this.config,
  41. $container = $('' + config.item + ''),
  42. filter = $container.attr('lay-filter');
  43. if (filter === undefined || filter === '') {
  44. console.log('错误:请设置Tab菜单选项卡属性lay-filter过滤器');
  45. }
  46. objTab.titleBox = $container.children('ul.layui-tab-title');
  47. objTab.contentBox = $container.children('div.layui-tab-content');
  48. objTab.tabFilter = filter;
  49. _this.hideRightMenu();
  50. _this.drag();
  51. return _this;
  52. }
  53. /**
  54. *@todo 检查页面是否已打开,如果已打开则返回索引值,否则返回-1
  55. *@param string title 打开页面的标题
  56. *@return int tab的索引值,元则返回-1
  57. */
  58. tabMenu.prototype.exited = function(title) {
  59. var tab_index = -1;
  60. if (objTab.titleBox === undefined) {
  61. this.init()
  62. }
  63. objTab.titleBox.find('li').each(function(i, e) {
  64. var $em = $(this).children('em');
  65. if ($em.text() === title) {
  66. tab_index = $(this).attr('lay-id');
  67. };
  68. });
  69. return tab_index;
  70. }
  71. /**
  72. * 菜单操作的接口
  73. */
  74. tabMenu.prototype.beforeTabAdd = function(data, _this) {}
  75. tabMenu.prototype.changeTab = function(data, _this, tab_index) {}
  76. tabMenu.prototype.afterTabClose = function(data, _this) {}
  77. /**
  78. *@todo 添加tab菜单选项卡
  79. *@param object data [ title 菜单选项卡标题
  80. ,href 菜单URL地址
  81. ,icon 菜单的ICON图标
  82. ]
  83. */
  84. tabMenu.prototype.tabAdd = function(data, fresh) {
  85. var tab_index = this.exited(data.title),
  86. _this = this;
  87. if (tab_index === -1) {
  88. _this.beforeTabAdd(data, _this);
  89. var layID = data.layId ? data.layId : new Date().getTime();
  90. var content = '<iframe src="' + data.href + '" data-id="' + layID + '" class="jqadmin-iframe"></iframe>';
  91. var title = '';
  92. // 如果icon有定义则添加到标题中
  93. if (!_this.config.showIcon) {
  94. title += '<i class="iconfont hide-icon">' + data.icon + '</i>';
  95. } else {
  96. title += '<i class="iconfont">' + data.icon + '</i>';
  97. }
  98. title += '<em>' + data.title + '</em>';
  99. if (this.config.closed) {
  100. title += '<i class="layui-icon layui-unselect layui-tab-close" data-id="' + layID + '">&#x1006;</i>';
  101. }
  102. //添加tab
  103. element.tabAdd(objTab.tabFilter, {
  104. title: title,
  105. content: content,
  106. id: layID
  107. });
  108. //添加打开的菜单到列表,刷新打开列表时不操作数据
  109. if (!data.nodo) {
  110. data.layId = layID;
  111. _this.storage(data, "add");
  112. }
  113. if (data.old) {
  114. objTab.titleBox.find('li[lay-id=' + layID + ']').attr("fresh", 1);
  115. }
  116. //页面淡出效果
  117. _this.effect(layID);
  118. if (this.config.closed) {
  119. //监听关闭事件
  120. objTab.titleBox.find('li').children('i.layui-tab-close[data-id=' + layID + ']').on('click', function() {
  121. element.tabDelete(objTab.tabFilter, $(this).parent('li').attr('lay-id'), data.parent);
  122. _this.tabMove(1, 1);
  123. _this.afterTabClose(data, _this);
  124. //删除数组中的对应元素
  125. element.init();
  126. _this.storage(data, "close");
  127. });
  128. };
  129. this.tabMove(tab_index, 0);
  130. //切换到当前打开的选项卡
  131. element.tabChange(objTab.tabFilter, layID);
  132. //监听鼠标右键事件
  133. objTab.titleBox.find('li:not([bind])').on('contextmenu', function(event) {
  134. $(this).attr("bind", 1);
  135. var layId = $(this).attr('lay-id');
  136. var e = event || window.event;
  137. $('.right-click-menu').attr('lay-id', layId).css({ "top": e.pageY, "left": e.pageX }).slideDown(100);
  138. $('.right-click-menu').find("a:not([bind])").on('click', { _this: _this }, _this.rightMenuEvent);
  139. return false;
  140. });
  141. } else {
  142. element.tabChange(objTab.tabFilter, tab_index);
  143. _this.changeTab(data, _this, tab_index);
  144. _this.effect(tab_index, true);
  145. data.layId = tab_index;
  146. _this.storage(data, "change");
  147. this.tabMove(tab_index, 0);
  148. if (fresh) {
  149. _this.fresh(tab_index);
  150. }
  151. }
  152. }
  153. tabMenu.prototype.effect = function(layID, ischange) {
  154. //页面淡出效果
  155. var l = layer.load(1);
  156. if (ischange) {
  157. objTab.contentBox.find('iframe[data-id=' + layID + ']').css({ "opacity": "0", "margin-top": "50px" }).delay(200).animate({ opacity: '1', marginTop: "0" }, 500);
  158. layer.close(l);
  159. } else {
  160. objTab.contentBox.find('iframe[data-id=' + layID + ']').css({ "opacity": "0", "margin-top": "50px" }).load(function() {
  161. $(this).delay(100).animate({ opacity: '1', marginTop: "0" }, 500);
  162. layer.close(l);
  163. });
  164. }
  165. }
  166. tabMenu.prototype.rightMenuEvent = function(event) {
  167. var _this = event.data._this;
  168. var event = $(this).data('event'),
  169. parent = $(this).parents('.right-click-menu').attr("parent");
  170. $(this).attr("bind", 1);
  171. var index = $(this).parents('.right-click-menu').attr('lay-id');
  172. $(this).parents('.right-click-menu').hide();
  173. $('.menu-list').slideUp();
  174. $('.tab-move-btn').find('i').html("&#xe604;");
  175. switch (event) {
  176. case "close":
  177. if (index != 0) {
  178. element.tabDelete(objTab.tabFilter, index);
  179. }
  180. var data = {
  181. layId: index
  182. }
  183. _this.storage(data, "close");
  184. break;
  185. case "other":
  186. objTab.titleBox.find('li').each(function(i, o) {
  187. var layId = $(o).attr('lay-id');
  188. if (layId != index && layId != 0) {
  189. element.tabDelete(objTab.tabFilter, layId);
  190. }
  191. })
  192. var data = {
  193. layId: index
  194. }
  195. _this.storage(data, "other");
  196. break;
  197. case "all":
  198. objTab.titleBox.find('li').each(function(i, o) {
  199. var layId = $(o).attr('lay-id');
  200. if (layId != 0) {
  201. element.tabDelete(objTab.tabFilter, layId);
  202. }
  203. var data = {
  204. layId: layId
  205. }
  206. _this.storage(data, "all");
  207. })
  208. break;
  209. default:
  210. _this.fresh(index);
  211. }
  212. _this.tabMove(1, 1);
  213. $(this).parents('.right-click-menu').hide();
  214. element.init();
  215. }
  216. tabMenu.prototype.storage = function(data, action) {
  217. if (data.title == undefined && action != "all") {
  218. return false;
  219. }
  220. var storage = window.sessionStorage;
  221. var _data = JSON.stringify(data);
  222. if (action == "add") {
  223. if (storage.menu) {
  224. var menulist = storage.menu;
  225. menulist = menulist.split("|");
  226. menulist.remove(_data);
  227. menulist.push(_data);
  228. var menu = menulist.join("|");
  229. storage.menu = menu;
  230. } else {
  231. storage.menu = _data;
  232. }
  233. storage.curMenu = _data;
  234. } else if (action == "all") {
  235. storage.removeItem("curMenu");
  236. storage.removeItem("menu");
  237. } else {
  238. //取得打开的菜单数组
  239. var menulist = storage.menu;
  240. if (!menulist) {
  241. return;
  242. }
  243. menulist = menulist.split("|");
  244. if (action == "close") {
  245. for (index in menulist) {
  246. if (index == "remove") {
  247. continue;
  248. }
  249. if (typeof menulist[index] === 'string') {
  250. var menu = JSON.parse(menulist[index]);
  251. }
  252. if (menu.layId == data.layId) {
  253. menulist.splice(index, 1);
  254. }
  255. }
  256. storage.menu = menulist.join("|");
  257. storage.removeItem("curMenu");
  258. } else {
  259. for (index in menulist) {
  260. if (index == "remove") {
  261. continue;
  262. }
  263. if (typeof menulist[index] === 'string') {
  264. var menu = JSON.parse(menulist[index]);
  265. }
  266. if (menu.layId == data.layId) {
  267. var _data = menulist[index];
  268. }
  269. }
  270. if (action == "change") { //切换
  271. storage.curMenu = _data;
  272. } else if (action == "other") {
  273. storage.curMenu = _data;
  274. storage.menu = _data;
  275. }
  276. }
  277. }
  278. }
  279. tabMenu.prototype.fresh = function(index) {
  280. element.tabChange(objTab.tabFilter, index);
  281. var othis = objTab.titleBox.find('>li[lay-id="' + index + '"]'),
  282. index = othis.parent().children('li').index(othis),
  283. parents = othis.parents('.layui-tab').eq(0),
  284. item = parents.children('.layui-tab-content').children('.layui-tab-item'),
  285. src = item.eq(index).find('iframe').attr("src");
  286. item.eq(index).find('iframe').attr("src", src);
  287. }
  288. tabMenu.prototype.hideRightMenu = function() {
  289. $(document).on('contextmenu', function(event) {
  290. $(this).find('.right-click-menu').hide();
  291. });
  292. $(document).on('click', function(event) {
  293. $(this).find('.right-click-menu').hide();
  294. })
  295. }
  296. /**
  297. *@todo 判断菜单选项卡是否已超出了总宽度,若超出则显示左右移动按钮,否则隐藏按钮
  298. *@param int index 大于等于0时表示菜单选项卡已经存在,才有移动的需求
  299. *@param int scene 为1时表示删除tab菜单选项卡,为0时表示切换或是添加菜单选项卡
  300. */
  301. tabMenu.prototype.tabMove = function(index, scene) {
  302. //取得屏幕总宽度
  303. var navWidth = parseInt(objTab.titleBox.parent('div').width());
  304. //取得菜单选项卡总宽度
  305. var $tabNav = objTab.titleBox.find('li'),
  306. tab_all_width = 0;
  307. $tabNav.each(function(i, n) {
  308. tab_all_width += $(n).outerWidth(true);
  309. });
  310. if (!$tabNav[0]) { return }
  311. if (tab_all_width > navWidth + 1) {
  312. var ml = navWidth - tab_all_width - 54;
  313. if (ml < 0) {
  314. if (index >= 0) {
  315. var current_tab_left = parseInt(objTab.titleBox.find('.layui-this').position().left),
  316. curent_tab_ml = parseInt(objTab.titleBox.css("marginLeft")),
  317. curent_ml = current_tab_left + parseInt(curent_tab_ml);
  318. if (curent_ml <= 0) {
  319. ml = 0 - current_tab_left;
  320. } else {
  321. var is_show = -(curent_tab_ml - navWidth + parseInt(objTab.titleBox.find('.layui-this').outerWidth(true)) + current_tab_left + 54);
  322. if (is_show <= 0) {
  323. ml = navWidth - current_tab_left - parseInt(objTab.titleBox.find('.layui-this').outerWidth(true)) - 54;
  324. } else {
  325. if (scene == 1 && parseInt(curent_tab_ml) < 0) {
  326. ml = navWidth - current_tab_left - parseInt(objTab.titleBox.find('.layui-this').outerWidth(true)) - 54;
  327. if (ml > 0) {
  328. ml = 0;
  329. }
  330. } else {
  331. return;
  332. }
  333. }
  334. }
  335. }
  336. objTab.titleBox.css({ "marginLeft": ml });
  337. }
  338. if (ml == 0 && tab_all_width < navWidth + 1) {
  339. objTab.titleBox.parent('div').find('.tab-move-btn').hide();
  340. } else {
  341. objTab.titleBox.parent('div').find('.tab-move-btn').show();
  342. }
  343. } else {
  344. objTab.titleBox.parent('div').find('.tab-move-btn').hide();
  345. objTab.titleBox.css({ "marginLeft": 0 });
  346. }
  347. }
  348. tabMenu.prototype.drag = function() {
  349. var _this = this;
  350. objTab.titleBox.on("mousedown", function(e) {
  351. //取得屏幕总宽度
  352. var navWidth = parseInt(objTab.titleBox.parent('div').width());
  353. //取得菜单选项卡总宽度
  354. var $tabNav = objTab.titleBox.find('li'),
  355. tab_all_width = 0;
  356. $tabNav.each(function(i, n) {
  357. tab_all_width += $(n).outerWidth(true);
  358. });
  359. if (!$tabNav[0]) { return };
  360. if (tab_all_width > navWidth + 1) {
  361. var maxml = tab_all_width - navWidth + 54
  362. var _x = e.pageX - parseInt(objTab.titleBox.css("marginLeft")); //取得鼠标到标签左边left的距离
  363. objTab.titleBox.on("mousemove", function(e) {
  364. x = e.pageX - _x;
  365. if (x > 0) {
  366. x = 0;
  367. } else if (x < -maxml) {
  368. x = -maxml;
  369. }
  370. objTab.titleBox.css({ "marginLeft": x });
  371. });
  372. objTab.titleBox.parents('body').on("mouseup", function(e) {
  373. objTab.titleBox.off("mousemove");
  374. })
  375. return false;
  376. }
  377. });
  378. }
  379. /**
  380. *根据值查找所在位置index值,查不到就返回-1
  381. */
  382. Array.prototype.indexOf = function(val) {
  383. for (var i = 0; i < this.length; i++) {
  384. if (this[i] == val) return i;
  385. }
  386. return -1;
  387. };
  388. /**
  389. *根椐值删除元素
  390. */
  391. Array.prototype.remove = function(val) {
  392. var index = this.indexOf(val);
  393. if (index > -1) {
  394. this.splice(index, 1);
  395. }
  396. };
  397. exports('tabmenu', function(options) {
  398. var navtab = new tabMenu();
  399. return navtab.set(options)
  400. });
  401. });