Badge sorting (#844)

* sort badges correctly in user-defined group

* rename sorting list that is used for badges and groups

* remove trailing space

* better array iteration

* Revert "better array iteration"

This reverts commit 0a87b3350cf2bd60450e3d2985c7449efe73c18d.

* simplify sorting of badges
This commit is contained in:
Abílio Costa 2018-02-09 22:30:35 +00:00 committed by Paulus Schoutsen
parent be3f35c8cd
commit 3701e022bc
2 changed files with 30 additions and 19 deletions

View File

@ -164,7 +164,7 @@
value: false, value: false,
}, },
orderedGroups: Array, orderedGroupEntities: Array,
cards: Object, cards: Object,
}; };
@ -172,7 +172,7 @@
static get observers() { static get observers() {
return [ return [
'updateCards(columns, states, panelVisible, viewVisible, orderedGroups)', 'updateCards(columns, states, panelVisible, viewVisible, orderedGroupEntities)',
]; ];
} }
@ -181,7 +181,7 @@
states, states,
panelVisible, panelVisible,
viewVisible, viewVisible,
orderedGroups orderedGroupEntities
) { ) {
if (!panelVisible || !viewVisible) { if (!panelVisible || !viewVisible) {
if (this.$.main.parentNode) { if (this.$.main.parentNode) {
@ -198,7 +198,7 @@
() => { () => {
// Things might have changed since it got scheduled. // Things might have changed since it got scheduled.
if (this.panelVisible && this.viewVisible) { if (this.panelVisible && this.viewVisible) {
this.cards = this.computeCards(columns, states, orderedGroups); this.cards = this.computeCards(columns, states, orderedGroupEntities);
} }
} }
); );
@ -212,7 +212,7 @@
}; };
} }
computeCards(columns, states, orderedGroups) { computeCards(columns, states, orderedGroupEntities) {
const hass = this.hass; const hass = this.hass;
const cards = this.emptyCards(); const cards = this.emptyCards();
@ -285,9 +285,9 @@
} }
const splitted = window.HAWS.splitByGroups(states); const splitted = window.HAWS.splitByGroups(states);
if (orderedGroups) { if (orderedGroupEntities) {
splitted.groups.sort((gr1, gr2) => orderedGroups[gr1.entity_id] - splitted.groups.sort((gr1, gr2) => orderedGroupEntities[gr1.entity_id] -
orderedGroups[gr2.entity_id]); orderedGroupEntities[gr2.entity_id]);
} else { } else {
splitted.groups.sort((gr1, gr2) => gr1.attributes.order - gr2.attributes.order); splitted.groups.sort((gr1, gr2) => gr1.attributes.order - gr2.attributes.order);
} }
@ -327,9 +327,20 @@
coll[domain].states.push(state); coll[domain].states.push(state);
}); });
iterateDomainSorted(badgesColl, (domain) => { if (orderedGroupEntities) {
cards.badges.push.apply(cards.badges, domain.states); Object.keys(badgesColl)
}); .map(key => badgesColl[key])
.forEach((domain) => {
cards.badges.push.apply(cards.badges, domain.states);
});
cards.badges.sort((e1, e2) => orderedGroupEntities[e1.entity_id] -
orderedGroupEntities[e2.entity_id]);
} else {
iterateDomainSorted(badgesColl, (domain) => {
cards.badges.push.apply(cards.badges, domain.states);
});
}
iterateDomainSorted(beforeGroupColl, (domain) => { iterateDomainSorted(beforeGroupColl, (domain) => {
addEntitiesCard(domain.domain, domain.states); addEntitiesCard(domain.domain, domain.states);

View File

@ -113,7 +113,7 @@
columns='[[_columns]]' columns='[[_columns]]'
hass='[[hass]]' hass='[[hass]]'
panel-visible='[[panelVisible]]' panel-visible='[[panelVisible]]'
ordered-groups='[[orderedGroups]]' ordered-group-entities='[[orderedGroupEntities]]'
></ha-cards> ></ha-cards>
<template is='dom-repeat' items='[[views]]'> <template is='dom-repeat' items='[[views]]'>
@ -123,7 +123,7 @@
columns='[[_columns]]' columns='[[_columns]]'
hass='[[hass]]' hass='[[hass]]'
panel-visible='[[panelVisible]]' panel-visible='[[panelVisible]]'
ordered-groups='[[orderedGroups]]' ordered-group-entities='[[orderedGroupEntities]]'
></ha-cards> ></ha-cards>
</template> </template>
@ -198,9 +198,9 @@
computed: 'computeViewStates(currentView, hass, defaultView)', computed: 'computeViewStates(currentView, hass, defaultView)',
}, },
orderedGroups: { orderedGroupEntities: {
type: Array, type: Array,
computed: 'computeOrderedGroups(currentView, hass, defaultView)', computed: 'computeOrderedGroupEntities(currentView, hass, defaultView)',
}, },
showTabs: { showTabs: {
@ -372,19 +372,19 @@
/* /*
Compute the ordered list of groups for this view Compute the ordered list of groups for this view
*/ */
computeOrderedGroups(currentView, hass, defaultView) { computeOrderedGroupEntities(currentView, hass, defaultView) {
if (!this.isView(currentView, defaultView)) { if (!this.isView(currentView, defaultView)) {
return null; return null;
} }
var orderedGroups = {}; var orderedGroupEntities = {};
var entitiesList = hass.states[currentView || DEFAULT_VIEW_ENTITY_ID].attributes.entity_id; var entitiesList = hass.states[currentView || DEFAULT_VIEW_ENTITY_ID].attributes.entity_id;
for (var i = 0; i < entitiesList.length; i++) { for (var i = 0; i < entitiesList.length; i++) {
orderedGroups[entitiesList[i]] = i; orderedGroupEntities[entitiesList[i]] = i;
} }
return orderedGroups; return orderedGroupEntities;
} }
} }