@@ -82,6 +82,22 @@ UI.Element.prototype = {
8282
8383 } ,
8484
85+ addClass : function ( name ) {
86+
87+ this . dom . classList . add ( name ) ;
88+
89+ return this ;
90+
91+ } ,
92+
93+ removeClass : function ( name ) {
94+
95+ this . dom . classList . remove ( name ) ;
96+
97+ return this ;
98+
99+ } ,
100+
85101 setStyle : function ( style , array ) {
86102
87103 for ( var i = 0 ; i < array . length ; i ++ ) {
@@ -995,3 +1011,122 @@ UI.Button.prototype.setLabel = function ( value ) {
9951011 return this ;
9961012
9971013} ;
1014+
1015+
1016+ // TabbedPanel
1017+
1018+ UI . TabbedPanel = function ( ) {
1019+
1020+ UI . Element . call ( this ) ;
1021+
1022+ var dom = document . createElement ( 'div' ) ;
1023+
1024+ this . dom = dom ;
1025+
1026+ this . setClass ( 'TabbedPanel' ) ;
1027+
1028+ this . tabs = [ ] ;
1029+ this . panels = [ ] ;
1030+
1031+ this . tabsDiv = new UI . Div ( ) ;
1032+ this . tabsDiv . setClass ( 'Tabs' ) ;
1033+
1034+ this . panelsDiv = new UI . Div ( ) ;
1035+ this . panelsDiv . setClass ( 'Panels' ) ;
1036+
1037+ this . add ( this . tabsDiv ) ;
1038+ this . add ( this . panelsDiv ) ;
1039+
1040+ this . selected = '' ;
1041+
1042+ return this ;
1043+
1044+ }
1045+
1046+ UI . TabbedPanel . prototype = Object . create ( UI . Element . prototype ) ;
1047+ UI . TabbedPanel . prototype . constructor = UI . TabbedPanel ;
1048+
1049+ UI . TabbedPanel . prototype . select = function ( id ) {
1050+
1051+ var tab ;
1052+ var panel ;
1053+ var scope = this ;
1054+
1055+ // Deselect current selection
1056+ if ( this . selected && this . selected . length ) {
1057+ tab = this . tabs . find ( function ( item ) { return item . dom . id === scope . selected } ) ;
1058+ panel = this . panels . find ( function ( item ) { return item . dom . id === scope . selected } ) ;
1059+
1060+ if ( tab ) {
1061+
1062+ tab . removeClass ( 'selected' ) ;
1063+
1064+ }
1065+
1066+ if ( panel ) {
1067+
1068+ panel . setDisplay ( 'none' ) ;
1069+
1070+ }
1071+
1072+ }
1073+
1074+ tab = this . tabs . find ( function ( item ) { return item . dom . id === id } ) ;
1075+ panel = this . panels . find ( function ( item ) { return item . dom . id === id } ) ;
1076+
1077+ if ( tab ) {
1078+
1079+ tab . addClass ( 'selected' ) ;
1080+
1081+ }
1082+
1083+ if ( panel ) {
1084+
1085+ panel . setDisplay ( '' ) ;
1086+
1087+ }
1088+
1089+ this . selected = id ;
1090+
1091+ return this ;
1092+
1093+ }
1094+
1095+ UI . TabbedPanel . prototype . addTab = function ( id , label , items ) {
1096+
1097+ var tab = new UI . TabbedPanel . Tab ( label , this ) ;
1098+ tab . setId ( id ) ;
1099+ this . tabs . push ( tab ) ;
1100+ this . tabsDiv . add ( tab ) ;
1101+
1102+ var panel = new UI . Div ( ) ;
1103+ panel . setId ( id ) ;
1104+ panel . add ( items ) ;
1105+ panel . setDisplay ( 'none' ) ;
1106+ this . panels . push ( panel ) ;
1107+ this . panelsDiv . add ( panel ) ;
1108+
1109+ this . select ( id ) ;
1110+
1111+ }
1112+
1113+ UI . TabbedPanel . Tab = function ( text , parent ) {
1114+
1115+ UI . Text . call ( this , text ) ;
1116+ this . parent = parent ;
1117+
1118+ this . setClass ( 'Tab' ) ;
1119+
1120+ var scope = this ;
1121+ this . dom . addEventListener ( 'click' , function ( event ) {
1122+
1123+ scope . parent . select ( scope . dom . id ) ;
1124+
1125+ } )
1126+
1127+ return this ;
1128+
1129+ }
1130+
1131+ UI . TabbedPanel . Tab . prototype = Object . create ( UI . Text . prototype ) ;
1132+ UI . TabbedPanel . Tab . prototype . constructor = UI . TabbedPanel . Tab ;
0 commit comments