Skip to content

Commit 58f1465

Browse files
committed
move to calidayjs
1 parent 18392ba commit 58f1465

File tree

4 files changed

+165
-43
lines changed

4 files changed

+165
-43
lines changed

package-lock.json

Lines changed: 8 additions & 7 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
@@ -9,6 +9,6 @@
99
"dependencies": {
1010
"dayjs": "^1.11.12",
1111
"esbuild": "^0.23.1",
12-
"jalaliday": "^2.3.0"
12+
"@calidy/dayjs-calendarsystems": "^1.11.3"
1313
}
1414
}

resources/js/components/filament-jalali.js

Lines changed: 89 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
import dayjs from 'dayjs/esm'
2-
import advancedFormat from 'dayjs/plugin/advancedFormat'
32
import customParseFormat from 'dayjs/plugin/customParseFormat'
43
import localeData from 'dayjs/plugin/localeData'
54
import timezone from 'dayjs/plugin/timezone'
65
import utc from 'dayjs/plugin/utc'
7-
import jalaliday from 'jalaliday'
6+
import calendarSystems from "@calidy/dayjs-calendarsystems";
7+
import PersianCalendarSystem from "@calidy/dayjs-calendarsystems/calendarSystems/PersianCalendarSystem";
88

9-
dayjs.extend(advancedFormat)
109
dayjs.extend(customParseFormat)
1110
dayjs.extend(localeData)
1211
dayjs.extend(timezone)
1312
dayjs.extend(utc)
14-
dayjs.extend(jalaliday)
15-
dayjs.calendar('jalali')
13+
dayjs.extend(calendarSystems)
14+
dayjs.registerCalendarSystem("persian", new PersianCalendarSystem());
1615

1716
window.dayjs = dayjs
1817

@@ -23,6 +22,9 @@ export default function filamentJalaliFormComponent({
2322
locale,
2423
shouldCloseOnDateSelection,
2524
state,
25+
months,
26+
dayLabel,
27+
dayShortLabel
2628
}) {
2729
const timezone = dayjs.tz.guess()
2830

@@ -53,14 +55,17 @@ export default function filamentJalaliFormComponent({
5355

5456
months: [],
5557

56-
init: function () {
57-
dayjs.locale(locale ?? 'en')
58+
dayLabel,
59+
60+
dayShortLabel,
5861

59-
this.focusedDate = dayjs().tz(timezone)
62+
init: function () {
63+
dayjs.locale(locales[locale] ?? locales['en']);
64+
this.focusedDate = dayjs().tz(timezone).toCalendarSystem("persian")
6065

6166
let date =
6267
this.getSelectedDate() ??
63-
dayjs().tz(timezone).hour(0).minute(0).second(0)
68+
dayjs().tz(timezone).toCalendarSystem("persian").hour(0).minute(0).second(0)
6469

6570
if (this.getMaxDate() !== null && date.isAfter(this.getMaxDate())) {
6671
date = null
@@ -107,7 +112,7 @@ export default function filamentJalaliFormComponent({
107112
let year = +this.focusedYear
108113

109114
if (!Number.isInteger(year)) {
110-
year = dayjs().tz(timezone).year()
115+
year = dayjs().tz(timezone).toCalendarSystem("persian").year()
111116

112117
this.focusedYear = year
113118
}
@@ -268,25 +273,25 @@ export default function filamentJalaliFormComponent({
268273
return false
269274
}
270275

271-
return disabledDate.isSame(date, 'day')
276+
return disabledDate.isSame(date.toCalendarSystem("gregory"), 'day')
272277
},
273278
)
274279
) {
275280
return true
276281
}
277282

278-
if (this.getMaxDate() && date.isAfter(this.getMaxDate(), 'day')) {
283+
if (this.getMaxDate() && date.isAfter(this.getMaxDate())) {
279284
return true
280285
}
281-
if (this.getMinDate() && date.isBefore(this.getMinDate(), 'day')) {
286+
if (this.getMinDate() && date.isBefore(this.getMinDate())) {
282287
return true
283288
}
284289

285290
return false
286291
},
287292

288293
dayIsDisabled: function (day) {
289-
this.focusedDate ??= dayjs().tz(timezone)
294+
this.focusedDate ??= dayjs().tz(timezone).toCalendarSystem("persian")
290295

291296
return this.dateIsDisabled(this.focusedDate.date(day))
292297
},
@@ -298,7 +303,7 @@ export default function filamentJalaliFormComponent({
298303
return false
299304
}
300305

301-
this.focusedDate ??= dayjs().tz(timezone)
306+
this.focusedDate ??= dayjs().tz(timezone).toCalendarSystem("persian")
302307

303308
return (
304309
selectedDate.date() === day &&
@@ -308,7 +313,7 @@ export default function filamentJalaliFormComponent({
308313
},
309314

310315
dayIsToday: function (day) {
311-
let date = dayjs().tz(timezone)
316+
let date = dayjs().tz(timezone).toCalendarSystem("persian")
312317
this.focusedDate ??= date
313318

314319
return (
@@ -319,31 +324,45 @@ export default function filamentJalaliFormComponent({
319324
},
320325

321326
focusPreviousDay: function () {
322-
this.focusedDate ??= dayjs().tz(timezone)
327+
this.focusedDate ??= dayjs().tz(timezone).toCalendarSystem("persian")
323328

324329
this.focusedDate = this.focusedDate.subtract(1, 'day')
325330
},
326331

327332
focusPreviousWeek: function () {
328-
this.focusedDate ??= dayjs().tz(timezone)
333+
this.focusedDate ??= dayjs().tz(timezone).toCalendarSystem("persian")
329334

330335
this.focusedDate = this.focusedDate.subtract(1, 'week')
331336
},
332337

333338
focusNextDay: function () {
334-
this.focusedDate ??= dayjs().tz(timezone)
339+
this.focusedDate ??= dayjs().tz(timezone).toCalendarSystem("persian")
335340

336341
this.focusedDate = this.focusedDate.add(1, 'day')
337342
},
338343

339344
focusNextWeek: function () {
340-
this.focusedDate ??= dayjs().tz(timezone)
345+
this.focusedDate ??= dayjs().tz(timezone).toCalendarSystem("persian")
341346

342347
this.focusedDate = this.focusedDate.add(1, 'week')
343348
},
344349

345350
getDayLabels: function () {
346-
const labels = dayjs.weekdaysShort()
351+
let flag = this.$el.dataset.weekdaysShort;
352+
let labels = [];
353+
if (flag === 'short') {
354+
if (typeof this.dayShortLabel !== 'object') {
355+
labels = dayjs.weekdaysShort()
356+
} else {
357+
labels = Object.values(this.dayShortLabel);
358+
}
359+
} else {
360+
if (typeof this.dayLabel !== 'object') {
361+
labels = dayjs.weekdays()
362+
} else {
363+
labels = Object.values(this.dayLabel);
364+
}
365+
}
347366

348367
if (firstDayOfWeek === 0) {
349368
return labels
@@ -358,13 +377,13 @@ export default function filamentJalaliFormComponent({
358377
getMaxDate: function () {
359378
let date = dayjs(this.$refs.maxDate?.value)
360379

361-
return date.isValid() ? date : null
380+
return date.isValid() ? date.toCalendarSystem("persian") : null
362381
},
363382

364383
getMinDate: function () {
365384
let date = dayjs(this.$refs.minDate?.value)
366385

367-
return date.isValid() ? date : null
386+
return date.isValid() ? date.toCalendarSystem("persian") : null
368387
},
369388

370389
getSelectedDate: function () {
@@ -376,7 +395,7 @@ export default function filamentJalaliFormComponent({
376395
return null
377396
}
378397

379-
let date = dayjs(this.state)
398+
let date = dayjs(this.state).toCalendarSystem("persian")
380399

381400
if (!date.isValid()) {
382401
return null
@@ -390,7 +409,7 @@ export default function filamentJalaliFormComponent({
390409
this.focusedDate =
391410
this.getSelectedDate() ??
392411
this.getMinDate() ??
393-
dayjs().tz(timezone)
412+
dayjs().tz(timezone).toCalendarSystem("persian")
394413

395414
this.setupDaysGrid()
396415
}
@@ -403,7 +422,7 @@ export default function filamentJalaliFormComponent({
403422
this.setFocusedDay(day)
404423
}
405424

406-
this.focusedDate ??= dayjs().tz(timezone)
425+
this.focusedDate ??= dayjs().tz(timezone).toCalendarSystem("persian")
407426

408427
this.setState(this.focusedDate)
409428

@@ -419,17 +438,17 @@ export default function filamentJalaliFormComponent({
419438
},
420439

421440
setMonths: function () {
422-
this.months = dayjs.locale() === 'en' ?
423-
dayjs.en.jmonths :
424-
'فروردین_اردیبهشت_خرداد_تیر_مرداد_شهریور_مهر_آبان_آذر_دی_بهمن_اسفند'.split('_')
441+
if (typeof this.months !== 'object' || !Array.isArray(this.months) || this.months === null) {
442+
this.months = dayjs.months()
443+
}
425444
},
426445

427446
setDayLabels: function () {
428447
this.dayLabels = this.getDayLabels()
429448
},
430449

431450
setupDaysGrid: function () {
432-
this.focusedDate ??= dayjs().tz(timezone)
451+
this.focusedDate ??= dayjs().tz(timezone).toCalendarSystem("persian")
433452

434453
this.emptyDaysInFocusedMonth = Array.from(
435454
{
@@ -447,9 +466,7 @@ export default function filamentJalaliFormComponent({
447466
},
448467

449468
setFocusedDay: function (day) {
450-
this.focusedDate = (this.focusedDate ?? dayjs().tz(timezone)).date(
451-
day,
452-
)
469+
this.focusedDate = dayjs().toCalendarSystem("persian").year(this.focusedDate.year()).month(this.focusedDate.month()).date(day);
453470
},
454471

455472
setState: function (date) {
@@ -465,10 +482,10 @@ export default function filamentJalaliFormComponent({
465482
}
466483

467484
this.state = date
468-
.calendar('gregory')
469485
.hour(this.hour ?? 0)
470486
.minute(this.minute ?? 0)
471487
.second(this.second ?? 0)
488+
.toCalendarSystem('gregory')
472489
.format('YYYY-MM-DD HH:mm:ss')
473490

474491
this.setDisplayText()
@@ -479,3 +496,41 @@ export default function filamentJalaliFormComponent({
479496
},
480497
}
481498
}
499+
500+
const locales = {
501+
ar: require('dayjs/locale/ar'),
502+
bs: require('dayjs/locale/bs'),
503+
ca: require('dayjs/locale/ca'),
504+
cs: require('dayjs/locale/cs'),
505+
cy: require('dayjs/locale/cy'),
506+
da: require('dayjs/locale/da'),
507+
de: require('dayjs/locale/de'),
508+
en: require('dayjs/locale/en'),
509+
es: require('dayjs/locale/es'),
510+
fa: require('dayjs/locale/fa'),
511+
fi: require('dayjs/locale/fi'),
512+
fr: require('dayjs/locale/fr'),
513+
hi: require('dayjs/locale/hi'),
514+
hu: require('dayjs/locale/hu'),
515+
hy: require('dayjs/locale/hy-am'),
516+
id: require('dayjs/locale/id'),
517+
it: require('dayjs/locale/it'),
518+
ja: require('dayjs/locale/ja'),
519+
ka: require('dayjs/locale/ka'),
520+
km: require('dayjs/locale/km'),
521+
ku: require('dayjs/locale/ku'),
522+
ms: require('dayjs/locale/ms'),
523+
my: require('dayjs/locale/my'),
524+
nl: require('dayjs/locale/nl'),
525+
pl: require('dayjs/locale/pl'),
526+
pt_BR: require('dayjs/locale/pt-br'),
527+
pt_PT: require('dayjs/locale/pt'),
528+
ro: require('dayjs/locale/ro'),
529+
ru: require('dayjs/locale/ru'),
530+
sv: require('dayjs/locale/sv'),
531+
tr: require('dayjs/locale/tr'),
532+
uk: require('dayjs/locale/uk'),
533+
vi: require('dayjs/locale/vi'),
534+
zh_CN: require('dayjs/locale/zh-cn'),
535+
zh_TW: require('dayjs/locale/zh-tw'),
536+
}

resources/js/dist/components/filament-jalali.js

Lines changed: 67 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)