Skip to content

Commit 4c836c5

Browse files
committed
refactor: changes recommended in review
1 parent 429824e commit 4c836c5

File tree

1 file changed

+27
-22
lines changed

1 file changed

+27
-22
lines changed

html/classhelper.js

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,10 @@ class ClassHelper extends HTMLElement {
180180

181181
const handlePopupReadyEvent = (event) => {
182182
// we get a document Fragment in event.detail we replace it with the root
183-
// Assuming that browser implementors are smart enough to not replace the same document
184-
this.popupRef.document.replaceChild(event.detail, this.popupRef.document.documentElement);
183+
// replaceChild method consumes the documentFragment content, subsequent calls will be no-op.
184+
if (e.detail.childElementCount === 1) {
185+
this.popupRef.document.replaceChild(event.detail, this.popupRef.document.documentElement);
186+
}
185187
}
186188

187189
const handleNextPageEvent = (event) => {
@@ -896,22 +898,21 @@ class ClassHelper extends HTMLElement {
896898
nextPageURL = links.next[0].uri;
897899
}
898900

899-
const itemDesignator = window.location.pathname.split("/").at(-1);
900-
let title = `${itemDesignator} - Classhelper`;
901-
902901
if (props.formProperty) {
903902
// Find preselected values
904903
const input = document.getElementsByName(props.formProperty).item(0);
905904
if (input?.value) {
906905
preSelectedValues = input.value.split(',');
907906
}
908-
const label = document.getElementsByName(props.formProperty).item(0).parentElement.previousElementSibling;
909-
title = label.textContent + " - " + title;
910907
}
911908

912909
const popupFeatures = CLASSHELPER_POPUP_FEATURES(props.width, props.height);
913910
this.popupRef = window.open(CLASSHELPER_POPUP_URL, CLASSHELPER_POPUP_TARGET, popupFeatures);
914911

912+
if (this.popupRef == null) {
913+
throw new Error("Failed to open popup window");
914+
}
915+
915916
// Create the popup root level page
916917
const page = document.createDocumentFragment();
917918
const html = document.createElement("html");
@@ -920,8 +921,16 @@ class ClassHelper extends HTMLElement {
920921

921922
body.classList.add("flex-container");
922923

924+
const itemDesignator = window.location.pathname.split("/").at(-1);
925+
let titleText = `${itemDesignator} - Classhelper`;
926+
if (props.formName) {
927+
// main window lookup for the label of the form property
928+
const label = document.getElementsByName(props.formProperty).item(0).parentElement.previousElementSibling;
929+
titleText = label.textContent + " - " + titleText;
930+
}
931+
923932
const titleTag = document.createElement("title");
924-
titleTag.textContent = title;
933+
titleTag.textContent = titleText;
925934

926935
const styleSheet = document.createElement("link");
927936
styleSheet.rel = "stylesheet";
@@ -937,14 +946,13 @@ class ClassHelper extends HTMLElement {
937946
}
938947

939948
const paginationFrag = this.getPaginationFragment(prevPageURL, nextPageURL, props.pageIndex, props.pageSize, collection.length);
940-
941-
const separator = document.createElement("div");
942-
separator.classList.add("separator");
949+
body.appendChild(paginationFrag);
943950

944951
const tableFrag = this.getTableFragment(props.fields, collection, preSelectedValues, !!props.formProperty);
945-
946-
body.appendChild(paginationFrag);
947952
body.appendChild(tableFrag);
953+
954+
const separator = document.createElement("div");
955+
separator.classList.add("separator");
948956
body.appendChild(separator);
949957

950958
if (props.formProperty) {
@@ -956,21 +964,18 @@ class ClassHelper extends HTMLElement {
956964
html.appendChild(body);
957965
page.appendChild(html);
958966

959-
const popupReadyEvent = new CustomEvent("popupReady", { detail: page });
960-
961-
if (this.popupRef == null) {
962-
throw new Error("Failed to open popup window");
963-
}
967+
const dispatchPopupReady = () => this.dispatchEvent(new CustomEvent("popupReady", { detail: page }));
964968

965969
// Wait for the popup window to load, onload fire popupReady event on the classhelper
966-
this.popupRef.addEventListener("load", () => {
967-
this.dispatchEvent(popupReadyEvent);
968-
});
970+
this.popupRef.addEventListener("load", dispatchPopupReady);
969971

970972
// If load event was already fired way before the event listener was attached
971973
// we need to trigger it manually if popupRef is readyState complete
972974
if (this.popupRef.document.readyState === "complete") {
973-
this.dispatchEvent(popupReadyEvent);
975+
dispatchPopupReady();
976+
// if we did successfully trigger the event, we can remove the event listener
977+
// else wait for it to be removed with closing of popup window, this cleaning up closure
978+
this.popupRef.removeEventListener("load", dispatchPopupReady);
974979
}
975980

976981
this.popupRef.addEventListener("keydown", (e) => {

0 commit comments

Comments
 (0)