moving cards up and down

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

View File

@ -16,6 +16,16 @@ module Merged
redirect_to section_cards_url(@card.section.id)
end
def move
if( params[:dir] == "up")
@card.move_up
else
@card.move_down
end
@card.save
redirect_to section_cards_url(@card.section.id)
end
def update
@card.content.each do |key , value|
next if key == "id"

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? )

View File

@ -15,9 +15,9 @@
.p-4
%h3.mt-4.text-lg.font-bold Card #{index + 1}
%button.mt-4.rounded-lg.bg-yellow-500.p-4
=link_to( "Up" , "/index")
=link_to( "Up" , card_move_url(card.id , dir: :up) )
%button.mt-4.rounded-lg.bg-yellow-500.p-4
=link_to "Down" , "/index"
=link_to( "Down" , card_move_url(card.id , dir: :down) )
%button.mt-4.rounded-lg.bg-cyan-400.p-4
=link_to "New" , "/index"
%button.mt-4.rounded-lg.bg-red-400.p-4

View File

@ -12,6 +12,7 @@ Merged::Engine.routes.draw do
resources :cards do
get :select_image
get :set_image
get :move
end
end
end