Add grid layout options. Bugfixes

This commit is contained in:
thomas committed 2020-01-02 22:41:21 +01:00
1 parent 5d2842f934
commit c2808b9396
4 files changed
+94 -16

No files matched your search

+50 -1
View File
@@ -27,6 +27,8 @@ column_width: <column_width>
max_width: <max_width> max_width: <max_width>
min_width: <min_width> min_width: <min_width>
flex_grow: <flex_grow> flex_grow: <flex_grow>
gridcols: <grid-cols>
gridrows: <grid-rows>
justify_content: <justify_content> justify_content: <justify_content>
rtl: <rtl> rtl: <rtl>
cards: cards:
@@ -38,12 +40,13 @@ card_options:
## Options ## Options
- `<cards>` **Required** A list of lovelace cards to display. - `<cards>` **Required** A list of lovelace cards to display.
- `<card_options>` are options that are applied to all cards. - `<card_options>` are options that are applied to all cards.
- `<layout>` The layout method to use. `auto`, `vertical` or `horizontal`. See below. Default: `auto`. - `<layout>` The layout method to use. `auto`, `vertical`, `horizontal` or `grid`. See below. Default: `auto`.
- `<min_height>` The minimum length of a column in `auto` layout. Default: `5`. - `<min_height>` The minimum length of a column in `auto` layout. Default: `5`.
- `<min_columns>` The minimum number of columns to use. Default: `1`. - `<min_columns>` The minimum number of columns to use. Default: `1`.
- `<max_columns>` The maximum number of columns to use. Default: `100`. - `<max_columns>` The maximum number of columns to use. Default: `100`.
- `<column_width>` Width of columns. Default: `300px`. - `<column_width>` Width of columns. Default: `300px`.
- `<max_width>`, `<min_width>`, `<flex_grow>` Set the `max-width`, `min-width` and `flex-grow` CSS properties of the columns manually. Default: `column_width or 500px`, `undefined`, `undefined`. - `<max_width>`, `<min_width>`, `<flex_grow>` Set the `max-width`, `min-width` and `flex-grow` CSS properties of the columns manually. Default: `column_width or 500px`, `undefined`, `undefined`.
- `<grid-rows>`, `<grid-col>` Set the `grid-template-rows` and `grid-template-columns` CSS properties when using `layout: grid`.
- `<justify_content>` Set the `justify-content` CSS property of the column container. Default: `center`. - `<justify_content>` Set the `justify-content` CSS property of the column container. Default: `center`.
## Layouts ## Layouts
@@ -180,6 +183,52 @@ cards:
``` ```
![layout-card 4 - manual breaks](https://user-images.githubusercontent.com/1299821/48088461-62312500-e202-11e8-96ab-e4f560f8d4fc.png) ![layout-card 4 - manual breaks](https://user-images.githubusercontent.com/1299821/48088461-62312500-e202-11e8-96ab-e4f560f8d4fc.png)
### `grid` layout (experimental)
For maximum controll, you can place every card manually in a [CSS grid](https://css-tricks.com/snippets/css/complete-guide-grid/) by using the `grid` layout.
To do this, you need to specify `gridrows` and `gridcols` with the settings for `grid-template-rows` and `grid-template-columns` repectively **and** also add `gridcol:` and `gridrow:` for *each card* with the settings for `grid-column` and `grid-row` respectively.
> Hint: This may look better if you also have [card-mod](https://github.com/thomasloven/lovelace-card-mod) and set the card heights to `100 %`.
```yaml
type: custom:layout-card
layout: vertical
column_width: 100%
cards:
- type: markdown
content: "# Grid"
- type: custom:layout-card
layout: grid
gridrows: 180px 200px auto
gridcols: 180px auto 180px
cards:
- type: glance
entities:
- sun.sun
gridrow: 1 / 2
gridcol: 1 / 2
style: "ha-card { height: 100%; }"
- type: entities
entities: &ents
- light.bed_light
- light.kitchen_lights
- light.ceiling_lights
gridrow: 1 / 3
gridcol: 2 / 4
style: "ha-card { height: 100%; }"
- type: markdown
content: test
gridrow: 2 / 4
gridcol: 1 / 2
style: "ha-card { height: 100%; }"
- type: entities
entities: *ents
gridrow: 3 / 4
gridcol: 2 / 3
```
![layout-card - Grid](https://user-images.githubusercontent.com/1299821/71694902-e3f1f380-2db0-11ea-82f1-8f880a2fbb24.png)
## Tweaking layouts ## Tweaking layouts
- First of all `<min_columns>` and `<max_columns>` options, which can be used to force the number of columns displayed: - First of all `<min_columns>` and `<max_columns>` options, which can be used to force the number of columns displayed:
+15 -7
View File
@@ -1,4 +1,11 @@
!function(t){var e={};function n(s){if(e[s])return e[s].exports;var o=e[s]={i:s,l:!1,exports:{}};return t[s].call(o.exports,o,o.exports,n),o.l=!0,o.exports}n.m=t,n.c=e,n.d=function(t,e,s){n.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:s})},n.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},n.t=function(t,e){if(1&e&&(t=n(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var s=Object.create(null);if(n.r(s),Object.defineProperty(s,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var o in t)n.d(s,o,function(e){return t[e]}.bind(null,o));return s},n.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return n.d(e,"a",e),e},n.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},n.p="",n(n.s=0)}([function(t,e,n){"use strict";n.r(e);const s=customElements.get("home-assistant-main")?Object.getPrototypeOf(customElements.get("home-assistant-main")):Object.getPrototypeOf(customElements.get("hui-view")),o=s.prototype.html,i=s.prototype.css;const r="custom:";function c(t,e){const n=document.createElement("hui-error-card");return n.setConfig({type:"error",error:t,origConfig:e}),n}function a(t,e){if(!e||"object"!=typeof e||!e.type)return c(`No ${t} type configured`,e);let n=e.type;if(n=n.startsWith(r)?n.substr(r.length):`hui-${n}-${t}`,customElements.get(n))return function(t,e){const n=document.createElement(t);try{n.setConfig(e)}catch(t){return c(t,e)}return n}(n,e);const s=c(`Custom element doesn't exist: ${n}.`,e);s.style.display="None";const o=setTimeout(()=>{s.style.display=""},2e3);return customElements.whenDefined(n).then(()=>{clearTimeout(o),function(t,e,n=null){if((t=new Event(t,{bubbles:!0,cancelable:!1,composed:!0})).detail=e||{},n)n.dispatchEvent(t);else{var s=document.querySelector("home-assistant");(s=(s=(s=(s=(s=(s=(s=(s=(s=(s=(s=s&&s.shadowRoot)&&s.querySelector("home-assistant-main"))&&s.shadowRoot)&&s.querySelector("app-drawer-layout partial-panel-resolver"))&&s.shadowRoot||s)&&s.querySelector("ha-panel-lovelace"))&&s.shadowRoot)&&s.querySelector("hui-root"))&&s.shadowRoot)&&s.querySelector("ha-app-layout #view"))&&s.firstElementChild)&&s.dispatchEvent(t)}}("ll-rebuild",{},s)}),s}function l(){return document.querySelector("home-assistant").hass}const u=2;class d extends s{static get version(){return u}static get properties(){return{noHass:{type:Boolean}}}setConfig(t){var e;this._config=t,this.el?this.el.setConfig(t):(this.el=this.create(t),this._hass&&(this.el.hass=this._hass),this.noHass&&(e=this,document.querySelector("home-assistant").provideHass(e)))}set config(t){this.setConfig(t)}set hass(t){this._hass=t,this.el&&(this.el.hass=t)}createRenderRoot(){return this}render(){return o`${this.el}`}}const h=function(t,e){const n=Object.getOwnPropertyDescriptors(e.prototype);for(const[e,s]of Object.entries(n))"constructor"!==e&&Object.defineProperty(t.prototype,e,s);const s=Object.getOwnPropertyDescriptors(e);for(const[e,n]of Object.entries(s))"prototype"!==e&&Object.defineProperty(t,e,n);const o=Object.getPrototypeOf(e),i=Object.getOwnPropertyDescriptors(o.prototype);for(const[e,n]of Object.entries(i))"constructor"!==e&&Object.defineProperty(Object.getPrototypeOf(t).prototype,e,n);const r=Object.getOwnPropertyDescriptors(o);for(const[e,n]of Object.entries(r))"prototype"!==e&&Object.defineProperty(Object.getPrototypeOf(t),e,n)},m=customElements.get("card-maker");if(!m||!m.version||m.version<u){class t extends d{create(t){return function(t){return a("card",t)}(t)}getCardSize(){return this.firstElementChild&&this.firstElementChild.getCardSize?this.firstElementChild.getCardSize():1}}m?h(m,t):customElements.define("card-maker",t)}const f=customElements.get("element-maker");if(!f||!f.version||f.version<u){class t extends d{create(t){return function(t){return a("element",t)}(t)}}f?h(f,t):customElements.define("element-maker",t)}const p=customElements.get("entity-row-maker");if(!p||!p.version||p.version<u){class t extends d{create(t){return function(t){const e=new Set(["call-service","divider","section","weblink"]);if(!t)return c("Invalid configuration given.",t);if("string"==typeof t&&(t={entity:t}),"object"!=typeof t||!t.entity&&!t.type)return c("Invalid configuration given.",t);const n=t.type||"default";if(e.has(n)||n.startsWith(r))return a("row",t);const s=t.entity.split(".",1)[0];return Object.assign(t,{type:{alert:"toggle",automation:"toggle",climate:"climate",cover:"cover",fan:"toggle",group:"group",input_boolean:"toggle",input_number:"input-number",input_select:"input-select",input_text:"input-text",light:"toggle",lock:"lock",media_player:"media-player",remote:"toggle",scene:"scene",script:"script",sensor:"sensor",timer:"timer",switch:"toggle",vacuum:"toggle",water_heater:"climate",input_datetime:"input-datetime"}[s]||"text"}),a("entity-row",t)}(t)}}p?h(p,t):customElements.define("entity-row-maker"Line truncated !function(t){var e={};function n(i){if(e[i])return e[i].exports;var s=e[i]={i:i,l:!1,exports:{}};return t[i].call(s.exports,s,s.exports,n),s.l=!0,s.exports}n.m=t,n.c=e,n.d=function(t,e,i){n.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:i})},n.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},n.t=function(t,e){if(1&e&&(t=n(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var i=Object.create(null);if(n.r(i),Object.defineProperty(i,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var s in t)n.d(i,s,function(e){return t[e]}.bind(null,s));return i},n.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return n.d(e,"a",e),e},n.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},n.p="",n(n.s=0)}([function(t,e,n){"use strict";n.r(e);const i=customElements.get("home-assistant-main")?Object.getPrototypeOf(customElements.get("home-assistant-main")):Object.getPrototypeOf(customElements.get("hui-view")),s=i.prototype.html,o=i.prototype.css;const r="custom:";function c(t,e){const n=document.createElement("hui-error-card");return n.setConfig({type:"error",error:t,origConfig:e}),n}function a(t,e){if(!e||"object"!=typeof e||!e.type)return c(`No ${t} type configured`,e);let n=e.type;if(n=n.startsWith(r)?n.substr(r.length):`hui-${n}-${t}`,customElements.get(n))return function(t,e){const n=document.createElement(t);try{n.setConfig(e)}catch(t){return c(t,e)}return n}(n,e);const i=c(`Custom element doesn't exist: ${n}.`,e);i.style.display="None";const s=setTimeout(()=>{i.style.display=""},2e3);return customElements.whenDefined(n).then(()=>{clearTimeout(s),function(t,e,n=null){if((t=new Event(t,{bubbles:!0,cancelable:!1,composed:!0})).detail=e||{},n)n.dispatchEvent(t);else{var i=document.querySelector("home-assistant");(i=(i=(i=(i=(i=(i=(i=(i=(i=(i=(i=i&&i.shadowRoot)&&i.querySelector("home-assistant-main"))&&i.shadowRoot)&&i.querySelector("app-drawer-layout partial-panel-resolver"))&&i.shadowRoot||i)&&i.querySelector("ha-panel-lovelace"))&&i.shadowRoot)&&i.querySelector("hui-root"))&&i.shadowRoot)&&i.querySelector("ha-app-layout #view"))&&i.firstElementChild)&&i.dispatchEvent(t)}}("ll-rebuild",{},i)}),i}function l(t){return a("card",t)}function u(){return document.querySelector("home-assistant").hass}const d=2;class h extends i{static get version(){return d}static get properties(){return{noHass:{type:Boolean}}}setConfig(t){var e;this._config=t,this.el?this.el.setConfig(t):(this.el=this.create(t),this._hass&&(this.el.hass=this._hass),this.noHass&&(e=this,document.querySelector("home-assistant").provideHass(e)))}set config(t){this.setConfig(t)}set hass(t){this._hass=t,this.el&&(this.el.hass=t)}createRenderRoot(){return this}render(){return s`${this.el}`}}const m=function(t,e){const n=Object.getOwnPropertyDescriptors(e.prototype);for(const[e,i]of Object.entries(n))"constructor"!==e&&Object.defineProperty(t.prototype,e,i);const i=Object.getOwnPropertyDescriptors(e);for(const[e,n]of Object.entries(i))"prototype"!==e&&Object.defineProperty(t,e,n);const s=Object.getPrototypeOf(e),o=Object.getOwnPropertyDescriptors(s.prototype);for(const[e,n]of Object.entries(o))"constructor"!==e&&Object.defineProperty(Object.getPrototypeOf(t).prototype,e,n);const r=Object.getOwnPropertyDescriptors(s);for(const[e,n]of Object.entries(r))"prototype"!==e&&Object.defineProperty(Object.getPrototypeOf(t),e,n)},f=customElements.get("card-maker");if(!f||!f.version||f.version<d){class t extends h{create(t){return l(t)}getCardSize(){return this.firstElementChild&&this.firstElementChild.getCardSize?this.firstElementChild.getCardSize():1}}f?m(f,t):customElements.define("card-maker",t)}const p=customElements.get("element-maker");if(!p||!p.version||p.version<d){class t extends h{create(t){return function(t){return a("element",t)}(t)}}p?m(p,t):customElements.define("element-maker",t)}const g=customElements.get("entity-row-maker");if(!g||!g.version||g.version<d){class t extends h{create(t){return function(t){const e=new Set(["call-service","divider","section","weblink"]);if(!t)return c("Invalid configuration given.",t);if("string"==typeof t&&(t={entity:t}),"object"!=typeof t||!t.entity&&!t.type)return c("Invalid configuration given.",t);const n=t.type||"default";if(e.has(n)||n.startsWith(r))return a("row",t);const i=t.entity.split(".",1)[0];return Object.assign(t,{type:{alert:"toggle",automation:"toggle",climate:"climate",cover:"cover",fan:"toggle",group:"group",input_boolean:"toggle",input_number:"input-number",input_select:"input-select",input_text:"input-text",light:"toggle",lock:"lock",media_player:"media-player",remote:"toggle",scene:"scene",script:"script",sensor:"sensor",timer:"timer",switch:"toggle",vacuum:"toggle",water_heater:"climate",input_datetime:"input-datetime"}[i]||"text"}),a("entity-row",t)}(t)}}g?m(g,t):customElements.define("entity-row-makLine truncated
<div id="staging" class="grid cards"
style="
display: grid;
grid-template-rows: ${this._config.gridrows};
grid-template-columns: ${this._config.gridcols};
"></div>
`:s`
<div id="columns" <div id="columns"
class=" class="
${this._isPanel()?"panel":" "} ${this._isPanel()?"panel":" "}
@@ -6,12 +13,12 @@
style=" style="
${this._config.justify_content?`justify-content: ${this._config.justify_content};`:""} ${this._config.justify_content?`justify-content: ${this._config.justify_content};`:""}
"> ">
${this.columns.map(t=>o` ${this.columns.map(t=>s`
${t} ${t}
`)} `)}
</div> </div>
<div id="staging"></div> <div id="staging"></div>
`}static get styles(){return i` `}static get styles(){return o`
:host { :host {
padding: 0 4px; padding: 0 4px;
display: block; display: block;
@@ -34,18 +41,19 @@
overflow-x: hidden; overflow-x: hidden;
} }
card-maker>* {
.cards>* {
display: block; display: block;
margin: 4px 4px 8px; margin: 4px 4px 8px;
} }
card-maker:first-child>* { .cards>*:first-child {
margin-top: 8px; margin-top: 8px;
} }
card-maker:last-child>* { .cards>*:last-child {
margin-bottom: 4px; margin-bottom: 4px;
} }
#staging { #staging:not(.grid) {
visibility: hidden; visibility: hidden;
height: 0; height: 0;
} }
+1
View File
@@ -79,6 +79,7 @@ export function buildLayout(cards, width, config) {
for (let i = 0; i < colnum; i++) { for (let i = 0; i < colnum; i++) {
const newCol = document.createElement("div"); const newCol = document.createElement("div");
newCol.classList.add("column"); newCol.classList.add("column");
newCol.classList.add("cards");
newCol.length = 0; newCol.length = 0;
cols.push(newCol); cols.push(newCol);
} }
+28 -8
View File
@@ -1,5 +1,6 @@
import { LitElement, html, css } from "card-tools/src/lit-element"; import { LitElement, html, css } from "card-tools/src/lit-element";
import "card-tools/src/card-maker"; import "card-tools/src/card-maker";
import { createCard } from "card-tools/src/lovelace-element";
import { hass } from "card-tools/src/hass"; import { hass } from "card-tools/src/hass";
import {buildLayout} from "./layout"; import {buildLayout} from "./layout";
@@ -62,13 +63,20 @@ class LayoutCard extends LitElement {
async build_card(c) { async build_card(c) {
if(c === "break") if(c === "break")
return null; return null;
const el = document.createElement("card-maker"); const config = {...c, ...this._config.card_options};
el.config = {...c, ...this._config.card_options}; const el = createCard(config);
el.hass = hass(); el.hass = hass();
el.style.gridColumn = config.gridcol;
el.style.gridRow = config.gridrow;
// Cards are initially placed in the staging area // Cards are initially placed in the staging area
// That places them in the DOM and lets us read their getCardSize() function // That places them in the DOM and lets us read their getCardSize() function
this.shadowRoot.querySelector("#staging").appendChild(el); this.shadowRoot.querySelector("#staging").appendChild(el);
return new Promise((resolve, reject) => el.updateComplete.then(() => resolve(el))); return new Promise((resolve, reject) =>
el.updateComplete
? el.updateComplete.then(() => resolve(el))
: resolve(el)
);
} }
async build_cards() { async build_cards() {
@@ -83,6 +91,8 @@ class LayoutCard extends LitElement {
} }
place_cards() { place_cards() {
if(this._config.layout === "grid")
return;
const width = this.shadowRoot.querySelector("#columns").clientWidth; const width = this.shadowRoot.querySelector("#columns").clientWidth;
this.columns = buildLayout( this.columns = buildLayout(
@@ -141,7 +151,7 @@ class LayoutCard extends LitElement {
if(this.isPanel) return true; if(this.isPanel) return true;
let el = this.parentElement; let el = this.parentElement;
let steps = 10; let steps = 10;
while(steps--) { while(steps-- && el) {
if(el.localName === "hui-panel-view") return true; if(el.localName === "hui-panel-view") return true;
if(el.localName === "div") return false; if(el.localName === "div") return false;
el = el.parentElement; el = el.parentElement;
@@ -150,6 +160,15 @@ class LayoutCard extends LitElement {
} }
render() { render() {
if(this._config.layout === "grid")
return html`
<div id="staging" class="grid cards"
style="
display: grid;
grid-template-rows: ${this._config.gridrows};
grid-template-columns: ${this._config.gridcols};
"></div>
`;
return html` return html`
<div id="columns" <div id="columns"
class=" class="
@@ -190,18 +209,19 @@ class LayoutCard extends LitElement {
overflow-x: hidden; overflow-x: hidden;
} }
card-maker>* {
.cards>* {
display: block; display: block;
margin: 4px 4px 8px; margin: 4px 4px 8px;
} }
card-maker:first-child>* { .cards>*:first-child {
margin-top: 8px; margin-top: 8px;
} }
card-maker:last-child>* { .cards>*:last-child {
margin-bottom: 4px; margin-bottom: 4px;
} }
#staging { #staging:not(.grid) {
visibility: hidden; visibility: hidden;
height: 0; height: 0;
} }