-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhelper.tsx
More file actions
206 lines (180 loc) · 7.21 KB
/
helper.tsx
File metadata and controls
206 lines (180 loc) · 7.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import React from 'react';
import ReactDOMServer from 'react-dom/server';
import { IconContext } from '@react-icons/all-files';
import { BiLinkExternal } from '@react-icons/all-files/bi/BiLinkExternal';
import { IoIosArrowForward } from '@react-icons/all-files/io/IoIosArrowForward';
import { RiArrowDownSLine } from '@react-icons/all-files/ri/RiArrowDownSLine';
import selectors from '../../constants/selectorsContant';
export const getHTMLFromComponent = (icon: JSX.Element, iconClass?: string) => {
return ReactDOMServer.renderToStaticMarkup(
<IconContext.Provider value={{ className: `icon ${iconClass}` }}>
{icon}
</IconContext.Provider>,
);
};
const ArrowForwardHTML = getHTMLFromComponent(
<IoIosArrowForward />,
'forwardArrowIcon',
);
const ArrowDownHTML = getHTMLFromComponent(
<RiArrowDownSLine />,
'downArrowIcon',
);
export const addExpandCollapseImages = (
navContent: string,
pageId: string,
tabsClosed: { [key: string]: boolean },
) => {
const nav = document.createElement('div');
nav.innerHTML = navContent;
nav.classList.add('navWrapper');
nav.querySelectorAll('li').forEach((el, i) => {
if (el.children.length === 2) {
const paragraphElement = el.children[0];
if (paragraphElement.childNodes.length < 2) {
paragraphElement.classList.add('linkTitle');
const text = (paragraphElement as HTMLParagraphElement)
.innerText;
// Creating arrow icons to be added
const spanElementParent = document.createElement('span');
spanElementParent.classList.add('iconSpan');
const spanElementChild = document.createElement('span');
if (tabsClosed[text] === undefined || !tabsClosed[text]) {
spanElementChild.innerHTML = ArrowDownHTML;
} else {
spanElementChild.innerHTML = ArrowForwardHTML;
el.children[1].classList.add('displayNone');
}
// Checking if this div contains the active link
const allLinks = el.children[1].querySelectorAll('a');
for (let i = 0; i < allLinks.length; i++) {
const splitArr = allLinks[i].href.split('=');
if (splitArr.length > 1 && splitArr[1] === pageId) {
spanElementChild.innerHTML = ArrowDownHTML;
el.children[1].classList.remove('displayNone');
break;
}
}
// Adding arrow icon to the p tags
spanElementParent.appendChild(spanElementChild);
paragraphElement.appendChild(spanElementParent);
}
}
});
nav.innerHTML = addExternalLinkIcon(nav.innerHTML);
return nav.innerHTML;
};
export const trimTrailingSlash = (str: string) => str.replace(/\/*$/, '');
export const getPageIdFromUrl = (href: string) => {
const pageidMatches = href.match(/pageid=([A-z-0-9]*)/);
const pageid =
pageidMatches && pageidMatches.length > 1 && pageidMatches[1];
return pageid;
};
const isLinkMatching = (href: string, curLocation: Location) => {
const hostUrl = `${window.location.protocol}//${window.location.host}`;
if (href === trimTrailingSlash(hostUrl + window.location.pathname))
return true;
const pageid = getPageIdFromUrl(curLocation.href);
const newUrl = `${hostUrl}/${pageid}`;
return href === newUrl;
};
const isCurrentNavOpen = (liEle: HTMLLIElement) => {
const paraEle = liEle.children[0] as HTMLParagraphElement;
const divEle = liEle.children[1] as HTMLDivElement;
const isLinkParentOpen =
paraEle &&
isLinkMatching(
(paraEle.children[0] as HTMLAnchorElement).href,
window.location,
);
const isChildOpen: boolean =
divEle &&
Array.from(divEle.children[0].children)
.map((childLiEle): boolean => {
return isCurrentNavOpen(childLiEle as HTMLLIElement);
})
.reduce((prev, cur) => {
return prev || cur;
}, false);
return isLinkParentOpen || isChildOpen;
};
export const collapseAndExpandLeftNav = (
doc: HTMLDivElement,
setLeftNavOpen: Function,
toggleExpandOnTab: Function,
) => {
// Adding click listener to close left nav when in mobile resolution
doc.querySelectorAll(selectors.links).forEach((link) => {
link.addEventListener('click', () => {
setLeftNavOpen(false);
});
});
doc.querySelectorAll('li').forEach((el, i) => {
if (el.children.length === 2) {
const spanElement =
el.children[0].children.length === 2
? el.children[0].children[1]
: el.children[0].children[0];
const isOpen = isCurrentNavOpen(el);
const divElement = el.children[1];
if (!isOpen) {
divElement.classList.toggle('displayNone');
}
if (spanElement) {
(spanElement
.children[0] as HTMLImageElement).innerHTML = divElement.classList.contains(
'displayNone',
)
? ArrowForwardHTML
: ArrowDownHTML;
// Adding click listener to the headings
spanElement.addEventListener('click', () => {
toggleExpandOnTab(
(el.children[0] as HTMLParagraphElement).innerText,
);
divElement.classList.toggle('displayNone');
(spanElement
.children[0] as HTMLImageElement).innerHTML = divElement.classList.contains(
'displayNone',
)
? ArrowForwardHTML
: ArrowDownHTML;
});
}
}
});
};
export const getAllPageIds = (navContent: string): string[] => {
const divElement = document.createElement('div');
divElement.innerHTML = navContent;
const allPageIds: string[] = [];
divElement.querySelectorAll('a').forEach((link: HTMLAnchorElement) => {
const splitArr = link.href.split('?');
if (splitArr.length > 1) {
const urlParams = new URLSearchParams(splitArr[1]);
const pageId = urlParams.get('pageid');
if (pageId) {
allPageIds.push(pageId);
}
}
});
return allPageIds;
};
// Adding external icon to the external links
const addExternalLinkIcon = (navContent: string): string => {
const divElement = document.createElement('div');
divElement.innerHTML = navContent;
divElement.querySelectorAll('a[target="_blank"]').forEach((link) => {
const tempElement = document.createElement('span');
tempElement.innerHTML = ReactDOMServer.renderToStaticMarkup(
<IconContext.Provider
value={{ className: 'icon externalLinkIcon' }}
>
<BiLinkExternal />
</IconContext.Provider>,
);
link.appendChild(tempElement);
});
return divElement.innerHTML;
};