2020-10-30 21:22:58 +00:00
|
|
|
import { computed, get } from '@ember/object';
|
2020-10-13 16:50:28 +00:00
|
|
|
import { htmlSafe } from '@ember/template';
|
2017-09-19 14:47:10 +00:00
|
|
|
|
|
|
|
// An Ember.Computed property for transforming an object into an
|
|
|
|
// html compatible style attribute
|
|
|
|
//
|
|
|
|
// ex. styleProps: { color: '#FF0', border-width: '1px' }
|
|
|
|
// styleStr: styleStringProperty('styleProps') // color:#FF0;border-width:1px
|
|
|
|
export default function styleStringProperty(prop) {
|
2021-12-28 14:45:20 +00:00
|
|
|
return computed(prop, function () {
|
2020-10-30 21:22:58 +00:00
|
|
|
const styles = get(this, prop);
|
2017-09-19 14:47:10 +00:00
|
|
|
let str = '';
|
|
|
|
|
|
|
|
if (styles) {
|
|
|
|
str = Object.keys(styles)
|
2021-12-28 14:45:20 +00:00
|
|
|
.reduce(function (arr, key) {
|
2017-09-19 14:47:10 +00:00
|
|
|
const val = styles[key];
|
|
|
|
arr.push(key + ':' + (typeof val === 'number' ? val.toFixed(2) + 'px' : val));
|
|
|
|
return arr;
|
|
|
|
}, [])
|
|
|
|
.join(';');
|
|
|
|
}
|
|
|
|
|
2020-10-13 16:50:28 +00:00
|
|
|
return htmlSafe(str);
|
2017-09-19 14:47:10 +00:00
|
|
|
});
|
|
|
|
}
|