Add JS support for device-local theme name (#358)

* Add JS support for device-local theme name

* Keep themes in hass object

* Make it actually work

* Move themes from home-assistant-main to home-assistant
This commit is contained in:
Andrey 2017-07-30 02:31:51 +09:00 committed by Paulus Schoutsen
parent 8787304ed4
commit b57c86a29c
3 changed files with 22 additions and 17 deletions

View File

@ -125,6 +125,7 @@ Polymer({
connected: true, connected: true,
states: null, states: null,
config: null, config: null,
themes: null,
dockedSidebar: false, dockedSidebar: false,
moreInfoEntityId: null, moreInfoEntityId: null,
callService: function (domain, service, serviceData) { callService: function (domain, service, serviceData) {
@ -186,11 +187,25 @@ Polymer({
unsubConfig = unsub; unsubConfig = unsub;
}); });
var unsubThemes;
this.hass.callApi('get', 'themes').then(function (themes) {
el.hass.themes = themes;
window.hassUtil.applyThemesOnElement(el, themes);
});
conn.subscribeEvents(function (event) {
el.hass.themes = event.data;
window.hassUtil.applyThemesOnElement(el, event.data);
}, 'themes_updated').then(function (unsub) {
unsubThemes = unsub;
});
this.unsubConnection = function () { this.unsubConnection = function () {
conn.removeEventListener('ready', reconnected); conn.removeEventListener('ready', reconnected);
conn.removeEventListener('disconnected', disconnected); conn.removeEventListener('disconnected', disconnected);
unsubEntities(); unsubEntities();
unsubConfig(); unsubConfig();
unsubThemes();
}; };
}, },

View File

@ -111,20 +111,6 @@
'hass-start-voice': 'handleStartVoice', 'hass-start-voice': 'handleStartVoice',
}, },
ready: function () {
if (!this.hass) return;
var _this = this;
this.hass.callApi('get', 'themes').then(
function (themes) {
window.hassUtil.applyThemesOnElement(_this, themes);
});
this.hass.connection.subscribeEvents(
function (event) {
window.hassUtil.applyThemesOnElement(_this, event.data);
},
'themes_updated');
},
_routeChanged: function () { _routeChanged: function () {
if (this.narrow) { if (this.narrow) {
this.$.drawer.closeDrawer(); this.$.drawer.closeDrawer();

View File

@ -492,15 +492,19 @@ window.hassUtil.computeLocationName = function (hass) {
return hass && hass.config.core.location_name; return hass && hass.config.core.location_name;
}; };
window.hassUtil.applyThemesOnElement = function (element, themes) { window.hassUtil.applyThemesOnElement = function (element, themes, localTheme) {
if (!element._themes) { if (!element._themes) {
element._themes = {}; element._themes = {};
} }
if (themes.default_theme === 'default') { var themeName = themes.default_theme;
if (localTheme === 'default' || (localTheme && themes.themes[localTheme])) {
themeName = localTheme;
}
if (themeName === 'default') {
element.updateStyles(element._themes); element.updateStyles(element._themes);
return; return;
} }
var theme = themes.themes[themes.default_theme]; var theme = themes.themes[themeName];
var styles = Object.assign({}, element._themes); var styles = Object.assign({}, element._themes);
Object.keys(theme).forEach(function (key) { Object.keys(theme).forEach(function (key) {
var prefixedKey = '--' + key; var prefixedKey = '--' + key;