@slickgrid-universal/custom-tooltip-plugin 2.5.0 → 2.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,404 +1,407 @@
1
- import { calculateAvailableSpace, cancellablePromise, CancelledException, createDomElement, findFirstElementAttribute, getHtmlElementOffset, sanitizeTextByAvailableSanitizer, } from '@slickgrid-universal/common';
2
- /**
3
- * A plugin to add Custom Tooltip when hovering a cell, it subscribes to the cell "onMouseEnter" and "onMouseLeave" events.
4
- * The "customTooltip" is defined in the Column Definition OR Grid Options (the first found will have priority over the second)
5
- * To specify a tooltip when hovering a cell, extend the column definition like so:
6
- *
7
- * Available plugin options (same options are available in both column definition and/or grid options)
8
- * Example 1 - via Column Definition
9
- * this.columnDefinitions = [
10
- * {
11
- * id: "action", name: "Action", field: "action", formatter: fakeButtonFormatter,
12
- * customTooltip: {
13
- * formatter: tooltipTaskFormatter,
14
- * usabilityOverride: (args) => !!(args.dataContext.id % 2) // show it only every second row
15
- * }
16
- * }
17
- * ];
18
- *
19
- * OR Example 2 - via Grid Options (for all columns), NOTE: the column definition tooltip options will win over the options defined in the grid options
20
- * this.gridOptions = {
21
- * enableCellNavigation: true,
22
- * customTooltip: {
23
- * },
24
- * };
25
- */
26
- export class SlickCustomTooltip {
27
- constructor() {
28
- this._rxjs = null;
29
- this._sharedService = null;
30
- this._defaultOptions = {
31
- className: 'slick-custom-tooltip',
32
- offsetLeft: 0,
33
- offsetRight: 0,
34
- offsetTopBottom: 4,
35
- hideArrow: false,
36
- regularTooltipWhiteSpace: 'pre-line',
37
- whiteSpace: 'normal',
38
- };
39
- this.name = 'CustomTooltip';
40
- this._eventHandler = new Slick.EventHandler();
41
- }
42
- get addonOptions() {
43
- return this._addonOptions;
44
- }
45
- get cancellablePromise() {
46
- return this._cancellablePromise;
47
- }
48
- get cellAddonOptions() {
49
- return this._cellAddonOptions;
50
- }
51
- get className() {
52
- var _a, _b;
53
- return (_b = (_a = this._cellAddonOptions) === null || _a === void 0 ? void 0 : _a.className) !== null && _b !== void 0 ? _b : 'slick-custom-tooltip';
54
- }
55
- get dataView() {
56
- return this._grid.getData() || {};
57
- }
58
- /** Getter for the Grid Options pulled through the Grid Object */
59
- get gridOptions() {
60
- return this._grid.getOptions() || {};
61
- }
62
- /** Getter for the grid uid */
63
- get gridUid() {
64
- var _a;
65
- return ((_a = this._grid) === null || _a === void 0 ? void 0 : _a.getUID()) || '';
66
- }
67
- get gridUidSelector() {
68
- return this.gridUid ? `.${this.gridUid}` : '';
69
- }
70
- get tooltipElm() {
71
- return this._tooltipElm;
72
- }
73
- addRxJsResource(rxjs) {
74
- this._rxjs = rxjs;
75
- }
76
- init(grid, containerService) {
77
- var _a, _b;
78
- this._grid = grid;
79
- this._rxjs = containerService.get('RxJsFacade');
80
- this._sharedService = containerService.get('SharedService');
81
- this._addonOptions = { ...this._defaultOptions, ...((_b = (_a = this._sharedService) === null || _a === void 0 ? void 0 : _a.gridOptions) === null || _b === void 0 ? void 0 : _b.customTooltip) };
82
- this._eventHandler
83
- .subscribe(grid.onMouseEnter, this.handleOnMouseEnter.bind(this))
84
- .subscribe(grid.onHeaderMouseEnter, (e, args) => this.handleOnHeaderMouseEnterByType(e, args, 'slick-header-column'))
85
- .subscribe(grid.onHeaderRowMouseEnter, (e, args) => this.handleOnHeaderMouseEnterByType(e, args, 'slick-headerrow-column'))
86
- .subscribe(grid.onMouseLeave, this.hideTooltip.bind(this))
87
- .subscribe(grid.onHeaderMouseLeave, this.hideTooltip.bind(this))
88
- .subscribe(grid.onHeaderRowMouseLeave, this.hideTooltip.bind(this));
89
- }
90
- dispose() {
91
- // hide (remove) any tooltip and unsubscribe from all events
92
- this.hideTooltip();
93
- this._cancellablePromise = undefined;
94
- this._eventHandler.unsubscribeAll();
95
- }
96
- /**
97
- * hide (remove) tooltip from the DOM, it will also remove it from the DOM and also cancel any pending requests (as mentioned below).
98
- * When using async process, it will also cancel any opened Promise/Observable that might still be pending.
99
- */
100
- hideTooltip() {
101
- var _a, _b;
102
- (_a = this._cancellablePromise) === null || _a === void 0 ? void 0 : _a.cancel();
103
- (_b = this._observable$) === null || _b === void 0 ? void 0 : _b.unsubscribe();
104
- const prevTooltip = document.body.querySelector(`.${this.className}${this.gridUidSelector}`);
105
- prevTooltip === null || prevTooltip === void 0 ? void 0 : prevTooltip.remove();
106
- }
107
- getOptions() {
108
- return this._addonOptions;
109
- }
110
- setOptions(newOptions) {
111
- this._addonOptions = { ...this._addonOptions, ...newOptions };
112
- }
113
- // --
114
- // protected functions
115
- // ---------------------
116
- /**
117
- * Async process callback will hide any prior tooltip & then merge the new result with the item `dataContext` under a `__params` property
118
- * (unless a new prop name is provided) to provice as dataContext object to the asyncPostFormatter.
119
- */
120
- asyncProcessCallback(asyncResult, cell, value, columnDef, dataContext) {
121
- var _a, _b, _c;
122
- this.hideTooltip();
123
- const itemWithAsyncData = { ...dataContext, [(_b = (_a = this.addonOptions) === null || _a === void 0 ? void 0 : _a.asyncParamsPropName) !== null && _b !== void 0 ? _b : '__params']: asyncResult };
124
- if ((_c = this._cellAddonOptions) === null || _c === void 0 ? void 0 : _c.useRegularTooltip) {
125
- this.renderRegularTooltip(this._cellAddonOptions.asyncPostFormatter, cell, value, columnDef, itemWithAsyncData);
126
- }
127
- else {
128
- this.renderTooltipFormatter(this._cellAddonOptions.asyncPostFormatter, cell, value, columnDef, itemWithAsyncData);
129
- }
130
- }
131
- /** depending on the selector type, execute the necessary handler code */
132
- handleOnHeaderMouseEnterByType(event, args, selector) {
133
- var _a, _b, _c;
134
- // before doing anything, let's remove any previous tooltip before
135
- // and cancel any opened Promise/Observable when using async
136
- this.hideTooltip();
137
- const cell = {
138
- row: -1,
139
- cell: this._grid.getColumns().findIndex((col) => { var _a, _b; return ((_b = (_a = args === null || args === void 0 ? void 0 : args.column) === null || _a === void 0 ? void 0 : _a.id) !== null && _b !== void 0 ? _b : '') === col.id; })
140
- };
141
- const columnDef = args.column;
142
- const item = {};
143
- const isHeaderRowType = selector === 'slick-headerrow-column';
144
- // run the override function (when defined), if the result is false it won't go further
145
- args = args || {};
146
- args.cell = cell.cell;
147
- args.row = cell.row;
148
- args.columnDef = columnDef;
149
- args.dataContext = item;
150
- args.grid = this._grid;
151
- args.type = isHeaderRowType ? 'header-row' : 'header';
152
- this._cellAddonOptions = { ...this._addonOptions, ...(columnDef === null || columnDef === void 0 ? void 0 : columnDef.customTooltip) };
153
- if ((columnDef === null || columnDef === void 0 ? void 0 : columnDef.disableTooltip) || (typeof ((_a = this._cellAddonOptions) === null || _a === void 0 ? void 0 : _a.usabilityOverride) === 'function' && !this._cellAddonOptions.usabilityOverride(args))) {
154
- return;
155
- }
156
- if (columnDef && event.target) {
157
- this._cellNodeElm = event.target.closest(`.${selector}`);
158
- const formatter = isHeaderRowType ? this._cellAddonOptions.headerRowFormatter : this._cellAddonOptions.headerFormatter;
159
- if (((_b = this._cellAddonOptions) === null || _b === void 0 ? void 0 : _b.useRegularTooltip) || !formatter) {
160
- const formatterOrText = !isHeaderRowType ? columnDef.name : ((_c = this._cellAddonOptions) === null || _c === void 0 ? void 0 : _c.useRegularTooltip) ? null : formatter;
161
- this.renderRegularTooltip(formatterOrText, cell, null, columnDef, item);
162
- }
163
- else if (this._cellNodeElm && typeof formatter === 'function') {
164
- this.renderTooltipFormatter(formatter, cell, null, columnDef, item);
165
- }
166
- }
167
- }
168
- async handleOnMouseEnter(event) {
169
- var _a, _b, _c, _d, _e, _f, _g, _h;
170
- // before doing anything, let's remove any previous tooltip before
171
- // and cancel any opened Promise/Observable when using async
172
- this.hideTooltip();
173
- if (event && this._grid) {
174
- // get cell only when it's possible (ie, Composite Editor will not be able to get cell and so it will never show any tooltip)
175
- const targetClassName = (_b = (_a = event === null || event === void 0 ? void 0 : event.target) === null || _a === void 0 ? void 0 : _a.closest('.slick-cell')) === null || _b === void 0 ? void 0 : _b.className;
176
- const cell = (targetClassName && /l\d+/.exec(targetClassName || '')) ? this._grid.getCellFromEvent(event) : null;
177
- if (cell) {
178
- const item = this.dataView ? this.dataView.getItem(cell.row) : this._grid.getDataItem(cell.row);
179
- const columnDef = this._grid.getColumns()[cell.cell];
180
- this._cellNodeElm = this._grid.getCellNode(cell.row, cell.cell);
181
- if (item && columnDef) {
182
- this._cellAddonOptions = { ...this._addonOptions, ...(columnDef === null || columnDef === void 0 ? void 0 : columnDef.customTooltip) };
183
- if ((columnDef === null || columnDef === void 0 ? void 0 : columnDef.disableTooltip) || (typeof ((_c = this._cellAddonOptions) === null || _c === void 0 ? void 0 : _c.usabilityOverride) === 'function' && !this._cellAddonOptions.usabilityOverride({ cell: cell.cell, row: cell.row, dataContext: item, column: columnDef, grid: this._grid, type: 'cell' }))) {
184
- return;
185
- }
186
- const value = item.hasOwnProperty(columnDef.field) ? item[columnDef.field] : null;
187
- // when cell is currently lock for editing, we'll force a tooltip title search
188
- const cellValue = this._grid.getEditorLock().isActive() ? null : value;
189
- // when there aren't any formatter OR when user specifically want to use a regular tooltip (via "title" attribute)
190
- if ((this._cellAddonOptions.useRegularTooltip && !((_d = this._cellAddonOptions) === null || _d === void 0 ? void 0 : _d.asyncProcess)) || !((_e = this._cellAddonOptions) === null || _e === void 0 ? void 0 : _e.formatter)) {
191
- this.renderRegularTooltip(columnDef.formatter, cell, cellValue, columnDef, item);
192
- }
193
- else {
194
- // when we aren't using regular tooltip and we do have a tooltip formatter, let's render it
195
- if (typeof ((_f = this._cellAddonOptions) === null || _f === void 0 ? void 0 : _f.formatter) === 'function') {
196
- this.renderTooltipFormatter(this._cellAddonOptions.formatter, cell, cellValue, columnDef, item);
197
- }
198
- // when tooltip is an Async (delayed, e.g. with a backend API call)
199
- if (typeof ((_g = this._cellAddonOptions) === null || _g === void 0 ? void 0 : _g.asyncProcess) === 'function') {
200
- const asyncProcess = this._cellAddonOptions.asyncProcess(cell.row, cell.cell, value, columnDef, item, this._grid);
201
- if (!this._cellAddonOptions.asyncPostFormatter) {
202
- console.error(`[Slickgrid-Universal] when using "asyncProcess" with Custom Tooltip, you must also provide an "asyncPostFormatter" formatter.`);
203
- }
204
- if (asyncProcess instanceof Promise) {
205
- // create a new cancellable promise which will resolve, unless it's cancelled, with the udpated `dataContext` object that includes the `__params`
206
- this._cancellablePromise = cancellablePromise(asyncProcess);
207
- this._cancellablePromise.promise
208
- .then((asyncResult) => this.asyncProcessCallback(asyncResult, cell, value, columnDef, item))
209
- .catch((error) => {
210
- // we will throw back any errors, unless it's a cancelled promise which in that case will be disregarded (thrown by the promise wrapper cancel() call)
211
- if (!(error instanceof CancelledException)) {
212
- console.error(error);
213
- }
214
- });
215
- }
216
- else if ((_h = this._rxjs) === null || _h === void 0 ? void 0 : _h.isObservable(asyncProcess)) {
217
- const rxjs = this._rxjs;
218
- this._observable$ = asyncProcess
219
- .pipe(
220
- // use `switchMap` so that it cancels any previous subscription, it must return an observable so we can use `of` for that, and then finally we can subscribe to the new observable
221
- rxjs.switchMap((asyncResult) => rxjs.of(asyncResult))).subscribe((asyncResult) => this.asyncProcessCallback(asyncResult, cell, value, columnDef, item), (error) => console.error(error));
222
- }
223
- }
224
- }
225
- }
226
- }
227
- }
228
- }
229
- /**
230
- * Parse the Custom Formatter (when provided) or return directly the text when it is already a string.
231
- * We will also sanitize the text in both cases before returning it so that it can be used safely.
232
- */
233
- parseFormatterAndSanitize(formatterOrText, cell, value, columnDef, item) {
234
- if (typeof formatterOrText === 'function') {
235
- const tooltipText = formatterOrText(cell.row, cell.cell, value, columnDef, item, this._grid);
236
- const formatterText = (typeof tooltipText === 'object' && (tooltipText === null || tooltipText === void 0 ? void 0 : tooltipText.text)) ? tooltipText.text : (typeof tooltipText === 'string' ? tooltipText : '');
237
- return sanitizeTextByAvailableSanitizer(this.gridOptions, formatterText);
238
- }
239
- else if (typeof formatterOrText === 'string') {
240
- return sanitizeTextByAvailableSanitizer(this.gridOptions, formatterOrText);
241
- }
242
- return '';
243
- }
244
- /**
245
- * Parse the cell formatter and assume it might be html
246
- * then create a temporary html element to easily retrieve the first [title=""] attribute text content
247
- * also clear the "title" attribute from the grid div text content so that it won't show also as a 2nd browser tooltip
248
- */
249
- renderRegularTooltip(formatterOrText, cell, value, columnDef, item) {
250
- var _a, _b, _c, _d, _e, _f, _g, _h;
251
- const tmpDiv = createDomElement('div', { innerHTML: this.parseFormatterAndSanitize(formatterOrText, cell, value, columnDef, item) });
252
- let tooltipText = (_a = columnDef === null || columnDef === void 0 ? void 0 : columnDef.toolTip) !== null && _a !== void 0 ? _a : '';
253
- let tmpTitleElm;
254
- if (!tooltipText) {
255
- if (this._cellNodeElm && (this._cellNodeElm.clientWidth < this._cellNodeElm.scrollWidth) && !((_b = this._cellAddonOptions) === null || _b === void 0 ? void 0 : _b.useRegularTooltipFromFormatterOnly)) {
256
- tooltipText = (_d = (_c = this._cellNodeElm.textContent) === null || _c === void 0 ? void 0 : _c.trim()) !== null && _d !== void 0 ? _d : '';
257
- if (((_e = this._cellAddonOptions) === null || _e === void 0 ? void 0 : _e.tooltipTextMaxLength) && tooltipText.length > ((_f = this._cellAddonOptions) === null || _f === void 0 ? void 0 : _f.tooltipTextMaxLength)) {
258
- tooltipText = tooltipText.substring(0, this._cellAddonOptions.tooltipTextMaxLength - 3) + '...';
259
- }
260
- tmpTitleElm = this._cellNodeElm;
261
- }
262
- else {
263
- if ((_g = this._cellAddonOptions) === null || _g === void 0 ? void 0 : _g.useRegularTooltipFromFormatterOnly) {
264
- tmpTitleElm = tmpDiv.querySelector('[title], [data-slick-tooltip]');
265
- }
266
- else {
267
- tmpTitleElm = findFirstElementAttribute(this._cellNodeElm, ['title', 'data-slick-tooltip']) ? this._cellNodeElm : tmpDiv.querySelector('[title], [data-slick-tooltip]');
268
- if ((!tmpTitleElm || !findFirstElementAttribute(tmpTitleElm, ['title', 'data-slick-tooltip'])) && this._cellNodeElm) {
269
- tmpTitleElm = this._cellNodeElm.querySelector('[title], [data-slick-tooltip]');
270
- }
271
- }
272
- if (!tooltipText || (typeof formatterOrText === 'function' && ((_h = this._cellAddonOptions) === null || _h === void 0 ? void 0 : _h.useRegularTooltipFromFormatterOnly))) {
273
- tooltipText = findFirstElementAttribute(tmpTitleElm, ['title', 'data-slick-tooltip']) || '';
274
- }
275
- }
276
- }
277
- if (tooltipText !== '') {
278
- this.renderTooltipFormatter(formatterOrText, cell, value, columnDef, item, tooltipText);
279
- }
280
- // also clear any "title" attribute to avoid showing a 2nd browser tooltip
281
- this.swapAndClearTitleAttribute(tmpTitleElm, tooltipText);
282
- }
283
- renderTooltipFormatter(formatter, cell, value, columnDef, item, tooltipText, inputTitleElm) {
284
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
285
- // create the tooltip DOM element with the text returned by the Formatter
286
- this._tooltipElm = createDomElement('div', { className: this.className });
287
- this._tooltipElm.classList.add(this.gridUid);
288
- this._tooltipElm.classList.add('l' + cell.cell);
289
- this._tooltipElm.classList.add('r' + cell.cell);
290
- // when cell is currently lock for editing, we'll force a tooltip title search
291
- // that can happen when user has a formatter but is currently editing and in that case we want the new value
292
- // ie: when user is currently editing and uses the Slider, when dragging its value is changing, so we wish to use the editing value instead of the previous cell value.
293
- if (value === null || value === undefined) {
294
- const tmpTitleElm = (_a = this._cellNodeElm) === null || _a === void 0 ? void 0 : _a.querySelector('[title], [data-slick-tooltip]');
295
- value = findFirstElementAttribute(tmpTitleElm, ['title', 'data-slick-tooltip']) || value;
296
- }
297
- let outputText = tooltipText || this.parseFormatterAndSanitize(formatter, cell, value, columnDef, item) || '';
298
- outputText = (((_b = this._cellAddonOptions) === null || _b === void 0 ? void 0 : _b.tooltipTextMaxLength) && outputText.length > this._cellAddonOptions.tooltipTextMaxLength) ? outputText.substring(0, this._cellAddonOptions.tooltipTextMaxLength - 3) + '...' : outputText;
299
- let finalOutputText = '';
300
- if (!tooltipText || ((_c = this._cellAddonOptions) === null || _c === void 0 ? void 0 : _c.renderRegularTooltipAsHtml)) {
301
- finalOutputText = sanitizeTextByAvailableSanitizer(this.gridOptions, outputText);
302
- this._tooltipElm.innerHTML = finalOutputText;
303
- this._tooltipElm.style.whiteSpace = (_e = (_d = this._cellAddonOptions) === null || _d === void 0 ? void 0 : _d.whiteSpace) !== null && _e !== void 0 ? _e : this._defaultOptions.whiteSpace;
304
- }
305
- else {
306
- finalOutputText = outputText || '';
307
- this._tooltipElm.textContent = finalOutputText;
308
- this._tooltipElm.style.whiteSpace = (_g = (_f = this._cellAddonOptions) === null || _f === void 0 ? void 0 : _f.regularTooltipWhiteSpace) !== null && _g !== void 0 ? _g : this._defaultOptions.regularTooltipWhiteSpace; // use `pre` so that sequences of white space are collapsed. Lines are broken at newline characters
309
- }
310
- // optional max height/width of the tooltip container
311
- if ((_h = this._cellAddonOptions) === null || _h === void 0 ? void 0 : _h.maxHeight) {
312
- this._tooltipElm.style.maxHeight = `${this._cellAddonOptions.maxHeight}px`;
313
- }
314
- if ((_j = this._cellAddonOptions) === null || _j === void 0 ? void 0 : _j.maxWidth) {
315
- this._tooltipElm.style.maxWidth = `${this._cellAddonOptions.maxWidth}px`;
316
- }
317
- // when do have text to show, then append the new tooltip to the html body & reposition the tooltip
318
- if (finalOutputText) {
319
- document.body.appendChild(this._tooltipElm);
320
- // reposition the tooltip on top of the cell that triggered the mouse over event
321
- this.reposition(cell);
322
- // user could optionally hide the tooltip arrow (we can simply update the CSS variables, that's the only way we have to update CSS pseudo)
323
- if (!((_k = this._cellAddonOptions) === null || _k === void 0 ? void 0 : _k.hideArrow)) {
324
- this._tooltipElm.classList.add('tooltip-arrow');
325
- }
326
- // also clear any "title" attribute to avoid showing a 2nd browser tooltip
327
- this.swapAndClearTitleAttribute(inputTitleElm, outputText);
328
- }
329
- }
330
- /**
331
- * Reposition the Tooltip to be top-left position over the cell.
332
- * By default we use an "auto" mode which will allow to position the Tooltip to the best logical position in the window, also when we mention position, we are talking about the relative position against the grid cell.
333
- * We can assume that in 80% of the time the default position is top-right, the default is "auto" but we can also override it and use a specific position.
334
- * Most of the time positioning of the tooltip will be to the "top-right" of the cell is ok but if our column is completely on the right side then we'll want to change the position to "left" align.
335
- * Same goes for the top/bottom position, Most of the time positioning the tooltip to the "top" but if we are hovering a cell at the top of the grid and there's no room to display it then we might need to reposition to "bottom" instead.
336
- */
337
- reposition(cell) {
338
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
339
- if (this._tooltipElm) {
340
- this._cellNodeElm = this._cellNodeElm || this._grid.getCellNode(cell.row, cell.cell);
341
- const cellPosition = getHtmlElementOffset(this._cellNodeElm) || { top: 0, left: 0 };
342
- const cellContainerWidth = this._cellNodeElm.offsetWidth;
343
- const calculatedTooltipHeight = this._tooltipElm.getBoundingClientRect().height;
344
- const calculatedTooltipWidth = this._tooltipElm.getBoundingClientRect().width;
345
- const calculatedBodyWidth = document.body.offsetWidth || window.innerWidth;
346
- // first calculate the default (top/left) position
347
- let newPositionTop = (cellPosition.top || 0) - this._tooltipElm.offsetHeight - ((_b = (_a = this._cellAddonOptions) === null || _a === void 0 ? void 0 : _a.offsetTopBottom) !== null && _b !== void 0 ? _b : 0);
348
- let newPositionLeft = (cellPosition.left || 0) - ((_d = (_c = this._cellAddonOptions) === null || _c === void 0 ? void 0 : _c.offsetRight) !== null && _d !== void 0 ? _d : 0);
349
- // user could explicitely use a "left-align" arrow position, (when user knows his column is completely on the right in the grid)
350
- // or when using "auto" and we detect not enough available space then we'll position to the "left" of the cell
351
- // NOTE the class name is for the arrow and is inverse compare to the tooltip itself, so if user ask for "left-align", then the arrow will in fact be "arrow-right-align"
352
- const position = (_f = (_e = this._cellAddonOptions) === null || _e === void 0 ? void 0 : _e.position) !== null && _f !== void 0 ? _f : 'auto';
353
- if (position === 'center') {
354
- newPositionLeft += (cellContainerWidth / 2) - (calculatedTooltipWidth / 2) + ((_h = (_g = this._cellAddonOptions) === null || _g === void 0 ? void 0 : _g.offsetRight) !== null && _h !== void 0 ? _h : 0);
355
- this._tooltipElm.classList.remove('arrow-left-align');
356
- this._tooltipElm.classList.remove('arrow-right-align');
357
- this._tooltipElm.classList.add('arrow-center-align');
358
- }
359
- else if (position === 'right-align' || ((position === 'auto' || position !== 'left-align') && (newPositionLeft + calculatedTooltipWidth) > calculatedBodyWidth)) {
360
- newPositionLeft -= (calculatedTooltipWidth - cellContainerWidth - ((_k = (_j = this._cellAddonOptions) === null || _j === void 0 ? void 0 : _j.offsetLeft) !== null && _k !== void 0 ? _k : 0));
361
- this._tooltipElm.classList.remove('arrow-center-align');
362
- this._tooltipElm.classList.remove('arrow-left-align');
363
- this._tooltipElm.classList.add('arrow-right-align');
364
- }
365
- else {
366
- this._tooltipElm.classList.remove('arrow-center-align');
367
- this._tooltipElm.classList.remove('arrow-right-align');
368
- this._tooltipElm.classList.add('arrow-left-align');
369
- }
370
- // do the same calculation/reposition with top/bottom (default is top of the cell or in other word starting from the cell going down)
371
- // NOTE the class name is for the arrow and is inverse compare to the tooltip itself, so if user ask for "bottom", then the arrow will in fact be "arrow-top"
372
- if (position === 'bottom' || ((position === 'auto' || position !== 'top') && calculatedTooltipHeight > calculateAvailableSpace(this._cellNodeElm).top)) {
373
- newPositionTop = (cellPosition.top || 0) + ((_l = this.gridOptions.rowHeight) !== null && _l !== void 0 ? _l : 0) + ((_o = (_m = this._cellAddonOptions) === null || _m === void 0 ? void 0 : _m.offsetTopBottom) !== null && _o !== void 0 ? _o : 0);
374
- this._tooltipElm.classList.remove('arrow-down');
375
- this._tooltipElm.classList.add('arrow-up');
376
- }
377
- else {
378
- this._tooltipElm.classList.add('arrow-down');
379
- this._tooltipElm.classList.remove('arrow-up');
380
- }
381
- // reposition the tooltip over the cell (90% of the time this will end up using a position on the "right" of the cell)
382
- this._tooltipElm.style.top = `${newPositionTop}px`;
383
- this._tooltipElm.style.left = `${newPositionLeft}px`;
384
- }
385
- }
386
- /**
387
- * swap and copy the "title" attribute into a new custom attribute then clear the "title" attribute
388
- * from the grid div text content so that it won't show also as a 2nd browser tooltip
389
- */
390
- swapAndClearTitleAttribute(inputTitleElm, tooltipText) {
391
- var _a;
392
- // the title attribute might be directly on the slick-cell container element (when formatter returns a result object)
393
- // OR in a child element (most commonly as a custom formatter)
394
- const titleElm = inputTitleElm || (this._cellNodeElm && ((this._cellNodeElm.hasAttribute('title') && this._cellNodeElm.getAttribute('title')) ? this._cellNodeElm : (_a = this._cellNodeElm) === null || _a === void 0 ? void 0 : _a.querySelector('[title]')));
395
- // flip tooltip text from `title` to `data-slick-tooltip`
396
- if (titleElm) {
397
- titleElm.setAttribute('data-slick-tooltip', tooltipText || '');
398
- if (titleElm.hasAttribute('title')) {
399
- titleElm.setAttribute('title', '');
400
- }
401
- }
402
- }
403
- }
1
+ import { calculateAvailableSpace, cancellablePromise, CancelledException, createDomElement, findFirstElementAttribute, getHtmlElementOffset, sanitizeTextByAvailableSanitizer, } from '@slickgrid-universal/common';
2
+ /**
3
+ * A plugin to add Custom Tooltip when hovering a cell, it subscribes to the cell "onMouseEnter" and "onMouseLeave" events.
4
+ * The "customTooltip" is defined in the Column Definition OR Grid Options (the first found will have priority over the second)
5
+ * To specify a tooltip when hovering a cell, extend the column definition like so:
6
+ *
7
+ * Available plugin options (same options are available in both column definition and/or grid options)
8
+ * Example 1 - via Column Definition
9
+ * this.columnDefinitions = [
10
+ * {
11
+ * id: "action", name: "Action", field: "action", formatter: fakeButtonFormatter,
12
+ * customTooltip: {
13
+ * formatter: tooltipTaskFormatter,
14
+ * usabilityOverride: (args) => !!(args.dataContext.id % 2) // show it only every second row
15
+ * }
16
+ * }
17
+ * ];
18
+ *
19
+ * OR Example 2 - via Grid Options (for all columns), NOTE: the column definition tooltip options will win over the options defined in the grid options
20
+ * this.gridOptions = {
21
+ * enableCellNavigation: true,
22
+ * customTooltip: {
23
+ * },
24
+ * };
25
+ */
26
+ export class SlickCustomTooltip {
27
+ constructor() {
28
+ this._cellType = 'slick-cell';
29
+ this._rxjs = null;
30
+ this._sharedService = null;
31
+ this._defaultOptions = {
32
+ className: 'slick-custom-tooltip',
33
+ offsetLeft: 0,
34
+ offsetRight: 0,
35
+ offsetTopBottom: 4,
36
+ hideArrow: false,
37
+ regularTooltipWhiteSpace: 'pre-line',
38
+ whiteSpace: 'normal',
39
+ };
40
+ this.name = 'CustomTooltip';
41
+ this._eventHandler = new Slick.EventHandler();
42
+ }
43
+ get addonOptions() {
44
+ return this._addonOptions;
45
+ }
46
+ get cancellablePromise() {
47
+ return this._cancellablePromise;
48
+ }
49
+ get cellAddonOptions() {
50
+ return this._cellAddonOptions;
51
+ }
52
+ get className() {
53
+ var _a, _b;
54
+ return (_b = (_a = this._cellAddonOptions) === null || _a === void 0 ? void 0 : _a.className) !== null && _b !== void 0 ? _b : 'slick-custom-tooltip';
55
+ }
56
+ get dataView() {
57
+ return this._grid.getData() || {};
58
+ }
59
+ /** Getter for the Grid Options pulled through the Grid Object */
60
+ get gridOptions() {
61
+ return this._grid.getOptions() || {};
62
+ }
63
+ /** Getter for the grid uid */
64
+ get gridUid() {
65
+ var _a;
66
+ return ((_a = this._grid) === null || _a === void 0 ? void 0 : _a.getUID()) || '';
67
+ }
68
+ get gridUidSelector() {
69
+ return this.gridUid ? `.${this.gridUid}` : '';
70
+ }
71
+ get tooltipElm() {
72
+ return this._tooltipElm;
73
+ }
74
+ addRxJsResource(rxjs) {
75
+ this._rxjs = rxjs;
76
+ }
77
+ init(grid, containerService) {
78
+ var _a, _b;
79
+ this._grid = grid;
80
+ this._rxjs = containerService.get('RxJsFacade');
81
+ this._sharedService = containerService.get('SharedService');
82
+ this._addonOptions = { ...this._defaultOptions, ...((_b = (_a = this._sharedService) === null || _a === void 0 ? void 0 : _a.gridOptions) === null || _b === void 0 ? void 0 : _b.customTooltip) };
83
+ this._eventHandler
84
+ .subscribe(grid.onMouseEnter, this.handleOnMouseEnter.bind(this))
85
+ .subscribe(grid.onHeaderMouseEnter, (e, args) => this.handleOnHeaderMouseEnterByType(e, args, 'slick-header-column'))
86
+ .subscribe(grid.onHeaderRowMouseEnter, (e, args) => this.handleOnHeaderMouseEnterByType(e, args, 'slick-headerrow-column'))
87
+ .subscribe(grid.onMouseLeave, this.hideTooltip.bind(this))
88
+ .subscribe(grid.onHeaderMouseLeave, this.hideTooltip.bind(this))
89
+ .subscribe(grid.onHeaderRowMouseLeave, this.hideTooltip.bind(this));
90
+ }
91
+ dispose() {
92
+ // hide (remove) any tooltip and unsubscribe from all events
93
+ this.hideTooltip();
94
+ this._cancellablePromise = undefined;
95
+ this._eventHandler.unsubscribeAll();
96
+ }
97
+ /**
98
+ * hide (remove) tooltip from the DOM, it will also remove it from the DOM and also cancel any pending requests (as mentioned below).
99
+ * When using async process, it will also cancel any opened Promise/Observable that might still be pending.
100
+ */
101
+ hideTooltip() {
102
+ var _a, _b;
103
+ (_a = this._cancellablePromise) === null || _a === void 0 ? void 0 : _a.cancel();
104
+ (_b = this._observable$) === null || _b === void 0 ? void 0 : _b.unsubscribe();
105
+ const prevTooltip = document.body.querySelector(`.${this.className}${this.gridUidSelector}`);
106
+ prevTooltip === null || prevTooltip === void 0 ? void 0 : prevTooltip.remove();
107
+ }
108
+ getOptions() {
109
+ return this._addonOptions;
110
+ }
111
+ setOptions(newOptions) {
112
+ this._addonOptions = { ...this._addonOptions, ...newOptions };
113
+ }
114
+ // --
115
+ // protected functions
116
+ // ---------------------
117
+ /**
118
+ * Async process callback will hide any prior tooltip & then merge the new result with the item `dataContext` under a `__params` property
119
+ * (unless a new prop name is provided) to provice as dataContext object to the asyncPostFormatter.
120
+ */
121
+ asyncProcessCallback(asyncResult, cell, value, columnDef, dataContext) {
122
+ var _a, _b, _c;
123
+ this.hideTooltip();
124
+ const itemWithAsyncData = { ...dataContext, [(_b = (_a = this.addonOptions) === null || _a === void 0 ? void 0 : _a.asyncParamsPropName) !== null && _b !== void 0 ? _b : '__params']: asyncResult };
125
+ if ((_c = this._cellAddonOptions) === null || _c === void 0 ? void 0 : _c.useRegularTooltip) {
126
+ this.renderRegularTooltip(this._cellAddonOptions.asyncPostFormatter, cell, value, columnDef, itemWithAsyncData);
127
+ }
128
+ else {
129
+ this.renderTooltipFormatter(this._cellAddonOptions.asyncPostFormatter, cell, value, columnDef, itemWithAsyncData);
130
+ }
131
+ }
132
+ /** depending on the selector type, execute the necessary handler code */
133
+ handleOnHeaderMouseEnterByType(event, args, selector) {
134
+ var _a, _b, _c;
135
+ this._cellType = selector;
136
+ // before doing anything, let's remove any previous tooltip before
137
+ // and cancel any opened Promise/Observable when using async
138
+ this.hideTooltip();
139
+ const cell = {
140
+ row: -1,
141
+ cell: this._grid.getColumns().findIndex((col) => { var _a, _b; return ((_b = (_a = args === null || args === void 0 ? void 0 : args.column) === null || _a === void 0 ? void 0 : _a.id) !== null && _b !== void 0 ? _b : '') === col.id; })
142
+ };
143
+ const columnDef = args.column;
144
+ const item = {};
145
+ const isHeaderRowType = selector === 'slick-headerrow-column';
146
+ // run the override function (when defined), if the result is false it won't go further
147
+ args = args || {};
148
+ args.cell = cell.cell;
149
+ args.row = cell.row;
150
+ args.columnDef = columnDef;
151
+ args.dataContext = item;
152
+ args.grid = this._grid;
153
+ args.type = isHeaderRowType ? 'header-row' : 'header';
154
+ this._cellAddonOptions = { ...this._addonOptions, ...(columnDef === null || columnDef === void 0 ? void 0 : columnDef.customTooltip) };
155
+ if ((columnDef === null || columnDef === void 0 ? void 0 : columnDef.disableTooltip) || (typeof ((_a = this._cellAddonOptions) === null || _a === void 0 ? void 0 : _a.usabilityOverride) === 'function' && !this._cellAddonOptions.usabilityOverride(args))) {
156
+ return;
157
+ }
158
+ if (columnDef && event.target) {
159
+ this._cellNodeElm = event.target.closest(`.${selector}`);
160
+ const formatter = isHeaderRowType ? this._cellAddonOptions.headerRowFormatter : this._cellAddonOptions.headerFormatter;
161
+ if (((_b = this._cellAddonOptions) === null || _b === void 0 ? void 0 : _b.useRegularTooltip) || !formatter) {
162
+ const formatterOrText = !isHeaderRowType ? columnDef.name : ((_c = this._cellAddonOptions) === null || _c === void 0 ? void 0 : _c.useRegularTooltip) ? null : formatter;
163
+ this.renderRegularTooltip(formatterOrText, cell, null, columnDef, item);
164
+ }
165
+ else if (this._cellNodeElm && typeof formatter === 'function') {
166
+ this.renderTooltipFormatter(formatter, cell, null, columnDef, item);
167
+ }
168
+ }
169
+ }
170
+ async handleOnMouseEnter(event) {
171
+ var _a, _b, _c, _d, _e, _f, _g, _h;
172
+ this._cellType = 'slick-cell';
173
+ // before doing anything, let's remove any previous tooltip before
174
+ // and cancel any opened Promise/Observable when using async
175
+ this.hideTooltip();
176
+ if (event && this._grid) {
177
+ // get cell only when it's possible (ie, Composite Editor will not be able to get cell and so it will never show any tooltip)
178
+ const targetClassName = (_b = (_a = event === null || event === void 0 ? void 0 : event.target) === null || _a === void 0 ? void 0 : _a.closest('.slick-cell')) === null || _b === void 0 ? void 0 : _b.className;
179
+ const cell = (targetClassName && /l\d+/.exec(targetClassName || '')) ? this._grid.getCellFromEvent(event) : null;
180
+ if (cell) {
181
+ const item = this.dataView ? this.dataView.getItem(cell.row) : this._grid.getDataItem(cell.row);
182
+ const columnDef = this._grid.getColumns()[cell.cell];
183
+ this._cellNodeElm = this._grid.getCellNode(cell.row, cell.cell);
184
+ if (item && columnDef) {
185
+ this._cellAddonOptions = { ...this._addonOptions, ...(columnDef === null || columnDef === void 0 ? void 0 : columnDef.customTooltip) };
186
+ if ((columnDef === null || columnDef === void 0 ? void 0 : columnDef.disableTooltip) || (typeof ((_c = this._cellAddonOptions) === null || _c === void 0 ? void 0 : _c.usabilityOverride) === 'function' && !this._cellAddonOptions.usabilityOverride({ cell: cell.cell, row: cell.row, dataContext: item, column: columnDef, grid: this._grid, type: 'cell' }))) {
187
+ return;
188
+ }
189
+ const value = item.hasOwnProperty(columnDef.field) ? item[columnDef.field] : null;
190
+ // when cell is currently lock for editing, we'll force a tooltip title search
191
+ const cellValue = this._grid.getEditorLock().isActive() ? null : value;
192
+ // when there aren't any formatter OR when user specifically want to use a regular tooltip (via "title" attribute)
193
+ if ((this._cellAddonOptions.useRegularTooltip && !((_d = this._cellAddonOptions) === null || _d === void 0 ? void 0 : _d.asyncProcess)) || !((_e = this._cellAddonOptions) === null || _e === void 0 ? void 0 : _e.formatter)) {
194
+ this.renderRegularTooltip(columnDef.formatter, cell, cellValue, columnDef, item);
195
+ }
196
+ else {
197
+ // when we aren't using regular tooltip and we do have a tooltip formatter, let's render it
198
+ if (typeof ((_f = this._cellAddonOptions) === null || _f === void 0 ? void 0 : _f.formatter) === 'function') {
199
+ this.renderTooltipFormatter(this._cellAddonOptions.formatter, cell, cellValue, columnDef, item);
200
+ }
201
+ // when tooltip is an Async (delayed, e.g. with a backend API call)
202
+ if (typeof ((_g = this._cellAddonOptions) === null || _g === void 0 ? void 0 : _g.asyncProcess) === 'function') {
203
+ const asyncProcess = this._cellAddonOptions.asyncProcess(cell.row, cell.cell, value, columnDef, item, this._grid);
204
+ if (!this._cellAddonOptions.asyncPostFormatter) {
205
+ console.error(`[Slickgrid-Universal] when using "asyncProcess" with Custom Tooltip, you must also provide an "asyncPostFormatter" formatter.`);
206
+ }
207
+ if (asyncProcess instanceof Promise) {
208
+ // create a new cancellable promise which will resolve, unless it's cancelled, with the udpated `dataContext` object that includes the `__params`
209
+ this._cancellablePromise = cancellablePromise(asyncProcess);
210
+ this._cancellablePromise.promise
211
+ .then((asyncResult) => this.asyncProcessCallback(asyncResult, cell, value, columnDef, item))
212
+ .catch((error) => {
213
+ // we will throw back any errors, unless it's a cancelled promise which in that case will be disregarded (thrown by the promise wrapper cancel() call)
214
+ if (!(error instanceof CancelledException)) {
215
+ console.error(error);
216
+ }
217
+ });
218
+ }
219
+ else if ((_h = this._rxjs) === null || _h === void 0 ? void 0 : _h.isObservable(asyncProcess)) {
220
+ const rxjs = this._rxjs;
221
+ this._observable$ = asyncProcess
222
+ .pipe(
223
+ // use `switchMap` so that it cancels any previous subscription, it must return an observable so we can use `of` for that, and then finally we can subscribe to the new observable
224
+ rxjs.switchMap((asyncResult) => rxjs.of(asyncResult))).subscribe((asyncResult) => this.asyncProcessCallback(asyncResult, cell, value, columnDef, item), (error) => console.error(error));
225
+ }
226
+ }
227
+ }
228
+ }
229
+ }
230
+ }
231
+ }
232
+ /**
233
+ * Parse the Custom Formatter (when provided) or return directly the text when it is already a string.
234
+ * We will also sanitize the text in both cases before returning it so that it can be used safely.
235
+ */
236
+ parseFormatterAndSanitize(formatterOrText, cell, value, columnDef, item) {
237
+ if (typeof formatterOrText === 'function') {
238
+ const tooltipText = formatterOrText(cell.row, cell.cell, value, columnDef, item, this._grid);
239
+ const formatterText = (typeof tooltipText === 'object' && (tooltipText === null || tooltipText === void 0 ? void 0 : tooltipText.text)) ? tooltipText.text : (typeof tooltipText === 'string' ? tooltipText : '');
240
+ return sanitizeTextByAvailableSanitizer(this.gridOptions, formatterText);
241
+ }
242
+ else if (typeof formatterOrText === 'string') {
243
+ return sanitizeTextByAvailableSanitizer(this.gridOptions, formatterOrText);
244
+ }
245
+ return '';
246
+ }
247
+ /**
248
+ * Parse the cell formatter and assume it might be html
249
+ * then create a temporary html element to easily retrieve the first [title=""] attribute text content
250
+ * also clear the "title" attribute from the grid div text content so that it won't show also as a 2nd browser tooltip
251
+ */
252
+ renderRegularTooltip(formatterOrText, cell, value, columnDef, item) {
253
+ var _a, _b, _c, _d, _e, _f, _g, _h;
254
+ const tmpDiv = createDomElement('div', { innerHTML: this.parseFormatterAndSanitize(formatterOrText, cell, value, columnDef, item) });
255
+ let tooltipText = (_a = columnDef === null || columnDef === void 0 ? void 0 : columnDef.toolTip) !== null && _a !== void 0 ? _a : '';
256
+ let tmpTitleElm;
257
+ if (!tooltipText) {
258
+ if (this._cellType === 'slick-cell' && this._cellNodeElm && (this._cellNodeElm.clientWidth < this._cellNodeElm.scrollWidth) && !((_b = this._cellAddonOptions) === null || _b === void 0 ? void 0 : _b.useRegularTooltipFromFormatterOnly)) {
259
+ tooltipText = (_d = (_c = this._cellNodeElm.textContent) === null || _c === void 0 ? void 0 : _c.trim()) !== null && _d !== void 0 ? _d : '';
260
+ if (((_e = this._cellAddonOptions) === null || _e === void 0 ? void 0 : _e.tooltipTextMaxLength) && tooltipText.length > ((_f = this._cellAddonOptions) === null || _f === void 0 ? void 0 : _f.tooltipTextMaxLength)) {
261
+ tooltipText = tooltipText.substring(0, this._cellAddonOptions.tooltipTextMaxLength - 3) + '...';
262
+ }
263
+ tmpTitleElm = this._cellNodeElm;
264
+ }
265
+ else {
266
+ if ((_g = this._cellAddonOptions) === null || _g === void 0 ? void 0 : _g.useRegularTooltipFromFormatterOnly) {
267
+ tmpTitleElm = tmpDiv.querySelector('[title], [data-slick-tooltip]');
268
+ }
269
+ else {
270
+ tmpTitleElm = findFirstElementAttribute(this._cellNodeElm, ['title', 'data-slick-tooltip']) ? this._cellNodeElm : tmpDiv.querySelector('[title], [data-slick-tooltip]');
271
+ if ((!tmpTitleElm || !findFirstElementAttribute(tmpTitleElm, ['title', 'data-slick-tooltip'])) && this._cellNodeElm) {
272
+ tmpTitleElm = this._cellNodeElm.querySelector('[title], [data-slick-tooltip]');
273
+ }
274
+ }
275
+ if (!tooltipText || (typeof formatterOrText === 'function' && ((_h = this._cellAddonOptions) === null || _h === void 0 ? void 0 : _h.useRegularTooltipFromFormatterOnly))) {
276
+ tooltipText = findFirstElementAttribute(tmpTitleElm, ['title', 'data-slick-tooltip']) || '';
277
+ }
278
+ }
279
+ }
280
+ if (tooltipText !== '') {
281
+ this.renderTooltipFormatter(formatterOrText, cell, value, columnDef, item, tooltipText);
282
+ }
283
+ // also clear any "title" attribute to avoid showing a 2nd browser tooltip
284
+ this.swapAndClearTitleAttribute(tmpTitleElm, tooltipText);
285
+ }
286
+ renderTooltipFormatter(formatter, cell, value, columnDef, item, tooltipText, inputTitleElm) {
287
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
288
+ // create the tooltip DOM element with the text returned by the Formatter
289
+ this._tooltipElm = createDomElement('div', { className: this.className });
290
+ this._tooltipElm.classList.add(this.gridUid);
291
+ this._tooltipElm.classList.add('l' + cell.cell);
292
+ this._tooltipElm.classList.add('r' + cell.cell);
293
+ // when cell is currently lock for editing, we'll force a tooltip title search
294
+ // that can happen when user has a formatter but is currently editing and in that case we want the new value
295
+ // ie: when user is currently editing and uses the Slider, when dragging its value is changing, so we wish to use the editing value instead of the previous cell value.
296
+ if (value === null || value === undefined) {
297
+ const tmpTitleElm = (_a = this._cellNodeElm) === null || _a === void 0 ? void 0 : _a.querySelector('[title], [data-slick-tooltip]');
298
+ value = findFirstElementAttribute(tmpTitleElm, ['title', 'data-slick-tooltip']) || value;
299
+ }
300
+ let outputText = tooltipText || this.parseFormatterAndSanitize(formatter, cell, value, columnDef, item) || '';
301
+ outputText = (((_b = this._cellAddonOptions) === null || _b === void 0 ? void 0 : _b.tooltipTextMaxLength) && outputText.length > this._cellAddonOptions.tooltipTextMaxLength) ? outputText.substring(0, this._cellAddonOptions.tooltipTextMaxLength - 3) + '...' : outputText;
302
+ let finalOutputText = '';
303
+ if (!tooltipText || ((_c = this._cellAddonOptions) === null || _c === void 0 ? void 0 : _c.renderRegularTooltipAsHtml)) {
304
+ finalOutputText = sanitizeTextByAvailableSanitizer(this.gridOptions, outputText);
305
+ this._tooltipElm.innerHTML = finalOutputText;
306
+ this._tooltipElm.style.whiteSpace = (_e = (_d = this._cellAddonOptions) === null || _d === void 0 ? void 0 : _d.whiteSpace) !== null && _e !== void 0 ? _e : this._defaultOptions.whiteSpace;
307
+ }
308
+ else {
309
+ finalOutputText = outputText || '';
310
+ this._tooltipElm.textContent = finalOutputText;
311
+ this._tooltipElm.style.whiteSpace = (_g = (_f = this._cellAddonOptions) === null || _f === void 0 ? void 0 : _f.regularTooltipWhiteSpace) !== null && _g !== void 0 ? _g : this._defaultOptions.regularTooltipWhiteSpace; // use `pre` so that sequences of white space are collapsed. Lines are broken at newline characters
312
+ }
313
+ // optional max height/width of the tooltip container
314
+ if ((_h = this._cellAddonOptions) === null || _h === void 0 ? void 0 : _h.maxHeight) {
315
+ this._tooltipElm.style.maxHeight = `${this._cellAddonOptions.maxHeight}px`;
316
+ }
317
+ if ((_j = this._cellAddonOptions) === null || _j === void 0 ? void 0 : _j.maxWidth) {
318
+ this._tooltipElm.style.maxWidth = `${this._cellAddonOptions.maxWidth}px`;
319
+ }
320
+ // when do have text to show, then append the new tooltip to the html body & reposition the tooltip
321
+ if (finalOutputText) {
322
+ document.body.appendChild(this._tooltipElm);
323
+ // reposition the tooltip on top of the cell that triggered the mouse over event
324
+ this.reposition(cell);
325
+ // user could optionally hide the tooltip arrow (we can simply update the CSS variables, that's the only way we have to update CSS pseudo)
326
+ if (!((_k = this._cellAddonOptions) === null || _k === void 0 ? void 0 : _k.hideArrow)) {
327
+ this._tooltipElm.classList.add('tooltip-arrow');
328
+ }
329
+ // also clear any "title" attribute to avoid showing a 2nd browser tooltip
330
+ this.swapAndClearTitleAttribute(inputTitleElm, outputText);
331
+ }
332
+ }
333
+ /**
334
+ * Reposition the Tooltip to be top-left position over the cell.
335
+ * By default we use an "auto" mode which will allow to position the Tooltip to the best logical position in the window, also when we mention position, we are talking about the relative position against the grid cell.
336
+ * We can assume that in 80% of the time the default position is top-right, the default is "auto" but we can also override it and use a specific position.
337
+ * Most of the time positioning of the tooltip will be to the "top-right" of the cell is ok but if our column is completely on the right side then we'll want to change the position to "left" align.
338
+ * Same goes for the top/bottom position, Most of the time positioning the tooltip to the "top" but if we are hovering a cell at the top of the grid and there's no room to display it then we might need to reposition to "bottom" instead.
339
+ */
340
+ reposition(cell) {
341
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
342
+ if (this._tooltipElm) {
343
+ this._cellNodeElm = this._cellNodeElm || this._grid.getCellNode(cell.row, cell.cell);
344
+ const cellPosition = getHtmlElementOffset(this._cellNodeElm) || { top: 0, left: 0 };
345
+ const cellContainerWidth = this._cellNodeElm.offsetWidth;
346
+ const calculatedTooltipHeight = this._tooltipElm.getBoundingClientRect().height;
347
+ const calculatedTooltipWidth = this._tooltipElm.getBoundingClientRect().width;
348
+ const calculatedBodyWidth = document.body.offsetWidth || window.innerWidth;
349
+ // first calculate the default (top/left) position
350
+ let newPositionTop = (cellPosition.top || 0) - this._tooltipElm.offsetHeight - ((_b = (_a = this._cellAddonOptions) === null || _a === void 0 ? void 0 : _a.offsetTopBottom) !== null && _b !== void 0 ? _b : 0);
351
+ let newPositionLeft = (cellPosition.left || 0) - ((_d = (_c = this._cellAddonOptions) === null || _c === void 0 ? void 0 : _c.offsetRight) !== null && _d !== void 0 ? _d : 0);
352
+ // user could explicitely use a "left-align" arrow position, (when user knows his column is completely on the right in the grid)
353
+ // or when using "auto" and we detect not enough available space then we'll position to the "left" of the cell
354
+ // NOTE the class name is for the arrow and is inverse compare to the tooltip itself, so if user ask for "left-align", then the arrow will in fact be "arrow-right-align"
355
+ const position = (_f = (_e = this._cellAddonOptions) === null || _e === void 0 ? void 0 : _e.position) !== null && _f !== void 0 ? _f : 'auto';
356
+ if (position === 'center') {
357
+ newPositionLeft += (cellContainerWidth / 2) - (calculatedTooltipWidth / 2) + ((_h = (_g = this._cellAddonOptions) === null || _g === void 0 ? void 0 : _g.offsetRight) !== null && _h !== void 0 ? _h : 0);
358
+ this._tooltipElm.classList.remove('arrow-left-align');
359
+ this._tooltipElm.classList.remove('arrow-right-align');
360
+ this._tooltipElm.classList.add('arrow-center-align');
361
+ }
362
+ else if (position === 'right-align' || ((position === 'auto' || position !== 'left-align') && (newPositionLeft + calculatedTooltipWidth) > calculatedBodyWidth)) {
363
+ newPositionLeft -= (calculatedTooltipWidth - cellContainerWidth - ((_k = (_j = this._cellAddonOptions) === null || _j === void 0 ? void 0 : _j.offsetLeft) !== null && _k !== void 0 ? _k : 0));
364
+ this._tooltipElm.classList.remove('arrow-center-align');
365
+ this._tooltipElm.classList.remove('arrow-left-align');
366
+ this._tooltipElm.classList.add('arrow-right-align');
367
+ }
368
+ else {
369
+ this._tooltipElm.classList.remove('arrow-center-align');
370
+ this._tooltipElm.classList.remove('arrow-right-align');
371
+ this._tooltipElm.classList.add('arrow-left-align');
372
+ }
373
+ // do the same calculation/reposition with top/bottom (default is top of the cell or in other word starting from the cell going down)
374
+ // NOTE the class name is for the arrow and is inverse compare to the tooltip itself, so if user ask for "bottom", then the arrow will in fact be "arrow-top"
375
+ if (position === 'bottom' || ((position === 'auto' || position !== 'top') && calculatedTooltipHeight > calculateAvailableSpace(this._cellNodeElm).top)) {
376
+ newPositionTop = (cellPosition.top || 0) + ((_l = this.gridOptions.rowHeight) !== null && _l !== void 0 ? _l : 0) + ((_o = (_m = this._cellAddonOptions) === null || _m === void 0 ? void 0 : _m.offsetTopBottom) !== null && _o !== void 0 ? _o : 0);
377
+ this._tooltipElm.classList.remove('arrow-down');
378
+ this._tooltipElm.classList.add('arrow-up');
379
+ }
380
+ else {
381
+ this._tooltipElm.classList.add('arrow-down');
382
+ this._tooltipElm.classList.remove('arrow-up');
383
+ }
384
+ // reposition the tooltip over the cell (90% of the time this will end up using a position on the "right" of the cell)
385
+ this._tooltipElm.style.top = `${newPositionTop}px`;
386
+ this._tooltipElm.style.left = `${newPositionLeft}px`;
387
+ }
388
+ }
389
+ /**
390
+ * swap and copy the "title" attribute into a new custom attribute then clear the "title" attribute
391
+ * from the grid div text content so that it won't show also as a 2nd browser tooltip
392
+ */
393
+ swapAndClearTitleAttribute(inputTitleElm, tooltipText) {
394
+ var _a;
395
+ // the title attribute might be directly on the slick-cell container element (when formatter returns a result object)
396
+ // OR in a child element (most commonly as a custom formatter)
397
+ const titleElm = inputTitleElm || (this._cellNodeElm && ((this._cellNodeElm.hasAttribute('title') && this._cellNodeElm.getAttribute('title')) ? this._cellNodeElm : (_a = this._cellNodeElm) === null || _a === void 0 ? void 0 : _a.querySelector('[title]')));
398
+ // flip tooltip text from `title` to `data-slick-tooltip`
399
+ if (titleElm) {
400
+ titleElm.setAttribute('data-slick-tooltip', tooltipText || '');
401
+ if (titleElm.hasAttribute('title')) {
402
+ titleElm.setAttribute('title', '');
403
+ }
404
+ }
405
+ }
406
+ }
404
407
  //# sourceMappingURL=slickCustomTooltip.js.map