mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-15 05:16:34 +00:00
Turn ha-cards.html into ES6 class (#533)
This commit is contained in:
parent
5e63f14b38
commit
17402a1976
@ -1,4 +1,4 @@
|
|||||||
<link rel="import" href="../../bower_components/polymer/polymer.html">
|
<link rel="import" href="../../bower_components/polymer/polymer-element.html">
|
||||||
|
|
||||||
<link rel="import" href="../../bower_components/iron-flex-layout/iron-flex-layout-classes.html">
|
<link rel="import" href="../../bower_components/iron-flex-layout/iron-flex-layout-classes.html">
|
||||||
|
|
||||||
@ -46,7 +46,6 @@
|
|||||||
max-width: 600px;
|
max-width: 600px;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
</style>
|
</style>
|
||||||
|
|
||||||
<div class='main'>
|
<div class='main'>
|
||||||
@ -77,11 +76,9 @@
|
|||||||
</dom-module>
|
</dom-module>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
(function () {
|
{
|
||||||
'use strict';
|
|
||||||
|
|
||||||
// mapping domain to size of the card.
|
// mapping domain to size of the card.
|
||||||
var DOMAINS_WITH_CARD = {
|
const DOMAINS_WITH_CARD = {
|
||||||
camera: 4,
|
camera: 4,
|
||||||
history_graph: 4,
|
history_graph: 4,
|
||||||
media_player: 3,
|
media_player: 3,
|
||||||
@ -95,7 +92,7 @@
|
|||||||
// groups: X
|
// groups: X
|
||||||
// rest: 100
|
// rest: 100
|
||||||
|
|
||||||
var PRIORITY = {
|
const PRIORITY = {
|
||||||
// before groups < 0
|
// before groups < 0
|
||||||
configurator: -20,
|
configurator: -20,
|
||||||
persistent_notification: -15,
|
persistent_notification: -15,
|
||||||
@ -119,9 +116,9 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
function entitySortBy(entityA, entityB) {
|
function entitySortBy(entityA, entityB) {
|
||||||
var nameA = (entityA.attributes.friendly_name ||
|
const nameA = (entityA.attributes.friendly_name ||
|
||||||
entityA.entity_id).toLowerCase();
|
entityA.entity_id).toLowerCase();
|
||||||
var nameB = (entityB.attributes.friendly_name ||
|
const nameB = (entityB.attributes.friendly_name ||
|
||||||
entityB.entity_id).toLowerCase();
|
entityB.entity_id).toLowerCase();
|
||||||
|
|
||||||
if (nameA < nameB) {
|
if (nameA < nameB) {
|
||||||
@ -135,61 +132,53 @@
|
|||||||
|
|
||||||
function iterateDomainSorted(collection, func) {
|
function iterateDomainSorted(collection, func) {
|
||||||
Object.keys(collection)
|
Object.keys(collection)
|
||||||
.map(function (key) { return collection[key]; })
|
.map(key => collection[key])
|
||||||
.sort(sortPriority)
|
.sort(sortPriority)
|
||||||
.forEach(function (domain) {
|
.forEach((domain) => {
|
||||||
domain.states.sort(entitySortBy);
|
domain.states.sort(entitySortBy);
|
||||||
func(domain);
|
func(domain);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
var computeDomain = window.hassUtil.computeDomain;
|
const computeDomain = window.hassUtil.computeDomain;
|
||||||
|
|
||||||
Polymer({
|
class HaCards extends Polymer.Element {
|
||||||
is: 'ha-cards',
|
static get is() { return 'ha-cards'; }
|
||||||
|
static get properties() {
|
||||||
|
return {
|
||||||
|
hass: Object,
|
||||||
|
|
||||||
properties: {
|
showIntroduction: {
|
||||||
hass: {
|
type: Boolean,
|
||||||
type: Object,
|
value: false,
|
||||||
},
|
},
|
||||||
|
|
||||||
showIntroduction: {
|
columns: {
|
||||||
type: Boolean,
|
type: Number,
|
||||||
value: false,
|
value: 2,
|
||||||
},
|
},
|
||||||
|
|
||||||
columns: {
|
states: Object,
|
||||||
type: Number,
|
panelVisible: Boolean,
|
||||||
value: 2,
|
|
||||||
},
|
|
||||||
|
|
||||||
states: {
|
viewVisible: {
|
||||||
type: Object,
|
type: Boolean,
|
||||||
},
|
value: false,
|
||||||
|
},
|
||||||
|
|
||||||
panelVisible: {
|
orderedGroups: Array,
|
||||||
type: Boolean,
|
|
||||||
},
|
|
||||||
|
|
||||||
viewVisible: {
|
cards: Object,
|
||||||
type: Boolean,
|
};
|
||||||
value: false,
|
}
|
||||||
},
|
|
||||||
|
|
||||||
orderedGroups: {
|
static get observers() {
|
||||||
type: Array,
|
return [
|
||||||
},
|
'updateCards(columns, states, showIntroduction, panelVisible, viewVisible, orderedGroups)',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
cards: {
|
updateCards(
|
||||||
type: Object,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
|
|
||||||
observers: [
|
|
||||||
'updateCards(columns, states, showIntroduction, panelVisible, viewVisible, orderedGroups)',
|
|
||||||
],
|
|
||||||
|
|
||||||
updateCards: function (
|
|
||||||
columns,
|
columns,
|
||||||
states,
|
states,
|
||||||
showIntroduction,
|
showIntroduction,
|
||||||
@ -200,33 +189,36 @@
|
|||||||
if (!panelVisible || !viewVisible) {
|
if (!panelVisible || !viewVisible) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.debounce('updateCards', function () {
|
this._debouncer = Polymer.Debouncer.debounce(
|
||||||
// Things might have changed since it got scheduled.
|
this._debouncer,
|
||||||
if (this.panelVisible && this.viewVisible) {
|
Polymer.Async.timeOut.after(10),
|
||||||
this.cards = this.computeCards(columns, states, showIntroduction, orderedGroups);
|
() => {
|
||||||
|
// Things might have changed since it got scheduled.
|
||||||
|
if (this.panelVisible && this.viewVisible) {
|
||||||
|
this.cards = this.computeCards(columns, states, showIntroduction, orderedGroups);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}.bind(this), 10);
|
);
|
||||||
},
|
}
|
||||||
|
|
||||||
computeCards: function (columns, states, showIntroduction, orderedGroups) {
|
computeCards(columns, states, showIntroduction, orderedGroups) {
|
||||||
var hass = this.hass;
|
const hass = this.hass;
|
||||||
|
|
||||||
var cards = {
|
const cards = {
|
||||||
demo: false,
|
demo: false,
|
||||||
badges: [],
|
badges: [],
|
||||||
columns: [],
|
columns: [],
|
||||||
};
|
};
|
||||||
var entityCount = [];
|
const entityCount = [];
|
||||||
var i;
|
for (let i = 0; i < columns; i++) {
|
||||||
for (i = 0; i < columns; i++) {
|
|
||||||
cards.columns.push([]);
|
cards.columns.push([]);
|
||||||
entityCount.push(0);
|
entityCount.push(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find column with < 5 entities, else column with lowest count
|
// Find column with < 5 entities, else column with lowest count
|
||||||
function getIndex(size) {
|
function getIndex(size) {
|
||||||
var minIndex = 0;
|
let minIndex = 0;
|
||||||
for (i = 0; i < entityCount.length; i++) {
|
for (let i = 0; i < entityCount.length; i++) {
|
||||||
if (entityCount[i] < 5) {
|
if (entityCount[i] < 5) {
|
||||||
minIndex = i;
|
minIndex = i;
|
||||||
break;
|
break;
|
||||||
@ -250,19 +242,15 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
function addEntitiesCard(name, entities, groupEntity) {
|
function addEntitiesCard(name, entities, groupEntity) {
|
||||||
var owncard;
|
|
||||||
var other;
|
|
||||||
var size;
|
|
||||||
var curIndex;
|
|
||||||
if (entities.length === 0) return;
|
if (entities.length === 0) return;
|
||||||
|
|
||||||
owncard = [];
|
const owncard = [];
|
||||||
other = [];
|
const other = [];
|
||||||
|
|
||||||
size = 0;
|
let size = 0;
|
||||||
|
|
||||||
entities.forEach(function (entity) {
|
entities.forEach((entity) => {
|
||||||
var domain = computeDomain(entity);
|
const domain = computeDomain(entity);
|
||||||
|
|
||||||
if (domain in DOMAINS_WITH_CARD) {
|
if (domain in DOMAINS_WITH_CARD) {
|
||||||
owncard.push(entity);
|
owncard.push(entity);
|
||||||
@ -276,7 +264,7 @@
|
|||||||
// Add 1 to the size if we're rendering entities card
|
// Add 1 to the size if we're rendering entities card
|
||||||
size += other.length > 0;
|
size += other.length > 0;
|
||||||
|
|
||||||
curIndex = getIndex(size);
|
const curIndex = getIndex(size);
|
||||||
|
|
||||||
if (other.length > 0) {
|
if (other.length > 0) {
|
||||||
cards.columns[curIndex].push({
|
cards.columns[curIndex].push({
|
||||||
@ -287,7 +275,7 @@
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
owncard.forEach(function (entity) {
|
owncard.forEach((entity) => {
|
||||||
cards.columns[curIndex].push({
|
cards.columns[curIndex].push({
|
||||||
hass: hass,
|
hass: hass,
|
||||||
cardType: computeDomain(entity),
|
cardType: computeDomain(entity),
|
||||||
@ -296,7 +284,7 @@
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
var splitted = window.HAWS.splitByGroups(states);
|
const splitted = window.HAWS.splitByGroups(states);
|
||||||
if (orderedGroups) {
|
if (orderedGroups) {
|
||||||
splitted.groups.sort((gr1, gr2) => orderedGroups[gr1.entity_id] -
|
splitted.groups.sort((gr1, gr2) => orderedGroups[gr1.entity_id] -
|
||||||
orderedGroups[gr2.entity_id]);
|
orderedGroups[gr2.entity_id]);
|
||||||
@ -304,21 +292,21 @@
|
|||||||
splitted.groups.sort((gr1, gr2) => gr1.attributes.order - gr2.attributes.order);
|
splitted.groups.sort((gr1, gr2) => gr1.attributes.order - gr2.attributes.order);
|
||||||
}
|
}
|
||||||
|
|
||||||
var badgesColl = {};
|
const badgesColl = {};
|
||||||
var beforeGroupColl = {};
|
const beforeGroupColl = {};
|
||||||
var afterGroupedColl = {};
|
const afterGroupedColl = {};
|
||||||
|
|
||||||
Object.keys(splitted.ungrouped).forEach(function (key) {
|
Object.keys(splitted.ungrouped).forEach((key) => {
|
||||||
var state = splitted.ungrouped[key];
|
const state = splitted.ungrouped[key];
|
||||||
var domain = computeDomain(state);
|
const domain = computeDomain(state);
|
||||||
|
|
||||||
if (domain === 'a') {
|
if (domain === 'a') {
|
||||||
cards.demo = true;
|
cards.demo = true;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var priority = getPriority(domain);
|
const priority = getPriority(domain);
|
||||||
var coll;
|
let coll;
|
||||||
|
|
||||||
if (priority < 0) {
|
if (priority < 0) {
|
||||||
coll = beforeGroupColl;
|
coll = beforeGroupColl;
|
||||||
@ -339,36 +327,33 @@
|
|||||||
coll[domain].states.push(state);
|
coll[domain].states.push(state);
|
||||||
});
|
});
|
||||||
|
|
||||||
iterateDomainSorted(badgesColl, function (domain) {
|
iterateDomainSorted(badgesColl, (domain) => {
|
||||||
cards.badges.push.apply(cards.badges, domain.states);
|
cards.badges.push.apply(cards.badges, domain.states);
|
||||||
});
|
});
|
||||||
|
|
||||||
iterateDomainSorted(beforeGroupColl, function (domain) {
|
iterateDomainSorted(beforeGroupColl, (domain) => {
|
||||||
addEntitiesCard(domain.domain, domain.states);
|
addEntitiesCard(domain.domain, domain.states);
|
||||||
});
|
});
|
||||||
|
|
||||||
splitted.groups.forEach(function (groupState) {
|
splitted.groups.forEach((groupState) => {
|
||||||
var entities = window.HAWS.getGroupEntities(states, groupState);
|
const entities = window.HAWS.getGroupEntities(states, groupState);
|
||||||
addEntitiesCard(
|
addEntitiesCard(
|
||||||
groupState.entity_id,
|
groupState.entity_id,
|
||||||
Object.keys(entities).map(function (key) {
|
Object.keys(entities).map(key => entities[key]),
|
||||||
return entities[key];
|
|
||||||
}),
|
|
||||||
groupState
|
groupState
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
iterateDomainSorted(afterGroupedColl, function (domain) {
|
iterateDomainSorted(afterGroupedColl, (domain) => {
|
||||||
addEntitiesCard(domain.domain, domain.states);
|
addEntitiesCard(domain.domain, domain.states);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Remove empty columns
|
// Remove empty columns
|
||||||
cards.columns = cards.columns.filter(function (val) {
|
cards.columns = cards.columns.filter(val => val.length > 0);
|
||||||
return val.length > 0;
|
|
||||||
});
|
|
||||||
|
|
||||||
return cards;
|
return cards;
|
||||||
},
|
}
|
||||||
});
|
}
|
||||||
}());
|
customElements.define(HaCards.is, HaCards);
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user