Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions elm-package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"dependencies": {
"abadi199/dateparser": "1.0.0 <= v < 2.0.0",
"elm-community/list-extra": "4.0.0 <= v < 8.0.0",
"elm-community/string-extra": "1.4.0 <= v < 2.0.0",
"elm-lang/core": "5.0.0 <= v < 6.0.0",
"elm-lang/html": "2.0.0 <= v < 3.0.0",
"elm-lang/svg": "2.0.0 <= v < 3.0.0",
Expand Down
164 changes: 91 additions & 73 deletions src/DatePickerPanel.elm
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import DateTimePicker.Svg
import Html exposing (..)
import Html.Attributes exposing (value)
import List.Extra
import String.Extra


type alias State =
Expand All @@ -25,6 +26,8 @@ type alias Config otherConfig msg =
| onChange : State -> Maybe Date -> msg
, nameOfDays : NameOfDays
, firstDayOfWeek : Date.Day
, weekNumbers : Bool
, weekNumberPrefix : String
, allowYearNavigation : Bool
, titleFormatter : Date -> String
, footerFormatter : Date -> String
Expand All @@ -46,11 +49,11 @@ gotoNextMonth (InternalState state) =
updatedTitleDate =
Maybe.map (Date.Extra.Duration.add Date.Extra.Duration.Month 1) state.titleDate
in
InternalState
{ state
| event = "next"
, titleDate = updatedTitleDate
}
InternalState
{ state
| event = "next"
, titleDate = updatedTitleDate
}


gotoNextYear : State -> State
Expand All @@ -59,11 +62,11 @@ gotoNextYear (InternalState state) =
updatedTitleDate =
Maybe.map (Date.Extra.Duration.add Date.Extra.Duration.Year 1) state.titleDate
in
InternalState
{ state
| event = "nextYear"
, titleDate = updatedTitleDate
}
InternalState
{ state
| event = "nextYear"
, titleDate = updatedTitleDate
}


gotoPreviousMonth : State -> State
Expand All @@ -72,11 +75,11 @@ gotoPreviousMonth (InternalState state) =
updatedTitleDate =
Maybe.map (Date.Extra.Duration.add Date.Extra.Duration.Month -1) state.titleDate
in
InternalState
{ state
| event = "previous"
, titleDate = updatedTitleDate
}
InternalState
{ state
| event = "previous"
, titleDate = updatedTitleDate
}


gotoPreviousYear : State -> State
Expand All @@ -85,11 +88,11 @@ gotoPreviousYear (InternalState state) =
updatedTitleDate =
Maybe.map (Date.Extra.Duration.add Date.Extra.Duration.Year -1) state.titleDate
in
InternalState
{ state
| event = "previousYear"
, titleDate = updatedTitleDate
}
InternalState
{ state
| event = "previousYear"
, titleDate = updatedTitleDate
}



Expand Down Expand Up @@ -124,15 +127,15 @@ title config ((InternalState stateValue) as state) currentDate =
date =
stateValue.titleDate
in
span
[ config.class [ Title ]
, onMouseDownPreventDefault <| config.onChange (switchMode state) currentDate
]
[ date
|> Maybe.map config.titleFormatter
|> Maybe.withDefault "N/A"
|> text
]
span
[ config.class [ Title ]
, onMouseDownPreventDefault <| config.onChange (switchMode state) currentDate
]
[ date
|> Maybe.map config.titleFormatter
|> Maybe.withDefault "N/A"
|> text
]


previousYearButton : Config (CssConfig a msg CssClasses) msg -> State -> Maybe Date.Date -> Html msg
Expand Down Expand Up @@ -208,7 +211,7 @@ calendar config (InternalState state) currentDate =
year =
Date.year titleDate

days =
daysAndWeeks =
DateTimePicker.DateUtils.generateCalendar config.firstDayOfWeek month year

header =
Expand All @@ -233,46 +236,54 @@ calendar config (InternalState state) currentDate =
selectedDate =
DateTimePicker.DateUtils.toDate year month day
in
td
[ config.class
(case day.monthType of
DateTimePicker.DateUtils.Previous ->
[ PreviousMonth ]

DateTimePicker.DateUtils.Current ->
CurrentMonth
:: (if isHighlighted day then
[ SelectedDate ]
else if isToday day then
[ Today ]
else
[]
)

DateTimePicker.DateUtils.Next ->
[ NextMonth ]
)
, Html.Attributes.attribute "role" "button"
, Html.Attributes.attribute "aria-label" (Date.Extra.Format.format Date.Extra.Config.Config_en_us.config "%e, %A %B %Y" selectedDate)
, onMouseDownPreventDefault <| dateClickHandler config (InternalState state) year month day
, onTouchStartPreventDefault <| dateClickHandler config (InternalState state) year month day
]
[ text <| toString day.day ]

toWeekRow week =
tr [] (List.map toCell week)
td
[ config.class
(case day.monthType of
DateTimePicker.DateUtils.Previous ->
[ PreviousMonth ]

DateTimePicker.DateUtils.Current ->
CurrentMonth
:: (if isHighlighted day then
[ SelectedDate ]
else if isToday day then
[ Today ]
else
[]
)

DateTimePicker.DateUtils.Next ->
[ NextMonth ]
)
, Html.Attributes.attribute "role" "button"
, Html.Attributes.attribute "aria-label" (Date.Extra.Format.format Date.Extra.Config.Config_en_us.config "%e, %A %B %Y" selectedDate)
, onMouseDownPreventDefault <| dateClickHandler config (InternalState state) year month day
, onTouchStartPreventDefault <| dateClickHandler config (InternalState state) year month day
]
[ text <| toString day.day ]

weekNumberCell weekNumber =
td [ config.class [ WeekNumber ] ]
[ text (config.weekNumberPrefix ++ String.Extra.fromInt weekNumber) ]

toWeekRow ( weekNumber, week ) =
if config.weekNumbers then
tr [] (weekNumberCell weekNumber :: List.map toCell week)
else
tr [] (List.map toCell week)

body =
tbody [ config.class [ Days ] ]
(days
(daysAndWeeks.days
|> List.Extra.groupsOf 7
|> List.Extra.zip daysAndWeeks.weeks
|> List.map toWeekRow
)
in
table [ config.class [ Calendar ] ]
[ header
, body
]
table [ config.class [ Calendar ] ]
[ header
, body
]


dayNames : Config a msg -> List (Html msg)
Expand All @@ -290,10 +301,17 @@ dayNames config =

shiftAmount =
DateTimePicker.DateUtils.dayToInt Date.Sun config.firstDayOfWeek

insertWeekNumberColumn list =
if config.weekNumbers then
th [] [] :: list
else
list
in
days
|> List.Extra.splitAt shiftAmount
|> (\( head, tail ) -> tail ++ head)
days
|> List.Extra.splitAt shiftAmount
|> (\( head, tail ) -> tail ++ head)
|> insertWeekNumberColumn


dateClickHandler : Config a msg -> InternalState -> Int -> Date.Month -> DateTimePicker.DateUtils.Day -> msg
Expand All @@ -319,12 +337,12 @@ dateClickHandler config (InternalState state) year month day =
Nothing
}
in
case day.monthType of
DateTimePicker.DateUtils.Previous ->
config.onChange (gotoPreviousMonth updatedState) selectedDate
case day.monthType of
DateTimePicker.DateUtils.Previous ->
config.onChange (gotoPreviousMonth updatedState) selectedDate

DateTimePicker.DateUtils.Next ->
config.onChange (gotoNextMonth updatedState) selectedDate
DateTimePicker.DateUtils.Next ->
config.onChange (gotoNextMonth updatedState) selectedDate

DateTimePicker.DateUtils.Current ->
config.onChange updatedState selectedDate
DateTimePicker.DateUtils.Current ->
config.onChange updatedState selectedDate
Loading