moving cards up and down

This commit is contained in:
2022-12-01 19:25:22 +02:00
parent 33ea800735
commit a4af2d6872
5 changed files with 58 additions and 2 deletions

View File

@ -35,6 +35,13 @@ module Merged
@content[key] = value
end
def move_up
@section.move_card_up(self)
end
def move_down
@section.move_card_down(self)
end
def save
section.save
end
@ -45,5 +52,9 @@ module Merged
raise "Section not found #{id}" unless card
return card
end
def set_index(index)
@index = index
end
end
end

View File

@ -35,6 +35,40 @@ module Merged
! cards.empty?
end
def move_up
@page.move_section_up(self)
end
def move_down
@section.move_section_down(self)
end
def move_card_up(card)
return if cards.length == 1
return if card.index == 0
swap( card , cards[card.index - 1])
end
def move_card_down(card)
return if cards.length == 1
return if card.index == cards.last.index
swap( card , cards[card.index + 1])
end
def swap( this_card , that_card)
# swap in the actual objects, index is cached in the objects
this_old_index = this_card.index
this_card.set_index( that_card.index )
that_card.set_index( this_old_index )
# swap in the cards cache
cards[ this_card.index ] = this_card
cards[ that_card.index ] = that_card
# swap in the yaml
card_content = content["cards"]
card_content[this_card.index] = this_card.content
card_content[that_card.index] = that_card.content
end
def update(key , value)
return if key == "id" #not updating that
if(! @content[key].nil? )