1515 * limitations under the License.
1616 */
1717
18- const configJSON = require ( '../../../lighthouse-core/config/default.json' ) ;
1918const _flatten = arr => [ ] . concat . apply ( [ ] , arr ) ;
2019
21- const aggregations = _flatten (
22- configJSON . aggregations . map ( aggregation => {
23- if ( aggregation . items . length > 1 ) {
24- return aggregation . items ;
25- }
26-
27- return {
28- name : aggregation . name ,
29- criteria : aggregation . items [ 0 ] . criteria ,
30- } ;
31- } )
32- ) ;
33-
3420document . addEventListener ( 'DOMContentLoaded' , _ => {
3521 const background = chrome . extension . getBackgroundPage ( ) ;
22+ const aggregations = background . getListOfAudits ( ) ;
23+
3624 const siteNameEl = window . document . querySelector ( 'header h2' ) ;
3725 const generateReportEl = document . getElementById ( 'generate-report' ) ;
3826 const subpageVisibleClass = 'subpage--visible' ;
@@ -71,15 +59,13 @@ document.addEventListener('DOMContentLoaded', _ => {
7159 statusDetailsMessageEl . textContent = details ;
7260 } ;
7361
74- const getAuditsOfName = name => {
75- let aggregation = aggregations . filter ( aggregation => aggregation . name === name ) ;
76-
77- return Object . keys ( aggregation [ 0 ] . criteria ) ;
78- } ;
79-
80- const createOptionItem = text => {
62+ const createOptionItem = ( text , isChecked ) => {
8163 const input = document . createElement ( 'input' ) ;
82- const attributes = [ [ 'type' , 'checkbox' ] , [ 'checked' , 'checked' ] , [ 'value' , text ] ] ;
64+ const attributes = [ [ 'type' , 'checkbox' ] , [ 'value' , text ] ] ;
65+ if ( isChecked ) {
66+ attributes . push ( [ 'checked' , 'checked' ] ) ;
67+ }
68+
8369 attributes . forEach ( attr => input . setAttribute . apply ( input , attr ) ) ;
8470
8571 const label = document . createElement ( 'label' ) ;
@@ -91,34 +77,41 @@ document.addEventListener('DOMContentLoaded', _ => {
9177 return listItem ;
9278 } ;
9379
94- const generateOptionsList = list => {
80+ const generateOptionsList = ( list , selectedAudits ) => {
9581 const frag = document . createDocumentFragment ( ) ;
9682
9783 aggregations . forEach ( aggregation => {
98- frag . appendChild ( createOptionItem ( aggregation . name ) ) ;
84+ frag . appendChild ( createOptionItem ( aggregation . name , selectedAudits [ aggregation . name ] ) ) ;
9985 } ) ;
10086
10187 list . appendChild ( frag ) ;
10288 } ;
10389
90+ const getAuditsFromCategory = audits => _flatten (
91+ Object . keys ( audits ) . filter ( audit => audits [ audit ] ) . map ( audit => {
92+ const auditsInCategory = aggregations . find ( aggregation => aggregation . name === audit ) . criteria ;
93+
94+ return Object . keys ( auditsInCategory ) ;
95+ } )
96+ ) ;
97+
10498 background . listenForStatus ( logstatus ) ;
105- generateOptionsList ( optionsList ) ;
99+ background . fetchAudits ( ) . then ( audits => {
100+ generateOptionsList ( optionsList , audits ) ;
101+ } ) ;
106102
107103 generateReportEl . addEventListener ( 'click' , ( ) => {
108104 startSpinner ( ) ;
109105 feedbackEl . textContent = '' ;
110106
111- const audits = _flatten (
112- Array . from ( optionsEl . querySelectorAll ( ':checked' ) )
113- . map ( input => getAuditsOfName ( input . value ) )
114- ) ;
115-
116- background . runAudits ( {
107+ background . fetchAudits ( )
108+ . then ( getAuditsFromCategory )
109+ . then ( audits => background . runAudits ( {
117110 flags : {
118111 mobile : true ,
119112 loadPage : true
120113 }
121- } , audits )
114+ } , audits ) )
122115 . then ( results => {
123116 background . createPageAndPopulate ( results ) ;
124117 } )
@@ -139,6 +132,10 @@ document.addEventListener('DOMContentLoaded', _ => {
139132 } ) ;
140133
141134 okButton . addEventListener ( 'click' , ( ) => {
135+ const checkedAudits = Array . from ( optionsEl . querySelectorAll ( ':checked' ) )
136+ . map ( input => input . value ) ;
137+ background . saveAudits ( checkedAudits ) ;
138+
142139 optionsEl . classList . remove ( subpageVisibleClass ) ;
143140 } ) ;
144141
0 commit comments