Files
ha-frontend-cdce8p/panels/config/script/ha-config-script.html
T
Adam Mills fb0b1286d2 Convert remaining elements to ES6 classes (#538)
* Convert remaining elements to ES6 classes

* Use native DOM methods for tests

* Fix Polymer 2 debounce call
2017-10-29 21:47:03 -07:00

124 lines
2.9 KiB
HTML

<link rel="import" href="../../../bower_components/polymer/polymer-element.html">
<link rel='import' href='../../../bower_components/app-route/app-route.html'>
<link rel="import" href="./ha-script-picker.html">
<link rel="import" href="./ha-script-editor.html">
<dom-module id="ha-config-script">
<template>
<style>
ha-script-picker,
ha-script-editor {
height: 100%;
}
</style>
<app-route
route='[[route]]'
pattern='/edit/:script'
data="{{_routeData}}"
active="{{_edittingScript}}"
></app-route>
<app-route
route='[[route]]'
pattern='/new'
active="{{_creatingNew}}"
></app-route>
<template is='dom-if' if='[[!showEditor]]'>
<ha-script-picker
narrow='[[narrow]]'
show-menu='[[showMenu]]'
scripts='[[scripts]]'
is-wide='[[isWide]]'
></ha-script-picker>
</template>
<template is='dom-if' if='[[showEditor]]' restamp>
<ha-script-editor
hass='[[hass]]'
script='[[script]]'
is-wide='[[isWide]]'
creating-new='[[_creatingNew]]'
></ha-script-editor>
</template>
</template>
</dom-module>
<script>
class HaConfigScript extends Polymer.Element {
static get is() { return 'ha-config-script'; }
static get properties() {
return {
hass: Object,
narrow: Boolean,
showMenu: Boolean,
route: Object,
isWide: Boolean,
_routeData: Object,
_routeMatches: Boolean,
_creatingNew: Boolean,
_edittingScript: Boolean,
scripts: {
type: Array,
computed: 'computeScripts(hass)',
},
script: {
type: Object,
computed: 'computeScript(scripts, _edittingScript, _routeData)',
},
showEditor: {
type: Boolean,
computed: 'computeShowEditor(_edittingScript, _creatingNew)',
}
};
}
computeScript(scripts, edittingAddon, routeData) {
if (!scripts || !edittingAddon) {
return null;
}
for (var i = 0; i < scripts.length; i++) {
if (scripts[i].entity_id === routeData.script) {
return scripts[i];
}
}
return null;
}
computeScripts(hass) {
var scripts = [];
Object.keys(hass.states).forEach(function (key) {
var entity = hass.states[key];
if (window.hassUtil.computeDomain(entity) === 'script') {
scripts.push(entity);
}
});
return scripts.sort(function entitySortBy(entityA, entityB) {
var nameA = window.hassUtil.computeStateName(entityA);
var nameB = window.hassUtil.computeStateName(entityB);
if (nameA < nameB) {
return -1;
}
if (nameA > nameB) {
return 1;
}
return 0;
});
}
computeShowEditor(_edittingScript, _creatingNew) {
return _creatingNew || _edittingScript;
}
}
customElements.define(HaConfigScript.is, HaConfigScript);
</script>