jxxcarlson/elm-cell-grid/4.0.0

NOTE: the latest version is 8.0.1

CellGrid

The CellGrid package provides a type for representing a rectangular grid of cells. CellGrids can be created, transformed, and rendered as either SVG or HTML.

Types

type CellGrid a
= CellGrid ( Int, Int ) (Array a)

A value of type CellGrid a is a rectangular array of values of type a.

= Corner
| Edge
| Interior

A cell can be in the interior of the grid, on an edge, on in a cornder

type alias CellRenderer a =
{ cellSize : Float
, gridLineWidth : Float
, cellColorizer : a -> Color
, defaultColor : Color
, gridLineColor : Color
}

CellRenderer is a record that provides the information -- size and color -- that is needed to render a cell to SVG. Color is as defined in the package avh4/elm-color, e.g. Color.rgb 1 0 0, which is bright red.

type Msg
= MouseClick ( Int, Int ) ( Float, Float )

The MouseClick message sends the matrix index (i,j) of the cell on which the user has clicked as well as the local (x,y) coordinates of the cell.

Constructing and rendering CellGrids

cellGridFromList : Int -> Int -> List a -> Maybe (CellGrid a)

Construct a Maybe CellGrid from a list of values of type a. Here is a 2x2 cell grid of type Float:

> cg = cellGridFromList 2 2 [1.0,2.0,3.0,4.0]
  Just (CellGrid (2,2) (Array.fromList [1,2,3,4]))
  : Maybe (CellGrid Float)

If the length of the list is incompatible with the given number of rows and columns Nothing is returned.

> cellGridFromList 2 2 [1.0,2.0,3.0,4.0, 5.0]
  Nothing : Maybe (CellGrid Float)

The empty cell grid. Useful in conjunction with Maybe.withDefault

renderAsHtml : Float -> Float -> CellRenderer a -> CellGrid a -> Html Msg

Render a cell grid as Html. The first two parameters are the width and height of the rendered grid in pixels.

renderAsSvg : CellRenderer a -> CellGrid a -> Svg Msg

Render a cell grid as SVG

Work with cells

map : (a -> a) -> CellGrid a -> CellGrid a

Map a function over a CellGrid:

> map (\x -> 2*x) cg
CellGrid (2,2) (Array.fromList [2,4,6,8])
mapWithIndex : (( Int, Int ) -> a -> a) -> CellGrid a -> CellGrid a

Transform a CellGrid a with a function ((Int, Int) -> a -> a). Used when the transformed value depends on its matrixIndex as well as its value

foldl : (a -> b -> b) -> b -> CellGrid a -> b

Fold a reducer (a -> b -> b) over a CellGrid a)

transform : (( Int, Int ) -> CellGrid a -> a) -> CellGrid a -> CellGrid a

Transform a CellGrid using a function

(Int, Int) -> CellGrid a -> a)
classifyCell : CellGrid a -> ( Int, Int ) -> CellType

return the type of the cell at location (i,j). Thus classifyCell grid (0,0) = Corner

cellAtMatrixIndex : ( Int, Int ) -> CellGrid a -> Maybe a

Return the Maybe value of the cell at (i,j) from grid

> cellAtMatrixIndex (1,1) cg
Just 4 : Maybe Float
setValue : CellGrid a -> ( Int, Int ) -> a -> CellGrid a

Set the value of the cell at location (i,j)