PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / trunk
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages vtrunk
3.4.1 3.4.0 3.3.9 3.3.8 3.3.7 3.3.6 3.3.5 3.3.4 3.3.3 3.3.2 3.3.1 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.2.8 2.2.9 2.3.0 2.3.1 2.3.2 2.3.3 All 194 releases
convertkit / resources / backend / js / tabs.js

tabs.js in Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages trunk, at resources/backend/js/tabs.js

125 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /**
2 * Provides functionality for tabbed interfaces.
3 *
4 * @since 2.2.5
5 *
6 * @author ConvertKit
7 */
8
9 /* eslint-disable no-unused-vars */
10 /**
11 * Initializes tabbed interfaces.
12 *
13 * @since 2.2.5
14 */
15 function convertKitTabsInit() {
16 // eslint-disable-line no-unused-vars
17 // Safely call this function by destroying any previously initialized instances.
18 convertKitTabsDestroy();
19
20 // Iterate through all JS tab instances, initializing each one.
21 document
22 .querySelectorAll('.convertkit-js-tabs')
23 .forEach(function (navTabContainer) {
24 const navTabPanelsContainer =
25 navTabContainer.dataset.panelsContainer;
26 const navTabPanel = navTabContainer.dataset.panel;
27 const navTabActive = navTabContainer.dataset.active;
28
29 // Call update.
30 const activeTabElement = navTabContainer.querySelector(
31 'a.' + navTabActive
32 );
33 convertKitTabsUpdate(
34 navTabContainer,
35 navTabPanelsContainer,
36 navTabPanel,
37 navTabActive,
38 activeTabElement ? activeTabElement.getAttribute('href') : null
39 );
40
41 // Register a listener when a tab is clicked.
42 navTabContainer.addEventListener('click', function (e) {
43 if (e.target.tagName === 'A') {
44 e.preventDefault();
45
46 // Update the UI to show the active tab and content associated with it.
47 convertKitTabsUpdate(
48 navTabContainer,
49 navTabPanelsContainer,
50 navTabPanel,
51 navTabActive,
52 e.target.getAttribute('href')
53 );
54 }
55 });
56 });
57 }
58 /* eslint-enable no-unused-vars */
59
60 /**
61 * For the given active tab:
62 * - Hides all other content in the group
63 * - Shows content associated with the active tab
64 *
65 * @since 1.0.0
66 *
67 * @param {Object} navTabContainer <ul> Navigation Tab Container.
68 * @param {string} navTabPanelsContainer ID of element containing content panel, to display, which associate with the navigation tabs.
69 * @param {string} navTabPanel Class of elements containing content panels, to hide, which associate with the navigation tabs.
70 * @param {string} navTabActive Class to apply to the clicked activeTab element.
71 * @param {string} activeTab ID of <a> tab which has been selected / clicked.
72 */
73 function convertKitTabsUpdate(
74 navTabContainer,
75 navTabPanelsContainer,
76 navTabPanel,
77 navTabActive,
78 activeTab
79 ) {
80 // If we don't have an active tab at this point, we don't have any tabs, so bail.
81 if (
82 typeof activeTab === 'undefined' ||
83 activeTab === null ||
84 activeTab.length === 0
85 ) {
86 return;
87 }
88
89 // Deactivate all tabs in this container.
90 navTabContainer
91 .querySelectorAll('a')
92 .forEach((tab) => tab.classList.remove(navTabActive));
93
94 // Hide all panels in this container.
95 document
96 .querySelectorAll(navTabPanelsContainer + ' ' + navTabPanel)
97 .forEach((panel) => (panel.style.display = 'none'));
98
99 // Activate the clicked / selected tab in this container.
100 navTabContainer
101 .querySelector('a[href="' + activeTab + '"]')
102 .classList.add(navTabActive);
103
104 // Show the active tab's panels in this container.
105 document.querySelector(activeTab).style.display = 'block';
106 }
107
108 /**
109 * Destroys previously initialized tabbed interfaces, no longer
110 * listening to events.
111 *
112 * @since 1.0.0
113 */
114 function convertKitTabsDestroy() {
115 // Iterate through all JS tab instances, destroying each one.
116 document
117 .querySelectorAll('.convertkit-js-tabs')
118 .forEach(function (tabInstance) {
119 // Remove the click event listener.
120 tabInstance.removeEventListener('click', tabInstance.clickHandler);
121 // Remove the clickHandler property.
122 delete tabInstance.clickHandler;
123 });
124 }
125