@@ -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,14 +199,14 @@ 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
200206 // Find how many match at each end
201207 for ( left = 0 ; left < tabContainer . children . length ; left ++ ) {
202208 slot = tabContainer . children [ left ]
203- if ( left >= items . length || ! slot . subject . sameTerm ( items [ left ] ) ) {
209+ if ( left >= items . length || ( slot . subject && ! slot . subject . sameTerm ( items [ left ] ) ) ) {
204210 differ = true
205211 break
206212 }
@@ -211,7 +217,7 @@ UI.tabs.tabWidget = function (options) {
211217 for ( right = tabContainer . children . length - 1 ; right >= 0 ; right -- ) {
212218 slot = tabContainer . children [ right ]
213219 j = right - tabContainer . children . length + items . length
214- if ( ! slot . subject . sameTerm ( items [ j ] ) ) {
220+ if ( slot . subject && ! slot . subject . sameTerm ( items [ j ] ) ) {
215221 break
216222 }
217223 }
@@ -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,22 @@ UI.tabs.tabWidget = function (options) {
310323 tabContainer . children [ 0 ] . firstChild . click ( ) // Open first tab
311324 }
312325 return box
326+
327+ function addCancelButton ( tabContainer ) {
328+ if ( tabContainer . dataset . onCloseSet ) {
329+ // @@ TODO: this is only here to make the tests work
330+ // Discussion at https://github.com/solid/solid-ui/pull/110#issuecomment-527080663
331+ const existingCancelButton = tabContainer . querySelector ( '.unstyled' )
332+ tabContainer . removeChild ( existingCancelButton )
333+ }
334+ const extraTab = dom . createElement ( tabElement )
335+ extraTab . classList . add ( 'unstyled' )
336+ if ( tabElement === 'td' ) {
337+ extraTab . style . textAlign = 'right'
338+ }
339+ const cancelButton = UI . widgets . cancelButton ( dom , onClose )
340+ extraTab . appendChild ( cancelButton )
341+ tabContainer . appendChild ( extraTab )
342+ tabContainer . dataset . onCloseSet = 'true'
343+ }
313344}
0 commit comments