| 1 | package mapa |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "math" |
| 6 | "net/url" |
| 7 | "strings" |
| 8 | ) |
| 9 | |
| 10 | // The map (level-up spec §4.3): an SVG the server draws, with no |
| 11 | // simulation. This page sits at the centre and its neighbours on a ring |
| 12 | // around it, in a stable order, so the same page draws the same map. |
| 13 | |
| 14 | // MapView is the map's drawing. |
| 15 | type MapView struct { |
| 16 | Here MapNode |
| 17 | Nodes []MapNode |
| 18 | Edges []MapEdge |
| 19 | // Label is the map's accessible name: what it shows, in words. |
| 20 | Label string |
| 21 | } |
| 22 | |
| 23 | // MapNode is one circle and its label. |
| 24 | type MapNode struct { |
| 25 | Label, Href string |
| 26 | // Kind is "here", "linked", "pending" or "mentioned". |
| 27 | Kind string |
| 28 | X, Y, LabelX, LabelY float64 |
| 29 | // Anchor is the label's text-anchor. |
| 30 | Anchor string |
| 31 | } |
| 32 | |
| 33 | // MapEdge is one line. Kind is "linked", "pending" or "mentioned". |
| 34 | type MapEdge struct { |
| 35 | X1, Y1, X2, Y2 float64 |
| 36 | Kind string |
| 37 | } |
| 38 | |
| 39 | // The drawing's box and ring, in SVG units (viewBox 0 0 mapW mapH). |
| 40 | const ( |
| 41 | mapW, mapH = 284.0, 200.0 |
| 42 | mapCX, mapCY = 142.0, 98.0 |
| 43 | ringRX, ringRY = 104.0, 66.0 |
| 44 | labelGap = 13.0 |
| 45 | labelMargin = 34.0 |
| 46 | firstAngleDegree = -150.0 |
| 47 | ) |
| 48 | |
| 49 | // mapOf places the ring and draws its edges. |
| 50 | func (view scoped) mapOf(page *entry, ring []neighbour) MapView { |
| 51 | here := page.label(view.scope.Locale) |
| 52 | drawing := MapView{Here: MapNode{Label: here, Kind: "here", |
| 53 | X: mapCX, Y: mapCY, LabelX: mapCX, LabelY: mapCY - labelGap, |
| 54 | Anchor: "middle"}} |
| 55 | for at, near := range ring { |
| 56 | node := placed(near, at, len(ring)) |
| 57 | drawing.Nodes = append(drawing.Nodes, node) |
| 58 | drawing.Edges = append(drawing.Edges, MapEdge{X1: mapCX, Y1: mapCY, |
| 59 | X2: node.X, Y2: node.Y, Kind: near.kind}) |
| 60 | } |
| 61 | for one := range ring { |
| 62 | for other := range ring { |
| 63 | if one != other && view.links(ring[one].page, ring[other].page) && |
| 64 | (one < other || !view.links(ring[other].page, |
| 65 | ring[one].page)) { |
| 66 | drawing.Edges = append(drawing.Edges, MapEdge{ |
| 67 | X1: drawing.Nodes[one].X, Y1: drawing.Nodes[one].Y, |
| 68 | X2: drawing.Nodes[other].X, Y2: drawing.Nodes[other].Y, |
| 69 | Kind: "linked"}) |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | drawing.Label = mapLabel(here, ring) |
| 74 | return drawing |
| 75 | } |
| 76 | |
| 77 | // placed puts the at-th of count neighbours on the ring, its label |
| 78 | // outside the ring and inside the box. |
| 79 | func placed(near neighbour, at, count int) MapNode { |
| 80 | angle := (firstAngleDegree + 360*float64(at)/float64(count)) * |
| 81 | math.Pi / 180 |
| 82 | across := round1(mapCX + ringRX*math.Cos(angle)) |
| 83 | down := round1(mapCY + ringRY*math.Sin(angle)) |
| 84 | node := MapNode{Label: near.label, Href: near.href, Kind: near.kind, |
| 85 | X: across, Y: down, LabelX: across, Anchor: "middle"} |
| 86 | if down < mapCY { |
| 87 | node.LabelY = down - labelGap + 4 |
| 88 | } else { |
| 89 | node.LabelY = down + labelGap + 4 |
| 90 | } |
| 91 | switch { |
| 92 | case across < labelMargin: |
| 93 | node.Anchor = "start" |
| 94 | node.LabelX = across - 6 |
| 95 | case across > mapW-labelMargin: |
| 96 | node.Anchor = "end" |
| 97 | node.LabelX = across + 6 |
| 98 | } |
| 99 | return node |
| 100 | } |
| 101 | |
| 102 | func round1(value float64) float64 { return math.Round(value*10) / 10 } |
| 103 | |
| 104 | // mapLabel says the map in words, for anyone who cannot see it. |
| 105 | func mapLabel(here string, ring []neighbour) string { |
| 106 | if len(ring) == 0 { |
| 107 | return "A map of the pages around " + here + |
| 108 | ": nothing links here, and it links nowhere yet." |
| 109 | } |
| 110 | var names []string |
| 111 | for _, near := range ring { |
| 112 | names = append(names, near.label+" ("+mapKindWords[near.kind]+")") |
| 113 | } |
| 114 | return fmt.Sprintf("A map of the pages around %s: %s.", here, |
| 115 | strings.Join(names, ", ")) |
| 116 | } |
| 117 | |
| 118 | var mapKindWords = map[string]string{ |
| 119 | "linked": "linked", "pending": "not written yet", |
| 120 | "mentioned": "mentioned without a link", |
| 121 | } |
| 122 | |
| 123 | // pendingHref is where a link to a page not written yet goes: the new |
| 124 | // page form, its title filled in (base §6). |
| 125 | func pendingHref(text string) string { |
| 126 | return "/new?title=" + url.QueryEscape(text) |
| 127 | } |
| 128 | |