Homework-4/src/ClassSchedule/View.elm

137 lines
4.4 KiB
Elm

module ClassSchedule.View exposing (..)
import ClassSchedule.Model exposing (..)
import Html exposing (Html, Attribute, div, text, table, td, th, tr, span, input, h1)
import Html.Attributes exposing (class, classList, type_)
import Html.Events exposing (onClick)
import Tuple exposing (..)
import Dict exposing (..)
import FeatherIcons
iconButton : String -> FeatherIcons.Icon -> String -> List (Attribute Msg) -> Html Msg
iconButton c i s attrs = span ([ class "button", class ("color-" ++ c) ] ++ attrs)
[ FeatherIcons.toHtml [] i, text s ]
viewDept : Department -> String
viewDept d =
case d of
ComputerScience -> "CS"
Mathematics -> "MTH"
Art -> "ART"
Biology -> "BIO"
viewCrn : Crn -> String
viewCrn (d, i) = viewDept d ++ " " ++ (String.pad 3 '0' <| String.fromInt i)
viewTimeNumber : Int -> String
viewTimeNumber i = String.pad 2 '0' <| String.fromInt i
viewDayHalf : DayHalf -> String
viewDayHalf dh =
case dh of
AM -> "AM"
PM -> "PM"
viewTime : Time -> String
viewTime (m, h, dh) = viewTimeNumber m ++ ":" ++ viewTimeNumber h ++ viewDayHalf dh
viewTimeRange : (Time, Time) -> String
viewTimeRange (t1, t2) = viewTime t1 ++ " to " ++ viewTime t2
viewDayCode : DayOfWeek -> String
viewDayCode dw =
case dw of
Monday -> "M"
Tuesday -> "T"
Wednesday -> "W"
Thursday -> "R"
Friday -> "F"
Saturday -> "S"
Sunday -> "U"
unique : List eq -> List eq
unique l =
case l of
[] -> []
(x::xs) -> x :: unique (List.filter ((/=) x) xs)
extractTimes : Course -> List (Time, Time)
extractTimes c =
let extractTime (d, t1, t2) = (t1, t2)
in unique <| List.map extractTime (c.times)
extractClasses : Course -> (Time, Time) -> List DayOfWeek
extractClasses c (t1, t2) =
let matches (d, tt1, tt2) = if tt1 == t1 && tt2 == t2 then Just d else Nothing
in List.filterMap matches (c.times)
extractTimeCodes : Course -> List String
extractTimeCodes c =
let
fromTime tr =
let dayCodes = String.concat <| List.map viewDayCode <| extractClasses c tr
in dayCodes ++ " " ++ viewTimeRange tr
in List.map fromTime <| extractTimes c
viewClass : Maybe Int -> Int -> (CourseStatus, Course) -> Html Msg
viewClass sel i (cs, c) =
let
isSelected = sel == Just i
addedIndicator =
case cs of
Added -> [ FeatherIcons.check |> FeatherIcons.toHtml [] ]
NotAdded -> []
in
tr [ class "hoverable", onClick (SelectCourse i), classList [("selected", isSelected)] ]
[ td [] addedIndicator
, td [] [ text <| viewCrn (c.crn) ]
, td [] [ text <| c.name ]
, td [] [ text <| String.join ", " <| c.instructors ]
, td [] [ text <| String.join ", " <| extractTimeCodes c ]
, td [] <|
case (isSelected, cs) of
(True, NotAdded) -> [ iconButton "green" (FeatherIcons.check) "Add" [ onClick (AddCourse i)] ]
(True, Added) -> [ iconButton "red" (FeatherIcons.x) "Remove" [onClick (RemoveCourse i)] ]
_ -> []
]
viewClassTable : Maybe Int -> List (CourseStatus, Course) -> Html Msg
viewClassTable sel =
let
header = tr []
[ th [] [ ]
, th [] [ text "Crn." ]
, th [] [ text "Course Name" ]
, th [] [ text "Instructors" ]
, th [] [ text "Times" ]
, th [] []
]
in
div [ class "table-wrapper" ] << List.singleton << table [ class "class-table" ] << (::) header << List.indexedMap (viewClass sel)
viewToolbar : Model -> Html Msg
viewToolbar m = div []
[ span [] [ text "Search: " , input [ type_ "text" ] [] ]
]
viewClassList : Model -> Html Msg
viewClassList m = div []
[ viewToolbar m
, case get (m.term) (m.terms) of
Just cs -> viewClassTable (m.selected) cs
Nothing -> text "Please select a term!"
]
viewClassSchedule : Model -> Html Msg
viewClassSchedule m = div [ class "table-wrapper" ]
[ text "Nothing here yet!"
]
viewModel : Model -> Html Msg
viewModel m = div [ class "main" ]
[ h1 [] [ text "Oregon State University Course Schedule" ]
, div [ class "split-pane" ]
[ div [ class "split-elem" ] [ viewClassList m ]
, div [ class "split-elem" ] [ viewClassSchedule m ]
]
]