scoop:scoop.ui.component:0.1.0
src/calendar/pages.scoop
Selected production source · src/calendar/pages.scoop
1
package scoop.ui.component3
import scoop.datetime.*5
public enum CalendarWeekNumbers {6
None, Iso, Locale7
}9
public enum CalendarCaption {10
Label, Dropdowns11
}13
public class CalendarLocale public constructor(14
public val months: Array<String> =15
[16
"January",17
"February",18
"March",19
"April",20
"May",21
"June",22
"July",23
"August",24
"September",25
"October",26
"November",27
"December"28
],29
public val shortMonths: Array<String> =30
["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],31
public val weekdays: Array<String> = ["Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"],32
public val weekStartsOn: DayOfWeek = DayOfWeek.Sunday,33
public val firstWeekContainsDate: Int = 1,34
public val number: (Int) -> String = { value -> value.toString() },35
public val date: (LocalDate) -> String = { value -> value.toString() },36
/** Accessible name for the existing previous-page action. */37
public val previousMonth: String = "Previous month",38
/** Accessible name for the existing next-page action. */39
public val nextMonth: String = "Next month"40
) {41
init {42
requireComponent(43
months.size == 12L && shortMonths.size == 12L && weekdays.size == 7L,44
"calendar locale needs twelve month names and abbreviations and seven weekdays"45
)46
requireComponent(47
firstWeekContainsDate >= 1 && firstWeekContainsDate <= 7,48
"invalid calendar first week rule"49
)50
}51
}53
public class CalendarDayData public constructor(54
public val date: LocalDate,55
public val label: String,56
public val outside: Boolean = false,57
public val enabled: Boolean = true58
)60
public class CalendarWeek public constructor(61
public val days: Array<CalendarDayData?>,62
public val number: String? = None63
) {64
init {65
requireComponent(days.size == 7L, "calendar weeks need exactly seven cells")66
}67
}69
public class CalendarPageChoice public constructor(70
public val anchor: LocalDate,71
public val label: String72
)74
public class CalendarPage public constructor(75
public val anchor: LocalDate,76
public val caption: String,77
public val weeks: Array<CalendarWeek>,78
public val previous: LocalDate? = None,79
public val next: LocalDate? = None,80
public val previousYear: LocalDate? = None,81
public val nextYear: LocalDate? = None,82
public val months: Array<CalendarPageChoice> = [],83
public val years: Array<CalendarPageChoice> = []84
) {85
init {86
requireComponent(weeks.size > 0L, "calendar page needs at least one week")87
var last: LocalDate? = None88
var primary = false89
for (week in weeks)90
for (cell in week.days)91
when (cell) {92
case Some(val day) -> {93
requireComponent(94
(last?.compareTo(day.date) ?: -1L) < 0L,95
"calendar page dates must be unique and increasing"96
)97
last = Some(day.date)98
if (!day.outside)99
primary = true100
}101
case None -> {}102
}103
requireComponent(primary, "calendar page needs a date belonging to the page")104
requireComponent(105
previous != Some(anchor) && next != Some(anchor),106
"calendar neighbor must be a different page"107
)108
for (choices in [months, years])109
for (index in 0L ..< choices.size)110
for (before in 0L ..< index)111
requireComponent(112
choices[index].anchor != choices[before].anchor,113
"calendar page choices must be unique"114
)115
}116
}118
public class CalendarPageRequest public constructor(119
public val anchor: LocalDate,120
public val locale: CalendarLocale = CalendarLocale(),121
public val showOutsideDays: Boolean = true,122
public val fixedWeeks: Boolean = false,123
public val weekNumbers: CalendarWeekNumbers = CalendarWeekNumbers.None,124
public val years: Array<Int> = []125
) {126
public val weekStart: DayOfWeek get() =127
if (weekNumbers == CalendarWeekNumbers.Iso)128
DayOfWeek.Monday129
else130
locale.weekStartsOn131
}133
public interface CalendarPageSource {134
public fun page(request: CalendarPageRequest): CalendarPage135
}137
/** Calendar arithmetic comes from scoop.datetime; this source only lays dates into weeks. */138
public class GregorianCalendarPages public constructor(): CalendarPageSource {139
public override fun page(request: CalendarPageRequest): CalendarPage {140
val month = request.anchor.yearMonth141
val first = month.atDay(1)142
val offset = (first.dayOfWeek.isoDayNumber - request.weekStart.isoDayNumber + 7) % 7143
val start = first.toEpochDays() - offset.toLong()144
val count =145
if (request.fixedWeeks)146
6L147
else148
((offset + month.lengthOfMonth + 6) / 7).toLong()149
val weeks =150
Array<CalendarWeek>(count) {151
row ->152
val cells =153
Array<CalendarDayData?>(7L) {154
column ->155
val epoch = start + row * 7L + column156
if (157
epoch <158
LocalDate.MIN.toEpochDays() ||159
epoch >160
LocalDate.MAX.toEpochDays()161
)162
None163
else {164
val date = LocalDate.fromEpochDays(epoch)165
val outside = date.yearMonth != month166
if (outside && !request.showOutsideDays)167
None168
else169
Some(170
CalendarDayData(171
date,172
request.locale.number(date.dayOfMonth),173
outside174
)175
)176
}177
}178
var representative: LocalDate? = None179
for (cell in cells)180
if (representative == None)181
representative = cell?.date182
val number =183
when (request.weekNumbers) {184
case None -> None185
else -> when (representative) {186
case Some(val date) -> Some(187
request.locale.number(calendarWeekNumber(date, request))188
)189
case None -> None190
}191
}192
CalendarWeek(cells, number)193
}194
val months =195
Array<CalendarPageChoice>(12L) {196
index -> CalendarPageChoice(197
LocalDate(month.year, (index + 1L).toInt(), 1),198
request.locale.shortMonths[index]199
)200
}201
val years = ArrayList<CalendarPageChoice>()202
if (request.years.size == 0L) {203
val startYear =204
if (month.year < -999999899)205
-999999999206
else207
month.year - 100208
val endYear =209
if (month.year > 999999979)210
999999999211
else212
month.year + 20213
for (year in startYear ..< (endYear + 1))214
years.add(215
CalendarPageChoice(216
LocalDate(year, month.monthNumber, 1),217
request.locale.number(year)218
)219
)220
} else221
for (year in request.years)222
years.add(223
CalendarPageChoice(224
LocalDate(year, month.monthNumber, 1),225
request.locale.number(year)226
)227
)228
return CalendarPage(229
first,230
request.locale.months[(month.monthNumber - 1).toLong()] +231
" " +232
request.locale.number(month.year),233
weeks,234
calendarShiftMonth(first, -1L),235
calendarShiftMonth(first, 1L),236
calendarShiftMonth(first, -12L),237
calendarShiftMonth(first, 12L),238
months,239
years.toArray()240
)241
}242
}244
internal fun calendarShiftMonth(date: LocalDate, months: Long): LocalDate? =245
try {246
Some(date.plusMonths(months))247
} catch (_: ArithmeticException) {248
None249
}251
internal fun calendarShiftDay(date: LocalDate, days: Long): LocalDate? =252
try {253
Some(date.plusDays(days))254
} catch (_: ArithmeticException) {255
None256
}258
private fun weekStartEpoch(january: Long, day: Int, weekday: Int): Long {259
val epoch = january + day.toLong() - 1L260
val iso = ((epoch + 3L) % 7L + 7L) % 7L + 1L261
return epoch - (iso - weekday.toLong() + 7L) % 7L262
}264
internal fun calendarWeekNumber(date: LocalDate, request: CalendarPageRequest): Int {265
val iso = request.weekNumbers == CalendarWeekNumbers.Iso266
val day =267
if (iso)268
4269
else270
request.locale.firstWeekContainsDate271
val weekday = request.weekStart.isoDayNumber272
val january = LocalDate(date.year, 1, 1).toEpochDays()273
val epoch = date.toEpochDays()274
val start = weekStartEpoch(january, day, weekday)275
val nextJanuary = LocalDate(date.year, 12, 31).toEpochDays() + 1L276
val next = weekStartEpoch(nextJanuary, day, weekday)277
if (epoch >= next)278
return 1279
if (epoch >= start)280
return ((epoch - start) / 7L + 1L).toInt()281
// Week labels may refer to the preceding year even at the representable date boundary.282
val previousYear = date.year - 1283
val leap = previousYear % 4 == 0 && (previousYear % 100 != 0 || previousYear % 400 == 0)284
val previous =285
weekStartEpoch(286
january -287
(288
if (leap)289
366L290
else291
365L292
),293
day,294
weekday295
)296
return ((epoch - previous) / 7L + 1L).toInt()297
}