Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add conversion from image to number #16

Merged
merged 13 commits into from
Jun 2, 2018
38 changes: 38 additions & 0 deletions LintConfig.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
module LintConfig exposing (config)

import Lint.Types exposing (LintRule, Severity(..))
import Lint.Rules.DefaultPatternPosition
import Lint.Rules.NoConstantCondition
import Lint.Rules.NoDebug
import Lint.Rules.NoDuplicateImports
import Lint.Rules.NoExposingEverything
import Lint.Rules.NoImportingEverything
import Lint.Rules.NoNestedLet
import Lint.Rules.NoUnannotatedFunction
import Lint.Rules.NoUnusedVariables
import Lint.Rules.NoUselessIf
import Lint.Rules.NoUselessPatternMatching
import Lint.Rules.NoWarningComments
import Lint.Rules.SimplifyPiping
import Lint.Rules.SimplifyPropertyAccess
import Lint.Rules.ElmTest.NoDuplicateTestBodies


config : List ( Severity, LintRule )
config =
[ ( Critical, Lint.Rules.DefaultPatternPosition.rule { position = Lint.Rules.DefaultPatternPosition.Last } )
, ( Critical, Lint.Rules.NoConstantCondition.rule )
, ( Critical, Lint.Rules.NoDebug.rule )
, ( Critical, Lint.Rules.NoDuplicateImports.rule )
, ( Critical, Lint.Rules.NoExposingEverything.rule )
, ( Critical, Lint.Rules.NoImportingEverything.rule { exceptions = [ "Html" ] } )
, ( Critical, Lint.Rules.NoNestedLet.rule )
, ( Critical, Lint.Rules.NoUnannotatedFunction.rule )
, ( Critical, Lint.Rules.NoUnusedVariables.rule )
, ( Critical, Lint.Rules.NoUselessIf.rule )
, ( Critical, Lint.Rules.NoUselessPatternMatching.rule )
, ( Warning, Lint.Rules.NoWarningComments.rule )
, ( Critical, Lint.Rules.SimplifyPiping.rule )
, ( Critical, Lint.Rules.SimplifyPropertyAccess.rule )
, ( Critical, Lint.Rules.ElmTest.NoDuplicateTestBodies.rule )
]
14 changes: 11 additions & 3 deletions elm-src/Config.elm
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
module Config exposing (..)

stageCount = 2
module Config exposing ( stageCount
, imageInputId
, nonPrimeImageNumberId
)
stageCount : Int
stageCount = 3

imageInputId : String
imageInputId = "file"

nonPrimeImageNumberId : String
nonPrimeImageNumberId = "nonPrimeImageNumberId"

105 changes: 49 additions & 56 deletions elm-src/DisplayPanel.elm
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
module DisplayPanel exposing (Props, view)

import Array
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)

import Types
import Resources
import Config

type alias Props =
{ stage : Int
, canGoNext : Bool
, imagePreview : Maybe Types.Image
, nonPrimeImage : Maybe Types.ImageNumber
}


Expand All @@ -20,8 +21,6 @@ view props =
div []
[ div
[ class "display-panel stage-selecting"
, style
[ ( "--show-stage", toString props.stage ) ]
]
( List.map
( div <|
Expand All @@ -36,60 +35,54 @@ view props =
displays : Props -> List (List (Html.Html Types.Msg))
displays props =
let
imagePreview = Maybe.withDefault Resources.defaultImage props.imagePreview
imagePreview =
props.imagePreview
|> Maybe.withDefault Resources.defaultImage
nonPrimeImageList =
props.nonPrimeImage
|> Maybe.map imageNumber2displayString
|> Maybe.map text
|> maybeSingleton
in
[ [ span [ class "image-number" ]
[ text (String.filter (\c -> (c /= ' ')) """
1001010777777777777770777777777777777777770777777777777777777777
1010010777777777777702077777777777777777702077777777777777777777
1001000777777777777770777777777777777777770777777777777777777777
7777777777777777777705077777777777777777705077777777777777777777
7777777777777777777055107777777777777777015507777777777777777777
7777777777777777777055507777777777777777055507777777777777777777
7777777777777777777000037777777007777777300007777777777777777777
7777777777777777770433307777770880777777033340777777777777777777
7777777778777777777000007777771001777777000007777777777777777777
7778777077777777777099907777710550177777099907777777777707777777
7777777017777777777099907777105555017777099907777777777107777777
7777777047777777777099907717053003507177100007777777777407777777
7777770107777777787000800500511111150050080007777777777010777777
7777770107777777777109900051100000011500099907777777777010777777
7777777247777777777000001115051111605111001007777777777327777777
7777777007777787777099301500115005110051039907777777777007777777
7777777077877777771099300011110880111100039901777787777707777777
7777777047777777700000001111115005111112000000078777777407778777
7000700560110011055059901111111111111111099505501100110650070007
0656065555005500555009001111116005111111009005550055005555606560
5555555555555555555009001115088988805111009005555555555555555555
0000005500000000000090901119088888809111090900000000000055000000
1111105501111111111090901110888888880111090901111111111055011111
9511105501115995111090901118008008008111090901115995111055011159
0851105501168008511030301118888888888111030301159008512065011580
0801100001108008011053501118888888889111053501108008011000011080
0801105501108008010900001118888888888111000090108018011055011080
0802105501108008011055501110000330000111056501108008011055011080
0801105501108008011059503400000000099933059501108008011055011080
3331105501133333311093305555655555555555033901133333312055011333
0000005500000000000095909999999999999999095900000000000055000000
1111505505111111111094901111112111111111094901111111115055051111
1111105501111111111092901119000100009111092901111111111055011112
1111100001111112111095901210888988880111095901111111111000011111
8511105501115891111095901110800880080111195901111985111055011158
0051106501150008111092901110088888800111092901118000511055011500
0831105501138080110055901110188888800111095500110808311055011380
0831105501138080110555550110088888800110555550110808311056011390
0031105611130000110555550110088888800110555550110000311055011300
1111055550111111110555550110088888800110555550111111110555501112
1111055550111111110555550110088888800110555550111111110550000000
0000055550000000000555550000088888800000555550000000000550120397
"""
) ]
]
, [ img
[ src imagePreview.contents
, title imagePreview.filename
[ [ span
[ class "image-number" ]
[ text <|
imageNumber2displayString Resources.corpusImageNumber
]
]
, [ img
[ src imagePreview.contents
, title imagePreview.filename
]
[]
]
, [ span
[ class "image-number"
, id Config.nonPrimeImageNumberId ]
nonPrimeImageList
]
[]
]
]

maybeSingleton : Maybe a -> List a
maybeSingleton maybe =
case maybe of
Just val ->
[val]
Nothing ->
[]

imageNumber2rows : Types.ImageNumber -> List String
imageNumber2rows imageNumber =
let
{ width, number } = imageNumber
in
if number == "" then
[]
else
String.left width number :: imageNumber2rows { imageNumber | number = (String.dropLeft width number) }

imageNumber2displayString : Types.ImageNumber -> String
imageNumber2displayString imageNumber =
imageNumber
|> imageNumber2rows
|> String.join "\n"
10 changes: 0 additions & 10 deletions elm-src/ImageUploadPort.elm

This file was deleted.

104 changes: 60 additions & 44 deletions elm-src/InteractionPanel.elm
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
module InteractionPanel exposing (Props, view)

import Array
import Html exposing (..)
import Html exposing (button, div, section, input, h1, p, text, Html, label, strong)
import Svg
import Json.Decode as Decode
import Html.Attributes exposing (..)
import Html.Attributes exposing (class, attribute, type_, name, id, disabled, for)
import Svg.Attributes
import Html.Events exposing (..)
import Html.Events exposing (onClick, on)

import ToNumberConfig.View
import ToNumberConfig.Types

import Types
import Config
Expand All @@ -15,6 +18,7 @@ type alias Props =
{ stage : Int
, canGoBack : Bool
, canGoNext : Bool
, numberConfig : ToNumberConfig.Types.Model
}

view : Props -> Html.Html Types.Msg
Expand All @@ -24,7 +28,7 @@ view props =
Array.get props.stage (instructions props)
|> Maybe.withDefault (default_instructions props.stage)
interaction =
Array.get props.stage interactions
Array.get props.stage (interactions props)
|> Maybe.withDefault []

in
Expand Down Expand Up @@ -58,52 +62,64 @@ stageButton change props =

instructions : Props -> Array.Array (List (Html Types.Msg))
instructions props =
Array.fromList
[ [ h1 [] [ text "Welcome to Prime Image" ]
, p []
[ text "Click "
, stageButton 1 props [ text "Next" ]
, text "to begin."
Array.fromList
[ [ h1 [] [ text "Welcome to Prime Image" ]
, p []
[ text "Click "
, stageButton 1 props [ text "Next" ]
, text "to begin."
]
]
, [ h1 [] [ text "Select Image" ]
, p [] [ text "First you must select an image to turn into a prime number." ]
, p [] [ text "Use the box below to open an image." ]
]
, [ h1 [] [ text "Convert Image To Number" ]
, p [] [ text "Use the controls below to convert the image to a number" ]
]
]
]
, [ h1 [] [ text "Select Image" ]
, p [] [ text "First you must select an image to turn into a prime number." ]
, p [] [ text "Use the box below to open an image." ]
]
]

-- todo: xmlns="http://www.w3.org/2000/svg"


interactions : Array.Array (List (Html Types.Msg))
interactions = Array.fromList
[ []
, [ Html.form
[ class "image-pick" ]
[ Svg.svg [ Svg.Attributes.class "icon", Svg.Attributes.width "50", Svg.Attributes.height "43", Svg.Attributes.viewBox "0 0 50 43" ]
[ Svg.path
[ Svg.Attributes.d """
M48.4 26.5c-.9 0-1.7.7-1.7 1.7v11.6h-43.3v-11.6c0-.9-.7-1.7-1.7-1.7s-1.7.7-1.7 1.7v13.2c0
.9.7 1.7 1.7 1.7h46.7c.9 0 1.7-.7 1.7-1.7v-13.2c0-1-.7-1.7-1.7-1.7zm-24.5 6.1c.3.3.8.5
1.2.5.4 0 .9-.2 1.2-.5l10-11.6c.7-.7.7-1.7 0-2.4s-1.7-.7-2.4
0l-7.1 8.3v-25.3c0-.9-.7-1.7-1.7-1.7s-1.7.7-1.7 1.7v25.3l-7.1-8.3c-.7-.7-1.7-.7-2.4
0s-.7 1.7 0 2.4l10 11.6z
"""
] []
interactions : Props -> Array.Array (List (Html Types.Msg))
interactions props =
Array.fromList
[ []
, [ Html.form
[ class "image-pick" ]
[ Svg.svg
[ Svg.Attributes.class "icon"
, Svg.Attributes.width "50"
, Svg.Attributes.height "43"
, Svg.Attributes.viewBox "0 0 50 43"
]
[ Svg.path
[ Svg.Attributes.d """
M48.4 26.5c-.9 0-1.7.7-1.7 1.7v11.6h-43.3v-11.6c0-.9-.7-1.7-1.7-1.7s-1.7.7-1.7 1.7v13.2c0
.9.7 1.7 1.7 1.7h46.7c.9 0 1.7-.7 1.7-1.7v-13.2c0-1-.7-1.7-1.7-1.7zm-24.5 6.1c.3.3.8.5
1.2.5.4 0 .9-.2 1.2-.5l10-11.6c.7-.7.7-1.7 0-2.4s-1.7-.7-2.4
0l-7.1 8.3v-25.3c0-.9-.7-1.7-1.7-1.7s-1.7.7-1.7 1.7v25.3l-7.1-8.3c-.7-.7-1.7-.7-2.4
0s-.7 1.7 0 2.4l10 11.6z
"""
] []
]
, input
[ type_ "file"
, name "files[]"
, id Config.imageInputId
, on "change" <| Decode.succeed Types.ImageSelected
] []
, label [ for "file" ]
[ strong [] [ text "Choose a file" ]
, text " or drag it here."
]
]
]
, [ Html.map Types.UpdateNumberConfig (ToNumberConfig.View.view props.numberConfig)
]
]
, input
[ type_ "file"
, name "files[]"
, id Config.imageInputId
, on "change" <| Decode.succeed Types.ImageSelected
] []
, label [ for "file" ]
[ strong [] [ text "Choose a file" ]
, text " or drag it here."
]
]
]
]


default_instructions : int -> List(Html Types.Msg)
default_instructions stage =
Expand Down
35 changes: 35 additions & 0 deletions elm-src/Ports.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
port module Ports exposing ( fileSelected
, fileContentRead
, requestNonPrime
, nonPrimeGenerated
, prettyPrintState
, setInitialValues
, resizeImageNumber
, setCssProp
)
import Types
import ToNumberConfig.Types

{-| Allow file upload on click
credit to https://www.paramander.com/blog/using-ports-to-deal-with-files-in-elm-0-17
-}

port fileSelected : String -> Cmd msg
port fileContentRead : (Types.Image -> msg) -> Sub msg


port requestNonPrime : { toNumberConfig : ToNumberConfig.Types.Model, image : Types.Image } -> Cmd msg
port nonPrimeGenerated : (Types.ImageNumber -> msg) -> Sub msg

prettyPrintState : (Types.Model, Cmd msg) -> (Types.Model, Cmd msg)
prettyPrintState ( model, msg ) =
( model, Cmd.batch [msg, ppState model] )

port ppState : Types.Model -> Cmd msg

port setInitialValues : ToNumberConfig.Types.Model -> Cmd msg

port resizeImageNumber : String -> Cmd msg

-- selector prop value
port setCssProp : ( String, String, String ) -> Cmd msg
Loading