-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(plugins): replace AutoTooltips Extension by plugin
- use the code from the AutoTooltips SlickGrid Plugin code and implement it directly in the lib instead of having an extension that calls the plugin which is a bridge.
- Loading branch information
1 parent
95a6635
commit 80df14d
Showing
15 changed files
with
174 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 0 additions & 72 deletions
72
packages/common/src/extensions/__tests__/autoTooltipExtension.spec.ts
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 0 additions & 15 deletions
15
packages/common/src/interfaces/slickAutoTooltips.interface.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
packages/common/src/plugins/__tests__/autoTooltips.plugin.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { GridOption, SlickGrid, SlickNamespace } from '../../interfaces/index'; | ||
import { SharedService } from '../../services/shared.service'; | ||
import { AutoTooltipsPlugin } from '../autoTooltips.plugin'; | ||
|
||
declare const Slick: SlickNamespace; | ||
|
||
const gridStub = { | ||
getOptions: jest.fn(), | ||
registerPlugin: jest.fn(), | ||
onHeaderMouseEnter: new Slick.Event(), | ||
onMouseEnter: new Slick.Event(), | ||
} as unknown as SlickGrid; | ||
|
||
const mockAddon = jest.fn().mockImplementation(() => ({ | ||
init: jest.fn(), | ||
destroy: jest.fn() | ||
})); | ||
|
||
describe('AutoTooltip Plugin', () => { | ||
jest.mock('slickgrid/plugins/slick.autotooltips', () => mockAddon); | ||
Slick.AutoTooltips = mockAddon; | ||
|
||
let plugin: AutoTooltipsPlugin; | ||
let sharedService: SharedService; | ||
|
||
beforeEach(() => { | ||
sharedService = new SharedService(); | ||
plugin = new AutoTooltipsPlugin(); | ||
}); | ||
|
||
it('should create the plugin', () => { | ||
expect(plugin).toBeTruthy(); | ||
expect(plugin.eventHandler).toBeTruthy(); | ||
}); | ||
|
||
it('should use default options when instantiating the plugin without passing any arguments', () => { | ||
plugin.init(gridStub); | ||
|
||
expect(plugin.options).toEqual({ | ||
enableForCells: true, | ||
enableForHeaderCells: false, | ||
maxToolTipLength: undefined, | ||
replaceExisting: true | ||
}); | ||
}); | ||
|
||
// describe('registered addon', () => { | ||
// beforeEach(() => { | ||
// jest.spyOn(SharedService.prototype, 'slickGrid', 'get').mockReturnValue(gridStub); | ||
// jest.spyOn(SharedService.prototype, 'gridOptions', 'get').mockReturnValue(gridOptionsMock); | ||
// }); | ||
|
||
// it('should ', () => { | ||
|
||
// }); | ||
// }); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { | ||
AutoTooltipOption, | ||
Column, | ||
GetSlickEventType, | ||
SlickEventHandler, | ||
SlickGrid, | ||
SlickNamespace, | ||
} from '../interfaces/index'; | ||
|
||
// using external SlickGrid JS libraries | ||
declare const Slick: SlickNamespace; | ||
|
||
export class AutoTooltipsPlugin { | ||
private _eventHandler!: SlickEventHandler; | ||
private _grid!: SlickGrid; | ||
private _options?: AutoTooltipOption; | ||
private _defaults = { | ||
enableForCells: true, | ||
enableForHeaderCells: false, | ||
maxToolTipLength: undefined, | ||
replaceExisting: true | ||
} as AutoTooltipOption; | ||
pluginName: 'AutoTooltips' = 'AutoTooltips'; | ||
|
||
/** Constructor of the SlickGrid 3rd party plugin, it can optionally receive options */ | ||
constructor(options?: AutoTooltipOption) { | ||
this._eventHandler = new Slick.EventHandler(); | ||
this._options = options; | ||
} | ||
|
||
get eventHandler(): SlickEventHandler { | ||
return this._eventHandler; | ||
} | ||
|
||
get options(): AutoTooltipOption { | ||
return this._options as AutoTooltipOption; | ||
} | ||
|
||
/** Initialize plugin. */ | ||
init(grid: SlickGrid) { | ||
this._options = { ...this._defaults, ...this._options }; | ||
this._grid = grid; | ||
if (this._options.enableForCells) { | ||
const onMouseEnterHandler = this._grid.onMouseEnter; | ||
(this._eventHandler as SlickEventHandler<GetSlickEventType<typeof onMouseEnterHandler>>).subscribe(onMouseEnterHandler, this.handleMouseEnter.bind(this)); | ||
} | ||
if (this._options.enableForHeaderCells) { | ||
const onHeaderMouseEnterHandler = this._grid.onHeaderMouseEnter; | ||
(this._eventHandler as SlickEventHandler<GetSlickEventType<typeof onHeaderMouseEnterHandler>>).subscribe(onHeaderMouseEnterHandler, this.handleHeaderMouseEnter.bind(this)); | ||
} | ||
} | ||
|
||
/** Destroy (dispose) the SlickGrid 3rd party plugin */ | ||
destroy() { | ||
this._eventHandler?.unsubscribeAll(); | ||
} | ||
|
||
// -- | ||
// private functions | ||
// ------------------ | ||
|
||
/** | ||
* Handle mouse entering grid cell to add/remove tooltip. | ||
* @param {Object} event - The event | ||
*/ | ||
private handleMouseEnter(event: Event) { | ||
const cell = this._grid.getCellFromEvent(event); | ||
if (cell) { | ||
let node: HTMLElement | null = this._grid.getCellNode(cell.row, cell.cell); | ||
let text; | ||
if (this._options && node && (!node.title || this._options?.replaceExisting)) { | ||
if (node.clientWidth < node.scrollWidth) { | ||
text = node.textContent?.trim() ?? ''; | ||
if (this._options?.maxToolTipLength && text.length > this._options?.maxToolTipLength) { | ||
text = text.substr(0, this._options.maxToolTipLength - 3) + '...'; | ||
} | ||
} else { | ||
text = ''; | ||
} | ||
node.title = text; | ||
} | ||
node = null; | ||
} | ||
} | ||
|
||
/** | ||
* Handle mouse entering header cell to add/remove tooltip. | ||
* @param {Object} event - The event | ||
* @param {Object} args.column - The column definition | ||
*/ | ||
private handleHeaderMouseEnter(event: Event, args: { column: Column; }) { | ||
const column = args.column; | ||
let node = (event.target as HTMLDivElement).querySelector<HTMLDivElement>('.slick-header-column'); | ||
if (node && !column?.toolTip) { | ||
node.title = (node.clientWidth < node.scrollWidth) ? column.name ?? '' : ''; | ||
} | ||
node = null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './autoTooltips.plugin'; |
Oops, something went wrong.