Skip to content

Commit

Permalink
Use more robust method for inserting buttons into entity rows
Browse files Browse the repository at this point in the history
  • Loading branch information
jcwillox committed Dec 14, 2021
1 parent c6e769c commit a71f76d
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions src/generic-entity-row.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,48 @@ const ELEMENT = "hui-generic-entity-row";

customElements.whenDefined(ELEMENT).then(() => {
const Element = customElements.get(ELEMENT);
if (!Element) return;
const oFirstUpdated = Element.prototype.firstUpdated;

Element.prototype.firstUpdated = function (changedProperties) {
oFirstUpdated.call(this, changedProperties);

if (this.config.extend_paper_buttons_row) {
let paperButtons = createEntityRow({
const paperButtons = createEntityRow({
type: "custom:paper-buttons-row",
...this.config.extend_paper_buttons_row
});

provideHass(paperButtons);

let el = this.shadowRoot.querySelector("slot");
if (el.parentElement) {
if (el.parentElement.classList.contains("text-content")) {
el = el.parentElement;
} else {
console.error("unexpected parent node found");
}
}

if (this.config.extend_paper_buttons_row.position === "right") {
this.shadowRoot.appendChild(paperButtons);
insertAfter(paperButtons, el);
} else {
this.shadowRoot.insertBefore(
paperButtons,
this.shadowRoot.lastElementChild
);
insertBefore(paperButtons, el);
}
}
};

fireEvent("ll-rebuild", {});
});

function insertBefore(node, element) {
element.parentNode.insertBefore(node, element);
}

function insertAfter(node, element) {
if (element.nextElementSibling) {
insertBefore(node, element.nextElementSibling);
} else {
element.parentNode.appendChild(node);
}
}

0 comments on commit a71f76d

Please sign in to comment.