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

View File

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

View File

@ -7,7 +7,7 @@ import {makeT} from 'app/client/lib/localization';
import {localStorageBoolObs} from 'app/client/lib/localStorageObs'; import {localStorageBoolObs} from 'app/client/lib/localStorageObs';
import {ColumnToMapImpl} from 'app/client/models/ColumnToMap'; import {ColumnToMapImpl} from 'app/client/models/ColumnToMap';
import {ColumnRec, ViewSectionRec} from 'app/client/models/DocModel'; import {ColumnRec, ViewSectionRec} from 'app/client/models/DocModel';
import {labelsOrder} from 'app/client/models/entities/ColumnRec'; import {columnsOrder} from 'app/client/models/entities/ColumnRec';
import { import {
cssDeveloperLink, cssDeveloperLink,
cssWidgetMetadata, cssWidgetMetadata,
@ -81,7 +81,7 @@ class ColumnPicker extends Disposable {
label: col.label.peek() || '', label: col.label.peek() || '',
icon: 'FieldColumn' as const, icon: 'FieldColumn' as const,
})) }))
.sort(labelsOrder); .sort(columnsOrder);
// For optional mappings, add 'Blank' option but only if the value is set. // For optional mappings, add 'Blank' option but only if the value is set.
@ -205,8 +205,8 @@ class ColumnListPicker extends Disposable {
menu(() => { menu(() => {
const wrongTypeCount = notMapped.get().length - typedColumns.get().length; const wrongTypeCount = notMapped.get().length - typedColumns.get().length;
return [ return [
...typedColumns.get() ...typedColumns.get() // returns a temp table.
.sort(labelsOrder) .sort(columnsOrder)
.map((col) => menuItem( .map((col) => menuItem(
() => this._addColumn(col), () => this._addColumn(col),
col.label.peek(), col.label.peek(),

View File

@ -4,6 +4,7 @@ import {FocusLayer} from 'app/client/lib/FocusLayer';
import {makeT} from 'app/client/lib/localization'; import {makeT} from 'app/client/lib/localization';
import {reportError} from 'app/client/models/AppModel'; import {reportError} from 'app/client/models/AppModel';
import {ColumnRec, TableRec, ViewSectionRec} from 'app/client/models/DocModel'; import {ColumnRec, TableRec, ViewSectionRec} from 'app/client/models/DocModel';
import {columnsOrder} from 'app/client/models/entities/ColumnRec';
import {PERMITTED_CUSTOM_WIDGETS} from "app/client/models/features"; import {PERMITTED_CUSTOM_WIDGETS} from "app/client/models/features";
import {linkId, NoLink} from 'app/client/ui/selectBy'; import {linkId, NoLink} from 'app/client/ui/selectBy';
import {overflowTooltip, withInfoTooltip} from 'app/client/ui/tooltips'; import {overflowTooltip, withInfoTooltip} from 'app/client/ui/tooltips';
@ -32,7 +33,6 @@ import {
import Popper from 'popper.js'; import Popper from 'popper.js';
import {IOpenController, popupOpen, setPopupToCreateDom} from 'popweasel'; import {IOpenController, popupOpen, setPopupToCreateDom} from 'popweasel';
import without = require('lodash/without'); import without = require('lodash/without');
import {labelsOrder} from 'app/client/models/entities/ColumnRec';
const t = makeT('PageWidgetPicker'); const t = makeT('PageWidgetPicker');
@ -408,7 +408,7 @@ export class PageWidgetSelect extends Disposable {
(use) => use(this._columns) (use) => use(this._columns)
.filter((col) => !col.isHiddenCol() && col.parentId() === use(this._value.table)), .filter((col) => !col.isHiddenCol() && col.parentId() === use(this._value.table)),
(cols) => cols ? (cols) => cols ?
dom.forEach([...cols].sort(labelsOrder), (col) => dom.forEach([...cols].sort(columnsOrder), (col) =>
cssEntry(cssIcon('FieldColumn'), cssFieldLabel(dom.text(col.label)), cssEntry(cssIcon('FieldColumn'), cssFieldLabel(dom.text(col.label)),
dom.on('click', () => this._toggleColumnId(col.id())), dom.on('click', () => this._toggleColumnId(col.id())),
cssEntry.cls('-selected', (use) => use(this._value.columns).includes(col.id())), cssEntry.cls('-selected', (use) => use(this._value.columns).includes(col.id())),

View File

@ -4,7 +4,7 @@ import * as kf from 'app/client/lib/koForm';
import {makeT} from 'app/client/lib/localization'; import {makeT} from 'app/client/lib/localization';
import {addToSort, updatePositions} from 'app/client/lib/sortUtil'; import {addToSort, updatePositions} from 'app/client/lib/sortUtil';
import {ViewSectionRec} from 'app/client/models/DocModel'; import {ViewSectionRec} from 'app/client/models/DocModel';
import {labelsOrder} from 'app/client/models/entities/ColumnRec'; import {columnsOrder} from 'app/client/models/entities/ColumnRec';
import {ObjObservable} from 'app/client/models/modelUtil'; import {ObjObservable} from 'app/client/models/modelUtil';
import {dropdownWithSearch} from 'app/client/ui/searchDropdown'; import {dropdownWithSearch} from 'app/client/ui/searchDropdown';
import {cssIcon, cssRow, cssSortFilterColumn} from 'app/client/ui/RightPanelStyles'; import {cssIcon, cssRow, cssSortFilterColumn} from 'app/client/ui/RightPanelStyles';
@ -216,7 +216,7 @@ export class SortConfig extends Disposable {
const currentSection = this._section; const currentSection = this._section;
const currentSortSpec = use(currentSection.activeSortSpec); const currentSortSpec = use(currentSection.activeSortSpec);
const specRowIds = new Set(currentSortSpec.map(_sortRef => Sort.getColRef(_sortRef))); const specRowIds = new Set(currentSortSpec.map(_sortRef => Sort.getColRef(_sortRef)));
return use(columns).filter(_col => !specRowIds.has(_col.value)).sort(labelsOrder); return use(columns).filter(_col => !specRowIds.has(_col.value)).sort(columnsOrder);
}); });
const {menuOptions} = this._options; const {menuOptions} = this._options;
return cssButtonRow( return cssButtonRow(