Refactoring

This commit is contained in:
Jarosław Sadziński
2024-10-15 13:00:13 +02:00
parent 1315cc366f
commit 3718883bc9
5 changed files with 18 additions and 22 deletions

View File

@@ -235,17 +235,13 @@ function peekLabel(info: ColumnInfo): string {
return typeof info.label === 'string' ? info.label : info.label.peek();
}
export function labelsOrder(a: ColumnInfo, b: ColumnInfo): number {
const left = peekLabel(a).toLowerCase();
const right = peekLabel(b).toLowerCase();
// Order is as follows:
// - First columns with normal labels starting with a letter.
// - Second all columns starting with '_' (treated as private)
// - Third all columns starting with '#' (treated as private)
// - Rest.
if (left[0] === '_' && right[0] !== '_') { return 1; }
if (left[0] !== '_' && right[0] === '_') { return -1; }
/**
* Helper function to sort columns based on the label. Puts # columns at the end as this is
* treated as private columns.
*/
export function columnsOrder(a: ColumnInfo, b: ColumnInfo): number {
const left = peekLabel(a)?.toLowerCase() || '';
const right = peekLabel(b)?.toLowerCase() || '';
if (left[0] === '#' && right[0] !== '#') { return 1; }
if (left[0] !== '#' && right[0] === '#') { return -1; }
return left.localeCompare(right);

View File

@@ -16,7 +16,7 @@ import {
ViewFieldRec,
ViewRec
} from 'app/client/models/DocModel';
import {BEHAVIOR, labelsOrder} from 'app/client/models/entities/ColumnRec';
import {BEHAVIOR, columnsOrder} from 'app/client/models/entities/ColumnRec';
import * as modelUtil from 'app/client/models/modelUtil';
import {removeRule, RuleOwner} from 'app/client/models/RuleOwner';
import {LinkConfig} from 'app/client/ui/selectBy';
@@ -521,7 +521,7 @@ export function createViewSectionRec(this: ViewSectionRec, docModel: DocModel):
const savedFiltersByColRef = new Map(this._savedFilters().all().map(f => [f.colRef(), f]));
const viewFieldsByColRef = new Map(this.viewFields().all().map(f => [f.origCol().getRowId(), f]));
return [...this.columns()].sort(labelsOrder).map(column => {
return [...this.columns()].sort(columnsOrder).map(column => {
const savedFilter = savedFiltersByColRef.get(column.origColRef());
// Initialize with a saved filter, if one exists. Otherwise, use a blank filter.
const filter = modelUtil.customComputed({
@@ -700,7 +700,7 @@ export function createViewSectionRec(this: ViewSectionRec, docModel: DocModel):
const included = new Set(this.viewFields().all().map((f) => f.column().origColRef()));
return this.columns()
.filter(c => !included.has(c.getRowId()))
.sort(labelsOrder);
.sort(columnsOrder);
}));
this.hasFocus = ko.pureComputed({