github.com/hernad/nomad@v1.6.112/ui/app/utils/properties/style-string.js (about) 1 /** 2 * Copyright (c) HashiCorp, Inc. 3 * SPDX-License-Identifier: MPL-2.0 4 */ 5 6 import { computed, get } from '@ember/object'; 7 import { htmlSafe } from '@ember/template'; 8 9 // An Ember.Computed property for transforming an object into an 10 // html compatible style attribute 11 // 12 // ex. styleProps: { color: '#FF0', border-width: '1px' } 13 // styleStr: styleStringProperty('styleProps') // color:#FF0;border-width:1px 14 export default function styleStringProperty(prop) { 15 return computed(prop, function () { 16 const styles = get(this, prop); 17 let str = ''; 18 19 if (styles) { 20 str = Object.keys(styles) 21 .reduce(function (arr, key) { 22 const val = styles[key]; 23 arr.push( 24 key + ':' + (typeof val === 'number' ? val.toFixed(2) + 'px' : val) 25 ); 26 return arr; 27 }, []) 28 .join(';'); 29 } 30 31 return htmlSafe(str); 32 }); 33 }