tab.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. layui.define(['element', 'common'], function(exports) {
  2. "use strict";
  3. var modName = 'tab';
  4. var $ = layui.jquery;
  5. var element = layui.element;
  6. var common = layui.common;
  7. var globalTabIdIndex = 0;
  8. // ReSharper disable once InconsistentNaming
  9. var Tab = function() {
  10. this.config = {
  11. elem: undefined,
  12. closed: true, //是否包含删除按钮
  13. autoRefresh: false,
  14. contextMenu: false
  15. };
  16. };
  17. var elem = {};
  18. //版本号
  19. Tab.prototype.v = '0.1.5';
  20. /**
  21. * 参数设置
  22. * @param {Object} options
  23. */
  24. Tab.prototype.set = function(options) {
  25. var that = this;
  26. $.extend(true, that.config, options);
  27. return that;
  28. };
  29. /**
  30. * 初始化
  31. */
  32. Tab.prototype.init = function() {
  33. var that = this;
  34. var config = that.config;
  35. if(typeof(config.elem) !== 'string' && typeof(config.elem) !== 'object') {
  36. common.throwError('Tab error: elem参数未定义或设置出错,具体设置格式请参考文档API.');
  37. }
  38. var $container;
  39. if(typeof(config.elem) === 'string') {
  40. $container = $('' + config.elem + '');
  41. }
  42. if(typeof(config.elem) === 'object') {
  43. $container = config.elem;
  44. }
  45. // ReSharper disable once UsageOfPossiblyUnassignedValue
  46. // ReSharper disable once QualifiedExpressionMaybeNull
  47. if($container.length === 0) {
  48. common.throwError('Tab error:找不到elem参数配置的容器,请检查.');
  49. }
  50. var filter = $container.attr('lay-filter');
  51. if(filter === undefined || filter === '') {
  52. common.throwError('Tab error:请为elem容器设置一个lay-filter过滤器');
  53. }
  54. config.elem = $container;
  55. elem.titleBox = $container.children('ul.layui-tab-title');
  56. elem.contentBox = $container.children('div.layui-tab-content');
  57. elem.tabFilter = filter;
  58. return that;
  59. };
  60. /**
  61. * 查询tab是否存在,如果存在则返回索引值,不存在返回-1
  62. * @param {String} 标题
  63. */
  64. Tab.prototype.exists = function(title) {
  65. var that = elem.titleBox === undefined ? this.init() : this,
  66. tabIndex = -1;
  67. elem.titleBox.find('li').each(function(i) {
  68. var $cite = $(this).children('cite');
  69. if($cite.text() === title) {
  70. tabIndex = i;
  71. };
  72. });
  73. return tabIndex;
  74. };
  75. /**
  76. * 获取tabid
  77. * @param {String} 标题
  78. */
  79. Tab.prototype.getTabId=function(title){
  80. var that = elem.titleBox === undefined ? this.init() : this,
  81. tabId = -1;
  82. elem.titleBox.find('li').each(function() {
  83. var $cite = $(this).children('cite');
  84. if($cite.text() === title) {
  85. tabId = $(this).attr('lay-id');
  86. };
  87. });
  88. return tabId;
  89. };
  90. /**
  91. * 添加选择卡,如果选择卡存在则获取焦点
  92. * @param {Object} data
  93. */
  94. Tab.prototype.tabAdd = function (data) {
  95. var that = this;
  96. var config = that.config;
  97. var tabIndex = that.exists(data.title);
  98. if(tabIndex === -1) {
  99. //设置只能同时打开多少个tab选项卡
  100. if(config.maxSetting !== 'undefined') {
  101. var currentTabCount = config.elem.children('ul.layui-tab-title').children('li').length;
  102. if(typeof config.maxSetting === 'number') {
  103. if(currentTabCount === config.maxSetting) {
  104. layer.msg('为了系统的流畅度,只能同时打开' + config.maxSetting + '个选项卡。');
  105. return;
  106. }
  107. }
  108. if(typeof config.maxSetting === 'object') {
  109. var max = config.maxSetting.max || 8;
  110. var msg = config.maxSetting.tipMsg || '为了系统的流畅度,只能同时打开' + max + '个选项卡。';
  111. if(currentTabCount === max) {
  112. layer.msg(msg);
  113. return;
  114. }
  115. }
  116. }
  117. globalTabIdIndex++;
  118. var content = '<iframe src="' + data.href + '" data-id="' + globalTabIdIndex + '"></iframe>';
  119. var title = '';
  120. if(data.icon !== undefined) {
  121. if(data.icon.indexOf('fa-') !== -1) {
  122. title += '<i class="fa ' + data.icon + '" aria-hidden="true"></i>';
  123. } else {
  124. title += '<i class="layui-icon">' + data.icon + '</i>';
  125. }
  126. }
  127. title += '<cite>' + data.title + '</cite>';
  128. if(config.closed) {
  129. title += '<i class="layui-icon layui-unselect layui-tab-close" data-id="' + globalTabIdIndex + '">&#x1006;</i>';
  130. }
  131. //添加tab
  132. console.log(data.href);
  133. element.tabAdd(elem.tabFilter, {
  134. title: title,
  135. content: content,
  136. id:new Date().getTime()
  137. });
  138. //iframe 自适应
  139. elem.contentBox.find('iframe[data-id=' + globalTabIdIndex + ']').each(function() {
  140. $(this).height(elem.contentBox.height());
  141. });
  142. if(config.closed) {
  143. //监听关闭事件
  144. elem.titleBox.find('li').children('i.layui-tab-close[data-id=' + globalTabIdIndex + ']').on('click', function() {
  145. element.tabDelete(elem.tabFilter, $(this).parent('li').attr('lay-id')).init();
  146. if(config.contextMenu) {
  147. $(document).find('div.uiba-contextmenu').remove(); //移除右键菜单dom
  148. }
  149. });
  150. };
  151. //切换到当前打开的选项卡
  152. element.tabChange(elem.tabFilter, that.getTabId(data.title));
  153. } else {
  154. element.tabChange(elem.tabFilter, that.getTabId(data.title));
  155. //自动刷新
  156. if(config.autoRefresh) {
  157. config.elem.find('div.layui-tab-content > div').eq(tabIndex).children('iframe')[0].contentWindow.location.reload();
  158. }
  159. }
  160. if(config.contextMenu) {
  161. element.on('tab(' + elem.tabFilter + ')', function() {
  162. $(document).find('div.admin-contextmenu').remove();
  163. });
  164. elem.titleBox.find('li').on('contextmenu', function(e) {
  165. //var $that = $(e.target);
  166. e.preventDefault();
  167. e.stopPropagation();
  168. var $target = e.target.nodeName === 'LI' ? e.target : e.target.parentElement;
  169. //判断,如果存在右键菜单的div,则移除,保存页面上只存在一个
  170. if($(document).find('div.admin-contextmenu').length > 0) {
  171. $(document).find('div.admin-contextmenu').remove();
  172. }
  173. //创建一个div
  174. var div = document.createElement('div');
  175. //设置一些属性
  176. div.className = 'admin-contextmenu';
  177. div.style.width = '130px';
  178. div.style.backgroundColor = 'white';
  179. var ul = '<ul>';
  180. ul += '<li data-target="refresh" title="刷新当前选项卡"><i class="fa fa-refresh" aria-hidden="true"></i> 刷新</li>';
  181. ul += '<li data-target="closeCurrent" title="关闭当前选项卡"><i class="fa fa-close" aria-hidden="true"></i> 关闭当前</li>';
  182. ul += '<li data-target="closeOther" title="关闭其他选项卡"><i class="fa fa-window-close-o" aria-hidden="true"></i> 关闭其他</li>';
  183. ul += '<li data-target="closeAll" title="关闭全部选项卡"><i class="fa fa-window-close-o" aria-hidden="true"></i> 全部关闭</li>';
  184. ul += '</ul>';
  185. div.innerHTML = ul;
  186. div.style.top = e.pageY + 'px';
  187. div.style.left = e.pageX + 'px';
  188. //将dom添加到body的末尾
  189. document.getElementsByTagName('body')[0].appendChild(div);
  190. //获取当前点击选项卡的id值
  191. var id = $($target).find('i.layui-tab-close').data('id');
  192. //获取当前点击选项卡的索引值
  193. var clickIndex = $($target).attr('lay-id');
  194. var $context = $(document).find('div.admin-contextmenu');
  195. if($context.length > 0) {
  196. $context.eq(0).children('ul').children('li').each(function() {
  197. var $that = $(this);
  198. //绑定菜单的点击事件
  199. $that.on('click', function() {
  200. //获取点击的target值
  201. var target = $that.data('target');
  202. //
  203. switch(target) {
  204. case 'refresh': //刷新当前
  205. var src = elem.contentBox.find('iframe[data-id=' + id + ']')[0].src;
  206. elem.contentBox.find('iframe[data-id=' + id + ']')[0].src = src;
  207. break;
  208. case 'closeCurrent': //关闭当前
  209. if(clickIndex !== 0) {
  210. element.tabDelete(elem.tabFilter, clickIndex);
  211. }
  212. break;
  213. case 'closeOther': //关闭其他
  214. elem.titleBox.children('li').each(function() {
  215. var $t = $(this);
  216. var id1 = $t.find('i.layui-tab-close').data('id');
  217. if(id1 !== id && id1 !== undefined) {
  218. element.tabDelete(elem.tabFilter, $t.attr('lay-id'));
  219. }
  220. });
  221. break;
  222. case 'closeAll': //全部关闭
  223. elem.titleBox.children('li').each(function() {
  224. var $t = $(this);
  225. if($t.index() !== 0) {
  226. element.tabDelete(elem.tabFilter, $t.attr('lay-id'));
  227. }
  228. });
  229. break;
  230. }
  231. //处理完后移除右键菜单的dom
  232. $context.remove();
  233. });
  234. });
  235. $(document).on('click',function(){
  236. $context.remove();
  237. });
  238. }
  239. return false;
  240. });
  241. }
  242. };
  243. Tab.prototype.on = function() {
  244. }
  245. var tab = new Tab();
  246. exports(modName, function(options) {
  247. return tab.set(options);
  248. });
  249. });