1414 * See the License for the specific language governing permissions and
1515 * limitations under the License.
1616 */
17+ 'use strict' ;
1718
1819const _flatten = arr => [ ] . concat . apply ( [ ] , arr ) ;
1920
2021document . addEventListener ( 'DOMContentLoaded' , _ => {
2122 const background = chrome . extension . getBackgroundPage ( ) ;
22- const aggregations = background . getListOfAudits ( ) ;
23+ const defaultAggregations = background . getDefaultAggregations ( ) ;
2324
2425 const siteNameEl = window . document . querySelector ( 'header h2' ) ;
2526 const generateReportEl = document . getElementById ( 'generate-report' ) ;
@@ -38,7 +39,7 @@ document.addEventListener('DOMContentLoaded', _ => {
3839
3940 let spinnerAnimation ;
4041
41- const startSpinner = _ => {
42+ function startSpinner ( ) {
4243 statusEl . classList . add ( subpageVisibleClass ) ;
4344 spinnerAnimation = spinnerEl . animate ( [
4445 { transform : 'rotate(0deg)' } ,
@@ -47,72 +48,88 @@ document.addEventListener('DOMContentLoaded', _ => {
4748 duration : 1000 ,
4849 iterations : Infinity
4950 } ) ;
50- } ;
51+ }
5152
52- const stopSpinner = _ => {
53+ function stopSpinner ( ) {
5354 spinnerAnimation . cancel ( ) ;
5455 statusEl . classList . remove ( subpageVisibleClass ) ;
55- } ;
56+ }
5657
57- const logstatus = ( [ , message , details ] ) => {
58+ function logstatus ( [ , message , details ] ) {
5859 statusMessageEl . textContent = message ;
5960 statusDetailsMessageEl . textContent = details ;
60- } ;
61+ }
6162
62- const createOptionItem = ( text , isChecked ) => {
63+ function createOptionItem ( text , isChecked ) {
6364 const input = document . createElement ( 'input' ) ;
64- const attributes = [ [ 'type' , 'checkbox' ] , [ 'value' , text ] ] ;
65+ input . setAttribute ( 'type' , 'checkbox' ) ;
66+ input . setAttribute ( 'value' , text ) ;
6567 if ( isChecked ) {
66- attributes . push ( [ 'checked' , 'checked' ] ) ;
68+ input . setAttribute ( 'checked' , 'checked' ) ;
6769 }
6870
69- attributes . forEach ( attr => input . setAttribute . apply ( input , attr ) ) ;
70-
7171 const label = document . createElement ( 'label' ) ;
7272 label . appendChild ( input ) ;
7373 label . appendChild ( document . createTextNode ( text ) ) ;
7474 const listItem = document . createElement ( 'li' ) ;
7575 listItem . appendChild ( label ) ;
7676
7777 return listItem ;
78- } ;
79-
80- const generateOptionsList = ( list , selectedAudits ) => {
78+ }
79+
80+ /**
81+ * Generates a document fragment containing a list of checkboxes and labels
82+ * for the aggregation categories.
83+ * @param {!Object<boolean> } selectedAggregations
84+ * @return {!DocumentFragment }
85+ */
86+ function generateOptionsList ( list , selectedAggregations ) {
8187 const frag = document . createDocumentFragment ( ) ;
8288
83- aggregations . forEach ( aggregation => {
84- frag . appendChild ( createOptionItem ( aggregation . name , selectedAudits [ aggregation . name ] ) ) ;
89+ defaultAggregations . forEach ( aggregation => {
90+ const isChecked = selectedAggregations [ aggregation . name ] ;
91+ frag . appendChild ( createOptionItem ( aggregation . name , isChecked ) ) ;
8592 } ) ;
8693
87- list . appendChild ( frag ) ;
88- } ;
94+ return frag ;
95+ }
96+
97+ /**
98+ * Returns an array of names of audits from the selected aggregation
99+ * categories.
100+ * @param {!Object<boolean> } selectedAggregations
101+ * @return {!Array<string> }
102+ */
103+ function getAuditsFromSelected ( selectedAggregations ) {
104+ const auditLists = defaultAggregations . filter ( aggregation => {
105+ return selectedAggregations [ aggregation . name ] ;
106+ } ) . map ( selectedAggregation => {
107+ return selectedAggregation . audits ;
108+ } ) ;
89109
90- const getAuditsFromCategory = audits => _flatten (
91- Object . keys ( audits ) . filter ( audit => audits [ audit ] ) . map ( audit => {
92- const auditsInCategory = aggregations
93- . find ( aggregation => aggregation . name === audit ) . criteria ;
94-
95- return Object . keys ( auditsInCategory ) ;
96- } )
97- ) ;
110+ return _flatten ( auditLists ) ;
111+ }
98112
99113 background . listenForStatus ( logstatus ) ;
100- background . fetchAudits ( ) . then ( audits => {
101- generateOptionsList ( optionsList , audits ) ;
114+ background . loadSelectedAggregations ( ) . then ( aggregations => {
115+ const frag = generateOptionsList ( optionsList , aggregations ) ;
116+ optionsList . appendChild ( frag ) ;
102117 } ) ;
103118
104119 generateReportEl . addEventListener ( 'click' , ( ) => {
105120 startSpinner ( ) ;
106121 feedbackEl . textContent = '' ;
107122
108- background . fetchAudits ( )
109- . then ( getAuditsFromCategory )
110- . then ( audits => background . runAudits ( {
111- flags : {
112- mobile : true ,
113- loadPage : true
114- }
115- } , audits ) )
123+ background . loadSelectedAggregations ( )
124+ . then ( getAuditsFromSelected )
125+ . then ( selectedAudits => {
126+ return background . runLighthouse ( {
127+ flags : {
128+ mobile : true ,
129+ loadPage : true
130+ }
131+ } , selectedAudits ) ;
132+ } )
116133 . then ( results => {
117134 background . createPageAndPopulate ( results ) ;
118135 } )
@@ -133,9 +150,10 @@ document.addEventListener('DOMContentLoaded', _ => {
133150 } ) ;
134151
135152 okButton . addEventListener ( 'click' , ( ) => {
136- const checkedAudits = Array . from ( optionsEl . querySelectorAll ( ':checked' ) )
153+ // Save selected aggregation categories on options page close.
154+ const checkedAggregations = Array . from ( optionsEl . querySelectorAll ( ':checked' ) )
137155 . map ( input => input . value ) ;
138- background . saveAudits ( checkedAudits ) ;
156+ background . saveSelectedAggregations ( checkedAggregations ) ;
139157
140158 optionsEl . classList . remove ( subpageVisibleClass ) ;
141159 } ) ;
0 commit comments