office-ui-fabric-react
Version:
Reusable React components for building experiences for Microsoft 365.
120 lines • 4.1 kB
JavaScript
var _layersByHostId = {};
var _layerHostsById = {};
var _defaultHostSelector;
/**
* Register a layer for a given host id
* @param hostId - Id of the layer host
* @param layer - Layer instance
*/
export function registerLayer(hostId, callback) {
if (!_layersByHostId[hostId]) {
_layersByHostId[hostId] = [];
}
_layersByHostId[hostId].push(callback);
var layerHosts = _layerHostsById[hostId];
if (layerHosts) {
for (var _i = 0, layerHosts_1 = layerHosts; _i < layerHosts_1.length; _i++) {
var layerHost = layerHosts_1[_i];
layerHost.notifyLayersChanged();
}
}
}
/**
* Unregister a layer for a given host id
* @param hostId - Id of the layer host
* @param layer - Layer instance
*/
export function unregisterLayer(hostId, callback) {
var layers = _layersByHostId[hostId];
if (layers) {
var idx = layers.indexOf(callback);
if (idx >= 0) {
layers.splice(idx, 1);
if (layers.length === 0) {
delete _layersByHostId[hostId];
}
}
}
var layerHosts = _layerHostsById[hostId];
if (layerHosts) {
for (var _i = 0, layerHosts_2 = layerHosts; _i < layerHosts_2.length; _i++) {
var layerHost = layerHosts_2[_i];
layerHost.notifyLayersChanged();
}
}
}
/**
* Gets the number of layers currently registered with a host id.
* @param hostId - Id of the layer host.
* @returns The number of layers currently registered with the host.
*/
export function getLayerCount(hostId) {
var layers = _layerHostsById[hostId];
return layers ? layers.length : 0;
}
/**
* Gets the Layer Host instance associated with a hostId, if applicable.
* @param hostId - Id of the layer host.
* @returns A component ref for the associated layer host.
*/
export function getLayerHost(hostId) {
var layerHosts = _layerHostsById[hostId];
return (layerHosts && layerHosts[0]) || undefined;
}
/**
* Registers a Layer Host with an associated hostId.
* @param hostId - Id of the layer host
* @param layerHost - layer host instance
*/
export function registerLayerHost(hostId, layerHost) {
var layerHosts = _layerHostsById[hostId] || (_layerHostsById[hostId] = []);
// Insert this at the start of an array to avoid race conditions between mount and unmount.
// If a LayerHost is re-mounted, and mount of the new instance may occur before the unmount of the old one.
// Putting the new instance at the start of this array ensures that calls to `getLayerHost` will immediately
// get the new one even if the old one is around briefly.
layerHosts.unshift(layerHost);
}
/**
* Unregisters a Layer Host from the associated hostId.
* @param hostId - Id of the layer host
* @param layerHost - layer host instance
*/
export function unregisterLayerHost(hostId, layerHost) {
var layerHosts = _layerHostsById[hostId];
if (layerHosts) {
var idx = layerHosts.indexOf(layerHost);
if (idx >= 0) {
layerHosts.splice(idx, 1);
}
if (layerHosts.length === 0) {
delete _layerHostsById[hostId];
}
}
}
/**
* Used for notifying applicable Layers that a host is available/unavailable and to re-evaluate Layers that
* care about the specific host.
*/
export function notifyHostChanged(id) {
if (_layersByHostId[id]) {
_layersByHostId[id].forEach(function (callback) { return callback(); });
}
}
/**
* Sets the default target selector to use when determining the host in which
* Layered content will be injected into. If not provided, an element will be
* created at the end of the document body.
*
* Passing in a falsey value will clear the default target and reset back to
* using a created element at the end of document body.
*/
export function setDefaultTarget(selector) {
_defaultHostSelector = selector;
}
/**
* Get the default target selector when determining a host
*/
export function getDefaultTarget() {
return _defaultHostSelector;
}
//# sourceMappingURL=Layer.notification.js.map