-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patharea-background.js
138 lines (117 loc) · 3.77 KB
/
area-background.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
function setupEditor() {
const HuiViewEditor = customElements.get('hui-view-editor')
const { hass } = document.querySelector('home-assistant')
const firstUpdated = HuiViewEditor.prototype.firstUpdated
HuiViewEditor.prototype.firstUpdated = function () {
firstUpdated?.apply(this)
const oldSchema = this._schema
this._schema = (localize) => {
const schema = oldSchema.call(this, localize)
if (schema.find((e) => e.name === 'area_background') === undefined)
schema.push({
name: 'area_background',
selector: {
select: {
mode: 'dropdown',
options: [
{ value: '', label: '' },
...Object.values(hass.areas).map((area) => ({
value: area.area_id,
label: area.name,
})),
],
},
},
})
return schema
}
const oldComputeLabel = this._computeLabel
this._computeLabel = (schema) => {
switch (schema.name) {
case 'area_background':
return 'Area Background'
default:
return oldComputeLabel(schema)
}
}
this.requestUpdate()
}
}
function setupStyles(layout) {
const id = 'area-background-styles'
if (layout.querySelector(`#${id}`)) return
const styleElement = document.createElement('style')
styleElement.id = id
styleElement.innerHTML = `
#area-background {
position: fixed;
inset: 0;
opacity: 0.4;
background-repeat: no-repeat;
background-position: center;
background-size: cover;
filter: blur(10px);
}
#view, hui-view {
background: none!important;
}
`
layout.prepend(styleElement)
}
function getBackgroundElement(layout) {
const id = 'area-background'
let backgroundElement = layout.querySelector(`#${id}`)
if (backgroundElement) return backgroundElement
backgroundElement = document.createElement('div')
backgroundElement.id = id
layout.prepend(backgroundElement)
return backgroundElement
}
function setBackground(element, url) {
element.style.display = 'block'
element.style.backgroundImage = `url('${url}')`
}
function unsetBackground(element) {
element.style.display = 'none'
}
function maybeSetBackground(hass) {
const lovelacePanel = document
.querySelector('home-assistant')
.shadowRoot.querySelector('home-assistant-main')
.shadowRoot.querySelector('ha-panel-lovelace')
// Current view is not a lovelace view
if (!lovelacePanel) return
const { lovelace } = lovelacePanel
const layout = lovelacePanel.shadowRoot.querySelector('hui-root').shadowRoot.firstElementChild
const currentView = window.location.pathname.split('/').pop()
const viewConfig = isNaN(currentView)
? lovelace.config.views.find((v) => v.path === currentView)
: lovelace.config.views[+currentView]
const areaBackground = viewConfig?.area_background
setupStyles(layout)
const backgroundElement = getBackgroundElement(layout)
if (areaBackground) {
setBackground(backgroundElement, hass.areas[areaBackground].picture)
} else {
unsetBackground(backgroundElement)
}
}
function start() {
const { hass } = document.querySelector('home-assistant')
maybeSetBackground(hass)
window.addEventListener('popstate', () => {
maybeSetBackground(hass)
})
const originalPushState = window.history.pushState
window.history.pushState = function () {
originalPushState.apply(this, arguments)
maybeSetBackground(hass)
}
const originalReplaceState = window.history.replaceState
window.history.replaceState = function () {
originalReplaceState.apply(this, arguments)
maybeSetBackground(hass)
}
}
customElements.whenDefined('hui-view').then(start)
customElements.whenDefined('hui-view-editor').then(setupEditor)