Display attributions separately in more-info (#176)

* Display attributions separately in more-info

* Fix js lint

* Rename to formatAttributeValue

* Properly declare the computed bindings
This commit is contained in:
Adam Mills 2017-01-21 01:05:10 -05:00 committed by Paulus Schoutsen
parent abe798d121
commit c8af07ab6b
3 changed files with 62 additions and 28 deletions

View File

@ -14,23 +14,29 @@
} }
.attribution { .attribution {
color:grey; color: var(--secondary-text-color);
text-align: right;
} }
</style> </style>
<google-legacy-loader on-api-load='googleApiLoaded'></google-legacy-loader> <google-legacy-loader on-api-load='googleApiLoaded'></google-legacy-loader>
<ha-card header='[[computeTitle(stateObj)]]'> <ha-card header='[[computeTitle(stateObj)]]'>
<div class='content'> <div class='content'>
<template is='dom-repeat' items='[[computeCurrentWeather(stateObj)]]'> <template is='dom-repeat' items='[[computeDisplayAttributes(stateObj)]]' as="attribute">
<div>[[item.key]]: [[item.value]]</div> <div>[[formatAttribute(attribute)]]: [[formatAttributeValue(stateObj, attribute)]]</div>
</template> </template>
<div id='chart_area'></div> <div id='chart_area'></div>
<div class='attribution'>[[computeAttribution(stateObj)]]</div> <div class='attribution' hidden$='[[!computeAttribution(stateObj)]]'>[[computeAttribution(stateObj)]]</div>
</div> </div>
</ha-card> </ha-card>
</template> </template>
</dom-module> </dom-module>
<script> <script>
(function () {
'use strict';
var FILTER_KEYS = ['attribution', 'forecast', 'friendly_name'];
Polymer({ Polymer({
is: 'ha-weather-card', is: 'ha-weather-card',
@ -49,19 +55,20 @@
return stateObj.attributes.friendly_name; return stateObj.attributes.friendly_name;
}, },
computeAttribution: function (stateObj) { computeDisplayAttributes: function (stateObj) {
return stateObj.attributes.attribution; return window.hassUtil.computeDisplayAttributes(stateObj, FILTER_KEYS);
}, },
computeCurrentWeather: function (stateObj) { formatAttribute: function (attribute) {
return Object.keys(stateObj.attributes).filter(function (key) { return window.hassUtil.formatAttribute(attribute);
return ['attribution', 'forecast', 'friendly_name'].indexOf(key) === -1; },
}).map(function (key) {
return { formatAttributeValue: function (stateObj, attribute) {
key: key, return window.hassUtil.formatAttributeValue(stateObj, attribute);
value: stateObj.attributes[key] },
};
}); computeAttribution: function (stateObj) {
return window.hassUtil.computeAttribution(stateObj);
}, },
getDataArray: function () { getDataArray: function () {
@ -134,4 +141,5 @@
}); });
}, },
}); });
</script> }());
</script>

View File

@ -9,15 +9,21 @@
.data-entry .value { .data-entry .value {
max-width: 200px; max-width: 200px;
} }
.attribution {
color: var(--secondary-text-color);
text-align: right;
}
</style> </style>
<div class='layout vertical'> <div class='layout vertical'>
<template is='dom-repeat' items="[[computeDisplayAttributes(stateObj)]]" as="attribute"> <template is='dom-repeat' items="[[computeDisplayAttributes(stateObj)]]" as="attribute">
<div class='data-entry layout justified horizontal'> <div class='data-entry layout justified horizontal'>
<div class='key'>[[formatAttribute(attribute)]]</div> <div class='key'>[[formatAttribute(attribute)]]</div>
<div class='value'>[[getAttributeValue(stateObj, attribute)]]</div> <div class='value'>[[formatAttributeValue(stateObj, attribute)]]</div>
</div> </div>
</template> </template>
<div class='attribution' hidden$='[[!computeAttribution(stateObj)]]'>[[computeAttribution(stateObj)]]</div>
</div> </div>
</template> </template>
</dom-module> </dom-module>
@ -29,7 +35,7 @@
var FILTER_KEYS = [ var FILTER_KEYS = [
'entity_picture', 'friendly_name', 'icon', 'unit_of_measurement', 'entity_picture', 'friendly_name', 'icon', 'unit_of_measurement',
'emulated_hue', 'emulated_hue_name', 'haaska_hidden', 'haaska_name', 'emulated_hue', 'emulated_hue_name', 'haaska_hidden', 'haaska_name',
'homebridge_hidden', 'homebridge_name', 'homebridge_hidden', 'homebridge_name', 'attribution',
]; ];
Polymer({ Polymer({
@ -42,22 +48,19 @@
}, },
computeDisplayAttributes: function (stateObj) { computeDisplayAttributes: function (stateObj) {
if (!stateObj) { return window.hassUtil.computeDisplayAttributes(stateObj, FILTER_KEYS);
return [];
}
return Object.keys(stateObj.attributes).filter(
function (key) { return FILTER_KEYS.indexOf(key) === -1; });
}, },
formatAttribute: function (attribute) { formatAttribute: function (attribute) {
return attribute.replace(/_/g, ' '); return window.hassUtil.formatAttribute(attribute);
}, },
getAttributeValue: function (stateObj, attribute) { formatAttributeValue: function (stateObj, attribute) {
var value = stateObj.attributes[attribute]; return window.hassUtil.formatAttributeValue(stateObj, attribute);
},
return Array.isArray(value) ? value.join(', ') : value; computeAttribution: function (stateObj) {
return window.hassUtil.computeAttribution(stateObj);
}, },
}); });
}()); }());

View File

@ -341,4 +341,27 @@ window.hassUtil.stateIcon = function (state) {
return window.hassUtil.domainIcon(state.domain, state.state); return window.hassUtil.domainIcon(state.domain, state.state);
}; };
window.hassUtil.computeDisplayAttributes = function (stateObj, filterKeys) {
if (!stateObj) {
return [];
}
return Object.keys(stateObj.attributes).filter(
function (key) { return filterKeys.indexOf(key) === -1; });
};
window.hassUtil.formatAttribute = function (attribute) {
return attribute.replace(/_/g, ' ');
};
window.hassUtil.formatAttributeValue = function (stateObj, attribute) {
var value = stateObj.attributes[attribute];
return Array.isArray(value) ? value.join(', ') : value;
};
window.hassUtil.computeAttribution = function (stateObj) {
return stateObj.attributes.attribution;
};
</script> </script>