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