class Admin::NodesController < Admin::AdminController
|
|
|
|
before_action :set_node, only: %i[ edit update destroy children sort toggle ]
|
|
helper_method :current_node_id, :open_node_ids
|
|
|
|
# GET /nodes
|
|
def index
|
|
# Can be nil
|
|
@node = Node.find_by(id: current_node_id) || Node.roots.first
|
|
end
|
|
|
|
|
|
# GET /nodes/1/edit
|
|
def edit
|
|
respond_to do |format|
|
|
format.turbo_stream
|
|
format.html
|
|
end
|
|
end
|
|
|
|
# GET /nodes/tree /nodes/1/tree
|
|
def tree
|
|
@node = params[:id] ? Node.find(params[:id]) : Node.roots.first
|
|
|
|
session[:current_node_id] = @node&.id
|
|
|
|
respond_to do |format|
|
|
format.turbo_stream
|
|
end
|
|
end
|
|
|
|
|
|
# POST /nodes or /nodes.json
|
|
def create
|
|
|
|
@node = Node.new(node_params)
|
|
@node.status = :status_draft
|
|
@node.template = @node.root.site? ? Node::NODE_TEMPLATES.first : Node::TILE_TEMPLATES.first
|
|
|
|
base_title = t('ui.untitled')
|
|
title = base_title
|
|
count = 0
|
|
|
|
while @node.siblings.exists?(title: {I18n.locale => title})
|
|
count += 1
|
|
title = "#{base_title} #{count}"
|
|
end
|
|
|
|
@node.title = title
|
|
|
|
respond_to do |format|
|
|
if @node.save
|
|
session[:open_node_ids] << @node.id unless session[:open_node_ids].include?(@node.id)
|
|
|
|
format.turbo_stream
|
|
format.html { redirect_to edit_admin_node_url(@node), notice: t('ui.category_created', category: t(@node.template, scope: 'nodes.templates')) }
|
|
else
|
|
format.html { render :new, status: :unprocessable_entity }
|
|
end
|
|
end
|
|
end
|
|
|
|
|
|
# GET /nodes/1/children
|
|
def children
|
|
store_node_toggle_status
|
|
|
|
respond_to do |format|
|
|
format.turbo_stream
|
|
end
|
|
end
|
|
|
|
|
|
# PATCH/PUT /nodes/1
|
|
def update
|
|
|
|
params[:node][:expires_at] ||= nil
|
|
|
|
respond_to do |format|
|
|
if @node.update(node_params)
|
|
format.turbo_stream {
|
|
flash.now[:notice] = t('ui.category_updated', category: t(@node.template, scope: 'nodes.templates'))
|
|
}
|
|
format.html { redirect_to edit_node_url(@node), notice: t('ui.category_updated', category: t(@node.category, scope: 'nodes.categories')) }
|
|
else
|
|
format.html { render :edit, status: :unprocessable_entity }
|
|
end
|
|
end
|
|
end
|
|
|
|
|
|
# DELETE /nodes/1
|
|
def destroy
|
|
if @node.destroy!
|
|
@destroyed_node = @node
|
|
@node = @node.parent
|
|
end
|
|
respond_to do |format|
|
|
format.turbo_stream {
|
|
flash.now[:notice] = t('ui.category_destroyed', category: t(@destroyed_node.category, scope: 'nodes.categories'))
|
|
}
|
|
format.html { redirect_to nodes_url, notice: t(:'nodes.destroyed') }
|
|
end
|
|
end
|
|
|
|
|
|
# PATCH /nodes/sort?id=
|
|
# {"id"=>"2", "old_index"=>1, "new_index"=>4, "node"=>{"id"=>"2"}}
|
|
def sort
|
|
@node.insert_at(params[:new_index].to_i + 1)
|
|
respond_to do |format|
|
|
format.turbo_stream
|
|
end
|
|
end
|
|
|
|
|
|
|
|
# Expand or collapse node in drawer
|
|
# PATCH /nodes/toggle?id=1&expanded=true/false
|
|
def toggle
|
|
store_node_toggle_status
|
|
head :ok
|
|
end
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
# Use callbacks to share common setup or constraints between actions.
|
|
def set_node
|
|
@node = Node.find(params[:id])
|
|
end
|
|
|
|
|
|
# Only allow a list of trusted parameters through.
|
|
def node_params
|
|
params.require(:node).permit(
|
|
:parent_id,
|
|
:title_da, :title_en, :title_de,
|
|
:page_title_da, :page_title_en, :page_title_de,
|
|
:page_description_da, :page_description_en, :page_description_de,
|
|
:slug_da, :slug_en, :slug_de,
|
|
:template,
|
|
:href_da, :href_en, :href_de,
|
|
:status,
|
|
:page_description,
|
|
:parent_id,
|
|
:position,
|
|
:published_at,
|
|
:expires_at,
|
|
:is_allowlist,
|
|
excluded_locales: [],
|
|
settings: [],
|
|
attachments_attributes: [:id, :asset_id, :body_da, :body_en, :body_de, :fg_color, :bg_color, :alignment, :template, :position, :_destroy]
|
|
)
|
|
end
|
|
|
|
|
|
def current_node_id
|
|
session[:current_node_id]
|
|
end
|
|
|
|
|
|
def open_node_ids
|
|
Array(session[:open_node_ids])
|
|
end
|
|
|
|
|
|
def ensure_open_node_ids
|
|
session[:open_node_ids] ||= []
|
|
end
|
|
|
|
|
|
def store_node_toggle_status
|
|
ensure_open_node_ids
|
|
|
|
if params[:expanded] == false
|
|
session[:open_node_ids].delete(@node.id)
|
|
else
|
|
session[:open_node_ids] << @node.id unless session[:open_node_ids].include?(@node.id)
|
|
end
|
|
end
|
|
|
|
|
|
|
|
end
|