Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions public/images/image-broken.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
102 changes: 55 additions & 47 deletions src/panels/lovelace/components/hui-image.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import '@polymer/paper-toggle-button/paper-toggle-button.js';

import { STATES_OFF } from '../../../common/const.js';
import LocalizeMixin from '../../../mixins/localize-mixin.js';
import isValidObject from '../common/is-valid-object';

const UPDATE_INTERVAL = 10000;
const DEFAULT_FILTER = 'grayscale(100%)';

/*
* @appliesMixin LocalizeMixin
Expand All @@ -15,35 +15,33 @@ class HuiImage extends LocalizeMixin(PolymerElement) {
static get template() {
return html`
<style>
.state-off {
filter: grayscale(100%);
}

img {
display: block;
height: auto;
transition: filter .2s linear;
width: 100%;
}

.error {
width: 100%;
height: auto;
text-align: center;
}


.hidden {
display: none;
}

#brokenImage {
background: grey url('/static/images/image-broken.svg') center/36px no-repeat;
}

</style>

<template is="dom-if" if="[[_imageSrc]]">
<img
src="[[_imageSrc]]"
on-error="_onImageError"
on-load="_onImageLoad"
class$="[[_imageClass]]" />
</template>
<template is="dom-if" if="[[_error]]">
<div class="error">[[localize('ui.card.camera.not_available')]]</div>
</template>

<img
id="image"
src="[[_imageSrc]]"
on-error="_onImageError"
on-load="_onImageLoad" />
<div id="brokenImage"></div>
`;
}

Expand All @@ -57,11 +55,8 @@ class HuiImage extends LocalizeMixin(PolymerElement) {
image: String,
stateImage: Object,
cameraImage: String,
_error: {
type: Boolean,
value: false
},
_imageClass: String,
filter: String,
stateFilter: Object,
_imageSrc: String
};
}
Expand Down Expand Up @@ -91,35 +86,53 @@ class HuiImage extends LocalizeMixin(PolymerElement) {
}

_onImageError() {
this.setProperties({
_imageSrc: null,
_error: true
});
this._imageSrc = null;
this.$.image.classList.add('hidden');
this.$.brokenImage.style.setProperty('height', `${this._lastImageHeight || '100'}px`);
this.$.brokenImage.classList.remove('hidden');
}

_onImageLoad() {
this._error = false;
this.$.image.classList.remove('hidden');
this.$.brokenImage.classList.add('hidden');
this._lastImageHeight = this.$.image.offsetHeight;
}

_hassChanged(hass) {
if (this.cameraImage || !this.entity) {
return;
}

const state = hass.states[this.entity];
const unavailable = !isValidObject(state, ['state']);
const stateObj = hass.states[this.entity];
const newState = !stateObj ? 'unavailable' : stateObj.state;

if (newState === this._currentState) return;
this._currentState = newState;

this._updateStateImage();
this._updateStateFilter(stateObj);
}

_updateStateImage() {
if (!this.stateImage) {
this._imageClass = unavailable || STATES_OFF.includes(state.state) ? 'state-off' : '';
this._imageFallback = true;
return;
}
const stateImg = this.stateImage[this._currentState];
this._imageSrc = stateImg || this.image;
this._imageFallback = !stateImg;
}

const stateImg = !unavailable ? this.stateImage[state.state] : this.stateImage.unavailable;
_updateStateFilter(stateObj) {
let filter;
if (!this.stateFilter) {
filter = this.filter;
} else {
filter = this.stateFilter[this._currentState] || this.filter;
}

this.setProperties({
_imageClass: !stateImg && (unavailable || STATES_OFF.includes(state.state)) ? 'state-off' : '',
_imageSrc: stateImg || this.image
});
const isOff = !stateObj || STATES_OFF.includes(stateObj.state);
this.$.image.style.filter = filter || (isOff && this._imageFallback && DEFAULT_FILTER) || '';
Copy link
Copy Markdown
Member

@balloob balloob Jul 9, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shit is getting so crazy, I feel like we need to add tests to make sure that all use cases are getting covered.

(out of scope for this PR)

}

_updateCameraImageSrc() {
Expand All @@ -128,17 +141,12 @@ class HuiImage extends LocalizeMixin(PolymerElement) {
entity_id: this.cameraImage,
}).then((resp) => {
if (resp.success) {
this.setProperties({
_imageSrc: `data:${resp.result.content_type};base64, ${resp.result.content}`,
_error: false
});
this._imageSrc = `data:${resp.result.content_type};base64, ${resp.result.content}`;
this._onImageLoad();
} else {
this.setProperties({
_imageSrc: null,
_error: true
});
this._onImageError();
}
});
}, () => this._onImageError());
}
}

Expand Down