* Gets the current cell height in pixels. This takes into account the unit type and converts to pixels if necessary. * * @param forcePixel if true, forces conversion to pixels even when cellHeight is specified in other units * @returns the cell height in pixels * * @example * const
(forcePixel = false)
| 855 | * const pixelHeight = grid.getCellHeight(true); |
| 856 | */ |
| 857 | public getCellHeight(forcePixel = false): number { |
| 858 | if (this.opts.cellHeight && this.opts.cellHeight !== 'auto' && |
| 859 | (!forcePixel || !this.opts.cellHeightUnit || this.opts.cellHeightUnit === 'px')) { |
| 860 | return this.opts.cellHeight as number; |
| 861 | } |
| 862 | // do rem/em/cm/mm to px conversion |
| 863 | if (this.opts.cellHeightUnit === 'rem') { |
| 864 | return (this.opts.cellHeight as number) * parseFloat(getComputedStyle(document.documentElement).fontSize); |
| 865 | } |
| 866 | if (this.opts.cellHeightUnit === 'em') { |
| 867 | return (this.opts.cellHeight as number) * parseFloat(getComputedStyle(this.el).fontSize); |
| 868 | } |
| 869 | if (this.opts.cellHeightUnit === 'cm') { |
| 870 | // 1cm = 96px/2.54. See https://www.w3.org/TR/css-values-3/#absolute-lengths |
| 871 | return (this.opts.cellHeight as number) * (96 / 2.54); |
| 872 | } |
| 873 | if (this.opts.cellHeightUnit === 'mm') { |
| 874 | return (this.opts.cellHeight as number) * (96 / 2.54) / 10; |
| 875 | } |
| 876 | // else get first cell height |
| 877 | const el = this.el.querySelector('.' + this.opts.itemClass) as HTMLElement; |
| 878 | if (el) { |
| 879 | const h = Utils.toNumber(el.getAttribute('gs-h')) || 1; // since we don't write 1 anymore |
| 880 | return Math.round(el.offsetHeight / h); |
| 881 | } |
| 882 | // else do entire grid and # of rows (but doesn't work if min-height is the actual constrain) |
| 883 | const rows = parseInt(this.el.getAttribute('gs-current-row')); |
| 884 | return rows ? Math.round(this.el.getBoundingClientRect().height / rows) : this.opts.cellHeight as number; |
| 885 | } |
| 886 | |
| 887 | /** |
| 888 | * Update current cell height - see `GridStackOptions.cellHeight` for format by updating eh Browser CSS variable. |
no test coverage detected