11import Mustache from 'mustache' ;
2+ import { buildRoute , readRoute , updateRoute } from './route.js' ;
23
34export const REV_LATEST = 'latest' ;
45
5- function assert ( condition , message ) {
6- if ( ! condition ) {
7- throw new Error ( message || "Assertion failed" ) ;
8- }
9- }
10-
116function domContentLoaded ( ) {
127 return new Promise ( resolve => document . addEventListener ( 'DOMContentLoaded' , resolve ) ) ;
138}
149export const DOM_READY = domContentLoaded ( ) ;
1510
16- export async function main ( load , display , opts ) {
17- // Immediately listen to DOM event
18-
11+ export async function main ( load , display ) {
1912 // Load initial data before DOM is available
2013 let data = await load ( ) ;
2114
2215 // Wait for DOM to be ready before displaying
2316 await DOM_READY ;
2417 await display ( data ) ;
18+ monitor_options ( ) ;
2519
2620 // Full workflow, loading then displaying data
2721 // used for following updates
2822 let full = async function ( ) {
2923 let data = await load ( ) ;
3024 await display ( data ) ;
25+ monitor_options ( ) ;
3126 } ;
32- monitor_options ( opts , full ) ;
27+
28+ // React to url changes
3329 window . onhashchange = full ;
3430}
3531
36-
3732// Coverage retrieval.
3833
3934const COVERAGE_BACKEND_HOST = process . env . BACKEND_URL ;
@@ -64,8 +59,9 @@ function cache_set(cache, key, value) {
6459}
6560
6661let path_coverage_cache = { } ;
67- export async function get_path_coverage ( path , changeset ) {
68- let data = cache_get ( path_coverage_cache , `${ changeset } _${ path } ` ) ;
62+ export async function get_path_coverage ( path , changeset , platform , suite ) {
63+ let cache_key = `${ changeset } _${ path } _${ platform } _${ suite } ` ;
64+ let data = cache_get ( path_coverage_cache , cache_key ) ;
6965 if ( data ) {
7066 return data ;
7167 }
@@ -74,33 +70,47 @@ export async function get_path_coverage(path, changeset) {
7470 if ( changeset && changeset !== REV_LATEST ) {
7571 params += `&changeset=${ changeset } ` ;
7672 }
73+ if ( platform && platform !== 'all' ) {
74+ params += `&platform=${ platform } ` ;
75+ }
76+ if ( suite && suite !== 'all' ) {
77+ params += `&suite=${ suite } ` ;
78+ }
7779 let response = await fetch ( `${ COVERAGE_BACKEND_HOST } /v2/path?${ params } ` ) . catch ( alert ) ;
7880 if ( response . status !== 200 ) {
7981 throw new Error ( response . status + ' - ' + response . statusText ) ;
8082 }
8183 data = await response . json ( ) ;
8284
83- cache_set ( path_coverage_cache , ` ${ changeset } _ ${ path } ` , data ) ;
85+ cache_set ( path_coverage_cache , cache_key , data ) ;
8486
8587 return data ;
8688}
8789
8890let history_cache = { } ;
89- export async function get_history ( path ) {
91+ export async function get_history ( path , platform , suite ) {
9092 // Backend needs path without trailing /
9193 if ( path && path . endsWith ( '/' ) ) {
9294 path = path . substring ( 0 , path . length - 1 ) ;
9395 }
9496
95- let data = cache_get ( history_cache , path ) ;
97+ let cache_key = `${ path } _${ platform } _${ suite } ` ;
98+ let data = cache_get ( history_cache , cache_key ) ;
9699 if ( data ) {
97100 return data ;
98101 }
99102
100- let response = await fetch ( `${ COVERAGE_BACKEND_HOST } /v2/history?path=${ path } ` ) ;
103+ let params = `path=${ path } ` ;
104+ if ( platform && platform !== 'all' ) {
105+ params += `&platform=${ platform } ` ;
106+ }
107+ if ( suite && suite !== 'all' ) {
108+ params += `&suite=${ suite } ` ;
109+ }
110+ let response = await fetch ( `${ COVERAGE_BACKEND_HOST } /v2/history?${ params } ` ) ;
101111 data = await response . json ( ) ;
102112
103- cache_set ( history_cache , path , data ) ;
113+ cache_set ( history_cache , cache_key , data ) ;
104114
105115 // Check data has coverage values
106116 // These values are missing when going above 2 levels right now
@@ -131,21 +141,57 @@ export async function get_zero_coverage_data() {
131141}
132142
133143
144+ let filters_cache = { } ;
145+ export async function get_filters ( ) {
146+ let data = cache_get ( filters_cache , '' ) ;
147+ if ( data ) {
148+ return data ;
149+ }
150+
151+ let response = await fetch ( `${ COVERAGE_BACKEND_HOST } /v2/filters` ) ;
152+ data = await response . json ( ) ;
153+
154+ cache_set ( filters_cache , '' , data ) ;
155+
156+ return data ;
157+ }
158+
159+
134160// Option handling.
135161
136162function is_enabled ( opt ) {
137- let elem = document . getElementById ( opt ) ;
138- return elem . checked ;
163+ let route = readRoute ( ) ;
164+ return route [ opt ] === 'on' ;
139165}
140166
141- function monitor_options ( opts , callback ) {
142- for ( let opt of opts ) {
143- let elem = document . getElementById ( opt ) ;
144- elem . onchange = callback ;
167+ function monitor_options ( ) {
168+ // Monitor input & select changes
169+ let fields = document . querySelectorAll ( 'input, select' ) ;
170+ for ( let field of fields ) {
171+ if ( field . type == 'text' ) {
172+ // React on enter
173+ field . onkeydown = async ( evt ) => {
174+ if ( evt . keyCode === 13 ) {
175+ let params = { } ;
176+ params [ evt . target . name ] = evt . target . value ;
177+ updateRoute ( params ) ;
178+ }
179+ }
180+ } else {
181+ // React on change
182+ field . onchange = async ( evt ) => {
183+ let value = evt . target . value ;
184+ if ( evt . target . type == 'checkbox' ) {
185+ value = evt . target . checked ? 'on' : 'off' ;
186+ }
187+ let params = { } ;
188+ params [ evt . target . name ] = value ;
189+ updateRoute ( params ) ;
190+ }
191+ }
145192 }
146193}
147194
148-
149195// hgmo.
150196
151197export async function get_source ( file ) {
@@ -267,14 +313,14 @@ export function build_navbar(path, revision) {
267313 let links = [
268314 {
269315 'name' : 'mozilla-central' ,
270- 'path ' : '' ,
316+ 'route ' : buildRoute ( { path : '' , revision } )
271317 }
272318 ] ;
273319 return links . concat ( path . split ( '/' ) . map ( file => {
274320 base += ( base ? '/' : '' ) + file ;
275321 return {
276322 'name' : file ,
277- 'path ' : base ,
323+ 'route ' : buildRoute ( { path : base , revision } )
278324 } ;
279325 } ) ) ;
280326}
0 commit comments