UNPKG

7.98 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.KeytipManager = void 0;
4var tslib_1 = require("tslib");
5var Utilities_1 = require("../../Utilities");
6var KeytipConstants_1 = require("../../utilities/keytips/KeytipConstants");
7/**
8 * This class is responsible for handling registering, updating, and unregistering of keytips
9 */
10var KeytipManager = /** @class */ (function () {
11 function KeytipManager() {
12 this.keytips = {};
13 this.persistedKeytips = {};
14 this.sequenceMapping = {};
15 // This is (and should be) updated and kept in sync
16 // with the inKeytipMode in KeytipLayer.
17 this.inKeytipMode = false;
18 // Boolean that gets checked before entering keytip mode by the KeytipLayer
19 // Used for an override in special cases (e.g. Disable entering keytip mode when a modal is shown)
20 this.shouldEnterKeytipMode = true;
21 // Boolean to indicate whether to delay firing an event to update subscribers of
22 // keytip data changed.
23 this.delayUpdatingKeytipChange = false;
24 }
25 /**
26 * Static function to get singleton KeytipManager instance
27 *
28 * @returns Singleton KeytipManager instance
29 */
30 KeytipManager.getInstance = function () {
31 return this._instance;
32 };
33 /**
34 * Initialization code to set set parameters to define
35 * how the KeytipManager handles keytip data.
36 *
37 * @param delayUpdatingKeytipChange - T/F if we should delay notifiying keytip subscribers
38 * of keytip changes
39 */
40 KeytipManager.prototype.init = function (delayUpdatingKeytipChange) {
41 this.delayUpdatingKeytipChange = delayUpdatingKeytipChange;
42 };
43 /**
44 * Registers a keytip
45 *
46 * @param keytipProps - Keytip to register
47 * @param persisted - T/F if this keytip should be persisted, default is false
48 * @returns Unique ID for this keytip
49 */
50 KeytipManager.prototype.register = function (keytipProps, persisted) {
51 if (persisted === void 0) { persisted = false; }
52 var props = keytipProps;
53 if (!persisted) {
54 // Add the overflowSetSequence if necessary
55 props = this.addParentOverflow(keytipProps);
56 this.sequenceMapping[props.keySequences.toString()] = props;
57 }
58 // Create a unique keytip
59 var uniqueKeytip = this._getUniqueKtp(props);
60 // Add to dictionary
61 persisted
62 ? (this.persistedKeytips[uniqueKeytip.uniqueID] = uniqueKeytip)
63 : (this.keytips[uniqueKeytip.uniqueID] = uniqueKeytip);
64 // We only want to add something new if we are currently showing keytip mode
65 if (this.inKeytipMode || !this.delayUpdatingKeytipChange) {
66 var event_1 = persisted ? KeytipConstants_1.KeytipEvents.PERSISTED_KEYTIP_ADDED : KeytipConstants_1.KeytipEvents.KEYTIP_ADDED;
67 Utilities_1.EventGroup.raise(this, event_1, {
68 keytip: props,
69 uniqueID: uniqueKeytip.uniqueID,
70 });
71 }
72 return uniqueKeytip.uniqueID;
73 };
74 /**
75 * Update a keytip
76 *
77 * @param keytipProps - Keytip to update
78 * @param uniqueID - Unique ID of this keytip
79 */
80 KeytipManager.prototype.update = function (keytipProps, uniqueID) {
81 var newKeytipProps = this.addParentOverflow(keytipProps);
82 var uniqueKeytip = this._getUniqueKtp(newKeytipProps, uniqueID);
83 var oldKeyTip = this.keytips[uniqueID];
84 if (oldKeyTip) {
85 // Update everything except 'visible'
86 uniqueKeytip.keytip.visible = oldKeyTip.keytip.visible;
87 // Update keytip in this.keytips
88 this.keytips[uniqueID] = uniqueKeytip;
89 // Update the sequence to be up to date
90 delete this.sequenceMapping[oldKeyTip.keytip.keySequences.toString()];
91 this.sequenceMapping[uniqueKeytip.keytip.keySequences.toString()] = uniqueKeytip.keytip;
92 // Raise event only if we are currently in keytip mode
93 if (this.inKeytipMode || !this.delayUpdatingKeytipChange) {
94 Utilities_1.EventGroup.raise(this, KeytipConstants_1.KeytipEvents.KEYTIP_UPDATED, {
95 keytip: uniqueKeytip.keytip,
96 uniqueID: uniqueKeytip.uniqueID,
97 });
98 }
99 }
100 };
101 /**
102 * Unregisters a keytip
103 *
104 * @param keytipToRemove - IKeytipProps of the keytip to remove
105 * @param uniqueID - Unique ID of this keytip
106 * @param persisted - T/F if this keytip should be persisted, default is false
107 */
108 KeytipManager.prototype.unregister = function (keytipToRemove, uniqueID, persisted) {
109 if (persisted === void 0) { persisted = false; }
110 persisted ? delete this.persistedKeytips[uniqueID] : delete this.keytips[uniqueID];
111 !persisted && delete this.sequenceMapping[keytipToRemove.keySequences.toString()];
112 var event = persisted ? KeytipConstants_1.KeytipEvents.PERSISTED_KEYTIP_REMOVED : KeytipConstants_1.KeytipEvents.KEYTIP_REMOVED;
113 // Update keytips only if we're in keytip mode
114 if (this.inKeytipMode || !this.delayUpdatingKeytipChange) {
115 Utilities_1.EventGroup.raise(this, event, {
116 keytip: keytipToRemove,
117 uniqueID: uniqueID,
118 });
119 }
120 };
121 /**
122 * Manual call to enter keytip mode
123 */
124 KeytipManager.prototype.enterKeytipMode = function () {
125 Utilities_1.EventGroup.raise(this, KeytipConstants_1.KeytipEvents.ENTER_KEYTIP_MODE);
126 };
127 /**
128 * Manual call to exit keytip mode
129 */
130 KeytipManager.prototype.exitKeytipMode = function () {
131 Utilities_1.EventGroup.raise(this, KeytipConstants_1.KeytipEvents.EXIT_KEYTIP_MODE);
132 };
133 /**
134 * Gets all IKeytipProps from this.keytips
135 *
136 * @returns All keytips stored in the manager
137 */
138 KeytipManager.prototype.getKeytips = function () {
139 var _this = this;
140 return Object.keys(this.keytips).map(function (key) { return _this.keytips[key].keytip; });
141 };
142 /**
143 * Adds the overflowSetSequence to the keytipProps if its parent keytip also has it
144 *
145 * @param keytipProps - Keytip props to add overflowSetSequence to if necessary
146 * @returns - Modified keytip props, if needed to be modified
147 */
148 KeytipManager.prototype.addParentOverflow = function (keytipProps) {
149 var fullSequence = tslib_1.__spreadArray([], keytipProps.keySequences);
150 fullSequence.pop();
151 if (fullSequence.length !== 0) {
152 var parentKeytip = this.sequenceMapping[fullSequence.toString()];
153 if (parentKeytip && parentKeytip.overflowSetSequence) {
154 return tslib_1.__assign(tslib_1.__assign({}, keytipProps), { overflowSetSequence: parentKeytip.overflowSetSequence });
155 }
156 }
157 return keytipProps;
158 };
159 /**
160 * Public function to bind for overflow items that have a submenu
161 */
162 KeytipManager.prototype.menuExecute = function (overflowButtonSequences, keytipSequences) {
163 Utilities_1.EventGroup.raise(this, KeytipConstants_1.KeytipEvents.PERSISTED_KEYTIP_EXECUTE, {
164 overflowButtonSequences: overflowButtonSequences,
165 keytipSequences: keytipSequences,
166 });
167 };
168 /**
169 * Creates an IUniqueKeytip object
170 *
171 * @param keytipProps - IKeytipProps
172 * @param uniqueID - Unique ID, will default to the next unique ID if not passed
173 * @returns IUniqueKeytip object
174 */
175 KeytipManager.prototype._getUniqueKtp = function (keytipProps, uniqueID) {
176 if (uniqueID === void 0) { uniqueID = Utilities_1.getId(); }
177 return { keytip: tslib_1.__assign({}, keytipProps), uniqueID: uniqueID };
178 };
179 KeytipManager._instance = new KeytipManager();
180 return KeytipManager;
181}());
182exports.KeytipManager = KeytipManager;
183//# sourceMappingURL=KeytipManager.js.map
\No newline at end of file