Skip to content

Commit

Permalink
Revert more internal renderer and export via namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkh committed Mar 30, 2020
1 parent d527cc8 commit a61ba84
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/renderer/BrightnessCellRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {IRenderContext, ERenderMode, ICellRendererFactory, IImposer, ICellRender
import {renderMissingCanvas, renderMissingDOM} from './missing';
import {noRenderer, setText} from './utils';

/** @internal */
export function toHeatMapColor(v: number | null, row: IDataRow, col: INumberColumn, imposer?: IImposer) {
if (v == null || isNaN(v)) {
v = 1; // max = brightest
Expand Down
8 changes: 8 additions & 0 deletions src/renderer/histogram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export interface IHistogramLike<T> {
readonly hist: ReadonlyArray<IBin<T>>;
}

/** @internal */
export function histogramUpdate<T>(n: HTMLElement, stats: IHistogramLike<T>, unfiltered: IHistogramLike<T> | null, formatter: (v: T) => string, colorOf: (bin: IBin<T>) => string) {
const hist = stats.hist;
const nodes = matchBins(n, hist.length);
Expand Down Expand Up @@ -69,10 +70,16 @@ export function histogramUpdate<T>(n: HTMLElement, stats: IHistogramLike<T>, unf
});
}

/**
* @internal
*/
export function mappingHintTemplate(range: [string, string]) {
return `<span class="${cssClass('mapping-hint')}" title="${range[0]}">${range[0]}</span><span class="${cssClass('mapping-hint')}" title="${range[1]}">${range[1]}</span>`;
}

/**
* @internal
*/
export function mappingHintUpdate(n: HTMLElement, range: [string, string]) {
Array.from(n.getElementsByTagName('span')).forEach((d: HTMLElement, i) => d.textContent = range[i]);
}
Expand All @@ -87,6 +94,7 @@ export interface IFilterContext<T> {
domain: [T, T];
}

/** @internal */
export interface IFilterInfo<T> {
filterMissing: boolean;
filterMin: T;
Expand Down
25 changes: 23 additions & 2 deletions src/renderer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {default as AnnotationRenderer} from './AnnotationRenderer';
import {default as BarCellRenderer} from './BarCellRenderer';
import {default as BooleanCellRenderer} from './BooleanCellRenderer';
import {default as BoxplotCellRenderer, computeLabel} from './BoxplotCellRenderer';
import {default as BrightnessCellRenderer} from './BrightnessCellRenderer';
import {default as BrightnessCellRenderer, toHeatMapColor} from './BrightnessCellRenderer';
import {default as CategoricalCellRenderer, interactiveHist} from './CategoricalCellRenderer';
import {default as CategoricalHeatmapCellRenderer} from './CategoricalHeatmapCellRenderer';
import {default as CategoricalStackedDistributionlCellRenderer} from './CategoricalStackedDistributionlCellRenderer';
Expand All @@ -37,9 +37,12 @@ import {default as StringCellRenderer} from './StringCellRenderer';
import {default as TableCellRenderer, groupByKey} from './TableCellRenderer';
import {default as UpSetCellRenderer} from './UpSetCellRenderer';
import {default as VerticalBarCellRenderer} from './VerticalBarCellRenderer';
import {histogramUpdate, mappingHintTemplate, mappingHintUpdate} from './histogram';
import {noop, noRenderer, setText, forEach, forEachChild, matchColumns, wideEnough, wideEnoughCat, adaptTextColorToBgColor, adaptDynamicColorToBgColor, uniqueId, exampleText, multiLevelGridCSSClass, colorOf} from './utils';

export {HasCategoricalFilter} from './CategoricalCellRenderer';
export {ICols as IMultiLevelCols} from './MultiLevelCellRenderer';
export {IFilterInfo} from './histogram';

export const rendererClasses = {
AAggregatedGroupRenderer,
Expand Down Expand Up @@ -82,8 +85,26 @@ export const rendererClasses = {
export const rendererUtils = {
matchRows,
computeLabel,
toHeatMapColor,
interactiveHist,
createData,
line,
groupByKey
groupByKey,
histogramUpdate,
mappingHintTemplate,
mappingHintUpdate,
noop,
noRenderer,
setText,
forEach,
forEachChild,
matchColumns,
wideEnough,
wideEnoughCat,
adaptTextColorToBgColor,
adaptDynamicColorToBgColor,
uniqueId,
exampleText,
multiLevelGridCSSClass,
colorOf
};
14 changes: 14 additions & 0 deletions src/renderer/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@ import {cssClass} from '../styles';
import {IRenderContext} from '.';
import {ISequence} from '../internal';

/** @internal */
export function noop() {
// no op
}

/** @internal */
export const noRenderer = {
template: `<div></div>`,
update: noop
};

/** @internal */
export function setText<T extends Node>(node: T, text?: string): T {
if (text === undefined) {
return node;
Expand All @@ -36,11 +39,13 @@ export function setText<T extends Node>(node: T, text?: string): T {
* @param node
* @param selector
* @param callback
* @internal
*/
export function forEach<T extends Element>(node: Element, selector: string, callback: (d: T, i: number) => void) {
(<T[]>Array.from(node.querySelectorAll(selector))).forEach(callback);
}

/** @internal */
export function forEachChild<T extends Element>(node: Element, callback: (d: T, i: number) => void) {
(<T[]>Array.from(node.children)).forEach(callback);
}
Expand All @@ -49,6 +54,7 @@ export function forEachChild<T extends Element>(node: Element, callback: (d: T,
* matches the columns and the dom nodes representing them
* @param {HTMLElement} node row
* @param columns columns to check
* @internal
*/
export function matchColumns(node: HTMLElement, columns: {column: Column, template: string, rendererId: string}[], ctx: IRenderContext) {
if (node.childElementCount === 0) {
Expand Down Expand Up @@ -98,11 +104,13 @@ export function matchColumns(node: HTMLElement, columns: {column: Column, templa
});
}

/** @internal */
export function wideEnough(col: IArrayColumn<any>, length: number = col.labels.length) {
const w = col.getWidth();
return w / length > MIN_LABEL_WIDTH; // at least 30 pixel
}

/** @internal */
export function wideEnoughCat(col: ICategoricalLikeColumn) {
const w = col.getWidth();
return w / col.categories.length > MIN_LABEL_WIDTH; // at least 30 pixel
Expand All @@ -116,6 +124,7 @@ const adaptColorCache: {[bg: string]: string} = {};
* Adapts the text color for a given background color
* @param {string} bgColor as `#ff0000`
* @returns {string} returns `black` or `white` for best contrast
* @internal
*/
export function adaptTextColorToBgColor(bgColor: string): string {
const bak = adaptColorCache[bgColor];
Expand All @@ -133,6 +142,7 @@ export function adaptTextColorToBgColor(bgColor: string): string {
* @param {string} bgColor as `#ff0000`
* @param {string} title the title to render
* @param {number} width for which percentages of the cell this background applies (0..1)
* @internal
*/
export function adaptDynamicColorToBgColor(node: HTMLElement, bgColor: string, title: string, width: number) {
const adapt = adaptTextColorToBgColor(bgColor);
Expand All @@ -154,6 +164,7 @@ export function adaptDynamicColorToBgColor(node: HTMLElement, bgColor: string, t
}


/** @internal */
export const uniqueId: (prefix: string) => string = (function () {
// side effect but just within the function itself, so good for the library
let idCounter = 0;
Expand All @@ -163,6 +174,7 @@ export const uniqueId: (prefix: string) => string = (function () {

const NUM_EXAMPLE_VALUES = 5;

/** @internal */
export function exampleText(col: Column, rows: ISequence<IDataRow>) {
const examples = <string[]>[];
rows.every((row) => {
Expand All @@ -177,11 +189,13 @@ export function exampleText(col: Column, rows: ISequence<IDataRow>) {
}


/** @internal */
export function multiLevelGridCSSClass(idPrefix: string, column: Column) {
return cssClass(`stacked-${idPrefix}-${column.id}`);
}


/** @internal */
export function colorOf(col: Column) {
if (isMapAbleColumn(col)) {
return col.getColorMapping().apply(0);
Expand Down

0 comments on commit a61ba84

Please sign in to comment.