moving sections up and down

This commit is contained in:
Torsten 2022-12-01 20:14:34 +02:00
parent a4af2d6872
commit 4f6538e135
8 changed files with 51 additions and 40 deletions

View File

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

View File

@ -38,11 +38,30 @@ module Merged
@content[0]["template"]
end
def new_section
section = Hash.new
section['id'] = SecureRandom.hex(10)
@content << section
Section.new(self , 0 , section)
def move_section_up(section)
return if sections.length == 1
return if section.index == 0
swap( section , sections[section.index - 1])
end
def move_section_down(section)
return if sections.length == 1
return if section.index == sections.last.index
swap( section , sections[section.index + 1])
end
def swap( this_section , that_section)
# swap in the actual objects, index is cached in the objects
this_old_index = this_section.index
this_section.set_index( that_section.index )
that_section.set_index( this_old_index )
# swap in the sections cache
sections[ this_section.index ] = this_section
sections[ that_section.index ] = that_section
# swap in the yaml
content[this_section.index] = this_section.content
content[that_section.index] = that_section.content
end
def save
@ -55,7 +74,6 @@ module Merged
page = @@all[name]
raise "Page not found #{name}" unless page
return page
end
end

View File

@ -39,7 +39,7 @@ module Merged
@page.move_section_up(self)
end
def move_down
@section.move_section_down(self)
@page.move_section_down(self)
end
def move_card_up(card)
@ -83,6 +83,10 @@ module Merged
page.save
end
def set_index(index)
@index = index
end
def self.find(section_id)
raise "nil given" if section_id.blank?
section = @@all[section_id]

View File

@ -3,11 +3,12 @@
.flex.flex-col.bg-white
.flex.items-center.justify-center.flex-1
.max-w-xl.px-4.py-8.mx-auto.text-center
%h1.text-2xl.font-bold.tracking-tight.text-gray-900
Page #{@section.page.name}
%h1.text-3xl.font-bold.tracking-tight.text-gray-900
Page #{link_to @section.page.name, page_sections_url(@section.page.name)}
.flex.items-center.justify-center.flex-1
%h3.text-xl.font-bold.tracking-tight.text-gray-900
Cards for Section #{@section.index + 1}
Cards for
= link_to "Section #{@section.index + 1}", page_sections_url(page_id: @section.page.name)
.grid.grid-cols-4.gap-2.m-8
- @section.cards.each_with_index do |card , index|

View File

@ -1,24 +0,0 @@
%p#notice= notice
.flex.flex-col.bg-white
.flex.items-center.justify-center.flex-1
.max-w-xl.px-4.py-8.mx-auto.text-center
%h1.text-2xl.font-bold.tracking-tight.text-gray-900.sm:text-4xl
Page #{@page.name}
-@page.sections.each do |section |
.grid.grid-cols-6.gap-2.m-8{class: (section.index%2)==1 ? 'bg-cyan-50' : 'bg-red-50' }
.relative.block.border.border-gray-100
.p-4
%h3.mt-4.text-lg.font-bold Section #{section.index + 1}
%button.mt-4.rounded-lg.bg-yellow-500.p-4
=link_to( "Up" , "/index")
%button.mt-4.rounded-lg.bg-yellow-500.p-4
=link_to "Down" , "/index"
%button.mt-4.rounded-lg.bg-blue-400.p-4
=link_to "Edit" , section_path(section.id)
%button.mt-4.rounded-lg.bg-cyan-400.p-4
=link_to "New" , "/index"
%button.mt-4.rounded-lg.bg-red-400.p-4
=link_to "Delete" , "/index"
-section.content.each do |key , value|
= render "merged/sections/overview/#{key}", section: section , key: key , value: value

View File

@ -3,16 +3,17 @@
.flex.items-center.justify-center.flex-1
.max-w-xl.px-4.py-8.mx-auto.text-center
%h1.text-2xl.font-bold.tracking-tight.text-gray-900.sm:text-4xl
Page #{@page.name}
Page
= @page.name
-@page.sections.each do |section |
.grid.grid-cols-5.gap-2.m-8{class: (section.index%2)==1 ? 'bg-cyan-50' : 'bg-red-50' }
.relative.block.border.border-gray-100.p-4
%h3.mt-4.text-lg.font-bold Section #{section.index + 1}
%button.mt-4.rounded-lg.bg-yellow-500.p-4
=link_to( "Up" , "/index")
=link_to( "Up" , section_move_url(section.id , dir: :up) )
%button.mt-4.rounded-lg.bg-yellow-500.p-4
=link_to "Down" , "/index"
=link_to( "Down" , section_move_url(section.id , dir: :down) )
%button.mt-4.rounded-lg.bg-blue-400.p-4
=link_to "Edit" , section_path(section.id)
%button.mt-4.rounded-lg.bg-cyan-400.p-4

View File

@ -3,8 +3,8 @@
.flex.flex-col.bg-white
.flex.items-center.justify-center.flex-1
.max-w-xl.px-4.py-8.mx-auto.text-center
%h1.text-2xl.font-bold.tracking-tight.text-gray-900
Page #{link_to @section.page.name, page_url(@section.page.name)}
%h1.text-3xl.font-bold.tracking-tight.text-gray-900
Page #{link_to @section.page.name, page_sections_url(@section.page.name)}
.flex.items-center.justify-center.flex-1
%h3.text-xl.font-bold.tracking-tight.text-gray-900
Section #{@section.id}

View File

@ -1,7 +1,7 @@
Merged::Engine.routes.draw do
get "/styles/index" , to: "styles#index"
resources :pages , shallow: true do
resources :pages , except: [:show] , shallow: true do
resources :sections do
get :select_image
get :set_image
@ -9,6 +9,7 @@ Merged::Engine.routes.draw do
get :set_template
get :select_card_template
get :set_card_template
get :move
resources :cards do
get :select_image
get :set_image