Skip to content

Commit a15e9ad

Browse files
committed
Issue #26 refactor partial update
1 parent 63022f4 commit a15e9ad

File tree

2 files changed

+47
-56
lines changed

2 files changed

+47
-56
lines changed

html/classhelper.css

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,8 @@
99

1010
.popup-table {
1111
table-layout: fixed; /* compromises quality for speed */
12-
max-height: 100px;
1312
width: 100%;
1413
font-size: .9em;
15-
padding-bottom: 3em;
1614
}
1715

1816
table th {
@@ -59,24 +57,16 @@ table tr:hover {
5957
/* Accumulator */
6058

6159
.popup-control {
62-
display: block;
63-
top: auto;
64-
right: 0;
65-
bottom: 0;
66-
left: 0;
67-
padding: .5em;
60+
display: flex;
61+
flex-wrap: wrap;
62+
justify-content: space-evenly;
63+
gap: 8px;
64+
padding: 8px;
6865
border-top: 2px solid #444;
6966
background-color: #eee;
7067
}
7168

72-
.popup-preview {
73-
margin-right: 3em;
74-
margin-left: 1em;
75-
}
76-
7769
.popup-control button {
78-
margin-right: 2em;
79-
margin-left: 2em;
8070
width: 7em;
8171
}
8272

@@ -134,7 +124,8 @@ table tr:hover {
134124
margin-left: 60px;
135125
}
136126

137-
.flexcontainer{
127+
.flex-container{
128+
margin: 0;
138129
display: flex;
139130
flex-direction: column;
140131
height: 100%;

html/classhelper.js

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,15 @@ class ClassHelper extends HTMLElement {
6868
this.addEventListener("click", openPopUpClosure);
6969

7070
const nextPageClosure = (event) => {
71-
this.pageChange(properties, properties).catch(error => {
71+
this.pageChange(event.detail.value, properties).catch(error => {
7272
// Top level error handling for nextPage method.
7373
this.removeEventListener("click", nextPageClosure);
7474
});
7575
}
7676
this.addEventListener("nextPage", nextPageClosure);
7777

7878
const prevPageClosure = (event) => {
79-
this.pageChange(properties, properties).catch(error => {
79+
this.pageChange(event.detail.value, properties).catch(error => {
8080
// Top level error handling for prevPage method.
8181
this.removeEventListener("click", prevPageClosure);
8282
});
@@ -556,26 +556,14 @@ class ClassHelper extends HTMLElement {
556556
*/
557557
async openPopUp(apiURL, props) {
558558
// Find preselected values
559-
const input = document.getElementsByName(props.formProperty)[0];
559+
const input = document.getElementsByName(props.formProperty).item(0);
560560
let preSelectedValues = [];
561561
if (input.value) {
562562
preSelectedValues = input.value.split(',');
563563
}
564564

565565
const popupFeatures = `popup=yes,width=${props.width},height=${props.height}`;
566-
const popupWindow = window.open("about:blank", "_blank", popupFeatures);
567-
this.popupRef = popupWindow;
568-
569-
// This does not create a closure as this event is only triggered once.
570-
popupWindow.addEventListener("load", (event) => {
571-
/** @type {Document} */
572-
const doc = event.target;
573-
const css = doc.createElement("link");
574-
css.rel = "stylesheet";
575-
css.type = "text/css";
576-
css.href = props.origin + '/' + props.tracker + '/' + "@@file/classhelper.css";
577-
doc.head.appendChild(css);
578-
});
566+
this.popupRef = window.open("about:blank", "_blank", popupFeatures);
579567

580568
let resp;
581569
try {
@@ -594,40 +582,52 @@ class ClassHelper extends HTMLElement {
594582
}
595583

596584
const data = json.data;
597-
let prevURL = data["@links"].prev;
598-
if (prevURL) {
599-
prevURL = prevURL[0].uri;
600-
}
585+
const links = json.data["@links"];
586+
587+
let prevPageURL, nextPageURL;
601588

602-
let nextURL = data["@links"].next;
603-
if (nextURL) {
604-
nextURL = nextURL[0].uri;
589+
if (links.prev && links.prev.length > 0) {
590+
prevPageURL = links.prev[0].uri;
605591
}
592+
if (links.next && links.next.length > 0) {
593+
nextPageURL = links.next[0].uri;
594+
}
595+
596+
const popupDocument = this.popupRef.document;
597+
const popupBody = popupDocument.body;
598+
const popupHead = popupDocument.head;
606599

607-
const container = document.createElement("div");
608-
container.setAttribute("class", "flexcontainer");
600+
// Add external classhelper css to head
601+
const css = popupDocument.createElement("link");
602+
css.rel = "stylesheet";
603+
css.type = "text/css";
604+
css.href = props.origin + '/' + props.tracker + '/' + "@@file/classhelper.css";
605+
popupHead.appendChild(css);
606+
607+
popupBody.classList.add("flex-container");
609608

610-
const b = this.popupRef.document.body;
611609
if (this.getAttribute("searchWith")) {
612-
container.appendChild(this.getSearchFragment());
610+
const searchFrag = this.getSearchFragment();
611+
popupBody.appendChild(searchFrag);
613612
}
614-
container.appendChild(this.getPaginationFragment(prevURL, nextURL, props.pageIndex, props.pageSize));
615613

616-
const tableFragment = this.getTableFragment(props.fields, data.collection, preSelectedValues);
617-
const popupTable = document.createElement("div");
618-
popupTable.appendChild(tableFragment);
619-
popupTable.setAttribute("class", "popup-table");
620-
container.appendChild(popupTable);
614+
const paginationFrag = this.getPaginationFragment(prevPageURL, nextPageURL, props.pageIndex, props.pageSize);
615+
popupBody.appendChild(paginationFrag);
616+
617+
const tableFrag = this.getTableFragment(props.fields, data.collection, preSelectedValues);
618+
popupBody.appendChild(tableFrag);
619+
621620
const separator = document.createElement("div");
622-
separator.setAttribute("class", "separator");
623-
container.appendChild(separator);
624-
container.appendChild(this.getAccumulatorFragment(preSelectedValues));
625-
b.appendChild(container);
621+
separator.classList.add("separator");
622+
popupBody.appendChild(separator);
623+
624+
const accumulatorFrag = this.getAccumulatorFragment(preSelectedValues);
625+
popupBody.appendChild(accumulatorFrag);
626626
}
627627

628628
/** method when next or previous button is clicked */
629629
async pageChange(apiURL, props) {
630-
let preSelectedValues = this.popupRef.document.getElementById("popup-preview").value.split(",");
630+
let preSelectedValues = this.popupRef.document.getElementsByClassName("popup-preview").item(0).value.split(",");
631631

632632
fetch(apiURL).then(resp => resp.json()).then(({ data }) => {
633633
const b = this.popupRef.document.body;
@@ -642,9 +642,9 @@ class ClassHelper extends HTMLElement {
642642
let selfUrl = new URL(data["@links"].self[0].uri);
643643
props.pageIndex = selfUrl.searchParams.get("@page_index");
644644

645-
let oldPagination = this.popupRef.document.getElementById("popup-pagination");
645+
let oldPagination = this.popupRef.document.getElementsByClassName("popup-pagination").item(0);
646646
oldPagination.parentElement.replaceChild(this.getPaginationFragment(prevURL, nextURL, props.pageIndex, props.pageSize), oldPagination);
647-
let oldTable = this.popupRef.document.getElementById("popup-table");
647+
let oldTable = this.popupRef.document.getElementsByClassName("popup-table").item(0);
648648
let ancestor = oldTable.parentElement.parentElement;
649649
ancestor.replaceChild(this.getTableFragment(props.fields, data.collection, preSelectedValues), oldTable.parentElement);
650650
});

0 commit comments

Comments
 (0)