Skip to content

Commit ece3037

Browse files
committed
better background color detection for themes that takes into account inheritance.
1 parent 5556ef1 commit ece3037

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

octoprint_tasmota/static/js/tasmota.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,34 @@ $(function() {
187187
}
188188
};
189189

190+
self.getDefaultBackground = function() {
191+
// have to add to the document in order to use getComputedStyle
192+
var div = document.createElement("div");
193+
document.head.appendChild(div);
194+
var bg = window.getComputedStyle(div).backgroundColor;
195+
document.head.removeChild(div);
196+
return bg;
197+
};
198+
199+
self.getInheritedBackgroundColor = function(el) {
200+
// get default style for current browser
201+
if (!self.defaultStyle) {
202+
self.defaultStyle = self.getDefaultBackground(); // typically "rgba(0, 0, 0, 0)"
203+
}
204+
205+
// get computed color for el
206+
var backgroundColor = window.getComputedStyle(el).backgroundColor;
207+
208+
// if we got a real value, return it
209+
if (backgroundColor !== self.defaultStyle) return backgroundColor;
210+
211+
// if we've reached the top parent el without getting an explicit color, return default
212+
if (!el.parentElement) return self.defaultStyle;
213+
214+
// otherwise, recurse and try again on parent element
215+
return self.getInheritedBackgroundColor(el.parentElement);
216+
};
217+
190218
self.plotEnergyData = function(){
191219
$.ajax({
192220
url: API_BASEURL + "plugin/tasmota",
@@ -232,7 +260,7 @@ $(function() {
232260
}
233261
}
234262

235-
var background_color = ($('.tab-content').css('background-color') == 'rgba(0, 0, 0, 0)') ? '#FFFFFF' : $('.tab-content').css('background-color');
263+
var background_color = (self.getInheritedBackgroundColor(document.getElementById('tab_plugin_tasmota')) == 'rgba(0, 0, 0, 0)') ? '#FFFFFF' : self.getInheritedBackgroundColor(document.getElementById('tab_plugin_tasmota'));
236264
var foreground_color = ($('.tab-content').css('color') == 'rgba(0, 0, 0, 0)') ? '#FFFFFF' : $('#tabs_content').css('color');
237265

238266
var layout = {

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
plugin_name = "OctoPrint-Tasmota"
1515

1616
# The plugin's version. Can be overwritten within OctoPrint's internal data via __plugin_version__ in the plugin module
17-
plugin_version = "1.1.3rc5"
17+
plugin_version = "1.1.3rc6"
1818

1919
# The plugin's description. Can be overwritten within OctoPrint's internal data via __plugin_description__ in the plugin
2020
# module

0 commit comments

Comments
 (0)