@@ -27,26 +27,13 @@ function toggleLoading(caller) {
2727 element . classList . toggle ( "is-loading" ) ;
2828}
2929
30- function toggleChartLoading ( id ) {
31- const progress = document . getElementById ( `${ id } -loading` ) . parentElement ;
32- const chart = document . getElementById ( id ) . parentElement ;
33- progress . hidden = ! progress . hidden ;
34- chart . hidden = ! chart . hidden ;
35- }
36-
3730function resetForm ( page ) {
3831 window . location = page ;
3932}
4033
4134function setTheme ( ) {
42- const darkCss = document . getElementById ( "dark-theme" ) ;
43- const lightCss = document . getElementById ( "light-theme" ) ;
4435 const theme = getCookie ( "weatherdan_theme" ) ;
45-
46- if ( darkCss !== null && lightCss !== null ) {
47- darkCss . disabled = theme == "dark" ;
48- lightCss . disabled = theme == "light" ;
49- }
36+ document . documentElement . setAttribute ( "data-theme" , theme === "dark" ? "villain" : "hero" ) ;
5037}
5138
5239function toggleTheme ( ) {
@@ -57,7 +44,9 @@ function toggleTheme() {
5744 setTheme ( ) ;
5845}
5946
60- ready ( setTheme ( ) ) ;
47+ ready ( ( ) => {
48+ setTheme ( ) ;
49+ } ) ;
6150
6251async function submitRequest ( endpoint , method , body = { } ) {
6352 try {
@@ -80,28 +69,37 @@ async function submitRequest(endpoint, method, body = {}) {
8069 }
8170}
8271
83- async function refreshData ( endpoint ) {
84- const response = await submitRequest ( endpoint , "PUT" ) ;
85- if ( response !== null && response . status != 208 )
86- window . location . reload ( ) ;
72+ function getBulmaColour ( name ) {
73+ const tempElement = document . createElement ( "div" ) ;
74+ tempElement . className = `has-background-${ name } ` ;
75+ document . body . appendChild ( tempElement ) ;
76+ const computedStyle = getComputedStyle ( tempElement ) ;
77+ const color = computedStyle . backgroundColor ;
78+ document . body . removeChild ( tempElement ) ;
79+ return color ;
8780}
8881
89- const backgroundColours = [
90- "rgba(65,105,225,0.1)" ,
91- "rgba(255,65,105,0.1)" ,
92- "rgba(105,255,65,0.1)" ,
93- ] ;
82+ function setAlpha ( rgb , alpha ) {
83+ const rgbValues = rgb . match ( / \d + / g) . map ( Number ) ;
84+ return `rgba(${ rgbValues [ 0 ] } , ${ rgbValues [ 1 ] } , ${ rgbValues [ 2 ] } , ${ alpha } )` ;
85+ }
9486
95- const borderColours = [
96- "rgba(65,105,225,1)" ,
97- "rgba(255,65,105,1)" ,
98- "rgba(105,255,65,1)" ,
99- ] ;
87+ function getColours ( alpha ) {
88+ const colours = [ "info" , "danger" , "success" ] ;
89+ return colours . map ( colour => setAlpha ( getBulmaColour ( colour ) , alpha ) ) ;
90+ }
91+
92+ function toggleChartLoading ( id ) {
93+ const progress = document . getElementById ( `${ id } -loading` ) . parentElement ;
94+ const chart = document . getElementById ( id ) . parentElement ;
95+ progress . hidden = ! progress . hidden ;
96+ chart . hidden = ! chart . hidden ;
97+ }
10098
10199function createDataset ( index , data , label , type , yAxisID = "y" ) {
102100 return {
103- backgroundColor : backgroundColours [ index ] ,
104- borderColor : borderColours [ index ] ,
101+ backgroundColor : getColours ( 0.5 ) [ index ] ,
102+ borderColor : getColours ( 1 ) [ index ] ,
105103 borderWidth : 2 ,
106104 borderSkipped : false ,
107105 data : data ,
@@ -124,7 +122,10 @@ function createGraph(elementId, labels, datasets, unit, unitLabel) {
124122 } ,
125123 plugins : {
126124 legend : {
127- display : datasets . length > 1
125+ display : datasets . length > 1 ,
126+ labels : {
127+ color : getBulmaColour ( "text" )
128+ }
128129 } ,
129130 tooltip : {
130131 callbacks : {
@@ -143,21 +144,42 @@ function createGraph(elementId, labels, datasets, unit, unitLabel) {
143144 title : {
144145 display : true ,
145146 text : "Date" ,
147+ color : getBulmaColour ( "text" )
146148 } ,
147149 position : "bottom" ,
150+ ticks : {
151+ color : getBulmaColour ( "text" )
152+ } ,
153+ grid : {
154+ color : getBulmaColour ( "text-50" )
155+ }
148156 } ,
149157 y : {
150158 title : {
151159 display : true ,
152- text : unitLabel
160+ text : unitLabel ,
161+ color : getBulmaColour ( "text" )
153162 } ,
154163 position : "left" ,
155164 beginAtZero : false ,
165+ ticks : {
166+ color : getBulmaColour ( "text" )
167+ } ,
168+ grid : {
169+ color : getBulmaColour ( "text-50" )
170+ }
156171 }
157172 }
158173 }
159174 }
160175 new Chart ( document . getElementById ( elementId ) , config ) ;
176+ toggleChartLoading ( elementId ) ;
177+ }
178+
179+ async function refreshData ( endpoint ) {
180+ const response = await submitRequest ( endpoint , "PUT" ) ;
181+ if ( response !== null && response . status != 208 )
182+ window . location . reload ( ) ;
161183}
162184
163185async function loadTotalStats ( timeframe , graphId , endpoint , unit , unitLabel , maxEntries ) {
0 commit comments