@adaptabletools/adaptable
Version:
Powerful AG Grid extension which provides advanced, cutting-edge functionality to meet all DataGrid requirements
233 lines (232 loc) • 9.16 kB
JavaScript
import { VersionUpgrade } from './VersionUpgrade';
import { mergeBarStyleCellTextProperties } from '../Utilities/Helpers/StyledColumns/BarStylesHelper';
const migrateLegacyPercentBarCellText = (legacy) => {
const tokens = legacy.CellText ?? [];
if (!tokens.length) {
return undefined;
}
const vertical = legacy.CellTextPosition ?? 'Below';
const layout = {};
if (tokens.includes('CellValue')) {
layout.CellValue = { Horizontal: 'Left', Vertical: vertical };
}
if (tokens.includes('PercentageValue')) {
layout.PercentValue = { Horizontal: 'Left', Vertical: vertical };
}
return mergeBarStyleCellTextProperties(undefined, { CellTextLayout: layout });
};
export class VersionUpgrade23 extends VersionUpgrade {
migrateState(state) {
this.migrateStyledColumnGradientZeroCentred(state);
this.migrateBadgeStyleToPillStyle(state);
this.migrateBadgeIconProperties(state);
this.migratePercentBarCellText(state);
this.patchStyledColumnName(state);
this.liftBadgeStyleRowScope(state);
return this.migrateSchedulingState(state);
}
migrateSchedulingState(state) {
if (!state.Schedule) {
return state;
}
const legacySchedule = state.Schedule;
const removedPluginCount = (legacySchedule.IPushPullSchedules?.length ?? 0) +
(legacySchedule.OpenFinSchedules?.length ?? 0);
const removedReportCount = legacySchedule.ReportSchedules?.length ?? 0;
const removedReminderCount = legacySchedule.Reminders?.length ?? 0;
if (removedPluginCount > 0) {
this.logger.info(`Removed ${removedPluginCount} ipushpull/OpenFin schedule(s).`);
}
if (removedReportCount > 0) {
this.logger.info(`Removed ${removedReportCount} legacy ReportSchedules from Schedule state.`);
}
if (removedReminderCount > 0) {
this.logger.info(`Removed ${removedReminderCount} legacy Reminders from Schedule state.`);
}
return state;
}
migratePercentBarCellText(state) {
const columns = state.StyledColumn?.StyledColumns;
if (!columns?.length) {
return;
}
for (const sc of columns) {
const percentBarStyle = sc.PercentBarStyle;
if (!percentBarStyle) {
continue;
}
const legacy = percentBarStyle;
if (legacy.CellText == undefined && legacy.CellTextPosition == undefined) {
continue;
}
const migrated = migrateLegacyPercentBarCellText(legacy);
delete legacy.CellText;
delete legacy.CellTextPosition;
if (migrated && !percentBarStyle.CellTextProperties) {
percentBarStyle.CellTextProperties = migrated;
this.logger.info(`Migrated v22 Percent Bar cell-text settings to per-value layout on column "${sc.ColumnId}" (v23).`);
}
}
}
migrateBadgeIconProperties(state) {
const columns = state.StyledColumn?.StyledColumns;
if (!columns?.length) {
return;
}
for (const sc of columns) {
const badges = sc.BadgeStyle?.Badges;
if (!badges?.length) {
continue;
}
let didMigrate = false;
for (const badge of badges) {
const hasAnyFlatKey = badge.Icon !== undefined ||
badge.IconPosition !== undefined ||
badge.IconGap !== undefined ||
badge.IconOnly !== undefined;
if (!hasAnyFlatKey) {
continue;
}
if (badge.Icon && !badge.IconProperties) {
const next = { Icon: badge.Icon };
if (badge.IconPosition !== undefined)
next.Position = badge.IconPosition;
if (badge.IconGap !== undefined)
next.Gap = badge.IconGap;
if (badge.IconOnly !== undefined)
next.IconOnly = badge.IconOnly;
badge.IconProperties = next;
}
delete badge.Icon;
delete badge.IconPosition;
delete badge.IconGap;
delete badge.IconOnly;
didMigrate = true;
}
if (didMigrate) {
this.logger.info(`Migrated flat Badge icon settings to IconProperties on column "${sc.ColumnId}" (v23).`);
}
}
}
liftBadgeStyleRowScope(state) {
const columns = state.StyledColumn?.StyledColumns;
if (!columns?.length) {
return;
}
for (const sc of columns) {
const badgeRowScope = sc.BadgeStyle?.RowScope;
if (!badgeRowScope) {
continue;
}
if (!sc.RowScope) {
sc.RowScope = badgeRowScope;
}
delete sc.BadgeStyle.RowScope;
this.logger.info(`Promoted BadgeStyle.RowScope → StyledColumn.RowScope on column "${sc.ColumnId}" (v23).`);
}
}
patchStyledColumnName(state) {
const columns = state.StyledColumn?.StyledColumns;
if (!columns?.length) {
return;
}
for (const sc of columns) {
if (!sc.Name) {
sc.Name = `${this.getStyledColumnTypeLabel(sc)}-${sc.ColumnId ?? 'column'}`;
}
}
}
getStyledColumnTypeLabel(sc) {
if (sc.GradientStyle)
return 'Gradient';
if (sc.PercentBarStyle)
return 'PercentBar';
if (sc.SparklineStyle)
return 'Sparkline';
if (sc.BadgeStyle)
return 'Badge';
if (sc.BulletChartStyle)
return 'BulletChart';
if (sc.RatingStyle)
return 'Rating';
return 'StyledColumn';
}
migrateBadgeStyleToPillStyle(state) {
const columns = state.StyledColumn?.StyledColumns;
if (!columns?.length) {
return;
}
for (const sc of columns) {
const badgeStyle = sc.BadgeStyle;
const badges = badgeStyle?.Badges;
if (!badgeStyle || !badges?.length) {
continue;
}
let liftedAlignment;
let didMigrate = false;
for (const badge of badges) {
const legacy = badge.Style;
if (!legacy) {
continue;
}
didMigrate = true;
const pill = {};
if (legacy.BackColor)
pill.BackColor = legacy.BackColor;
if (legacy.ForeColor)
pill.ForeColor = legacy.ForeColor;
if (legacy.BorderColor)
pill.BorderColor = legacy.BorderColor;
if (legacy.FontWeight === 'Bold')
pill.FontWeight = 'Bold';
if (legacy.FontStyle === 'Italic')
pill.FontStyle = 'Italic';
if (legacy.TextDecoration && legacy.TextDecoration !== 'None') {
pill.TextDecoration = legacy.TextDecoration;
}
if (Object.keys(pill).length > 0) {
badge.PillStyle = pill;
}
if (legacy.BorderRadius != null && !badge.Shape) {
const inferred = legacy.BorderRadius >= 12 ? 'Pill' : legacy.BorderRadius === 0 ? 'Square' : 'Rounded';
badge.Shape = inferred;
}
if (legacy.Alignment && !liftedAlignment) {
liftedAlignment = legacy.Alignment;
}
delete badge.Style;
}
if (liftedAlignment && !badgeStyle.Font?.Alignment) {
badgeStyle.Font = { ...(badgeStyle.Font ?? {}), Alignment: liftedAlignment };
didMigrate = true;
}
if (didMigrate) {
this.logger.info(`Migrated BadgeStyle on column "${sc.ColumnId}" to PillStyle (v23).`);
}
}
}
migrateStyledColumnGradientZeroCentred(state) {
const columns = state.StyledColumn?.StyledColumns;
if (!columns?.length) {
return;
}
for (const sc of columns) {
const g = sc.GradientStyle;
if (!g || g.ColumnComparison || g.ZeroCentred) {
continue;
}
const ranges = g.CellRanges;
if (ranges?.length === 2 &&
ranges[0].Max === 0 &&
ranges[1].Min === 0 &&
ranges[0].ReverseGradient === true) {
g.ZeroCentred = {
NegativeColor: ranges[0].Color,
PositiveColor: ranges[1].Color,
};
delete g.CellRanges;
this.logger.info(`Migrated GradientStyle on column "${sc.ColumnId}" to ZeroCentred (v23).`);
}
}
}
}