Skip to content

Commit dd5afea

Browse files
author
Blueshift Staff
committed
Synced from the Blueshift Repository
1 parent 8be5dfc commit dd5afea

File tree

12 files changed

+111
-4
lines changed

12 files changed

+111
-4
lines changed

projects/ngx-grid-core/src/lib/controller/cell-operations/get-formatted-value.operation.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { IGridCellCoordinates, IGridColumn, IGridDataType, INumberOptions } from
33
import { ICellOperationFactory } from '../../typings/interfaces/grid-cell-operation-factory.interface'
44
import { NumberOptionsParser } from '../../utils/number-format-parser/number-options-parser'
55
import { ParseDate } from '../../utils/parse-date-string'
6+
import { ParseDateTime } from '../../utils/parse-datetime-string'
67
import { Operation } from '../operation.abstract'
78

89
export class GetFormattedValue extends Operation {
@@ -111,6 +112,13 @@ export class GetFormattedValue extends Operation {
111112
if (formattedDate) return formattedDate
112113
}
113114
break
115+
case 'DateTime':
116+
const dateTime = typeof value === 'string' ? ParseDateTime(value) : value
117+
if (dateTime) {
118+
const formattedDateTime = this.controller.datePipe.transform(dateTime, this._dateTimeFormat)
119+
if (formattedDateTime) return formattedDateTime
120+
}
121+
break
114122
case 'File':
115123
if (typeof value === 'object' && value !== null && value.fileName !== undefined) return value.fileName
116124
break
@@ -140,4 +148,8 @@ export class GetFormattedValue extends Operation {
140148
return this.controller.getDateFormat()
141149
}
142150

151+
private get _dateTimeFormat(): string {
152+
return this.controller.getDateTimeFormat()
153+
}
154+
143155
}

projects/ngx-grid-core/src/lib/controller/column-operations/initialise-column-width.operation.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export class InitialiseColumnWidth extends Operation {
88

99
private readonly columnWidthsMap = new Map<TColumnKey, number>()
1010
private readonly headerWidthsMap = new Map<TColumnKey, number>()
11+
private readonly columnKeysBasedOnHeaderWidthOnly = new Set<string>()
1112
private readonly commonCellPadding = 14
1213
private readonly bufferOperation = new BufferOperation((args: any) => this._measureCellWidth(args))
1314

@@ -25,6 +26,7 @@ export class InitialiseColumnWidth extends Operation {
2526
*/
2627
public reset() {
2728
this.columnWidthsMap.clear()
29+
this.columnKeysBasedOnHeaderWidthOnly.clear()
2830
}
2931

3032
/**
@@ -65,17 +67,19 @@ export class InitialiseColumnWidth extends Operation {
6567
this.headerWidthsMap.get(columnKey) ?? 0,
6668
width + this.commonCellPadding)
6769
)
70+
this.columnKeysBasedOnHeaderWidthOnly.add(columnKey)
6871
wasChanged = true
6972
} else {
7073
const cellType = cellTypeOrColumnWidth
71-
if (existingColumnWidths.has(cellType.coordinates.columnKey)) continue
74+
if (existingColumnWidths.has(cellType.coordinates.columnKey) && (!cellType.value || !this.columnKeysBasedOnHeaderWidthOnly.has(cellType.coordinates.columnKey))) continue
7275
const minWidth = this.dataSource.getColumn(cellType.coordinates.columnKey)?.minWidth ?? 0
7376
this.columnWidthsMap.set(cellType.coordinates.columnKey, Math.max(
7477
this.headerWidthsMap.get(cellType.coordinates.columnKey) ?? 0,
7578
this.columnWidthsMap.get(cellType.coordinates.columnKey) ?? 0,
7679
cellType.measureWidth() + this.commonCellPadding,
7780
minWidth)
7881
)
82+
this.columnKeysBasedOnHeaderWidthOnly.delete(cellType.coordinates.columnKey)
7983
wasChanged = true
8084
}
8185
}

projects/ngx-grid-core/src/lib/controller/grid-controller.service.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export class GridControllerService {
3939
public keyboardTriggers = this.grid.KeyBindings.manualKeyboardTriggers
4040

4141
public defaultDateFormat = 'yyyy/MM/dd'
42+
public defaultDateTimeFormat = 'yyyy/MM/dd h:mm a'
4243

4344
private _subs : Set<Subscription> = new Set()
4445

@@ -222,8 +223,11 @@ export class GridControllerService {
222223
}
223224

224225
public getDateFormat() {
225-
const format = this.localize.getLocalizedString('dateFormat')
226-
return format === 'dateFormat' ? this.defaultDateFormat : format
226+
return this.defaultDateFormat
227+
}
228+
229+
public getDateTimeFormat() {
230+
return this.defaultDateTimeFormat
227231
}
228232

229233
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { BehaviorSubject } from 'rxjs'
2+
3+
import { GridControllerService } from '../../../controller/grid-controller.service'
4+
import { GridOverlayService } from '../../../services/grid-overlay-service.service'
5+
import { ECellMode } from '../../../typings/enums'
6+
import { IGridCellComponent } from '../../../typings/interfaces'
7+
import { CharacterSizer } from '../../../utils/character-sizer'
8+
import { BaseCellType } from './abstractions/base-cell-type.abstract'
9+
10+
export class DateTimeCellType extends BaseCellType {
11+
12+
public mode = new BehaviorSubject<ECellMode>(ECellMode.Readonly)
13+
14+
private readonly readonlyCssClassName = 'datetime-readonly'
15+
16+
private _displayNode?: HTMLElement
17+
18+
constructor(
19+
gridController: GridControllerService,
20+
overlayService: GridOverlayService,
21+
parentCell : IGridCellComponent
22+
) { super(overlayService, parentCell, gridController) }
23+
24+
public get displayNode() { return this._displayNode ?? this._generateDisplayNode() }
25+
public get editableNode() { return this.displayNode }
26+
27+
public override receiveValue(value: any = this.value): void {
28+
super.receiveValue(value)
29+
if (!this._displayNode) return
30+
this._displayNode.innerText = this._displayValue
31+
}
32+
33+
private _generateDisplayNode(): HTMLElement {
34+
this._displayNode = this.createDiv(this.readonlyCssClassName)
35+
this.receiveValue()
36+
return this._displayNode
37+
}
38+
39+
private get _displayValue(): string {
40+
return this.gridController.cell.GetFormattedValue.getHTML(this.coordinates, this.value)
41+
}
42+
43+
public override measureWidth(): number {
44+
if (!this.value) return 0
45+
const additionalWidth = 17
46+
const formattedVal = this.gridController.cell.GetFormattedValue.getPlainText(this.coordinates, this.value)
47+
return CharacterSizer.measure(formattedVal, this.getFont(), this.maxWidth) + additionalWidth
48+
}
49+
50+
}

projects/ngx-grid-core/src/lib/ui/cell/cell-types/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { BooleanCellType as Boolean } from './boolean.cell-type'
22
import { ColorCellType as Color } from './color.cell-type'
33
import { DateRangeCellType as DateRange } from './date-range.cell-type'
44
import { DateCellType as Date } from './date.cell-type'
5+
import { DateTimeCellType as DateTime } from './date-time.cell-type'
56
import { DropdownMultiSelectCellType as DropdownMultiSelect } from './dropdown-multi-select.cell-type'
67
import { DropdownSingleSelectCellType as DropdownSingleSelect } from './dropdown-single-select.cell-type'
78
import { FileCellType as File } from './file.cell-type'
@@ -21,6 +22,7 @@ const CELL_TYPES = {
2122
Number,
2223
NumberRange,
2324
Date,
25+
DateTime,
2426
DateRange,
2527
DropdownSingleSelect,
2628
DropdownMultiSelect,

projects/ngx-grid-core/src/lib/ui/cell/cell-types/styles/cell-type-styles.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
.number-readonly,
33
.rich-text-readonly,
44
.date-readonly,
5+
.datetime-readonly,
56
.color-readonly,
67
.file-cell-type {
78
height: min-content;

projects/ngx-grid-core/src/lib/ui/cell/cell-types/value-multi-editing/multi-editors-factory.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export const CELL_MULTI_EDITORS: {[key in TCellTypeName]: Array<new (cellValue:
99
RichText : [ editors.TextAppend, editors.TextPrepend, editors.ClearValues ],
1010
Number : [ editors.NumberAdd, editors.NumberSubtract, editors.NumberDivide, editors.NumberMultiply, editors.ClearValues],
1111
Date : [ editors.DateAddDays, editors.DateSubtractDays, editors.ClearValues ],
12+
DateTime : [ ],
1213
NumberRange : [ editors.ClearValues ],
1314
DateRange : [ editors.ClearValues ],
1415
DropdownSingleSelect: [ editors.ClearValues ],

projects/ngx-grid-core/src/lib/ui/cell/cell-types/value-parsing/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export const CELL_VALUE_PARSERS: {[key in TCellTypeName]: ICellValueParser} = {
1313
Color : new ValueParser([parsers.String, parsers.Color]),
1414
Number : new ValueParser([parsers.Number]),
1515
Date : new ValueParser([parsers.Date]),
16+
DateTime : new ValueParser([parsers.DateTime]),
1617
DropdownSingleSelect: new ValueParser([parsers.MultiSelect]),
1718
DropdownMultiSelect : new ValueParser([parsers.Array, parsers.MultiSelect]),
1819
DateRange : new ValueParser([parsers.Array]),
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { IGridValueParsingResult } from '../../../../../typings/interfaces'
2+
import { ParseDateTime } from '../../../../../utils/parse-datetime-string'
3+
import { BaseParser } from './base-parser.abstract'
4+
import { IParsingTest } from './parsing-test.interface'
5+
6+
export class DateTimeParser extends BaseParser implements IParsingTest {
7+
constructor (public readonly initialValue: any) { super() }
8+
9+
// Parses a datetime string into a Date object
10+
public run(): IGridValueParsingResult<Date> {
11+
12+
if (this.initialValue instanceof Date) return this.passed(this.initialValue)
13+
14+
if (typeof this.initialValue !== 'string') return this.failed()
15+
16+
const dateObj = ParseDateTime(this.initialValue)
17+
18+
if (!dateObj) return this.failed()
19+
20+
return this.passed(dateObj)
21+
}
22+
}

projects/ngx-grid-core/src/lib/ui/cell/cell-types/value-parsing/parsers/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { ArrayParser as Array } from './array.parser'
22
import { BooleanParser as Boolean } from './boolean.parser'
33
import { ColorParser as Color } from './color.parser'
44
import { DateParser as Date } from './date.parser'
5+
import { DateTimeParser as DateTime } from './datetime.parser'
56
import { FileParser as File } from './file.parser'
67
import { MultiSelectParser as MultiSelect } from './multi-select.parser'
78
import { NumberParser as Number } from './number.parser'
@@ -14,6 +15,7 @@ export {
1415
Array,
1516
Boolean,
1617
Date,
18+
DateTime,
1719
MultiSelect,
1820
Number,
1921
String,

0 commit comments

Comments
 (0)