getDependentTabs
This method returns the list of child tabs or dependent tabs of a tab. Use this API to track all the child tabs opened from a parent tab.
This API is an exposed over tabContext
object.
Here's the syntax:
getDependentTabs(): Promise<ITabContext[]>;
Here's a TypeScript example of the getDependentTabs
.
/// <reference path="uiEventsFramework.d.ts"/>
const frameworkProvider: IUiEventsFrameworkProvider = await CX_SVC_UI_EVENTS_FRAMEWORK.uiEventsFramework.initialize('MyFirstExtensionID', 'v1');
const currentTabContext: ITabContext = await frameworkProvider.getCurrentBrowserTabContext();
const childTabContexts: ITabContext[] = await currentTabContext.getDependentTabs();
Here's a JavaScript example the getCurrentBrowserTabContext
.
const frameworkProvider = await CX_SVC_UI_EVENTS_FRAMEWORK.uiEventsFramework.initialize('MyFirstExtensionID', 'v1');
const currentTabContext = await frameworkProvider.getCurrentBrowserTabContext();
const childTabContexts = await currentTabContext.getDependentTabs();
Example
Here's a code sample which recursively finds out a tabContext's
child tabs and inner child tabs and closes them:
async function getChildTabs(selectedTab, grandChildren) {
return new Promise(async (resolve, reject) => {
if (!grandChildren) {
grandChildren = [];
childTabsInitiatorPromiseResolve = resolve;
}
const newChildTabs = await selectedTab.getDependentTabs();
grandChildren = grandChildren.concat(newChildTabs);
newChildTabs.forEach(async (child) => {
await getChildTabs(child, grandChildren);
});
if (newChildTabs.length === 0) {
childTabsInitiatorPromiseResolve(grandChildren);
}
})
}
// Function to close the child tabs
function closeChildTabs(extAction) {
getChildTabs(selectedTabContext).then((childTabs) => {
if (childTabs) {
const payload = uiEventsFrameworkInstance.requestHelper.createPublishRequest('cxEventBusCloseTabOperation');
setCallbackCounter(extAction, 'call');
childTabs.forEach((selectedChildTabContext, i) => {
selectedChildTabContext.publish(payload).then((message) => {
childTabsList.push = message.responseDetails.data.payload.tabId;
if (i == childTabs.length - 1) {
document.getElementById('allActionsResults').innerHTML = `<div><span id="tabId"> Closed Tabs: ${childTabsList}</span></div>`;
}
}).catch((error) => {
console.log(error.message);
});
})
}
})
}