Skip to content

Commit ea7300c

Browse files
committed
refactor: Removing origin and tracker fields from HelpUrlProps and updating its uses to instead use trackerBaseURL
1 parent 05e3670 commit ea7300c

File tree

1 file changed

+22
-26
lines changed

1 file changed

+22
-26
lines changed

html/classhelper.js

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22
* Properties for the ClassHelper component,
33
* made into a type for better readability.
44
* @typedef {Object} HelpUrlProps
5-
* The qualified domain name with protocol and port(if any)
6-
* @property {string} origin
7-
* The tracker name from the url
8-
* @property {string} tracker
95
* Type of data that needs to be shown (eg. issue, user, keywords) parsed from helpurl
106
* @property {string} apiClassName
117
* @property {number} width // width of the popup window
@@ -86,8 +82,7 @@ class ClassHelper extends HTMLElement {
8682
* Stores the result from api calls made to rest api,
8783
* for the parameters in searchWith attribute of this web component
8884
* where a parameter is defined as a dropdown in
89-
* @type {Object.<string, Object.<string, string>>}
90-
* */
85+
* @type {Object.<string, Object.<string, string>>} */
9186
dropdowns = null;
9287

9388
/** @type {HTMLAnchorElement} */
@@ -100,6 +95,13 @@ class ClassHelper extends HTMLElement {
10095
/** no-op function */
10196
preventDefault = e => e.preventDefault();
10297

98+
/**
99+
* The qualified domain name with protocol and port(if any)
100+
* with the tracker name if any.
101+
* eg. http://localhost:8080/demo or https://demo.roundup-tracker.org
102+
* @type {string} */
103+
trackerBaseURL = null;
104+
103105
connectedCallback() {
104106
/** @type {URL} */
105107
let apiURL;
@@ -112,7 +114,10 @@ class ClassHelper extends HTMLElement {
112114
this.helpurl.setAttribute("onclick", "");
113115
this.helpurl.addEventListener("click", this.preventDefault);
114116

115-
this.helpurlProps = ClassHelper.parseHelpUrlData(this.helpurl);
117+
this.helpurlProps = ClassHelper.parseHelpUrlProps(this.helpurl);
118+
119+
this.trackerBaseURL = window.location.href.substring(0, window.location.href.lastIndexOf("/"));
120+
116121
} catch (err) {
117122
console.warn("Classhelper not intercepting helpurl.");
118123
if (this.helpurl != null) {
@@ -124,8 +129,7 @@ class ClassHelper extends HTMLElement {
124129
}
125130

126131
try {
127-
this.helpurlProps = ClassHelper.parseHelpUrlData(this.helpurl);
128-
apiURL = ClassHelper.getRestURL(this.helpurlProps);
132+
apiURL = ClassHelper.getRestURL(this.trackerBaseURL, this.helpurlProps);
129133
} catch (e) {
130134
// Failed parsing props -> reset, log and return.
131135
this.helpurl.removeEventListener("click", this.preventDefault);
@@ -182,7 +186,7 @@ class ClassHelper extends HTMLElement {
182186

183187
const handleSearchEvent = (event) => {
184188
this.helpurlProps.pageIndex = 1;
185-
const searchURL = ClassHelper.getSearchURL(this.helpurlProps, event.detail.value);
189+
const searchURL = ClassHelper.getSearchURL(this.trackerBaseURL, this.helpurlProps, event.detail.value);
186190
this.searchEvent(searchURL, this.helpurlProps).catch(error => {
187191
// Top level error handling for searchEvent method.
188192
this.removeEventListener("search", handleSearchEvent);
@@ -275,7 +279,7 @@ class ClassHelper extends HTMLElement {
275279
const splitResult = param.split("[]");
276280
param = splitResult[0];
277281
const sortOrder = splitResult[1];
278-
let url = `${props.origin}/${props.tracker}/rest/data/${param}?@fields=id,name`;
282+
let url = `${this.trackerBaseURL}/rest/data/${param}?@fields=id,name`;
279283
if (sortOrder) {
280284
url += `&@sort=${sortOrder}`;
281285
}
@@ -342,7 +346,7 @@ class ClassHelper extends HTMLElement {
342346
* @param {HTMLAnchorElement} link
343347
* @returns {HelpUrlProps}
344348
*/
345-
static parseHelpUrlData(link) {
349+
static parseHelpUrlProps(link) {
346350
const width = parseInt(link.dataset.width);
347351
if (isNaN(width)) {
348352
throw new Error("width in helpurl must be a number");
@@ -381,12 +385,6 @@ class ClassHelper extends HTMLElement {
381385
const sort = searchParams.get("@sort")?.split(",");
382386
const fields = searchParams.get("properties")?.split(",");
383387

384-
const origin = window.location.origin;
385-
const tracker = window.location.pathname.split('/')[1];
386-
if (!tracker) {
387-
throw new Error("error parsing tracker name from window url");
388-
}
389-
390388
return {
391389
width,
392390
height,
@@ -397,9 +395,7 @@ class ClassHelper extends HTMLElement {
397395
pageIndex,
398396
pageSize,
399397
sort,
400-
fields,
401-
origin,
402-
tracker
398+
fields
403399
}
404400
}
405401

@@ -410,9 +406,9 @@ class ClassHelper extends HTMLElement {
410406
* @returns {URL}
411407
* @throws {Error}
412408
*/
413-
static getRestURL(props) {
409+
static getRestURL(trackerBaseURL, props) {
414410
const restDataPath = "rest/data";
415-
const base = props.origin + "/" + props.tracker + "/" + restDataPath + "/" + props.apiClassName;
411+
const base = trackerBaseURL + "/" + restDataPath + "/" + props.apiClassName;
416412
let url = new URL(base);
417413

418414
url.searchParams.append("@page_index", props.pageIndex);
@@ -428,8 +424,8 @@ class ClassHelper extends HTMLElement {
428424
return url;
429425
}
430426

431-
static getSearchURL(props, formData) {
432-
const url = new URL(ClassHelper.getRestURL(props).toString());
427+
static getSearchURL(trackerBaseURL, props, formData) {
428+
const url = new URL(ClassHelper.getRestURL(trackerBaseURL, props).toString());
433429
for (let entry of formData.entries()) {
434430
if (entry[1] != null && entry[1] != "") {
435431
url.searchParams.append(entry[0], entry[1]);
@@ -811,7 +807,7 @@ class ClassHelper extends HTMLElement {
811807
const css = popupDocument.createElement("link");
812808
css.rel = "stylesheet";
813809
css.type = "text/css";
814-
css.href = props.origin + '/' + props.tracker + '/' + CSS_FILE_NAME;
810+
css.href = this.trackerBaseURL + '/' + CSS_FILE_NAME;
815811
popupHead.appendChild(css);
816812

817813
popupBody.classList.add("flex-container");

0 commit comments

Comments
 (0)