Tabs.hx 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package hide.comp;
  2. class Tabs extends Component {
  3. public var currentTab(default, set) : Element;
  4. var header : Element;
  5. public function new(?parent,?el,bottomTabs=false) {
  6. super(parent,el);
  7. element.addClass("hide-tabs");
  8. if( bottomTabs ) element.addClass("tabs-bottom");
  9. header = new Element("<div>").addClass("tabs-header").prependTo(element);
  10. syncTabs();
  11. var t = getTabs()[0];
  12. if( t != null ) currentTab = new Element(t);
  13. }
  14. public function createTab( title : String, ?icon : String ) {
  15. var e = new Element('<div class="tab" name="$title">');
  16. if( icon != null ) e.attr("icon",icon);
  17. e.appendTo(element);
  18. syncTabs();
  19. if( currentTab == null )
  20. currentTab = e;
  21. return e;
  22. }
  23. public function getHeader( tab : Element ) {
  24. var index = [for( t in getTabs() ) t].indexOf(tab[0]);
  25. if( index < 0 ) return null;
  26. return header.find('[index=$index]');
  27. }
  28. public function allowMask(scene : hide.comp.Scene) {
  29. new Element('<a href="#" class="maskToggle"></a>').prependTo(element).click((_) -> {
  30. element.toggleClass("masked");
  31. @:privateAccess scene.window.checkResize();
  32. });
  33. }
  34. function set_currentTab( e : Element ) {
  35. var index = Std.parseInt(e.attr("index"));
  36. getTabs().hide();
  37. header.children().removeClass("active").filter("[index=" + index + "]").addClass("active");
  38. currentTab = e;
  39. onTabChange(index);
  40. e.show();
  41. return e;
  42. }
  43. public function getTabs() : Element {
  44. return element.children(".tab");
  45. }
  46. public dynamic function onTabRightClick( index : Int ) {
  47. }
  48. public dynamic function onTabChange( index : Int ) {
  49. }
  50. function syncTabs() {
  51. header.html("");
  52. var index = 0;
  53. for( t in getTabs().elements() ) {
  54. var icon = t.attr("icon");
  55. var name = t.attr("name");
  56. var index = index++;
  57. var tab = new Element("<div>").html( (icon != null ? '<div class="ico ico-$icon"></div> ' : '') + (name != null ? name : '') );
  58. t.attr("index", index);
  59. tab.attr("index", index);
  60. tab.appendTo(header);
  61. tab.click(function(e) {
  62. currentTab = t;
  63. }).contextmenu(function(e) {
  64. e.preventDefault();
  65. onTabRightClick(index);
  66. });
  67. }
  68. if( currentTab != null )
  69. this.currentTab = currentTab;
  70. }
  71. }