2 * Copyright 2013 IBM Corp.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 RED.tabs = (function() {
22 function createTabs(options) {
25 var ul = $("#"+options.id)
26 ul.addClass("red-ui-tabs");
27 ul.children().first().addClass("active");
28 ul.children().addClass("red-ui-tab");
30 function onTabClick() {
35 function onTabDblClick() {
36 if (options.ondblclick) {
37 options.ondblclick(tabs[$(this).attr('href').slice(1)]);
42 function activateTab(link) {
43 if (typeof link === "string") {
44 link = ul.find("a[href='#"+link+"']");
46 if (!link.parent().hasClass("active")) {
47 ul.children().removeClass("active");
48 link.parent().addClass("active");
49 if (options.onchange) {
50 options.onchange(tabs[link.attr('href').slice(1)]);
55 function updateTabWidths() {
56 var tabs = ul.find("li.red-ui-tab");
57 var width = ul.width();
58 var tabCount = tabs.size();
59 var tabWidth = (width-6-(tabCount*7))/tabCount;
60 var pct = 100*tabWidth/width;
61 tabs.css({width:pct+"%"});
64 ul.find("li.red-ui-tab a").on("click",onTabClick).on("dblclick",onTabDblClick);
68 function removeTab(id) {
69 var li = ul.find("a[href='#"+id+"']").parent();
70 if (li.hasClass("active")) {
72 if (tab.size() === 0) {
75 activateTab(tab.find("a"));
78 if (options.onremove) {
79 options.onremove(tabs[id]);
86 addTab: function(tab) {
88 var li = $("<li/>",{class:"red-ui-tab"}).appendTo(ul);
89 var link = $("<a/>",{href:"#"+tab.id, class:"red-ui-tab-label"}).appendTo(li);
92 link.on("click",onTabClick);
93 link.on("dblclick",onTabDblClick);
95 var closeLink = $("<a/>",{href:"#",class:"red-ui-tab-close"}).appendTo(li);
96 closeLink.html('<i class="fa fa-times" />');
98 closeLink.on("click",function(event) {
106 link.attr("title",tab.label);
107 if (ul.find("li.red-ui-tab").size() == 1) {
111 removeTab: removeTab,
112 activateTab: activateTab,
113 resize: updateTabWidths,
115 return ul.find("li.red-ui-tab").size();
117 contains: function(id) {
118 return ul.find("a[href='#"+id+"']").length > 0;