|
41 | 41 | var title = $(this).attr('title') ? $(this).attr('title') : $.trim($(this).text()); |
42 | 42 | var url = $(this).attr('url'); |
43 | 43 | var content = options.content ? options.content : $(this).attr('content'); |
44 | | - var ajax = $(this).attr('ajax') ? true : false; |
| 44 | + var ajax = $(this).attr('ajax') === '1' || $(this).attr('ajax') === 'true'; |
45 | 45 | var state = ({ |
46 | 46 | url: url, title: title, id: id, content: content, ajax: ajax |
47 | 47 | }); |
48 | 48 |
|
49 | 49 | document.title = title; |
50 | 50 | if (history.pushState && !$(this).data("pushstate")) { |
51 | | - var pushurl = url.indexOf("ref=addtabs") == -1 ? (url + (url.indexOf("?") > -1 ? "&" : "?") + "ref=addtabs") : url; |
| 51 | + var pushurl = url.indexOf("ref=addtabs") === -1 ? (url + (url.indexOf("?") > -1 ? "&" : "?") + "ref=addtabs") : url; |
52 | 52 | try { |
53 | 53 | window.history.pushState(state, title, pushurl); |
54 | 54 | } catch (e) { |
|
66 | 66 | } |
67 | 67 | }); |
68 | 68 |
|
69 | | - navobj.on('click', '.close-tab', function (e) { |
70 | | - id = $(this).prev("a").attr("aria-controls"); |
| 69 | + navobj.on('click', '.close-tab', function () { |
| 70 | + var id = $(this).prev("a").attr("aria-controls"); |
71 | 71 | _close(id); |
72 | 72 | return false; |
73 | 73 | }); |
74 | | - navobj.on('dblclick', 'li[role=presentation]', function (e) { |
| 74 | + navobj.on('dblclick', 'li[role=presentation]', function () { |
75 | 75 | $(this).find(".close-tab").trigger("click"); |
76 | 76 | }); |
77 | | - navobj.on('click', 'li[role=presentation]', function (e) { |
| 77 | + navobj.on('click', 'li[role=presentation]', function () { |
78 | 78 | $("a[addtabs=" + $("a", this).attr("node-id") + "]").trigger("click"); |
79 | 79 | }); |
80 | 80 |
|
81 | 81 | $(window).resize(function () { |
82 | | - if (typeof options.nav == 'object') { |
| 82 | + if (typeof options.nav === 'object') { |
83 | 83 | var siblingsWidth = 0; |
84 | 84 | navobj.siblings().each(function () { |
85 | 85 | siblingsWidth += $(this).outerWidth(); |
86 | 86 | }); |
87 | 87 | navobj.width(navobj.parent().width() - siblingsWidth); |
88 | 88 | } else { |
89 | | - $("#nav").width($("#header > .navbar").width() - $(".sidebar-toggle").outerWidth() - $(".navbar-custom-menu").outerWidth() - 20); |
| 89 | + $("#nav").width($("#header").find("> .navbar").width() - $(".sidebar-toggle").outerWidth() - $(".navbar-custom-menu").outerWidth() - 20); |
90 | 90 | } |
91 | 91 | _drop(); |
92 | 92 | }); |
|
98 | 98 | conid = 'con_' + opts.id; |
99 | 99 | url = opts.url; |
100 | 100 | url += (opts.url.indexOf("?") > -1 ? "&addtabs=1" : "?addtabs=1"); |
| 101 | + |
| 102 | + var tabitem = $('#' + tabid, navobj); |
| 103 | + var conitem = $('#' + conid, tabobj); |
| 104 | + |
101 | 105 | navobj.find("[role='presentation']").removeClass('active'); |
102 | 106 | tabobj.find("[role='tabpanel']").removeClass('active'); |
| 107 | + |
103 | 108 | //如果TAB不存在,创建一个新的TAB |
104 | | - if ($("#" + tabid).size() == 0) { |
| 109 | + if (tabitem.size() === 0) { |
105 | 110 | //创建新TAB的title |
106 | | - title = $('<li role="presentation" id="' + tabid + '"><a href="#' + conid + '" node-id="' + opts.id + '" aria-controls="' + id + '" role="tab" data-toggle="tab">' + opts.title + '</a></li>'); |
| 111 | + tabitem = $('<li role="presentation" id="' + tabid + '"><a href="#' + conid + '" node-id="' + opts.id + '" aria-controls="' + id + '" role="tab" data-toggle="tab">' + opts.title + '</a></li>'); |
107 | 112 | //是否允许关闭 |
108 | 113 | if (options.close && $("li", navobj).size() > 0) { |
109 | | - title.append(' <i class="close-tab fa fa-remove"></i>'); |
| 114 | + tabitem.append(' <i class="close-tab fa fa-remove"></i>'); |
110 | 115 | } |
111 | 116 | //创建新TAB的内容 |
112 | | - content = $('<div role="tabpanel" class="tab-pane" id="' + conid + '"></div>'); |
| 117 | + conitem = $('<div role="tabpanel" class="tab-pane" id="' + conid + '"></div>'); |
113 | 118 | //是否指定TAB内容 |
114 | 119 | if (opts.content) { |
115 | | - content.append(opts.content); |
| 120 | + conitem.append(opts.content); |
116 | 121 | } else if (options.iframeUse && !opts.ajax) {//没有内容,使用IFRAME打开链接 |
117 | 122 | var height = options.iframeHeight; |
118 | | - content.append('<iframe src="' + url + '" width="100%" height="' + height + '" frameborder="no" border="0" marginwidth="0" marginheight="0" scrolling-x="no" scrolling-y="auto" allowtransparency="yes"></iframe></div>'); |
| 123 | + conitem.append('<iframe src="' + url + '" width="100%" height="' + height + '" frameborder="no" border="0" marginwidth="0" marginheight="0" scrolling-x="no" scrolling-y="auto" allowtransparency="yes"></iframe></div>'); |
119 | 124 | } else { |
120 | 125 | $.get(url, function (data) { |
121 | | - content.append(data); |
| 126 | + conitem.append(data); |
122 | 127 | }); |
123 | 128 | } |
124 | 129 | //加入TABS |
125 | | - if ($('.tabdrop li').size() > 0) { |
126 | | - $('.tabdrop ul').append(title); |
| 130 | + if ($('.tabdrop li', navobj).size() > 0) { |
| 131 | + $('.tabdrop ul', navobj).append(tabitem); |
127 | 132 | } else { |
128 | | - navobj.append(title); |
| 133 | + navobj.append(tabitem); |
129 | 134 | } |
130 | | - tabobj.append(content); |
| 135 | + tabobj.append(conitem); |
131 | 136 | } else { |
132 | 137 | //强制刷新iframe |
133 | 138 | if (options.iframeForceRefresh) { |
|
138 | 143 | } |
139 | 144 | localStorage.setItem("addtabs", $(this).prop('outerHTML')); |
140 | 145 | //激活TAB |
141 | | - $("#" + tabid).addClass('active'); |
142 | | - $("#" + conid).addClass("active"); |
| 146 | + tabitem.addClass('active'); |
| 147 | + conitem.addClass("active"); |
143 | 148 | _drop(); |
144 | 149 | }; |
145 | 150 |
|
146 | 151 | var _close = function (id) { |
147 | 152 | var tabid = 'tab_' + id; |
148 | 153 | var conid = 'con_' + id; |
| 154 | + var tabitem = $('#' + tabid, navobj); |
| 155 | + var conitem = $('#' + conid, tabobj); |
149 | 156 | //如果关闭的是当前激活的TAB,激活他的前一个TAB |
150 | | - if (obj.find("li.active").not('.tabdrop').attr('id') == tabid) { |
151 | | - if ($("#" + tabid).prev().not(".tabdrop").size() > 0) { |
152 | | - $("#" + tabid).prev().not(".tabdrop").find("a").trigger("click"); |
153 | | - } else if ($("#" + tabid).next().size() > 0) { |
154 | | - $("#" + tabid).next().trigger("click"); |
| 157 | + if (obj.find("li.active").not('.tabdrop').attr('id') === tabid) { |
| 158 | + var prev = tabitem.prev().not(".tabdrop"); |
| 159 | + var next = tabitem.next().not(".tabdrop"); |
| 160 | + if (prev.size() > 0) { |
| 161 | + prev.find('a').trigger("click"); |
| 162 | + } else if (next.size() > 0) { |
| 163 | + next.find('a').trigger("click"); |
155 | 164 | } else { |
156 | | - $(">li:last > a", navobj).trigger('click'); |
| 165 | + $(">li:not(.tabdrop):last > a", navobj).trigger('click'); |
157 | 166 | } |
158 | 167 | } |
159 | 168 | //关闭TAB |
160 | | - $("#" + tabid).remove(); |
161 | | - $("#" + conid).remove(); |
| 169 | + tabitem.remove(); |
| 170 | + conitem.remove(); |
162 | 171 | _drop(); |
163 | 172 | options.callback(); |
164 | 173 | }; |
|
171 | 180 | $.fn.refreshAddtabs = function () { |
172 | 181 | var navobj = $(this); |
173 | 182 | var dropdown = $(".tabdrop", navobj); |
174 | | - if (dropdown.size() == 0) { |
175 | | - var dropdown = $('<li class="dropdown pull-right hide tabdrop"><a class="dropdown-toggle" data-toggle="dropdown" href="javascript:;">' + |
| 183 | + if (dropdown.size() === 0) { |
| 184 | + dropdown = $('<li class="dropdown pull-right hide tabdrop"><a class="dropdown-toggle" data-toggle="dropdown" href="javascript:;">' + |
176 | 185 | '<i class="glyphicon glyphicon-align-justify"></i>' + |
177 | 186 | ' <b class="caret"></b></a><ul class="dropdown-menu"></ul></li>'); |
178 | | - dropdown.appendTo(navobj); |
| 187 | + dropdown.prependTo(navobj); |
179 | 188 | } |
180 | 189 |
|
181 | 190 | //检测是否有下拉样式 |
|
190 | 199 | //检查超过一行的标签页 |
191 | 200 | var litabs = navobj.append(dropdown.find('li')).find('>li').not('.tabdrop'); |
192 | 201 | var totalwidth = 0; |
193 | | - litabs.each(function (i, j) { |
| 202 | + litabs.each(function () { |
194 | 203 | totalwidth += $(this).outerWidth(true); |
195 | 204 | }); |
196 | 205 | if (navobj.width() < totalwidth) { |
197 | | - litabs.each(function (i, j) { |
| 206 | + litabs.each(function () { |
198 | 207 | liwidth += $(this).outerWidth(true); |
199 | 208 | if (liwidth > maxwidth) { |
200 | 209 | dropdown.find('ul').append($(this)); |
|
203 | 212 | }); |
204 | 213 | if (collection > 0) { |
205 | 214 | dropdown.removeClass('hide'); |
206 | | - if (dropdown.find('.active').length == 1) { |
| 215 | + if (dropdown.find('.active').length === 1) { |
207 | 216 | dropdown.addClass('active'); |
208 | 217 | } else { |
209 | 218 | dropdown.removeClass('active'); |
|
0 commit comments