Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
ra3orblade committed Nov 15, 2024
1 parent 724916b commit 800eaa2
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 12 deletions.
35 changes: 30 additions & 5 deletions src/ts/component/block/dataview/view/grid/foot/cell.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react';
import { observer } from 'mobx-react';
import { Select } from 'Component';
import { I, S, keyboard, Relation, Dataview } from 'Lib';
import { I, S, C, keyboard, Relation, Dataview } from 'Lib';

interface Props extends I.ViewComponent, I.ViewRelation {
rootId?: string;
Expand Down Expand Up @@ -36,7 +36,7 @@ const FootCell = observer(class FootCell extends React.Component<Props, State> {
return null;
};

const cn = [ 'cellFoot', `cell-key-${this.props.relationKey}`, Relation.className(relation.format) ];
const cn = [ 'cellFoot', `cell-key-${relationKey}` ];
const options = Relation.formulaByType(relation.format);

return (
Expand Down Expand Up @@ -64,6 +64,7 @@ const FootCell = observer(class FootCell extends React.Component<Props, State> {
};

componentDidMount (): void {
this.calculate();
};

componentDidUpdate (): void {
Expand All @@ -73,11 +74,35 @@ const FootCell = observer(class FootCell extends React.Component<Props, State> {
this.setState({ isEditing });
};

calculate () {
const { rootId, block, relationKey, getView } = this.props;
const view = getView();
const viewRelation = view.getRelation(relationKey);
const result = Dataview.getFormulaResult(rootId, block.id, relationKey, viewRelation);

this.setState({ result });
};

onChange (id: string): void {
const { rootId, block, relationKey } = this.props;
const result = Dataview.getFormulaResult(rootId, block.id, relationKey, Number(id) || 0);
const { rootId, block, relationKey, getView } = this.props;
const view = getView();
const relations = view.getRelations();
const idx = relations.findIndex(it => it.relationKey == relationKey);

console.log(view);

/*
const item = view.getRelation(relationKey);
item.formulaType = Number(id) || I.FormulaType.None;
console.log(JSON.stringify(view, null, 3));
this.setState({ isEditing: false, result });
S.Record.viewUpdate(rootId, view.id, view);
//C.BlockDataviewViewRelationReplace(rootId, block.id, view.id, item.relationKey, { ...item, formulaType: item.formulaType });
this.setState({ isEditing: false });
*/
};

onMouseEnter (): void {
Expand Down
2 changes: 1 addition & 1 deletion src/ts/component/block/dataview/view/grid/head/cell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const HeadCell = observer(class HeadCell extends React.Component<Props> {
};

const allowed = !readonly && S.Block.checkFlags(rootId, block.id, [ I.RestrictionDataview.View ]);
const cn = [ 'cellHead', `cell-key-${this.props.relationKey}`, Relation.className(relation.format) ];
const cn = [ 'cellHead', `cell-key-${relationKey}`, Relation.className(relation.format) ];

if (allowed) {
cn.push('canDrag');
Expand Down
2 changes: 2 additions & 0 deletions src/ts/interface/block/dataview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ export interface ViewRelation {
includeTime?: boolean;
dateFormat?: I.DateFormat;
timeFormat?: I.TimeFormat;
formulaType?: I.FormulaType;
};

export interface ViewComponent {
Expand Down Expand Up @@ -202,6 +203,7 @@ export interface View {
defaultTemplateId?: string;
defaultTypeId?: string;
getVisibleRelations?: () => I.ViewRelation[];
getRelations?: () => I.ViewRelation[];
getRelation?: (relationKey: string) => I.ViewRelation;
isGrid?(): boolean;
isList?(): boolean;
Expand Down
22 changes: 16 additions & 6 deletions src/ts/lib/dataview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -545,40 +545,50 @@ class Dataview {
return ret;
};

getFormulaResult (rootId: string, blockId: string, relationKey: string, type: I.FormulaType) {
getFormulaResult (rootId: string, blockId: string, relationKey: string, viewRelation: I.ViewRelation): any {
console.log(viewRelation);

const { formulaType, includeTime, timeFormat, dateFormat } = viewRelation;
const relation = S.Record.getRelationByKey(relationKey);
const subId = S.Record.getSubId(rootId, blockId);
const { total } = S.Record.getMeta(subId, '');

let records = [];
let needRecords = false;

if (![ I.FormulaType.None, I.FormulaType.Count ].includes(type)) {
if (![ I.FormulaType.None, I.FormulaType.Count ].includes(formulaType)) {
needRecords = true;
};

if (needRecords) {
records = S.Record.getRecords(subId);
};

const date = (t: number) => {
const date = U.Date.dateWithFormat(dateFormat, t);
const time = U.Date.timeWithFormat(timeFormat, t);

return includeTime ? [ date, time ].join(' ') : date;
};

const min = () => {
let ret: any = Math.min(...records.map(it => Number(it[relationKey] || 0)));
if (relation.format == I.RelationType.Date) {
ret = ret ? U.Date.dateWithFormat(I.DateFormat.MonthAbbrAfterDay, ret) : '';
ret = ret ? date(ret) : '';
};
return ret;
};
const max = () => {
let ret: any = Math.max(...records.map(it => Number(it[relationKey] || 0)))
let ret: any = Math.max(...records.map(it => Number(it[relationKey] || 0)));
if (relation.format == I.RelationType.Date) {
ret = ret ? U.Date.dateWithFormat(I.DateFormat.MonthAbbrAfterDay, ret) : '';
ret = ret ? date(ret) : '';
};
return ret;
};

let ret = null;

switch (type) {
switch (formulaType) {
case I.FormulaType.None: {
break;
};
Expand Down
3 changes: 3 additions & 0 deletions src/ts/model/viewRelation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class ViewRelation implements I.ViewRelation {
includeTime = false;
dateFormat: I.DateFormat = I.DateFormat.MonthAbbrBeforeDay;
timeFormat: I.TimeFormat = I.TimeFormat.H12;
formulaType: I.FormulaType = I.FormulaType.None;

constructor (props: I.ViewRelation) {
this.relationKey = String(props.relationKey || '');
Expand All @@ -17,13 +18,15 @@ class ViewRelation implements I.ViewRelation {
this.includeTime = Boolean(props.includeTime);
this.dateFormat = Number(props.dateFormat) || I.DateFormat.MonthAbbrBeforeDay;
this.timeFormat = Number(props.timeFormat) || I.TimeFormat.H12;
this.formulaType = Number(props.formulaType) || I.FormulaType.None;

makeObservable(this, {
width: observable,
isVisible: observable,
includeTime: observable,
dateFormat: observable,
timeFormat: observable,
formulaType: observable,
});

intercept(this as any, change => U.Common.intercept(this, change));
Expand Down

0 comments on commit 800eaa2

Please sign in to comment.