thought2/elm-wikimedia-commons/1.0.0

NOTE: the latest version is 1.0.1

Renamed to m-bock/elm-wikimedia-commons. That is where the repository lives now. It has not been published as a package under the new name.

Versions published as thought2/elm-wikimedia-commons still install under that name. 2 versions of 2 come from the new location (see their notes).

Rerouted (renamed, verified). The GitHub user or repository was renamed. The tag is still there, at the new name. The Lamdera package server serves a copy recovered from https://github.com/m-bock/elm-wikimedia-commons.

What we checked: The repo moved to github.com/m-bock/elm-wikimedia-commons. Rebuilt with git archive from commit 2af63c4936dc of https://github.com/m-bock/elm-wikimedia-commons (zip prefix thought2-elm-wikimedia-commons-022afd7/, America/Los_Angeles times): the zip is byte-identical to the one package.elm-lang.org recorded, SHA-1 f2675ed4fa670f6026188cedafbaf340be377be4.

The Lamdera compiler uses this copy and prints a one-line notice saying so. elm itself fails to download this version.

Details · package.elm-lang.org's zip: https://github.com/thought2/elm-wikimedia-commons/zipball/1.0.0/

An Elm library for dealing with the Wikimedia Commons API.

See the API and the example for usage instructions.


module Viewer
    exposing
        ( Model
        , Msg(..)
        , constants
        , init
        , isValidResource
        , main
        , subscriptions
        , update
        , view
        , viewLarge
        )

import Browser
import Html as Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import Http as Http
import Task
import WikimediaCommons.RandomPictures as RP


main : Program () Model Msg
main =
    Browser.element
        { init = \_ -> init
        , view = view
        , update = update
        , subscriptions = subscriptions
        }


constants :
    { thumbWidth : Int
    , thumbMaxHeight : Int
    , largeWidth : Int
    , count : Int
    }
constants =
    { thumbWidth = 100
    , thumbMaxHeight = 150
    , largeWidth = 500
    , count = 40
    }



-- MODEL


type alias Model =
    { resources : List RP.PictureResource
    , selection : Maybe RP.PictureResource
    }


init : ( Model, Cmd Msg )
init =
    ( { resources = [], selection = Nothing }
    , getResources
    )


getResources : Cmd Msg
getResources =
    Task.attempt NewResources (RP.fetchResources constants.count)



-- UPDATE


type Msg
    = NewResources (Result Http.Error (List RP.PictureResource))
    | Refresh
    | Select RP.PictureResource
    | Close


isValidResource : RP.PictureResource -> Bool
isValidResource resource =
    let
        ( maxWidth, maxHeight ) =
            RP.getMaxSize resource

        aspect =
            toFloat maxWidth / toFloat maxHeight

        maxAspect =
            toFloat constants.thumbWidth / toFloat constants.thumbMaxHeight
    in
    maxWidth >= constants.largeWidth && aspect > maxAspect


update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
    case msg of
        Refresh ->
            ( model, getResources )

        NewResources (Ok resources) ->
            ( { model | resources = List.filter isValidResource resources }
            , Cmd.none
            )

        NewResources (Err error) ->
            let
                _ =
                    Debug.log "unhandled error" error
            in
            ( model, Cmd.none )

        Select selection ->
            ( { model | selection = Just selection }
            , Cmd.none
            )

        Close ->
            ( { model | selection = Nothing }
            , Cmd.none
            )



-- SUBSCRIPTIONS


subscriptions : Model -> Sub Msg
subscriptions model =
    Sub.none



-- VIEW


viewLarge : RP.PictureResource -> Html Msg
viewLarge resource =
    let
        styleWrapper =
            [ style "position" "fixed"
            , style "top" "0px"
            , style "right" "0px"
            , style "bottom" "0px"
            , style "left" "0px"
            , style "background-color" "rgbastyle0,0,0,0.5 "
            , style "display" "flex"
            , style "align-items" "center"
            , style "justify-content" "center"
            , style "cursor" "pointer"
            ]
    in
    div ([ onClick Close ] ++ styleWrapper)
        [ img
            [ src (RP.getUrl constants.largeWidth resource |> Maybe.withDefault "")
            ]
            []
        ]


view : Model -> Html Msg
view { resources, selection } =
    let
        styleWrapper =
            [ style "margin" "25px" ]

        styleWrapperList =
            [ style "display" "flex"
            , style "flex-wrap" "wrap"
            ]

        styleItem =
            [ style "border" "1px solid rgbastyle0,0,0, 0.3 "
            , style "width" <| String.fromInt constants.thumbWidth ++ "px"
            , style "height" <| String.fromInt constants.thumbMaxHeight ++ "px"
            , style "margin" "7px"
            , style "display" "flex"
            , style "align-items" "flex-end"
            , style "cursor" "pointer"
            ]

        styleButton =
            [ style "margin-left" "7px"
            , style "margin-bottom" "20px"
            ]
    in
    div styleWrapper
        [ button ([ onClick Refresh ] ++ styleButton) [ text "refresh" ]
        , div
            styleWrapperList
            (List.map
                (\res ->
                    div
                        ([ onClick (Select res)
                         ]
                            ++ styleItem
                        )
                        [ img
                            [ width 100
                            , src (RP.getUrl 100 res |> Maybe.withDefault "")
                            ]
                            []
                        ]
                )
                resources
            )
        , Maybe.map viewLarge selection |> Maybe.withDefault (text "")
        ]