This repository was archived by the owner on Jun 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSubscriptionView.scala
More file actions
66 lines (55 loc) · 1.85 KB
/
SubscriptionView.scala
File metadata and controls
66 lines (55 loc) · 1.85 KB
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
package tmt.views
import org.scalajs.dom.ext.Ajax
import rx._
import tmt.framework.Framework._
import tmt.framework.Helpers._
import tmt.shared.models.{Connection, ConnectionSet, RoleMappings}
import monifu.concurrent.Implicits.globalScheduler
import scalatags.JsDom.all._
class SubscriptionView(roleMappings: RoleMappings, connectionSet: ConnectionSet) {
val serverName = Var("")
val topicName = Var("")
val connection = Rx(Connection(serverName(), topicName()))
val connections = Var(connectionSet.flatConnections)
val sortedConnections = Rx(connections().toSeq.sortBy(c => (c.server, c.topic)))
def frag = div(
selectOf(topicName), label("Subscribed by"), selectOf(serverName),
button(onclick := {() => addConnection()})("Connect"),
Rx {
ul(id := "connections")(
sortedConnections().map { c =>
li(
s"${c.server} is subscribed to ${c.topic}",
button(onclick := {() => removeConnection(c)})("unsubscribe")
)
}
)
}
)
def selectOf(item: Var[String]) = {
val role = Var("")
div(
select(onchange := setValue(role))(
optionHint("select role"),
makeOptions(roleMappings.roles)
),
Rx {
select(onchange := setValue(item))(
optionHint("select-server"),
makeOptions(roleMappings.getServers(role()))
)
}
)
}
def addConnection() = {
subscribe(connection()).onSuccess {
case _ => connections() = connections() + connection()
}
}
def removeConnection(connection: Connection) = {
unsubscribe(connection)
connections() = connections() - connection
}
def subscribe(connection: Connection) = Ajax.post(s"${connection.server}/subscribe/${connection.topic}")
def unsubscribe(connection: Connection) = Ajax.post(s"${connection.server}/unsubscribe/${connection.topic}")
}