@@ -23,38 +23,41 @@ function getElementIndex(el) {
2323
2424var mappingTableInfos = [ ] ;
2525
26- function viewAsSingleTable ( mappingTableInfo ) {
27- hideElement ( mappingTableInfo . detailsContainer ) ;
28- // add <summary> @id to ids array and remove @id from summary
29- queryAll ( 'summary' , mappingTableInfo . detailsContainer ) . forEach ( function (
26+ function viewAsSingleTable ( tableContainer , detailsContainer ) {
27+ hideElement ( detailsContainer ) ;
28+ showElement ( tableContainer ) ;
29+
30+ // Remove ids from summary
31+ queryAll ( 'summary' , detailsContainer ) . forEach ( function (
3032 summary
3133 ) {
3234 summary . dataset [ 'id' ] = summary . id ;
3335 summary . removeAttribute ( 'id' ) ;
3436 } ) ;
35- showElement ( mappingTableInfo . tableContainer ) ;
3637
37- // add relevant @id to tr
38- queryAll ( 'tbody tr' , mappingTableInfo . tableContainer ) . forEach ( function (
38+ // Add ids to table
39+ queryAll ( 'tbody tr' , tableContainer ) . forEach ( function (
3940 tr
4041 ) {
4142 tr . id = tr . dataset [ 'id' ] ;
4243 tr . removeAttribute ( 'data-id' ) ;
4344 } ) ;
4445}
4546
46- function viewAsDetails ( mappingTableInfo ) {
47- hideElement ( mappingTableInfo . tableContainer ) ;
48- // add tr @id to ids array and remove @id from tr
49- queryAll ( 'tbody tr' , mappingTableInfo . tableContainer ) . forEach ( function (
47+ function viewAsDetails ( tableContainer , detailsContainer ) {
48+ hideElement ( tableContainer ) ;
49+ showElement ( detailsContainer ) ;
50+
51+ // Remove ids from table
52+ queryAll ( 'tbody tr' , tableContainer ) . forEach ( function (
5053 tr
5154 ) {
5255 tr . dataset [ 'id' ] = tr . id ;
5356 tr . removeAttribute ( 'id' ) ;
5457 } ) ;
55- showElement ( mappingTableInfo . detailsContainer ) ;
56- // add relevant @id to summary
57- queryAll ( 'summary' , mappingTableInfo . detailsContainer ) . forEach ( function (
58+
59+ // Add ids to summary
60+ queryAll ( 'summary' , detailsContainer ) . forEach ( function (
5861 summary
5962 ) {
6063 summary . id = summary . dataset [ 'id' ] ;
@@ -72,6 +75,7 @@ function expandReferredDetails(summaryFragId) {
7275
7376function mappingTables ( ) {
7477 queryAll ( '.table-container' ) . forEach ( function ( container ) {
78+
7579 // object to store information about a mapping table.
7680 var tableInfo = { } ;
7781 mappingTableInfos . push ( tableInfo ) ;
@@ -94,22 +98,8 @@ function mappingTables() {
9498
9599 // add switch to view as single table or details/summary
96100 var viewSwitch = document . createElement ( 'button' ) ;
97- viewSwitch . className = 'switch-view removeOnSave ' ;
101+ viewSwitch . className = 'switch-view' ;
98102 viewSwitch . innerHTML = mappingTableLabels . viewByTable ;
99- viewSwitch . addEventListener ( 'click' , function ( ) {
100- // if current view is details/summary
101- if ( tableInfo . detailsContainer . style . display !== 'none' ) {
102- viewAsSingleTable ( tableInfo ) ;
103- // toggle the viewSwitch label from view-as-single-table to view-by-X
104- viewSwitch . innerHTML =
105- mappingTableLabels . viewByLabels [ tableInfo . table . id ] ;
106- } else {
107- viewAsDetails ( tableInfo ) ;
108- // toggle the viewSwitch label from view-by-X to view-as-single-table.
109- viewSwitch . innerHTML = mappingTableLabels . viewByTable ;
110- }
111- } ) ;
112-
113103 tableInfo . tableContainer . insertAdjacentElement ( 'beforebegin' , viewSwitch ) ;
114104
115105 // store the table's column headers in array colHeaders
@@ -127,7 +117,9 @@ function mappingTables() {
127117 var caption = row . querySelector ( 'th' ) . innerHTML ;
128118 var summary = caption . replace ( / < a [ ^ > ] + > | < \/ a > / g, '' ) ;
129119 // get the tr's @id
130- var id = row . dataset . id ;
120+ var id = row . id ;
121+ row . dataset . id = id ;
122+
131123 // remove the tr's @id since same id will be used in the relevant summary element
132124 row . removeAttribute ( 'id' ) ;
133125 // store the row's cells in array rowCells
@@ -187,12 +179,12 @@ function mappingTables() {
187179
188180 // add 'expand/collapse all' functionality
189181 var expandAllButton = document . createElement ( 'button' ) ;
190- expandAllButton . className = 'expand removeOnSave ' ;
182+ expandAllButton . className = 'expand' ;
191183 expandAllButton . innerHTML = mappingTableLabels . expand ;
192184
193185 var collapseAllButton = document . createElement ( 'button' ) ;
194186 collapseAllButton . disabled = true ;
195- collapseAllButton . className = 'collapse removeOnSave ' ;
187+ collapseAllButton . className = 'collapse' ;
196188 collapseAllButton . innerHTML = mappingTableLabels . collapse ;
197189
198190 tableInfo . detailsContainer . insertBefore (
@@ -203,97 +195,6 @@ function mappingTables() {
203195 expandAllButton ,
204196 tableInfo . detailsContainer . firstChild
205197 ) ;
206-
207- var expandCollapseDetails = function ( detCont , action ) {
208- queryAll ( 'details' , detCont ) . forEach ( function ( details ) {
209- details . open = action !== 'collapse'
210- } ) ;
211- } ;
212-
213- expandAllButton . addEventListener ( 'click' , function ( ) {
214- expandCollapseDetails ( tableInfo . detailsContainer , 'expand' ) ;
215- expandAllButton . disabled = true ;
216- tableInfo . detailsContainer
217- . querySelector ( 'button.collapse' )
218- . removeAttribute ( 'disabled' ) ;
219- } ) ;
220-
221- collapseAllButton . addEventListener ( 'click' , function ( ) {
222- expandCollapseDetails ( tableInfo . detailsContainer , 'collapse' ) ;
223- collapseAllButton . disabled = true ;
224- tableInfo . detailsContainer
225- . querySelector ( 'button.expand' )
226- . removeAttribute ( 'disabled' ) ;
227- } ) ;
228-
229- // add collapsible table columns functionality
230- var showHideCols = document . createElement ( 'div' ) ;
231- showHideCols . className = 'show-hide-cols removeOnSave' ;
232- showHideCols . innerHTML =
233- '<span>' + mappingTableLabels . showHideCols + '</span>' ;
234-
235- for ( var i = 0 , len = colHeaders . length ; i < len ; i ++ ) {
236- ( function ( i ) {
237- var toggleLabel = colHeaders [ i ]
238- . replace ( / < a [ ^ < ] + > | < \/ a > / g, '' )
239- . replace ( / < s u p > \[ N o t e [ 0 - 9 ] + \] < \/ s u p > / g, '' ) ;
240-
241- var showHideColButton = document . createElement ( 'button' ) ;
242- showHideColButton . className = 'hide-col' ;
243- showHideColButton . setAttribute ( 'aria-pressed' , false ) ;
244- showHideColButton . setAttribute (
245- 'title' ,
246- mappingTableLabels . hideToolTipText
247- ) ;
248- showHideColButton . innerHTML =
249- '<span class="action">' +
250- mappingTableLabels . hideActionText +
251- '</span>' +
252- toggleLabel ;
253-
254- showHideColButton . addEventListener ( 'click' , function ( ) {
255- var index = getElementIndex ( showHideColButton ) ;
256- console . log ( "index?" , index ) ;
257- var wasHidden = showHideColButton . className === 'hide-col' ;
258-
259- queryAll (
260- 'tr>th:nth-child(' + index + '), tr>td:nth-child(' + index + ')' ,
261- tableInfo . table
262- ) . forEach ( function ( element ) {
263- if ( wasHidden ) {
264- hideElement ( element ) ;
265- } else {
266- showElement ( element ) ;
267- }
268- } ) ;
269-
270- showHideColButton . className = wasHidden ? 'show-col' : 'hide-col' ;
271- showHideColButton . setAttribute ( 'aria-pressed' , wasHidden ) ;
272- showHideColButton . setAttribute (
273- 'title' ,
274- wasHidden
275- ? mappingTableLabels . showToolTipText
276- : mappingTableLabels . hideToolTipText
277- ) ;
278- showHideColButton . querySelector ( 'span' ) . innerText = wasHidden
279- ? mappingTableLabels . showActionText
280- : mappingTableLabels . hideActionText ;
281- } ) ;
282- queryAll ( 'span' , showHideColButton )
283- . filter ( function ( span ) {
284- return ! span . classList . contains ( 'action' ) ;
285- } )
286- . forEach ( function ( span ) {
287- span . parentNode . removeChild ( span ) ;
288- } ) ;
289- showHideCols . appendChild ( showHideColButton ) ;
290- } ) ( i )
291- }
292-
293- tableInfo . tableContainer . insertBefore (
294- showHideCols ,
295- tableInfo . tableContainer . firstChild
296- ) ;
297198 } ) ;
298199
299200 // if page URL links to frag id, reset location to frag id once details/summary view is set
@@ -318,3 +219,53 @@ function mappingTables() {
318219 }
319220 } ) ;
320221}
222+
223+ document . addEventListener ( "DOMContentLoaded" , ( ) => {
224+
225+ document . querySelectorAll ( 'button.switch-view' ) . forEach ( function ( b ) {
226+ b . addEventListener ( 'click' , function ( ) {
227+ tableContainer = b . parentElement . querySelector ( '.table-container' ) ;
228+ table = tableContainer . querySelector ( 'table' ) ;
229+ detailsContainer = b . parentElement . querySelector ( '.details' ) ;
230+
231+ if ( detailsContainer . style . display !== 'none' ) {
232+ viewAsSingleTable ( tableContainer , detailsContainer ) ;
233+ // toggle the viewSwitch label from view-as-single-table to view-by-X
234+ b . innerHTML =
235+ mappingTableLabels . viewByLabels [ table . id ] ;
236+ } else {
237+ viewAsDetails ( tableContainer , detailsContainer ) ;
238+ // toggle the viewSwitch label from view-by-X to view-as-single-table.
239+ b . innerHTML = mappingTableLabels . viewByTable ;
240+ }
241+ } ) ;
242+ } ) ;
243+
244+ var expandCollapseDetails = function ( detCont , action ) {
245+ queryAll ( 'details' , detCont ) . forEach ( function ( details ) {
246+ details . open = action !== 'collapse'
247+ } ) ;
248+ } ;
249+
250+ document . querySelectorAll ( 'button.expand' ) . forEach ( function ( b ) {
251+ b . addEventListener ( 'click' , function ( ) {
252+ detailsContainer = b . parentElement . querySelector ( '.details' ) ;
253+ expandCollapseDetails ( detailsContainer , 'expand' ) ;
254+ b . disabled = true ;
255+ b . parentElement
256+ . querySelector ( 'button.collapse' )
257+ . removeAttribute ( 'disabled' ) ;
258+ } ) ;
259+ } ) ;
260+
261+ document . querySelectorAll ( 'button.collapse' ) . forEach ( function ( b ) {
262+ b . addEventListener ( 'click' , function ( ) {
263+ detailsContainer = b . parentElement . querySelector ( '.details' ) ;
264+ expandCollapseDetails ( detailsContainer , 'collapse' ) ;
265+ b . disabled = true ;
266+ b . parentElement
267+ . querySelector ( 'button.expand' )
268+ . removeAttribute ( 'disabled' ) ;
269+ } ) ;
270+ } ) ;
271+ } ) ;
0 commit comments