Skip to content

Commit a6f91f7

Browse files
authored
Merge pull request #1 from kshetline/development
Add locale setting for <tbw-calendar>. Improve calendar CJK date formatting.
2 parents 45b47eb + 7881432 commit a6f91f7

9 files changed

Lines changed: 112 additions & 37 deletions

File tree

package-lock.json

Lines changed: 41 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"@angular/platform-browser-dynamic": "~12.1.3",
2525
"@angular/router": "~12.1.3",
2626
"@tubular/math": "^3.1.0",
27-
"@tubular/time": "^3.3.1",
27+
"@tubular/time": "^3.5.1",
2828
"@tubular/util": "^4.3.1",
2929
"rxjs": "~6.6.0",
3030
"tslib": "^2.1.0",

projects/ng-widgets-showcase/src/app/app.component.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,11 +179,12 @@
179179

180180
<div class='calendar'>
181181
<tbw-calendar minYear=-9999
182+
[locale]="customLocale"
182183
[backgroundDecorator]="getBackground"
183184
[foregroundDecorator]="getForeground"
184185
showDst
185186
weekDayFormat="dd"
186-
yearMonthFormat="MMM y n"
187+
yearMonthFormat="MMM~y~n"
187188
[(ngModel)]="calendarDate"></tbw-calendar>
188189
</div>
189190
</div>

projects/ng-widgets-showcase/src/main.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import { enableProdMode } from '@angular/core';
22
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
33

4+
import { initTimezoneLarge } from '@tubular/time';
5+
46
import { AppModule } from './app/app.module';
57
import { environment } from './environments/environment';
68

9+
initTimezoneLarge();
10+
711
if (environment.production) {
812
enableProdMode();
913
}

projects/tubular-ng-widgets/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@tubular/ng-widgets",
3-
"version": "1.0.6",
3+
"version": "1.1.0",
44
"peerDependencies": {
55
"@angular/common": "^12.1.3",
66
"@angular/core": "^12.1.3",

projects/tubular-ng-widgets/src/lib/calendar-panel/calendar-panel.component.ts

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Component, EventEmitter, forwardRef, Input, OnDestroy, Output } from '@
22
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
33
import { div_rd, max, min } from '@tubular/math';
44
import { CalendarType, DateTime, defaultLocale, getStartOfWeek, GregorianChange, Timezone, YMDDate } from '@tubular/time';
5-
import { clone, isEqual, isObject, isString, noop, toBoolean, toNumber } from '@tubular/util';
5+
import { clone, convertDigits, convertDigitsToAscii, isEqual, isObject, isString, noop, toBoolean, toNumber } from '@tubular/util';
66
import { Subscription, timer } from 'rxjs';
77
import { SafeHtml } from '@angular/platform-browser';
88

@@ -32,21 +32,22 @@ const multiplier = [0, 1, 1, 10, 100, 1000];
3232
providers: [{ provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => CalendarPanelComponent), multi: true }]
3333
})
3434
export class CalendarPanelComponent implements ControlValueAccessor, OnDestroy {
35-
private ymd: YMDDate = { y: 2021, m: 1, d: 1 };
36-
private _gregorianChange: GregorianChange;
37-
private _showDst = false;
38-
private _minYear = 1;
39-
private _maxYear = 9999;
40-
private _firstDay = getStartOfWeek(defaultLocale);
4135
private baseValue = [0, 0, 0];
4236
private dateTime: DateTime = new DateTime();
43-
private onTouchedCallback: () => void = noop;
37+
private digitBase = '0';
38+
private _firstDay = getStartOfWeek(defaultLocale);
39+
private _gregorianChange: GregorianChange;
40+
private _maxYear = 9999;
41+
private _minYear = 1;
4442
private onChangeCallback: (_: any) => void = noop;
45-
private timerSubscription: Subscription;
43+
private onTouchedCallback: () => void = noop;
4644
private pendingDelta = 0;
4745
private pendingEvent: MouseEvent = null;
46+
private _showDst = false;
47+
private timerSubscription: Subscription;
4848
private _weekDayFormat = 'ddd';
49-
private _yearMonthFormat = 'MMM Y';
49+
private _yearMonthFormat = 'MMM~Y~';
50+
private ymd: YMDDate = { y: 2021, m: 1, d: 1 };
5051

5152
@Input() backgroundDecorator: DayDecorator;
5253
calendar: CalendarDateInfo[][] = [];
@@ -64,6 +65,9 @@ export class CalendarPanelComponent implements ControlValueAccessor, OnDestroy {
6465

6566
constructor() {
6667
this.updateDayHeadings();
68+
const base: string[] = [];
69+
convertDigitsToAscii(this.dateTime.format('D'), base);
70+
this.digitBase = base[0];
6771
}
6872

6973
ngOnDestroy(): void {
@@ -102,6 +106,17 @@ export class CalendarPanelComponent implements ControlValueAccessor, OnDestroy {
102106
}
103107
}
104108

109+
get locale(): string | string[] { return this.dateTime.locale; }
110+
@Input() set locale(value: string | string[]) {
111+
if (!isEqual(this.dateTime.locale, value)) {
112+
this.dateTime.locale = value;
113+
const base: string[] = [];
114+
convertDigitsToAscii(this.dateTime.format('D'), base);
115+
this.digitBase = base[0];
116+
this.updateCalendar();
117+
}
118+
}
119+
105120
get gregorianChangeDate(): GregorianChange { return this._gregorianChange; }
106121
@Input() set gregorianChangeDate(newChange: GregorianChange) {
107122
if (!isEqual(this._gregorianChange, newChange)) {
@@ -182,7 +197,7 @@ export class CalendarPanelComponent implements ControlValueAccessor, OnDestroy {
182197

183198
for (let d = 1; d <= 7; ++d)
184199
this.daysOfWeek.push(new DateTime({ y: 2017, m: 1, d: d + this._firstDay, hrs: 12 },
185-
'UTC', 'en-us').format(this._weekDayFormat));
200+
'UTC', this.dateTime.locale).format(this._weekDayFormat));
186201
}
187202

188203
updateCalendar(): void {
@@ -198,7 +213,7 @@ export class CalendarPanelComponent implements ControlValueAccessor, OnDestroy {
198213
const col = index % 7;
199214

200215
date.dayLength = dayLength;
201-
date.text = String(date.d);
216+
date.text = convertDigits(String(date.d), this.digitBase);
202217
date.otherMonth = (date.m !== month);
203218
date.highlight = (date.m === month && date.d === day);
204219

@@ -230,7 +245,8 @@ export class CalendarPanelComponent implements ControlValueAccessor, OnDestroy {
230245
}
231246

232247
private updateTitle(): void {
233-
this.title[0] = new DateTime({ y: this.ymd?.y ?? 2021, m: this.ymd?.m ?? 1 }, 'UTC', 'en-us').format(this._yearMonthFormat);
248+
this.title[0] = new DateTime({ y: this.ymd?.y ?? 2021, m: this.ymd?.m ?? 1 }, 'UTC',
249+
this.dateTime.locale).format(this._yearMonthFormat);
234250
}
235251

236252
reset(): void {
@@ -338,7 +354,7 @@ export class CalendarPanelComponent implements ControlValueAccessor, OnDestroy {
338354
else if (mode === SelectMode.MONTH) {
339355
const m = row * 4 + col + 1;
340356

341-
return (this.months[m] = new DateTime({ y: 4000, m, hrs: 12 }).format('MMM'));
357+
return (this.months[m] = new DateTime({ y: 4000, m, hrs: 12 }, 'UTC', this.dateTime.locale).format('MMM'));
342358
}
343359

344360
let index = row * this.cols + col;
@@ -353,7 +369,7 @@ export class CalendarPanelComponent implements ControlValueAccessor, OnDestroy {
353369
const maxx = (div_rd(this._maxYear - 1, multiplier[mode]) + 1) * multiplier[mode];
354370

355371
if ((minn <= value && value <= maxx))
356-
return min(max(value, this._minYear), this._maxYear).toString();
372+
return convertDigits(min(max(value, this._minYear), this._maxYear).toString(), this.digitBase);
357373
else
358374
return '';
359375
}

projects/tubular-ng-widgets/src/lib/digit-sequence-editor/digit-sequence-editor.directive.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,16 @@
5858
[style.bottom]="item.spinner || item.divider ? '0' : baselineShift">&nbsp;</div>
5959
<span *ngIf="swipeable(item, 1)"
6060
class="tbw-dse-draggable-extra"
61-
[style.top]="'calc(' + (-digitHeight + smoothedDeltaY) + 'px + ' + (item.deltaY || 0) + 'em)'"
61+
[ngStyle]="getStyleForItem(item, -digitHeight)"
6262
>{{(item.alt_swipeAbove || item.swipeAbove) ?? '\u00A0'}}</span>
6363
<span *ngIf="swipeable(item, -1)"
6464
class="tbw-dse-draggable-extra"
65-
[style.top]="'calc(' + (digitHeight + smoothedDeltaY) + 'px + ' + (item.deltaY || 0) + 'em)'"
65+
[ngStyle]="getStyleForItem(item, digitHeight)"
6666
>{{(item.alt_swipeBelow || item.swipeBelow) ?? '\u00A0'}}</span>
6767
<span *ngIf="!item.spinner && !item.divider; else dividerOrSpinner"
6868
class="tbw-dse-draggable dse-item-{{item.index}}"
6969
[class.tbw-dse-sized]="!!item.sizer"
70-
[style.top]="'calc(' + (swipeable(item, 0) ? smoothedDeltaY : 0) + 'px + ' + (item.deltaY || 0) + 'em)'"
70+
[ngStyle]="getStyleForItem(item, 0)"
7171
>{{filterDisplayChars(item.alt_value || item.value)}}</span>
7272
<span *ngIf="item.sizer"
7373
class="tbw-dse-sizer"

projects/tubular-ng-widgets/src/lib/digit-sequence-editor/digit-sequence-editor.directive.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export interface SequenceItemInfo {
2525
hidden?: boolean;
2626
index?: number;
2727
indicator?: boolean;
28+
maxChars?: number;
2829
monospaced?: boolean;
2930
name?: string;
3031
opacity?: number | string;
@@ -713,6 +714,33 @@ export abstract class DigitSequenceEditorDirective<T> implements
713714
return null;
714715
}
715716

717+
getStyleForItem(item: SequenceItemInfo, heightOffset: number): any {
718+
const itemStyle: any = {
719+
top: 'calc(' + (this.swipeable(item, 0) ? (this.smoothedDeltaY + heightOffset) : 0) +
720+
'px + ' + (item.deltaY || 0) + 'em)'
721+
};
722+
723+
if (item.maxChars) {
724+
let value: string;
725+
726+
if (heightOffset < 0)
727+
value = item.alt_swipeAbove || item.swipeAbove;
728+
else if (heightOffset > 0)
729+
value = item.alt_swipeBelow || item.swipeBelow;
730+
else
731+
value = this.filterDisplayChars(item.alt_value || item.value).toString();
732+
733+
const ratio = item.maxChars / value.toString().length;
734+
735+
if (ratio < 1) {
736+
itemStyle.transform = `scale(${floor(ratio * 100) / 100}, 1)`;
737+
itemStyle.width = '1px';
738+
}
739+
}
740+
741+
return itemStyle;
742+
}
743+
716744
getStaticBackgroundColor(): string {
717745
if (this._disabled)
718746
return DISABLED_BACKGROUND;

projects/tubular-ng-widgets/src/lib/time-editor/time-editor.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -868,7 +868,7 @@ export class TimeEditorComponent extends DigitSequenceEditorDirective<number> im
868868
break;
869869
case 'off':
870870
this.offsetIndex = i;
871-
this.items.push({ value: '+00:00', format: 'Z', indicator: true, monospaced: true });
871+
this.items.push({ value: '+00:00', format: 'Z', indicator: true, monospaced: true, sizer: '+00:00', maxChars: 6 });
872872
break;
873873
case 'dst':
874874
this.dstIndex = i;

0 commit comments

Comments
 (0)