@@ -26,6 +26,7 @@ const utils = require('./utils')
2626// options.showMain(div, subject) function to show subject in div when tab selected
2727// options.renderTabSettings function(subject, domContainer)
2828// options.renderTabSettings like showMain but when user has held Alt down
29+ // options.onClose if given, will present a cancelButton next to tabs that calls this optional method
2930//
3031
3132UI . tabs . tabWidget = function ( options ) {
@@ -41,6 +42,7 @@ UI.tabs.tabWidget = function (options) {
4142 var wholetable = box . appendChild ( dom . createElement ( 'table' ) )
4243 var mainTR , mainTD , tabTR
4344 var tabContainer , tabElement
45+ var onClose = options . onClose
4446
4547 var isLight = function ( x ) {
4648 var total = 0
@@ -140,7 +142,11 @@ UI.tabs.tabWidget = function (options) {
140142
141143 var resetTabStyle = function ( ) {
142144 for ( var i = 0 ; i < tabContainer . children . length ; i ++ ) {
143- tabContainer . children [ i ] . firstChild . setAttribute ( 'style' , unselectedStyle )
145+ const tab = tabContainer . children [ i ]
146+ if ( tab . classList . contains ( 'unstyled' ) ) {
147+ continue
148+ }
149+ tab . firstChild . setAttribute ( 'style' , unselectedStyle )
144150 }
145151 }
146152 var resetBodyStyle = function ( ) {
@@ -193,7 +199,7 @@ UI.tabs.tabWidget = function (options) {
193199 var orderedSync = function ( ) {
194200 var items = getItems ( )
195201 if ( ! vertical ) {
196- mainTD . setAttribute ( 'colspan' , items . length )
202+ mainTD . setAttribute ( 'colspan' , items . length + ( onClose ? 1 : 0 ) )
197203 }
198204 var slot , i , j , left , right
199205 var differ = false
@@ -238,13 +244,16 @@ UI.tabs.tabWidget = function (options) {
238244 bodyContainer . insertBefore ( newBodyTR , bodyContainer . children [ left + i ] )
239245 }
240246 }
247+ if ( onClose ) {
248+ addCancelButton ( tabContainer )
249+ }
241250 }
242251
243- // UNMAINTAINED
252+ // UNMAINTAINED
244253 var unorderedSync = function ( ) {
245254 var items = getItems ( )
246255 if ( ! vertical ) {
247- mainTD . setAttribute ( 'colspan' , items . length )
256+ mainTD . setAttribute ( 'colspan' , items . length + ( onClose ? 1 : 0 ) )
248257 }
249258 var slot , i , j , found , pair
250259 var missing = [ ]
@@ -280,6 +289,10 @@ UI.tabs.tabWidget = function (options) {
280289 tabContainer . removeChild ( slot )
281290 }
282291 }
292+
293+ if ( onClose ) {
294+ addCancelButton ( tabContainer )
295+ }
283296 }
284297
285298 var sync = function ( ) {
@@ -310,4 +323,13 @@ UI.tabs.tabWidget = function (options) {
310323 tabContainer . children [ 0 ] . firstChild . click ( ) // Open first tab
311324 }
312325 return box
326+
327+ function addCancelButton ( tabContainer ) {
328+ const extraTab = dom . createElement ( 'td' )
329+ extraTab . classList . add ( 'unstyled' )
330+ extraTab . style . textAlign = 'right'
331+ const cancelButton = UI . widgets . cancelButton ( dom , onClose )
332+ extraTab . appendChild ( cancelButton )
333+ tabContainer . appendChild ( extraTab )
334+ }
313335}
0 commit comments