/
Constraint.elm
79 lines (63 loc) · 2.21 KB
/
Constraint.elm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
module Constraint exposing (Model, init, positions, updateAttachments)
import Dict exposing (Dict)
type Model
= Model
{ -- comment ID to comment height
heights : Dict Int Float
-- comment ID to ideal position
, attachments : Dict Int Float
-- comment ID to actual position
, positions : Dict Int Float
-- margin, in pixels, to leave around comments
, margin : Float
}
init :
{ heights : Dict Int Float
, attachments : Dict Int Float
, margin : Float
}
-> Model
init { heights, attachments, margin } =
Model
{ heights = heights
, attachments = attachments
, positions = Dict.empty
, margin = margin
}
|> solveWithoutFocus
solveWithoutFocus : Model -> Model
solveWithoutFocus (Model guts) =
Model
{ guts
| positions =
guts.attachments
|> Dict.toList
|> List.sortBy Tuple.second
|> List.foldl
(\( id, idealPosition ) ( finalPositions, progressLine ) ->
let
height =
Dict.get id guts.heights
|> Maybe.withDefault 0
in
if idealPosition >= progressLine then
( Dict.insert id idealPosition finalPositions
, idealPosition + height + guts.margin
)
else
( Dict.insert id progressLine finalPositions
, progressLine + height + guts.margin
)
)
( Dict.empty, 0 )
|> Tuple.first
}
updateAttachments : Dict Int Float -> Model -> Model
updateAttachments attachments (Model guts) =
if attachments == guts.attachments then
Model guts
else
Model { guts | attachments = attachments } |> solveWithoutFocus
positions : Model -> Dict Int Float
positions (Model guts) =
guts.positions