navtab.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. layui.define(['element'], function (exports) {
  2. var element = layui.element(),
  3. $ = layui.jquery,
  4. layer = parent.layer === undefined ? layui.layer : parent.layer,
  5. module_name = 'navtab',
  6. globalTabIdIndex = 0,
  7. LarryTab = function () {
  8. this.config = {
  9. elem: undefined,
  10. closed: true
  11. };
  12. };
  13. var ELEM = {};
  14. /**
  15. * [参数设置 options]
  16. */
  17. LarryTab.prototype.set = function (options) {
  18. var _this = this;
  19. $.extend(true, _this.config, options);
  20. return _this;
  21. };
  22. /**
  23. * [init 对象初始化]
  24. * @return {[type]} [返回对象初始化结果]
  25. */
  26. LarryTab.prototype.init = function () {
  27. var _this = this;
  28. var _config = _this.config;
  29. if (typeof(_config.elem) !== 'string' && typeof(_config.elem) !== 'object') {
  30. layer.alert('Tab选项卡错误提示: elem参数未定义或设置出错,具体设置格式请参考文档API.');
  31. }
  32. var $container;
  33. if (typeof(_config.elem) === 'string') {
  34. $container = $('' + _config.elem + '');
  35. //console.log($container);
  36. }
  37. if (typeof(_config.elem) === 'object') {
  38. $container = _config.elem;
  39. }
  40. if ($container.length === 0) {
  41. layer.alert('Tab选项卡错误提示:找不到elem参数配置的容器,请检查.');
  42. }
  43. var filter = $container.attr('lay-filter');
  44. if (filter === undefined || filter === '') {
  45. layer.alert('Tab选项卡错误提示:请为elem容器设置一个lay-filter过滤器');
  46. }
  47. _config.elem = $container;
  48. ELEM.titleBox = $container.children('ul.layui-tab-title');
  49. ELEM.contentBox = $container.children('div.layui-tab-content');
  50. ELEM.tabFilter = filter;
  51. return _this;
  52. };
  53. /**
  54. * [exists 在layui-tab中检查对应layui-tab-title是否存在,如果存在则返回索引值,不存在返回-1]
  55. * @param {[type]} title [description]
  56. * @return {[type]} [description]
  57. */
  58. LarryTab.prototype.exists = function (title) {
  59. var _this = ELEM.titleBox === undefined ? this.init() : this,
  60. tabIndex = -1;
  61. ELEM.titleBox.find('li').each(function (i, e) {
  62. var $em = $(this).children('em');
  63. if ($em.text() === title) {
  64. tabIndex = i;
  65. }
  66. ;
  67. });
  68. return tabIndex;
  69. };
  70. /**
  71. * [tabAdd 增加选项卡,如果已存在则增加this样式]
  72. * @param {[type]} data [description]
  73. * @return {[type]} [description]
  74. */
  75. LarryTab.prototype.tabAdd = function (data) {
  76. var _this = this;
  77. var tabIndex = _this.exists(data.title);
  78. // 若不存在
  79. if (tabIndex === -1) {
  80. globalTabIdIndex++;
  81. var content = '<iframe src="' + data.href + '" data-id="' + globalTabIdIndex + '" class="larry-iframe"></iframe>';
  82. var title = '';
  83. // 若icon有定义
  84. if (data.icon !== undefined) {
  85. if (data.icon.indexOf('fa-') !== -1) {
  86. title += '<i class="' + data.icon + '"></i>';
  87. } else {
  88. title += '<i class="layui-icon ">' + data.icon + '</i>';
  89. }
  90. }
  91. title += '<em>' + data.title + '</em>';
  92. if (_this.config.closed) {
  93. title += '<i class="layui-icon layui-unselect layui-tab-close" data-id="' + globalTabIdIndex + '">&#x1006;</i>';
  94. }
  95. //添加tab
  96. element.tabAdd(ELEM.tabFilter, {
  97. title: title,
  98. content: content
  99. });
  100. //iframe 自适应
  101. ELEM.contentBox.find('iframe[data-id=' + globalTabIdIndex + ']').each(function () {
  102. $(this).height(ELEM.contentBox.height());
  103. });
  104. if (_this.config.closed) {
  105. //监听关闭事件
  106. ELEM.titleBox.find('li').children('i.layui-tab-close[data-id=' + globalTabIdIndex + ']').on('click', function () {
  107. element.tabDelete(ELEM.tabFilter, $(this).parent('li').index()).init();
  108. });
  109. }
  110. ;
  111. //切换到当前打开的选项卡
  112. element.tabChange(ELEM.tabFilter, ELEM.titleBox.find('li').length - 1);
  113. } else {
  114. element.tabChange(ELEM.tabFilter, tabIndex);
  115. }
  116. };
  117. var navtab = new LarryTab();
  118. exports(module_name, function (options) {
  119. return navtab.set(options);
  120. });
  121. });