Skip to content

Commit f7b4080

Browse files
committed
refactor: add required deps to panels-util; fix JS errors from undefined variables; refactor, clean up logic that opens/closes the active code panel
1 parent ceff63e commit f7b4080

File tree

1 file changed

+24
-21
lines changed

1 file changed

+24
-21
lines changed

packages/uikit-workshop/src/js/panels-util.js renamed to packages/uikit-workshop/src/scripts/components/panels-util.js

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,17 @@
99
*
1010
*/
1111

12-
var panelsUtil = {
12+
import { urlHandler } from './utils';
13+
14+
export const panelsUtil = {
1315
/**
1416
* Add click events to the template that was rendered
1517
* @param {String} the rendered template for the modal
1618
* @param {String} the pattern partial for the modal
1719
*/
1820
addClickEvents: function(templateRendered, patternPartial) {
1921
var els = templateRendered.querySelectorAll('.pl-js-tab-link');
20-
for (var i = 0; i < els.length; ++i) {
22+
for (let i = 0; i < els.length; ++i) {
2123
els[i].onclick = function(e) {
2224
e.preventDefault();
2325

@@ -36,32 +38,33 @@ var panelsUtil = {
3638
* @param {String} the ID of the panel to be shown
3739
*/
3840
show: function(patternPartial, panelID) {
39-
var els;
41+
const activeTabClass = 'pl-is-active-tab';
4042

4143
// turn off all of the active tabs
42-
els = document.querySelectorAll(
43-
'#pl-' + patternPartial + '-tabs .pl-js-tab-link'
44-
);
45-
for (i = 0; i < els.length; ++i) {
46-
els[i].classList.remove('pl-is-active-tab');
47-
}
44+
const allTabLinks = document.querySelectorAll(`.pl-js-tab-link`);
4845

4946
// hide all of the panels
50-
els = document.querySelectorAll(
51-
'#pl-' + patternPartial + '-panels .pl-js-tab-panel'
47+
const allTabPanels = document.querySelectorAll(`.pl-js-tab-panel`);
48+
49+
// tabLink about to become active
50+
const activeTabLink = document.querySelector(
51+
`#pl-${patternPartial}-${panelID}-tab`
5252
);
53-
for (i = 0; i < els.length; ++i) {
54-
els[i].classList.remove('pl-is-active-tab');
53+
54+
// tabPanelabout to become active
55+
const activeTabPanel = document.querySelector(
56+
`#pl-${patternPartial}-${panelID}-panel`
57+
);
58+
59+
for (let i = 0; i < allTabLinks.length; ++i) {
60+
allTabLinks[i].classList.remove(activeTabClass);
5561
}
5662

57-
// add active tab class
58-
document
59-
.getElementById('pl-' + patternPartial + '-' + panelID + '-tab')
60-
.classList.add('pl-is-active-tab');
63+
for (let i = 0; i < allTabPanels.length; ++i) {
64+
allTabPanels[i].classList.remove(activeTabClass);
65+
}
6166

62-
// show the panel
63-
document
64-
.getElementById('pl-' + patternPartial + '-' + panelID + '-panel')
65-
.classList.add('pl-is-active-tab');
67+
activeTabLink.classList.add(activeTabClass);
68+
activeTabPanel.classList.add(activeTabClass);
6669
},
6770
};

0 commit comments

Comments
 (0)