/
TableEditor.scala
271 lines (251 loc) · 9.07 KB
/
TableEditor.scala
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package reactive
package web
package widgets
import scala.xml.NodeSeq
import net.liftweb.util.Helpers._
import net.liftweb.util.{ A => _, _ }
import reactive._
import reactive.web._
import scala.util.Try
import scala.util.Failure
/**
* Renders a table editor
* Binds the following classes:
* new - click to add a new item
* save - click to persist changes
* items - contains one element which is dynamically repeated (using Repeater). That element can contain the following classes:
* o remove - click to remove the item
* o any name specified in cols - binds the Col/Row
*/
trait TableEditor[A] extends TableView[A] {
// ABSTRACT
type RowType <: EditableRow
/**
* Instantiate a blank item
*/
def newItem: A
/**
* Apply all the actions to the data store
*/
def save(actions: Seq[Edit]): Unit
implicit def page: Page
// ACTIONS
sealed trait Edit {
def item: A
def inverse: Edit
}
case class Insert(item: A) extends Edit {
def inverse = Delete(item)
}
case class Update[C](item: A, col: EditableCol[C], v: C) extends Edit {
def inverse = Update(item, col, col.get(item))
}
case class Delete(item: A) extends Edit {
def inverse = Insert(item)
}
def messages = Messages.defaultMessages
/**
* get and set form a lens to get and set the column value in a row.
* editor turns that into a NodeSeq=>NodeSeq
*/
class EditableCol[T](val selector: String, val get: A => T, val set: T => A => A, editor: T => Editor[T])(implicit observing: Observing) extends Col {
private def rowMap[TT] = new AtomicRef(Vector.empty[(RowType, TT)])
private def getOrElseUpdate[TT](map: AtomicRef[Vector[(RowType, TT)]])(row: RowType, makeNew: => TT): TT = map.run { xs =>
xs.find(_._1 eq row) match {
case Some((_, x)) => (xs, x)
case None =>
val x = makeNew
(xs :+ (row, x), x)
}
}
val validities = rowMap[Signal[Validity[T, NodeSeq]]]
def validity(row: RowType): Signal[Validity[T, NodeSeq]] = getOrElseUpdate(validities)(row, renderer(row).value)
val editors = rowMap[Editor[T]]
val renderer = { row: RowType =>
getOrElseUpdate(editors)(row, editor(get(row.item)))
}
override def render(row: RowType)(implicit page: Page) = {
println("In render for (%s, %s)" format (row, this))
def opt(i: Int) = Some(i) filter (-1 != _)
selector #> (renderer(row) andThen
PropertyVar.toggleClass("modified")(row.modifieds(cols.indexOf(this)).distinct) andThen
onServer[KeyUp]{ // we have to go to the server because we don't know what rows will be adjacent at the moment the key is pressed until then.
case KeyUp(38, _) => //Up
opt(rows.now.indexOf(row)) filter (_ > 0) foreach { i =>
val prevId = renderer(rows.now(i - 1)).id
page.queue(js"document.getElementById($prevId).focus()")
}
case KeyUp(40, _) => //Down
opt(rows.now.indexOf(row)) filter (_ < rows.now.size - 1) foreach { i =>
val nextId = renderer(rows.now(i + 1)).id
page.queue(js"document.getElementById($nextId).focus()")
}
case _ =>
}
)
}
override def toString = "EditableCol("+selector+")"
}
/**
* Extends Row, adding a remove click handler
*/
class EditableRow(val item: A) extends Row { this: RowType =>
lazy val remove = DomEventSource.click
lazy val removes = remove.eventStream map (_ => Delete(item))
lazy val modifieds = cols.map {
case c: EditableCol[_] =>
val v = c.validity(this)
val initial = v.now
v.map(_ != initial)
case _ => Val(false)
}
override def render = super.render &
".remove" #> remove
}
val newClicks = DomEventSource.click
val inserts = newClicks.eventStream map (_ => Insert(newItem))
val actions = Var[List[Edit]](Nil) =>> { as =>
println(as.mkString("Actions: \n ", "\n ", ""))
}
val redoActions = Var[List[Edit]](Nil) =>> { rs =>
println(rs.mkString("Redo actions: \n ", "\n ", ""))
}
override lazy val rows = SeqSignal(
filterRows(initial).flatMap{ is =>
def applyAction: Seq[RowType] => Edit => Seq[RowType] = xs => {
case Insert(x) => xs :+ Row(x)
case Delete(x) =>
val (before, at) = xs span (_.item != x)
before ++ at.drop(1)
case _ => xs //TODO apply updates that don't come from UI
}
val current = actions.now.reverse.foldLeft(is)(applyAction(_)(_))
SeqSignal(actions).deltas.map(d => SeqDelta.flatten(d :: Nil)).foldLeft(current){
case (xs0, ds) =>
ds.sortBy{
case Remove(_, Insert(_)) => Int.MaxValue
case _ => 0
}.foldLeft(xs0) {
case (xs, Include(_, edit)) => applyAction(xs)(edit)
case (xs, Remove(_, edit)) => applyAction(xs)(edit.inverse)
case (xs, reactive.Update(_, old, edit)) => applyAction(applyAction(xs)(old.inverse))(edit)
}
}.hold(current)
},
{ (a: Seq[RowType], b: Seq[RowType]) => LCS.lcsdiff[RowType, RowType](a, b, _.item == _.item) }
)
rows.deltas foreach { d => println("rows deltas: "+d) }
val edits = (inserts | rows.flatMap {
_.flatMap { r =>
r.removes :: cols.collect {
case c: EditableCol[_] => c.validity(r).distinct.change.collect {
case Valid(v) => Update(r.item, c, v)
case Warning(v, msg) => Update(r.item, c, v)
}
}
}.foldLeft(EventStream.empty[Edit])(_ | _)
})
edits foreach { e => println("Edit: "+e) }
actions <<: (edits.map(Left(_)) | refreshes.map(Right(_))).foldLeft(actions.now){
case (as, Left(e)) => e :: as
case (_, Right(())) => Nil
}
redoActions <<: (edits | refreshes).map(_ => List.empty[Edit])
override def fireRefresh {
if (actions.now.isEmpty)
super.fireRefresh
else {
lazy val msg: NodeSeq = <xml:group>
<p>You have usaved changes. Are you sure you want to discard them?</p>
<button onclick={
onServer[Click]{ _ =>
messages -= msg
super.fireRefresh
}.js
}>Yes</button>
</xml:group>
messages += msg
}
}
override def render = {
super.render &
".unsaved" #> web.Cell {
actions.map(_.isEmpty).distinct.map{
case true => ClearNodes
case false => PassThru
}
} &
".new" #> newClicks &
".undo" #> {
onServer[Click]{ _ =>
actions.synchronized {
if (actions.now.nonEmpty) {
redoActions () = actions.now.head :: redoActions.now
actions () = actions.now.tail
}
}
} andThen PropertyVar("disabled").fromSignal(actions map (_.isEmpty))
} &
".redo" #> {
onServer[Click]{ _ =>
actions.synchronized {
if (redoActions.now.nonEmpty) {
actions () = redoActions.now.head :: actions.now
redoActions () = redoActions.now.tail
}
}
} andThen PropertyVar("disabled").fromSignal(redoActions map (_.isEmpty))
} &
".save" #> onServer[Click]{ _ => //TODO use nonblocking
def doSave {
Try {
println("actions.now: "+actions.now)
save(actions.now.reverse)
refreshes fire ()
} match {
case Failure(e) =>
messages += "Save failed: "+e.getMessage
case _ =>
messages += "Saved successfully!"
}
}
val (warnings, errors) = cols.collect {
case c: EditableCol[_] => c.validities.get.map(_._2.now)
}.flatten.foldLeft((List[Warning[_, NodeSeq]](), List[Invalid[NodeSeq]]())){
case ((ws, is), v: Valid[_]) => (ws, is)
case ((ws, is), w: Warning[_, _]) => (w :: ws, is)
case ((ws, is), i: Invalid[_]) => (ws, i :: is)
}
if (errors.nonEmpty)
messages += "Some values are not valid. Please fix them first."
else if (warnings.nonEmpty) {
lazy val msg: NodeSeq =
<xml:group>
<p>
Some values have warnings.
Are you sure you want to save anyway?
</p>
<p><button class="btn btn-danger" onclick={ onServer[Click]{ _ => messages -= msg; doSave }.js }>Yes</button></p>
</xml:group>
messages += msg
} else
doSave
}
}
}
trait TableEditorPlus[A] extends TableEditor[A] {
actions.distinct foreach { as => page queue js"window.isClean = ${as.isEmpty}" }
override def render = {
page queue js"""
window.onbeforeunload = function(evt) {
if(!window.isClean) {
var reply = "You have unsaved changes!"
if (!evt) evt = window.event
if (evt) evt.returnValue = reply
return reply
}
}
"""
super.render
}
}