Skip to content

Commit

Permalink
feat(masthead-contact): improve interactions with contact module (car…
Browse files Browse the repository at this point in the history
…bon-design-system#9902)

Closes carbon-design-system#9369

[ADCMS-2602](https://jsw.ibm.com/browse/ADCMS-2602)

- Masthead container adds an event listener to the Document for `cm-app-ready` from the Contact Module ([source](https://github.ibm.com/live-advisor/cm-app/blob/943dfbcac5b8efde05a00c4c5558947495f96a58/js/helpers/documentEvents.js#L7))
- Stores reference to `window.CM_APP` internally to DDSMastheadContainer
- Uses internal reference to CM_APP to close with `.init()` method

**Changed**

- Improved UX between masthead and contact module
  • Loading branch information
andy-blum authored and jkaeser committed Apr 6, 2023
1 parent 25c9be8 commit 0527d92
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1284,7 +1284,23 @@ export default {
}
});
}
return story();
return html`
${story()}
<script>
window.digitalData.page.pageInfo.ibm.contactModuleConfiguration = {
contactInformationBundleKey: {
focusArea: 'Cloud - Automation - All',
languageCode: 'en',
regionCode: 'US',
},
contactModuleTranslationKey: {
languageCode: 'en',
regionCode: 'US',
},
};
</script>
<script src="//www.ibm.com/common/digitaladvisor/cm-app/latest/cm-app.min.js" defer></script>
`;
},
],
parameters: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,23 @@ export default {
}
});
}
return story();
return html`
${story()}
<script>
window.digitalData.page.pageInfo.ibm.contactModuleConfiguration = {
contactInformationBundleKey: {
focusArea: 'Cloud - Automation - All',
languageCode: 'en',
regionCode: 'US',
},
contactModuleTranslationKey: {
languageCode: 'en',
regionCode: 'US',
},
};
</script>
<script src="//www.ibm.com/common/digitaladvisor/cm-app/latest/cm-app.min.js" defer></script>
`;
},
],
parameters: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,27 @@ export enum NAV_ITEMS_RENDER_TARGET {
LEFT_NAV = 'left-nav',
}

/**
* Globally-scoped Contact Module variable.
*
* @see https://github.ibm.com/live-advisor/cm-app
*/
export interface CMApp {
version: string;
ready: boolean;
init: Function;
refresh: Function;
register: Function;
deregister: Function;
fireEvent: Function;
update: Function;
props: {
eventHandlers: any;
events: CustomEvent[];
getLoadedBundle: Function;
};
}

/**
* Component that renders masthead from links, etc. data.
*
Expand Down Expand Up @@ -744,6 +765,18 @@ class DDSMastheadComposite extends HostListenerMixin(LitElement) {
resolveFn();
};

/**
* Stores a reference of the globally-scoped CM_APP object within the masthead.
*/
@HostListener('document:cm-app-ready')
// @ts-ignore: The decorator refers to this method but TS thinks this method is not referred to
protected _getContactModuleReference = () => {
// @ts-ignore: CM_APP will definitely exist if this event is fired
this.contactModuleApp = window.CM_APP;
};

contactModuleApp?: CMApp;

/**
* Whether or not a nav item has automatically been designated as "selected".
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @license
*
* Copyright IBM Corp. 2022
* Copyright IBM Corp. 2022, 2023
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
Expand All @@ -14,6 +14,8 @@ import Chat20 from 'carbon-web-components/es/icons/chat/20.js';
import ddsSettings from '../../internal/vendor/@carbon/ibmdotcom-utilities/utilities/settings/settings';
import styles from './masthead.scss';
import DDSMastheadProfile from './masthead-profile';
import DDSMastheadContainer from './masthead-container';
import { CMApp } from './masthead-composite';

const { prefix } = settings;
const { stablePrefix: ddsPrefix } = ddsSettings;
Expand Down Expand Up @@ -45,6 +47,23 @@ class DDSMastheadContact extends DDSMastheadProfile {
`;
}

updated(changedProperties) {
if (changedProperties.has('expanded')) {
if (!this.expanded) {
const mastheadContainer = this.closest(`${ddsPrefix}-masthead-container`) as DDSMastheadContainer;

/**
* This is a workaround to minimize the chat module. Currently no minimize methods exist.
*
* @see https://github.ibm.com/live-advisor/cm-app
*/
if (mastheadContainer.contactModuleApp) {
(mastheadContainer.contactModuleApp as CMApp).init();
}
}
}
}

static styles = styles; // `styles` here is a `CSSResult` generated by custom WebPack loader
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import DDSMegaMenu from './megamenu';
import DDSTopNav from './top-nav';
import DDSTopNavMenu from './top-nav-menu';
import DDSMegaMenuOverlay from './megamenu-overlay';
import DDSMastheadContainer from './masthead-container';
import { CMApp } from './masthead-composite';
import styles from './masthead.scss';
import { carbonElement as customElement } from '../../internal/vendor/@carbon/web-components/globals/decorators/carbon-element';

Expand Down Expand Up @@ -201,6 +203,10 @@ class DDSMegaMenuTopNavMenu extends DDSTopNavMenu {
}
);

const mastheadContainer = this.closest(`
${ddsPrefix}-masthead-container,
${ddsPrefix}-cloud-masthead-container`) as DDSMastheadContainer;

// add the scrollbar width as right-margin to prevent content from shifting when
// scrollbar disappears on megamenu expand
const masthead: HTMLElement | null = doc.querySelector('dds-masthead');
Expand All @@ -222,6 +228,14 @@ class DDSMegaMenuTopNavMenu extends DDSTopNavMenu {
(this.parentElement as DDSTopNav).importedMegamenu = true;
}

/**
* This is a workaround to minimize the chat module. Currently no minimize methods exist.
*
* @see https://github.ibm.com/live-advisor/cm-app
*/
if (mastheadContainer.contactModuleApp) {
(mastheadContainer.contactModuleApp as CMApp).init();
}
// Ask masthead-composite to render megamenu.
// Pause further execution until the render is complete.
await this._requestMegaMenuRenderUpdate();
Expand Down

0 comments on commit 0527d92

Please sign in to comment.