|
import { |
|
LitElement, html |
|
} from 'https://unpkg.com/@polymer/lit-element@^0.5.2/lit-element.js?module'; |
|
|
|
class ResponsiveContainer extends LitElement { |
|
constructor() { |
|
super(); |
|
this._cards = []; |
|
this._columns = { |
|
s: 12, |
|
m: 6, |
|
l: 4, |
|
xl: 3 |
|
}; |
|
} |
|
|
|
static get properties() { |
|
return { |
|
hass: Object, |
|
config: Object, |
|
} |
|
} |
|
|
|
set hass(hass) { |
|
this._hass = hass; |
|
|
|
if(this._cards) { |
|
this._cards.filter(c => typeof c !== 'string').forEach(c => c.hass = hass); |
|
} |
|
} |
|
|
|
_render(config) { |
|
const itemTemplates = []; |
|
let columns = {}; |
|
|
|
for (const card of this._cards) { |
|
if (!!card.config && !!card.config.cols) { |
|
columns = this._calcColumns(this.config.columns, card.config.cols); |
|
} else if (!!card._config && !!card._config.cols) { |
|
columns = this._calcColumns(this.config.columns, card._config.cols); |
|
} else { |
|
columns = this._columns; |
|
} |
|
|
|
const className = 'col s' + columns.s + ' m' + columns.m + ' l' + columns.l + ' xl' + columns.xl; |
|
itemTemplates.push(html`<div class$="${className}">${card}</div>`); |
|
} |
|
|
|
return html` |
|
<link rel="stylesheet" href="/local/custom_cards/responsive-panel/responsive-container.css"> |
|
<style> |
|
</style> |
|
<div class="container"> |
|
<div class="row"> |
|
${itemTemplates} |
|
</div> |
|
</div> |
|
`; |
|
} |
|
|
|
setConfig(config) { |
|
if (!config || !config.cards || !Array.isArray(config.cards)) { |
|
this._cards = []; |
|
return; |
|
} |
|
if (!config || !config.columns) { |
|
config.columns = 6; |
|
this._columns = { |
|
s: 12, |
|
m: 6, |
|
l: 4, |
|
xl: 3 |
|
}; |
|
} |
|
|
|
if (config && config.columns) { |
|
this._columns = this._calcColumns(config.columns); |
|
} |
|
|
|
this._cards = config.cards.map((card) => { |
|
if (typeof card === 'string') { |
|
return card; |
|
} |
|
|
|
const element = this._createElement(card); |
|
try { |
|
element.setConfig(card); |
|
} catch(err) {} |
|
|
|
if (this._hass) { |
|
element.hass = this._hass; |
|
} |
|
|
|
return element; |
|
}); |
|
|
|
this.config = config; |
|
} |
|
|
|
_createElement(card) { |
|
let tag = card.type; |
|
if (tag.startsWith("custom:")) { |
|
tag = tag.substr(7); |
|
} else { |
|
tag = `hui-${tag}-card`; |
|
} |
|
|
|
return document.createElement(tag); |
|
} |
|
|
|
_calcColumns(cols, redo) { |
|
let columns = { |
|
s: 12, |
|
m: 6, |
|
l: 4, |
|
xl: 3 |
|
}; |
|
|
|
redo = parseInt(!!redo ? redo : 1, 10); |
|
cols = parseInt(cols, 10); |
|
const reverse = { |
|
x12: 1, |
|
x6: 2, |
|
x4: 3, |
|
x3: 4, |
|
x2: 6, |
|
x1: 12, |
|
}; |
|
|
|
if (cols >= 1 && cols <= 12) { |
|
columns = { |
|
s: reverse['x' + Math.ceil(cols/redo/6)], |
|
m: reverse['x' + Math.ceil(cols/redo/4)], |
|
l: reverse['x' + Math.ceil(cols/redo/2)], |
|
xl: reverse['x' + Math.ceil(cols/redo)], |
|
}; |
|
} |
|
|
|
return columns; |
|
} |
|
|
|
|
|
getCardSize() { |
|
return this.config.cards.length + 1; |
|
} |
|
} |
|
|
|
customElements.define('responsive-container', ResponsiveContainer); |