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 */
17
2
18
3
function hideElement ( element ) {
19
4
element . style . display = 'none' ;
@@ -29,7 +14,7 @@ function queryAll(selector, context) {
29
14
}
30
15
31
16
function getElementIndex ( el ) {
32
- var i = 0 ;
17
+ var i = 1 ;
33
18
while ( ( el = el . previousElementSibling ) ) {
34
19
i ++ ;
35
20
}
@@ -44,6 +29,7 @@ function viewAsSingleTable(mappingTableInfo) {
44
29
queryAll ( 'summary' , mappingTableInfo . detailsContainer ) . forEach ( function (
45
30
summary
46
31
) {
32
+ summary . dataset [ 'id' ] = summary . id ;
47
33
summary . removeAttribute ( 'id' ) ;
48
34
} ) ;
49
35
showElement ( mappingTableInfo . tableContainer ) ;
@@ -52,7 +38,8 @@ function viewAsSingleTable(mappingTableInfo) {
52
38
queryAll ( 'tbody tr' , mappingTableInfo . tableContainer ) . forEach ( function (
53
39
tr
54
40
) {
55
- tr . id = mappingTableInfo . ids [ getElementIndex ( tr ) ] ;
41
+ tr . id = tr . dataset [ 'id' ] ;
42
+ tr . removeAttribute ( 'data-id' ) ;
56
43
} ) ;
57
44
}
58
45
@@ -62,21 +49,16 @@ function viewAsDetails(mappingTableInfo) {
62
49
queryAll ( 'tbody tr' , mappingTableInfo . tableContainer ) . forEach ( function (
63
50
tr
64
51
) {
52
+ tr . dataset [ 'id' ] = tr . id ;
65
53
tr . removeAttribute ( 'id' ) ;
66
54
} ) ;
67
55
showElement ( mappingTableInfo . detailsContainer ) ;
68
56
// add relevant @id to summary
69
57
queryAll ( 'summary' , mappingTableInfo . detailsContainer ) . forEach ( function (
70
58
summary
71
59
) {
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' ) ;
80
62
} ) ;
81
63
}
82
64
@@ -103,22 +85,18 @@ function mappingTables() {
103
85
104
86
// create a container div to hold all the details element and insert after table
105
87
tableInfo . detailsContainer = document . createElement ( 'div' ) ;
106
- tableInfo . detailsContainer . className = 'details removeOnSave ' ;
88
+ tableInfo . detailsContainer . className = 'details' ;
107
89
tableInfo . id = tableInfo . table . id + '-details' ;
108
90
tableInfo . tableContainer . insertAdjacentElement (
109
91
'afterend' ,
110
92
tableInfo . detailsContainer
111
93
) ;
112
94
113
- // array to store @id attributes for rows and summaries.
114
- tableInfo . ids = [ ] ;
115
-
116
95
// add switch to view as single table or details/summary
117
96
var viewSwitch = document . createElement ( 'button' ) ;
118
97
viewSwitch . className = 'switch-view removeOnSave' ;
119
98
viewSwitch . innerHTML = mappingTableLabels . viewByTable ;
120
99
viewSwitch . addEventListener ( 'click' , function ( ) {
121
- // array to store summary/tr @ids
122
100
// if current view is details/summary
123
101
if ( tableInfo . detailsContainer . style . display !== 'none' ) {
124
102
viewAsSingleTable ( tableInfo ) ;
@@ -144,13 +122,12 @@ function mappingTables() {
144
122
// remove first column header from array
145
123
colHeaders . shift ( ) ;
146
124
// for each row in the table, create details/summary..
125
+
147
126
queryAll ( 'tbody tr' , tableInfo . table ) . forEach ( function ( row ) {
148
127
var caption = row . querySelector ( 'th' ) . innerHTML ;
149
128
var summary = caption . replace ( / < a [ ^ > ] + > | < \/ a > / g, '' ) ;
150
129
// 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 ;
154
131
// remove the tr's @id since same id will be used in the relevant summary element
155
132
row . removeAttribute ( 'id' ) ;
156
133
// store the row's cells in array rowCells
@@ -175,7 +152,7 @@ function mappingTables() {
175
152
176
153
// create content for each <details> element; add row header's content to summary
177
154
var details = document . createElement ( 'details' ) ;
178
- details . className = 'map removeOnSave ' ;
155
+ details . className = 'map' ;
179
156
180
157
var detailsHTML = '<summary id="' + id + '">' + summary ;
181
158
@@ -276,6 +253,7 @@ function mappingTables() {
276
253
277
254
showHideColButton . addEventListener ( 'click' , function ( ) {
278
255
var index = getElementIndex ( showHideColButton ) ;
256
+ console . log ( "index?" , index ) ;
279
257
var wasHidden = showHideColButton . className === 'hide-col' ;
280
258
281
259
queryAll (
@@ -340,71 +318,3 @@ function mappingTables() {
340
318
}
341
319
} ) ;
342
320
}
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