-
Notifications
You must be signed in to change notification settings - Fork 382
/
Copy pathamp-validation-detail-toggle.js
85 lines (71 loc) · 2.54 KB
/
amp-validation-detail-toggle.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/**
* WordPress dependencies
*/
import domReady from '@wordpress/dom-ready';
import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import setValidationErrorRowsClasses from './set-validation-error-rows-classes';
const OPEN_CLASS = 'is-open';
/**
* Adds detail toggle buttons to the header and footer rows of the validation error "details" column.
* The buttons are added via JS because there's no easy way to append them to the heading of a sortable
* table column via backend code.
*
* @param {string} containerSelector Selector for elements that will have the button added.
* @param {string} ariaLabel Screen reader label for the button.
* @return {Array} Array of added buttons.
*/
function addToggleButtons( containerSelector, ariaLabel ) {
const addButton = ( container ) => {
const button = document.createElement( 'button' );
button.setAttribute( 'aria-label', ariaLabel );
button.setAttribute( 'type', 'button' );
button.setAttribute( 'class', 'error-details-toggle' );
container.appendChild( button );
return button;
};
return [ ...document.querySelectorAll( containerSelector ) ].map( ( container ) => addButton( container ) );
}
function addToggleAllListener( { btn, toggleAllButtonSelector = null, targetDetailsSelector } ) {
let open = false;
const targetDetails = [ ...document.querySelectorAll( targetDetailsSelector ) ];
let toggleAllButtons = [];
if ( toggleAllButtonSelector ) {
toggleAllButtons = [ ...document.querySelectorAll( toggleAllButtonSelector ) ];
}
const onButtonClick = () => {
open = ! open;
toggleAllButtons.forEach( ( toggleAllButton ) => {
toggleAllButton.classList.toggle( OPEN_CLASS );
} );
targetDetails.forEach( ( detail ) => {
if ( open ) {
detail.setAttribute( 'open', true );
} else {
detail.removeAttribute( 'open' );
}
} );
};
btn.addEventListener( 'click', onButtonClick );
}
domReady( () => {
addToggleButtons( 'th.column-details.manage-column', __( 'Toggle all details', 'amp' ) )
.forEach( ( btn ) => {
addToggleAllListener( {
btn,
toggleAllButtonSelector: '.column-details button.error-details-toggle',
targetDetailsSelector: '.column-details details',
} );
} );
addToggleButtons( 'th.manage-column.column-sources_with_invalid_output', __( 'Toggle all sources', 'amp' ) )
.forEach( ( btn ) => {
addToggleAllListener( {
btn,
toggleAllButtonSelector: '.column-sources_with_invalid_output button.error-details-toggle',
targetDetailsSelector: 'details.source',
} );
} );
setValidationErrorRowsClasses();
} );