1- // check for require() and respec context
2- /* global require , mappingTables */
3-
4- if ( typeof require !== 'undefined' ) {
5- require ( [ 'core/pubsubhub' ] , function ( respecEvents ) {
6- mapTables ( respecEvents ) ;
7- } ) ;
8- } else {
9- if ( document . readyState !== 'loading' ) {
10- mapTables ( false ) ;
11- } else {
12- document . addEventListener ( 'DOMContentLoaded' , function ( ) {
13- mapTables ( false ) ;
14- } ) ;
15- }
16- }
1+ /* global mappingTables */
172
183function hideElement ( element ) {
194 element . style . display = 'none' ;
@@ -29,7 +14,7 @@ function queryAll(selector, context) {
2914}
3015
3116function getElementIndex ( el ) {
32- var i = 0 ;
17+ var i = 1 ;
3318 while ( ( el = el . previousElementSibling ) ) {
3419 i ++ ;
3520 }
@@ -44,6 +29,7 @@ function viewAsSingleTable(mappingTableInfo) {
4429 queryAll ( 'summary' , mappingTableInfo . detailsContainer ) . forEach ( function (
4530 summary
4631 ) {
32+ summary . dataset [ 'id' ] = summary . id ;
4733 summary . removeAttribute ( 'id' ) ;
4834 } ) ;
4935 showElement ( mappingTableInfo . tableContainer ) ;
@@ -52,7 +38,8 @@ function viewAsSingleTable(mappingTableInfo) {
5238 queryAll ( 'tbody tr' , mappingTableInfo . tableContainer ) . forEach ( function (
5339 tr
5440 ) {
55- tr . id = mappingTableInfo . ids [ getElementIndex ( tr ) ] ;
41+ tr . id = tr . dataset [ 'id' ] ;
42+ tr . removeAttribute ( 'data-id' ) ;
5643 } ) ;
5744}
5845
@@ -62,21 +49,16 @@ function viewAsDetails(mappingTableInfo) {
6249 queryAll ( 'tbody tr' , mappingTableInfo . tableContainer ) . forEach ( function (
6350 tr
6451 ) {
52+ tr . dataset [ 'id' ] = tr . id ;
6553 tr . removeAttribute ( 'id' ) ;
6654 } ) ;
6755 showElement ( mappingTableInfo . detailsContainer ) ;
6856 // add relevant @id to summary
6957 queryAll ( 'summary' , mappingTableInfo . detailsContainer ) . forEach ( function (
7058 summary
7159 ) {
72- const details = mappingTableInfo . detailsContainer . querySelector (
73- 'details'
74- ) ;
75- summary . id =
76- mappingTableInfo . ids [
77- // TODO: check that this works
78- getElementIndex ( details ) - getElementIndex ( summary . parentNode )
79- ] ;
60+ summary . id = summary . dataset [ 'id' ] ;
61+ summary . removeAttribute ( 'data-id' ) ;
8062 } ) ;
8163}
8264
@@ -103,22 +85,18 @@ function mappingTables() {
10385
10486 // create a container div to hold all the details element and insert after table
10587 tableInfo . detailsContainer = document . createElement ( 'div' ) ;
106- tableInfo . detailsContainer . className = 'details removeOnSave ' ;
88+ tableInfo . detailsContainer . className = 'details' ;
10789 tableInfo . id = tableInfo . table . id + '-details' ;
10890 tableInfo . tableContainer . insertAdjacentElement (
10991 'afterend' ,
11092 tableInfo . detailsContainer
11193 ) ;
11294
113- // array to store @id attributes for rows and summaries.
114- tableInfo . ids = [ ] ;
115-
11695 // add switch to view as single table or details/summary
11796 var viewSwitch = document . createElement ( 'button' ) ;
11897 viewSwitch . className = 'switch-view removeOnSave' ;
11998 viewSwitch . innerHTML = mappingTableLabels . viewByTable ;
12099 viewSwitch . addEventListener ( 'click' , function ( ) {
121- // array to store summary/tr @ids
122100 // if current view is details/summary
123101 if ( tableInfo . detailsContainer . style . display !== 'none' ) {
124102 viewAsSingleTable ( tableInfo ) ;
@@ -144,13 +122,12 @@ function mappingTables() {
144122 // remove first column header from array
145123 colHeaders . shift ( ) ;
146124 // for each row in the table, create details/summary..
125+
147126 queryAll ( 'tbody tr' , tableInfo . table ) . forEach ( function ( row ) {
148127 var caption = row . querySelector ( 'th' ) . innerHTML ;
149128 var summary = caption . replace ( / < a [ ^ > ] + > | < \/ a > / g, '' ) ;
150129 // get the tr's @id
151- var id = row . id ;
152- // store the row's @id
153- tableInfo . ids . push ( id ) ;
130+ var id = row . dataset . id ;
154131 // remove the tr's @id since same id will be used in the relevant summary element
155132 row . removeAttribute ( 'id' ) ;
156133 // store the row's cells in array rowCells
@@ -175,7 +152,7 @@ function mappingTables() {
175152
176153 // create content for each <details> element; add row header's content to summary
177154 var details = document . createElement ( 'details' ) ;
178- details . className = 'map removeOnSave ' ;
155+ details . className = 'map' ;
179156
180157 var detailsHTML = '<summary id="' + id + '">' + summary ;
181158
@@ -276,6 +253,7 @@ function mappingTables() {
276253
277254 showHideColButton . addEventListener ( 'click' , function ( ) {
278255 var index = getElementIndex ( showHideColButton ) ;
256+ console . log ( "index?" , index ) ;
279257 var wasHidden = showHideColButton . className === 'hide-col' ;
280258
281259 queryAll (
@@ -340,71 +318,3 @@ function mappingTables() {
340318 }
341319 } ) ;
342320}
343-
344- function mapTables ( respecEvents ) {
345- var mappingTableInfos = [ ] ;
346-
347- function viewAsSingleTable ( mappingTableInfo ) {
348- hideElement ( mappingTableInfo . detailsContainer ) ;
349- // add <summary> @id to ids array and remove @id from summary
350- queryAll ( 'summary' , mappingTableInfo . detailsContainer ) . forEach ( function (
351- summary
352- ) {
353- summary . removeAttribute ( 'id' ) ;
354- } ) ;
355- showElement ( mappingTableInfo . tableContainer ) ;
356-
357- // add relevant @id to tr
358- queryAll ( 'tbody tr' , mappingTableInfo . tableContainer ) . forEach ( function (
359- tr
360- ) {
361- tr . id = mappingTableInfo . ids [ getElementIndex ( tr ) ] ;
362- } ) ;
363- }
364-
365- function viewAsDetails ( mappingTableInfo ) {
366- hideElement ( mappingTableInfo . tableContainer ) ;
367- // add tr @id to ids array and remove @id from tr
368- queryAll ( 'tbody tr' , mappingTableInfo . tableContainer ) . forEach ( function (
369- tr
370- ) {
371- tr . removeAttribute ( 'id' ) ;
372- } ) ;
373- showElement ( mappingTableInfo . detailsContainer ) ;
374- // add relevant @id to summary
375- queryAll ( 'summary' , mappingTableInfo . detailsContainer ) . forEach ( function (
376- summary
377- ) {
378- const details = mappingTableInfo . detailsContainer . querySelector (
379- 'details'
380- ) ;
381- summary . id =
382- mappingTableInfo . ids [
383- // TODO: check that this works
384- getElementIndex ( details ) - getElementIndex ( summary . parentNode )
385- ] ;
386- } ) ;
387- }
388-
389- if ( respecEvents ) {
390- // Fix the scroll-to-fragID:
391- // - if running with ReSpec, do not invoke the mapping tables script until
392- // ReSpec executes its own scroll-to-fragID.
393- // - if running on a published document (no ReSpec), invoke the mapping tables
394- // script on document ready.
395- respecEvents . sub ( 'start' , function ( details ) {
396- if ( details === 'core/location-hash' ) {
397- mappingTables ( ) ;
398- }
399- } ) ;
400- // Subscribe to ReSpec "save" message to set the mapping tables to
401- // view-as-single-table state.
402- respecEvents . sub ( 'save' , function ( details ) {
403- mappingTableInfos . forEach ( function ( item ) {
404- viewAsSingleTable ( item ) ;
405- } ) ;
406- } ) ;
407- } else {
408- mappingTables ( ) ;
409- }
410- }
0 commit comments