ha-floorplan.html (continued!):
getServerMoment() {
let serverMoment = moment();
if (this.timeDifference >= 0)
serverMoment.subtract(this.timeDifference, 'milliseconds');
else
serverMoment.add(Math.abs(this.timeDifference), 'milliseconds');
return serverMoment;
},
getArray(list) {
return Array.isArray(list) ? list : Object.keys(list).map(key => list[key]);
},
assemble(code, entity, entities) {
let functionBody = (code.indexOf('return') >= 0) ? code : `return \`${code}\`;`;
let func = new Function('entity', 'entities', 'hass', 'config', functionBody);
return func(entity, entities, this.hass, this.config);
},
error(message) {
let errors = Polymer.dom(this.$.errors).node;
$(errors).find('ul').append(`<li>${message}</li>`)
$(errors).css('display', 'block');
},
warn(message) {
if ((this.config.warnings === null) || (this.config.warnings !== undefined)) {
let warnings = Polymer.dom(this.$.warnings).node;
$(warnings).find('ul').append(`<li>${message}</li>`)
$(warnings).css('display', 'block');
}
},
debug(message) {
let debug = Polymer.dom(this.$.debug).node;
$(debug).find('ul').append(`<li>${message}</li>`)
$(debug).css('display', 'block');
},
loadStyleSheet(path, fn, scope) {
let head = document.getElementsByTagName('head')[0]; // reference to document.head for appending/ removing link nodes
let link = document.createElement('link'); // create the link node
link.setAttribute('href', path);
link.setAttribute('rel', 'stylesheet');
link.setAttribute('type', 'text/css');
let sheet, cssRules;
// get the correct properties to check for depending on the browser
if ('sheet' in link) {
sheet = 'sheet'; cssRules = 'cssRules';
}
else {
sheet = 'styleSheet'; cssRules = 'rules';
}
let interval_id = setInterval(function () { // start checking whether the style sheet has successfully loaded
try {
if (link[sheet] && link[sheet][cssRules].length) { // SUCCESS! our style sheet has loaded
clearInterval(interval_id); // clear the counters
clearTimeout(timeout_id);
fn.call(scope || window, true, link); // fire the callback with success == true
}
} catch (e) { } finally { }
}, 10), // how often to check if the stylesheet is loaded
timeout_id = setTimeout(function () { // start counting down till fail
clearInterval(interval_id); // clear the counters
clearTimeout(timeout_id);
head.removeChild(link); // since the style sheet didn't load, remove the link node from the DOM
fn.call(scope || window, false, link); // fire the callback with success == false
}, 15000); // how long to wait before failing
head.appendChild(link); // insert the link node into the DOM and start loading the style sheet
return link; // return the link node;
},
rgbToHex(rgb) {
return "#" + ((1 << 24) + (rgb[0] << 16) + (rgb[1] << 8) + rgb[2]).toString(16).slice(1);
},
hexToRgb(hex) {
// Expand shorthand form (e.g. "03F") to full form (e.g. "0033FF")
let shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
hex = hex.replace(shorthandRegex, (m, r, g, b) => {
return r + r + g + g + b + b;
});
let result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16)
} : null;
},
mix(color1, color2, weight) {
let p = weight;
let w = p * 2 - 1;
let w1 = ((w / 1) + 1) / 2;
let w2 = 1 - w1;
let rgb = [
Math.round(color1.r * w1 + color2.r * w2),
Math.round(color1.g * w1 + color2.g * w2),
Math.round(color1.b * w1 + color2.b * w2)
];
return rgb;
}
});
</script>