office-ui-fabric-react
Version:
Reusable React components for building experiences for Office 365.
170 lines • 6.97 kB
JavaScript
import * as tslib_1 from "tslib";
import { arraysEqual, replaceElement, findIndex, find, EventGroup, getId } from '../../Utilities';
import { KeytipEvents } from '../../utilities/keytips/KeytipConstants';
/**
* This class is responsible for handling registering, updating, and unregistering of keytips
*/
var KeytipManager = /** @class */ (function () {
function KeytipManager() {
this.keytips = [];
this.persistedKeytips = [];
// This is (and should be) updated and kept in sync
// with the inKeytipMode in KeytipLayer.
this.inKeytipMode = false;
// Boolean that gets checked before entering keytip mode by the KeytipLayer
// Used for an override in special cases (e.g. Disable entering keytip mode when a modal is shown)
this.shouldEnterKeytipMode = true;
}
/**
* Static function to get singleton KeytipManager instance
*
* @returns {KeytipManager} Singleton KeytipManager instance
*/
KeytipManager.getInstance = function () {
return this._instance;
};
/**
* Registers a keytip
*
* @param keytipProps - Keytip to register
* @param persisted - T/F if this keytip should be persisted, default is false
* @returns {string} Unique ID for this keytip
*/
KeytipManager.prototype.register = function (keytipProps, persisted) {
if (persisted === void 0) { persisted = false; }
var props = keytipProps;
if (!persisted) {
// Add the overflowSetSequence if necessary
props = this.addParentOverflow(keytipProps);
}
// Create a unique keytip
var uniqueKeytip = this._getUniqueKtp(props);
// Add to array
persisted ? this.persistedKeytips.push(uniqueKeytip) : this.keytips.push(uniqueKeytip);
var event = persisted ? KeytipEvents.PERSISTED_KEYTIP_ADDED : KeytipEvents.KEYTIP_ADDED;
EventGroup.raise(this, event, {
keytip: props,
uniqueID: uniqueKeytip.uniqueID
});
return uniqueKeytip.uniqueID;
};
/**
* Update a keytip
*
* @param keytipProps - Keytip to update
* @param uniqueID - Unique ID of this keytip
*/
KeytipManager.prototype.update = function (keytipProps, uniqueID) {
var newKeytipProps = this.addParentOverflow(keytipProps);
var uniqueKeytip = this._getUniqueKtp(newKeytipProps, uniqueID);
var keytipIndex = findIndex(this.keytips, function (ktp) {
return ktp.uniqueID === uniqueID;
});
if (keytipIndex >= 0) {
// Update everything except 'visible'
uniqueKeytip.keytip.visible = this.keytips[keytipIndex].keytip.visible;
// Update keytip in this.keytips
this.keytips = replaceElement(this.keytips, uniqueKeytip, keytipIndex);
// Raise event
EventGroup.raise(this, KeytipEvents.KEYTIP_UPDATED, {
keytip: uniqueKeytip.keytip,
uniqueID: uniqueKeytip.uniqueID
});
}
};
/**
* Unregisters a keytip
*
* @param keytipToRemove - IKeytipProps of the keytip to remove
* @param uniqueID - Unique ID of this keytip
* @param persisted - T/F if this keytip should be persisted, default is false
*/
KeytipManager.prototype.unregister = function (keytipToRemove, uniqueID, persisted) {
if (persisted === void 0) { persisted = false; }
if (persisted) {
// Remove keytip from this.persistedKeytips
this.persistedKeytips = this.persistedKeytips.filter(function (uniqueKtp) {
return uniqueKtp.uniqueID !== uniqueID;
});
}
else {
// Remove keytip from this.keytips
this.keytips = this.keytips.filter(function (uniqueKtp) {
return uniqueKtp.uniqueID !== uniqueID;
});
}
var event = persisted ? KeytipEvents.PERSISTED_KEYTIP_REMOVED : KeytipEvents.KEYTIP_REMOVED;
EventGroup.raise(this, event, {
keytip: keytipToRemove,
uniqueID: uniqueID
});
};
/**
* Manual call to enter keytip mode
*/
KeytipManager.prototype.enterKeytipMode = function () {
EventGroup.raise(this, KeytipEvents.ENTER_KEYTIP_MODE);
};
/**
* Manual call to exit keytip mode
*/
KeytipManager.prototype.exitKeytipMode = function () {
EventGroup.raise(this, KeytipEvents.EXIT_KEYTIP_MODE);
};
/**
* Gets all IKeytipProps from this.keytips
*
* @returns {IKeytipProps[]} All keytips stored in the manager
*/
KeytipManager.prototype.getKeytips = function () {
return this.keytips.map(function (uniqueKeytip) {
return uniqueKeytip.keytip;
});
};
/**
* Adds the overflowSetSequence to the keytipProps if its parent keytip also has it
*
* @param keytipProps - Keytip props to add overflowSetSequence to if necessary
* @returns {IKeytipProps} - Modified keytip props, if needed to be modified
*/
KeytipManager.prototype.addParentOverflow = function (keytipProps) {
var fullSequence = keytipProps.keySequences.slice();
fullSequence.pop();
if (fullSequence.length !== 0) {
var parentKeytip = find(this.getKeytips(), function (keytip) {
return arraysEqual(fullSequence, keytip.keySequences);
});
if (parentKeytip && parentKeytip.overflowSetSequence) {
return tslib_1.__assign({}, keytipProps, { overflowSetSequence: parentKeytip.overflowSetSequence });
}
}
return keytipProps;
};
/**
* Public function to bind for overflow items that have a submenu
*
* @param overflowButtonSequences
* @param keytipSequences
*/
KeytipManager.prototype.menuExecute = function (overflowButtonSequences, keytipSequences) {
EventGroup.raise(this, KeytipEvents.PERSISTED_KEYTIP_EXECUTE, {
overflowButtonSequences: overflowButtonSequences,
keytipSequences: keytipSequences
});
};
/**
* Creates an IUniqueKeytip object
*
* @param keytipProps - IKeytipProps
* @param uniqueID - Unique ID, will default to the next unique ID if not passed
* @returns {IUniqueKeytip} IUniqueKeytip object
*/
KeytipManager.prototype._getUniqueKtp = function (keytipProps, uniqueID) {
if (uniqueID === void 0) { uniqueID = getId(); }
return { keytip: tslib_1.__assign({}, keytipProps), uniqueID: uniqueID };
};
KeytipManager._instance = new KeytipManager();
return KeytipManager;
}());
export { KeytipManager };
//# sourceMappingURL=KeytipManager.js.map