scoop:scoop.ui.component:0.1.0
src/tree/drop_model.scoop
Selected production source · src/tree/drop_model.scoop
1
package scoop.ui.component3
import scoop.collections.HashMap4
import scoop.ui.ViewKey6
internal fun <T> treeDrop(7
data: TreeData<T>,8
source: ViewKey,9
target: ViewKey,10
position: TreeDropPosition,11
sorted: Boolean = false12
): TreeDrop<T>? {13
val from = data.indexOfKey(source) ?: return None14
val to = data.indexOfKey(target) ?: return None15
val origin = data.records[from]16
val destination = data.records[to]17
if (!origin.node.enabled ||18
!origin.node.draggable ||19
!destination.node.enabled ||20
(to >= from && to < origin.end)21
)22
return None23
if (sorted && position != TreeDropPosition.Inside) return None24
val parent = if (position == TreeDropPosition.Inside) Some(to) else destination.parent25
val siblings = when (parent) {26
case Some(val index) -> {27
val row = data.records[index]28
if (!row.node.enabled) return None29
when (row.node.children) {30
case Leaf -> return None;31
else -> {}32
}33
row.children34
}36
case None -> data.rootIndices37
}38
val insertion = when (position) {39
case Before -> destination.position40
case After -> destination.position + 1L41
case Inside -> siblings.size42
}43
if (origin.parent == parent) {44
val adjusted = insertion - if (origin.position < insertion) 1L else 0L45
if (adjusted == origin.position) return None46
}47
var before: ViewKey? = None48
var next = insertion49
while (next < siblings.size) {50
val node = data.records[siblings[next]].node51
if (node.key != source) {52
before = Some(node.key);53
break54
}55
next += 1L56
}57
return Some(58
TreeDrop(59
origin.node, destination.node, position,60
when (parent) {61
case Some(val index) -> Some(data.records[index].node.key);62
case None -> None63
},64
before65
)66
)67
}69
/** Moves one loaded subtree. Unknown destination children require application-specific IO. */70
public fun <T> TreeData<T>.moved(71
source: ViewKey,72
target: ViewKey,73
position: TreeDropPosition74
): TreeData<T>? {75
val move = treeDrop(this, source, target, position) ?: return None76
when (move.parent) {77
case Some(val key) ->78
when (node(key)!!.children) {79
case Deferred -> return None80
else -> {}81
}82
case None -> {}83
}84
val replacements = HashMap<Long, TreeNode<T>>()85
var index = count86
while (index > 0L) {87
index -= 1L88
val record = records[index]89
if (record.node.key == source) continue90
val children = ArrayList<TreeNode<T>>()91
var changed = false92
for (at in record.children) {93
val child = records[at].node94
if (child.key == source) {95
changed = true;96
continue97
}98
if (move.parent == Some(record.node.key) && move.before == Some(child.key)) {99
children.add(move.source)100
changed = true101
}102
when (replacements[at]) {103
case Some(val next) -> {104
children.add(next);105
changed = true106
}108
case None -> children.add(child)109
}110
}111
if (move.parent == Some(record.node.key) && move.before == None) {112
children.add(move.source)113
changed = true114
}115
if (changed)116
replacements[index] = record.node.withChildren(TreeChildren.Known(children.toArray()))117
}118
val nextRoots = ArrayList<TreeNode<T>>()119
for (at in rootIndices) {120
val root = records[at].node121
if (root.key == source) continue122
if (move.parent == None && move.before == Some(root.key)) nextRoots.add(move.source)123
nextRoots.add(replacements[at] ?: root)124
}125
if (move.parent == None && move.before == None) nextRoots.add(move.source)126
return Some(TreeData(nextRoots.toArray()))127
}