2024-01-23 23:58:04 +00:00
|
|
|
package board
|
|
|
|
|
|
|
|
type shuffleDirection int
|
|
|
|
|
|
|
|
const (
|
|
|
|
forwards shuffleDirection = iota
|
|
|
|
backwards
|
|
|
|
)
|
|
|
|
|
|
|
|
// moveAndShuffle creates a new list where the element specified at the index of the current
|
|
|
|
// position is moved to the index of the target position. Elements within the range of the
|
|
|
|
// old and new positions will then shuffle forwards or backwards in the list depending on the
|
|
|
|
// direction of the move.
|
|
|
|
// This is currently used to move specified columns forwards or backwards on the Kanban board.
|
|
|
|
// When a column changes position the other columns shuffle forward or backwards as required.
|
2024-01-24 01:09:56 +00:00
|
|
|
func moveAndShuffle(input []Status, currentIndex, targetIndex int) []Status {
|
2024-01-23 23:58:04 +00:00
|
|
|
var shuffle shuffleDirection
|
|
|
|
|
2024-01-24 01:09:56 +00:00
|
|
|
if targetIndex < currentIndex {
|
2024-01-23 23:58:04 +00:00
|
|
|
// affected elements will need to shuffle backwards if the focused
|
|
|
|
// element moves towards the beginning of the list...
|
|
|
|
shuffle = backwards
|
|
|
|
} else {
|
|
|
|
// ...or else the elements need to shuffle forwards if the focused
|
|
|
|
// element moves towards the end of the list.
|
|
|
|
shuffle = forwards
|
|
|
|
}
|
|
|
|
|
|
|
|
output := make([]Status, len(input))
|
|
|
|
|
|
|
|
for ind := range input {
|
|
|
|
switch {
|
2024-01-24 01:09:56 +00:00
|
|
|
case (shuffle == backwards) && (ind < targetIndex || ind > currentIndex):
|
2024-01-23 23:58:04 +00:00
|
|
|
output[ind] = input[ind]
|
2024-01-24 01:09:56 +00:00
|
|
|
case (shuffle == backwards) && (ind == currentIndex):
|
|
|
|
output[targetIndex] = input[ind]
|
2024-01-23 23:58:04 +00:00
|
|
|
case (shuffle == backwards):
|
|
|
|
output[ind+1] = input[ind]
|
2024-01-24 01:09:56 +00:00
|
|
|
case (shuffle == forwards) && (ind < currentIndex || ind > targetIndex):
|
2024-01-23 23:58:04 +00:00
|
|
|
output[ind] = input[ind]
|
2024-01-24 01:09:56 +00:00
|
|
|
case (shuffle == forwards) && (ind == currentIndex):
|
|
|
|
output[targetIndex] = input[ind]
|
2024-01-23 23:58:04 +00:00
|
|
|
case (shuffle == forwards):
|
|
|
|
output[ind-1] = input[ind]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return output
|
|
|
|
}
|