igniteui-webcomponents
Version:
Ignite UI for Web Components is a complete library of UI components, giving you the ability to build modern web applications using encapsulation and the concept of reusable components in a dependency-free approach.
67 lines • 2.18 kB
JavaScript
import { findElementFromEventPath, isEmpty } from '../util.js';
let rootClickListenerActive = false;
const HostConfigs = new WeakMap();
const ActiveHosts = new Set();
function handleRootClick(event) {
for (const host of ActiveHosts) {
const config = HostConfigs.get(host);
if (host.keepOpenOnOutsideClick) {
continue;
}
const targets = config?.target
? new Set([host, config.target])
: new Set([host]);
if (!findElementFromEventPath((node) => targets.has(node), event)) {
config?.onHide ? config.onHide.call(host) : host.hide();
}
}
}
class RootClickController {
constructor(host, config) {
this._host = host;
this._config = config;
this._host.addController(this);
if (this._config) {
HostConfigs.set(this._host, this._config);
}
}
_addActiveHost() {
ActiveHosts.add(this._host);
if (this._config) {
HostConfigs.set(this._host, this._config);
if (!rootClickListenerActive) {
document.addEventListener('click', handleRootClick, { capture: true });
rootClickListenerActive = true;
}
}
}
_removeActiveHost() {
ActiveHosts.delete(this._host);
if (isEmpty(ActiveHosts) && rootClickListenerActive) {
document.removeEventListener('click', handleRootClick, { capture: true });
rootClickListenerActive = false;
}
}
_configureListeners() {
this._host.open && !this._host.keepOpenOnOutsideClick
? this._addActiveHost()
: this._removeActiveHost();
}
update(config) {
if (config) {
this._config = { ...this._config, ...config };
HostConfigs.set(this._host, this._config);
}
this._configureListeners();
}
hostConnected() {
this._configureListeners();
}
hostDisconnected() {
this._removeActiveHost();
}
}
export function addRootClickController(host, config) {
return new RootClickController(host, config);
}
//# sourceMappingURL=root-click.js.map