Skip to content

Commit

Permalink
Card scaling
Browse files Browse the repository at this point in the history
Fixes: #23
  • Loading branch information
MrBartusek committed May 27, 2021
1 parent 1b057f8 commit ad7528e
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 9 deletions.
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
},
"dependencies": {
"custom-card-helpers": "^1.6.4",
"lit-element": "^2.3.1"
"lit-element": "^2.3.1",
"resize-observer-polyfill": "^1.5.1"
},
"devDependencies": {
"@babel/core": "^7.9.6",
Expand Down
31 changes: 31 additions & 0 deletions src/debounce.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// From: https://davidwalsh.name/javascript-debounce-function

// Returns a function, that, as long as it continues to be invoked, will not
// be triggered. The function will be called after it stops being called for
// N milliseconds. If `immediate` is passed, trigger the function on the
// leading edge, instead of the trailing.
// eslint-disable-next-line: ban-types
export const debounce = (func, wait, immediate = false) =>
{
let timeout;
return function (...args)
{
const context = this;
const later = () =>
{
timeout = null;
if (!immediate)
{
func.apply(context, args);
}
};
const callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow)
{
func.apply(context, args);
}
};
};

67 changes: 62 additions & 5 deletions src/meteoalarm-card.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { hasConfigOrEntityChanged, fireEvent } from 'custom-card-helpers';
import './editor'
import localize from './localize';
import styles from './styles';
import ResizeObserver from 'resize-observer-polyfill'
import { debounce } from './debounce'

import { MeteoAlarmIntegration } from './integrations/meteoalarm-integration';
import { MeteoFranceIntegration } from './integrations/meteofrance-integration';
Expand Down Expand Up @@ -117,6 +119,49 @@ class MeteoalarmCard extends LitElement
);
}

firstUpdated()
{
this.measureCard();
this.attachObserver();
}

async attachObserver()
{
if (!this._resizeObserver)
{
this.resizeObserver = new ResizeObserver(
debounce(() => this.measureCard(), 250, false)
);
}
const card = this.shadowRoot.querySelector('ha-card');
if (!card) return;
this.resizeObserver.observe(card);
}

measureCard()
{
if (!this.isConnected) return;
const card = this.shadowRoot.querySelector('ha-card');
if (!card) return;

if (card.offsetWidth < 375)
{
this.setAttribute('narrow', '');
}
else
{
this.removeAttribute('narrow');
}
if (card.offsetWidth < 200)
{
this.setAttribute('verynarrow', '');
}
else
{
this.removeAttribute('verynarrow');
}
}

keyToIntegration(key, entity = this.entity)
{
if(key == 'automatic')
Expand Down Expand Up @@ -150,7 +195,7 @@ class MeteoalarmCard extends LitElement
{
result = {
...result,
...this.integration.getResult(entity)
...this.integration.getResult(entity),
}

if(result.awarenessLevel == undefined || result.awarenessType == undefined)
Expand All @@ -160,17 +205,26 @@ class MeteoalarmCard extends LitElement

if(result.headline === undefined || this.overrideHeadline)
{
// If headline is not issued, generate default one
result.headline = this.generateHeadline(result.awarenessType, result.awarenessLevel)
}
result.headlineNarrow = this.generateHeadline(result.awarenessType, result.awarenessLevel, true)

}
return result
}

generateHeadline(awarenessType, awarenessLevel)
generateHeadline(awarenessType, awarenessLevel, narrow = false)
{
// If headline is not issued, generate default one
return localize(awarenessLevel.translationKey).replace('{0}', localize(awarenessType.translationKey))
if(narrow)
{
const awareness = localize(awarenessType.translationKey);
return awareness.charAt(0).toUpperCase() + awareness.slice(1)
}
else
{
return localize(awarenessLevel.translationKey).replace('{0}', localize(awarenessType.translationKey))
}
}

getBackgroundColor()
Expand Down Expand Up @@ -205,14 +259,17 @@ class MeteoalarmCard extends LitElement

renderStatus()
{
const { isWarningActive: isWarningActive, headline } = this.getAttributes(this.entity);
const { isWarningActive: isWarningActive, headline, headlineNarrow } = this.getAttributes(this.entity);

if(isWarningActive)
{
return html`
<div class="status">
${headline}
</div>
<div class="status-narrow">
${headlineNarrow}
</div>
`
}
else
Expand Down
28 changes: 25 additions & 3 deletions src/styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,29 +30,51 @@ export default css`
{
display: flex;
padding: 32px 28px;
justify-content: center;
}
.main-icon
.main-icon
{
--mdc-icon-size: 50px;
height: 50px;
flex: 0;
margin-right: 18px;
}
.status
.status, .status-narrow
{
flex: 1;
display: flex;
flex-direction: column;
font-size: 22px;
line-height: normal;
margin: auto;
margin-left: 18px;
text-align: center;
overflow: hidden;
text-overflow: ellipsis;
display: -webkit-box;
-webkit-line-clamp: 1;
-webkit-box-orient: vertical;
}
.status-narrow
{
display: none;
}
:host([narrow]) .status-narrow {
display: flex;
}
:host([narrow]) .status {
display: none;
}
:host([verynarrow]) .status-narrow {
display: none;
}
:host([verynarrow]) .status {
display: none;
}
`;

0 comments on commit ad7528e

Please sign in to comment.