No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

128 líneas
4.0KB

  1. /**
  2. * Copyright 2013 IBM Corp.
  3. *
  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
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  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.
  15. **/
  16. RED.tabs = (function() {
  17. function createTabs(options) {
  18. var tabs = {};
  19. var ul = $("#"+options.id)
  20. ul.addClass("red-ui-tabs");
  21. ul.children().first().addClass("active");
  22. ul.children().addClass("red-ui-tab");
  23. function onTabClick() {
  24. activateTab($(this));
  25. return false;
  26. }
  27. function onTabDblClick() {
  28. if (options.ondblclick) {
  29. options.ondblclick(tabs[$(this).attr('href').slice(1)]);
  30. }
  31. return false;
  32. }
  33. function activateTab(link) {
  34. if (typeof link === "string") {
  35. link = ul.find("a[href='#"+link+"']");
  36. }
  37. if (!link.parent().hasClass("active")) {
  38. ul.children().removeClass("active");
  39. link.parent().addClass("active");
  40. if (options.onchange) {
  41. options.onchange(tabs[link.attr('href').slice(1)]);
  42. }
  43. }
  44. }
  45. function updateTabWidths() {
  46. var tabs = ul.find("li.red-ui-tab");
  47. var width = ul.width();
  48. var tabCount = tabs.size();
  49. var tabWidth = (width-6-(tabCount*7))/tabCount;
  50. var pct = 100*tabWidth/width;
  51. tabs.css({width:pct+"%"});
  52. }
  53. ul.find("li.red-ui-tab a").on("click",onTabClick).on("dblclick",onTabDblClick);
  54. updateTabWidths();
  55. function removeTab(id) {
  56. var li = ul.find("a[href='#"+id+"']").parent();
  57. if (li.hasClass("active")) {
  58. var tab = li.prev();
  59. if (tab.size() === 0) {
  60. tab = li.next();
  61. }
  62. activateTab(tab.find("a"));
  63. }
  64. li.remove();
  65. if (options.onremove) {
  66. options.onremove(tabs[id]);
  67. }
  68. delete tabs[id];
  69. updateTabWidths();
  70. }
  71. return {
  72. addTab: function(tab) {
  73. tabs[tab.id] = tab;
  74. var li = $("<li/>",{class:"red-ui-tab"}).appendTo(ul);
  75. var link = $("<a/>",{href:"#"+tab.id, class:"red-ui-tab-label"}).appendTo(li);
  76. link.html(tab.label);
  77. link.on("click",onTabClick);
  78. link.on("dblclick",onTabDblClick);
  79. if (tab.closeable) {
  80. var closeLink = $("<a/>",{href:"#",class:"red-ui-tab-close"}).appendTo(li);
  81. closeLink.html('<i class="icon-remove" />');
  82. closeLink.on("click",function(event) {
  83. removeTab(tab.id);
  84. });
  85. }
  86. updateTabWidths();
  87. if (options.onadd) {
  88. options.onadd(tab);
  89. }
  90. link.attr("title",tab.label);
  91. if (ul.find("li.red-ui-tab").size() == 1) {
  92. activateTab(link);
  93. }
  94. },
  95. removeTab: removeTab,
  96. activateTab: activateTab,
  97. resize: updateTabWidths,
  98. count: function() {
  99. return ul.find("li.red-ui-tab").size();
  100. },
  101. contains: function(id) {
  102. return ul.find("a[href='#"+id+"']").length > 0;
  103. }
  104. }
  105. }
  106. return {
  107. create: createTabs
  108. }
  109. })();