tabrightmenu.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. layui.define(['element'], function (exports) {
  2. let element = layui.element;
  3. const $ = layui.jquery;
  4. let MOD_NAME = 'tabrightmenu';
  5. let RIGHTMENUMOD = function () {
  6. this.v = '1.0.0';
  7. this.author = 'TangHanF';
  8. this.email = 'guofu_gh@163.com';
  9. this.filter = '';//Tab选项卡的事件过滤
  10. };
  11. String.prototype.format = function () {
  12. if (arguments.length == 0) return this;
  13. let param = arguments[0];
  14. let s = this;
  15. if (typeof(param) == 'object') {
  16. for (var key in param) s = s.replace(new RegExp("\\{" + key + "\\}", "g"), param[key]);
  17. return s;
  18. } else {
  19. for (var i = 0; i < arguments.length; i++) s = s.replace(new RegExp("\\{" + i + "\\}", "g"), arguments[i]);
  20. return s;
  21. }
  22. }
  23. function createStyle(ulClassName) {
  24. let style = '.{name} {position: absolute;width: 110px;z-index: 9999;display: none;background-color: #fff;padding: 2px;color: #333;border: 1px solid #eee;border-radius: 2px;cursor: pointer;}.{name} li {text-align: center;display: block;height: 30px;line-height: 32px;}.{name} li:hover {background-color: #666;color: #fff;}'
  25. .format({name: ulClassName});
  26. return style;
  27. }
  28. /**
  29. * 初始化
  30. */
  31. RIGHTMENUMOD.prototype.render = function (opt) {
  32. createStyle();
  33. if (!opt.container) {
  34. console.error("[ERROR]使用rightmenu组件需要制定'container'属性!");
  35. return;
  36. }
  37. if (!opt.filter) {
  38. console.error("[ERROR]使用rightmenu组件需要制定'filter'属性!");
  39. return;
  40. }
  41. this.filter = opt.filter;
  42. let defaultNavArr = [
  43. {eventName: 'closethis', title: '关闭当前'},
  44. {eventName: 'closeall', title: '关闭所有'},
  45. {eventName: 'closeothers', title: '关闭其它'},
  46. {eventName: 'closeleft', title: '关闭左侧所有'},
  47. {eventName: 'closeright', title: '关闭右侧所有'},
  48. {eventName: 'refresh', title: '刷新当前页'},
  49. ];
  50. opt = opt || {};
  51. opt.navArr = opt.navArr || defaultNavArr;
  52. CreateRightMenu(opt);
  53. };
  54. /**
  55. * 创建右键菜单项目
  56. * @param rightMenuConfig
  57. * @constructor
  58. */
  59. function CreateRightMenu(rightMenuConfig) {
  60. // 使用"filter"属性作为css样式名称
  61. $("<style></style>").text(createStyle(rightMenuConfig.filter)).appendTo($("head"));
  62. let li = '';
  63. $.each(rightMenuConfig.navArr, function (index, conf) {
  64. // li += '<li data-type="' + conf.eventName + '"><i class="layui-icon layui-icon-face-smile"></i> ' + conf.title + '</li>';
  65. li += '<li data-type="{eventName}"><i class="layui-icon {icon}"></i>{title}</li>'
  66. .format({eventName:conf.eventName,icon:conf.icon?conf.icon:"",title:conf.title});
  67. })
  68. let tmpHtml = '<ul class="{className}">{liStr} </ul>'.format({liStr: li, className: rightMenuConfig.filter})
  69. $(rightMenuConfig.container).after(tmpHtml);
  70. _CustomRightClick(rightMenuConfig);
  71. }
  72. /**
  73. * 绑定右键菜单
  74. * @constructor
  75. */
  76. function _CustomRightClick(rightMenuConfig) {
  77. //屏蔽Tab右键
  78. $("#" + rightmenuObj.filter + ' li').on('contextmenu', function () {
  79. return false;
  80. })
  81. $('#' + rightmenuObj.filter + ',' + rightmenuObj.filter + ' li').click(function () {
  82. $('.' + rightMenuConfig.filter).hide();
  83. });
  84. console.log("[事件绑定,元素]" + rightmenuObj.filter + ' li')
  85. $("#" + rightmenuObj.filter + ' li').on('contextmenu', function (e) {
  86. let popupmenu = $("." + rightMenuConfig.filter);
  87. let leftValue = ($(document).width() - e.clientX) < popupmenu.width() ? (e.clientX - popupmenu.width()) : e.clientX;
  88. let topValue = ($(document).height() - e.clientY) < popupmenu.height() ? (e.clientY - popupmenu.height()) : e.clientY;
  89. popupmenu.css({left: leftValue, top: topValue}).show();
  90. return false;
  91. });
  92. // 点击空白处隐藏弹出菜单
  93. $(document).click(function (event) {
  94. event.stopPropagation();
  95. $("." + rightMenuConfig.filter).hide();
  96. });
  97. /**
  98. * 注册tab右键菜单点击事件
  99. */
  100. console.log("[注册tab右键菜单点击事件]" + '.' + rightMenuConfig.filter + ' li');
  101. $('.' + rightMenuConfig.filter + ' li').click(function () {
  102. let currentActiveTabID = $("#" + rightMenuConfig.filter + ">li[class='layui-this']").attr('lay-id');// 获取当前激活的选项卡ID
  103. let tabtitle = $("#" + rightMenuConfig.filter + " li");
  104. let allTabIDArr = [];
  105. $.each(tabtitle, function (i) {
  106. allTabIDArr[i] = $(this).attr("lay-id");
  107. })
  108. // 事件处理
  109. switch ($(this).attr("data-type")) {
  110. case "closethis"://关闭当前,如果开始了tab可关闭,实际意义不大
  111. tabDelete(currentActiveTabID, rightMenuConfig.filter);
  112. break;
  113. case "closeall"://关闭所有
  114. tabDeleteAll(allTabIDArr, rightMenuConfig.filter);
  115. break;
  116. case "closeothers"://关闭非当前
  117. $.each(allTabIDArr, function (i) {
  118. let tmpTabID = allTabIDArr[i];
  119. if (currentActiveTabID != tmpTabID)
  120. tabDelete(tmpTabID, rightMenuConfig.filter);
  121. })
  122. break;
  123. case "closeleft"://关闭左侧全部
  124. let indexCloseleft = allTabIDArr.indexOf(currentActiveTabID);
  125. tabDeleteAll(allTabIDArr.slice(0, indexCloseleft));
  126. break;
  127. case "closeright"://关闭右侧全部
  128. let indexCloseright = allTabIDArr.indexOf(currentActiveTabID);
  129. tabDeleteAll(allTabIDArr.slice(indexCloseright + 1));
  130. break;
  131. case "refresh":
  132. // 重新加载iFrame,实现更新。注意:如果你不是使用的iFrame则无效,具体刷新实现自行处理
  133. // document.getElementById(currentActiveTabID).contentWindow.location.reload(true);
  134. refreshiFrame();
  135. break;
  136. default:
  137. let currentTitle = $("#" + rightMenuConfig.filter + ">li[class='layui-this']").text();
  138. rightMenuConfig.registMethod[$(this).attr("data-type")](currentActiveTabID, currentTitle, rightMenuConfig.container, $(this)[0]);
  139. }
  140. $('.rightmenu').hide();
  141. })
  142. }
  143. let tabDelete = function (id, currentNav) {
  144. console.log("删除的TabID:" + id + ",所属组:" + currentNav);
  145. element.tabDelete(currentNav, id);//删除
  146. }
  147. let tabDeleteAll = function (ids, currentNav) {
  148. $.each(ids, function (i, item) {
  149. element.tabDelete(currentNav, item);
  150. })
  151. }
  152. /**
  153. * 重新加载iFrame,实现更新
  154. * @returns {boolean}
  155. */
  156. let refreshiFrame = function () {
  157. let $curFrame = $('iframe:visible');
  158. $curFrame.attr("src", $curFrame.attr("src"));
  159. return false;
  160. }
  161. let rightmenuObj = new RIGHTMENUMOD();
  162. exports(MOD_NAME, rightmenuObj);
  163. })