From 11cfd9e3103db1eff6f4d075cc34bee4f3f01946 Mon Sep 17 00:00:00 2001 From: Arnaud Levy <contact@arnaudlevy.com> Date: Tue, 21 Mar 2023 22:38:50 +0100 Subject: [PATCH] prototype --- .../stylesheets/admin/commons/blocks.sass | 6 ++ .../blocks/headings_controller.rb | 13 ---- .../admin/communication/blocks_controller.rb | 20 +++++- app/models/communication/block.rb | 2 + app/models/communication/block/heading.rb | 14 +++- .../admin/application/a11y/_widget.html.erb | 9 ++- .../communication/blocks/_block.html.erb | 16 +++++ .../blocks/_block_static.html.erb | 11 +++ .../admin/communication/blocks/_list.html.erb | 70 +++++-------------- .../communication/blocks/_static.html.erb | 10 +++ .../admin/communication/blocks/edit.html.erb | 8 ++- .../blocks/headings/_heading.html.erb | 12 ++++ .../blocks/headings/_heading_static.html.erb | 8 +++ .../communication/websites/_sidebar.html.erb | 4 +- .../websites/pages/show.html.erb | 34 ++++++--- .../layouts/themes/pure/_commands.html.erb | 2 +- ...mmunication_block_heading_default_level.rb | 5 ++ db/schema.rb | 4 +- 18 files changed, 158 insertions(+), 90 deletions(-) create mode 100644 app/views/admin/communication/blocks/_block.html.erb create mode 100644 app/views/admin/communication/blocks/_block_static.html.erb create mode 100644 app/views/admin/communication/blocks/headings/_heading.html.erb create mode 100644 app/views/admin/communication/blocks/headings/_heading_static.html.erb create mode 100644 db/migrate/20230321213542_change_communication_block_heading_default_level.rb diff --git a/app/assets/stylesheets/admin/commons/blocks.sass b/app/assets/stylesheets/admin/commons/blocks.sass index 462e9560f..deaacc5e5 100644 --- a/app/assets/stylesheets/admin/commons/blocks.sass +++ b/app/assets/stylesheets/admin/commons/blocks.sass @@ -1,7 +1,13 @@ .blocks &__list + &__element + &:hover + .blocks__list__handle + opacity: 1 &__handle + opacity: 0 width: 20px + transition: opacity 0.25s ease &__image width: 170px img diff --git a/app/controllers/admin/communication/blocks/headings_controller.rb b/app/controllers/admin/communication/blocks/headings_controller.rb index ee17fb8b3..5923bfb60 100644 --- a/app/controllers/admin/communication/blocks/headings_controller.rb +++ b/app/controllers/admin/communication/blocks/headings_controller.rb @@ -3,24 +3,11 @@ class Admin::Communication::Blocks::HeadingsController < Admin::Communication::B through: :current_university, through_association: :communication_block_headings - def reorder - ids = params[:ids] || [] - ids.each.with_index do |id, index| - @heading = current_university.communication_block_headings.find(id) - @heading.update position: index + 1 - end - @heading.about.sync_with_git - end - def new @heading.about = Polymorphic.find params, :about breadcrumb end - def show - breadcrumb - end - def edit breadcrumb end diff --git a/app/controllers/admin/communication/blocks_controller.rb b/app/controllers/admin/communication/blocks_controller.rb index cc346f1ff..511a159e2 100644 --- a/app/controllers/admin/communication/blocks_controller.rb +++ b/app/controllers/admin/communication/blocks_controller.rb @@ -5,9 +5,23 @@ class Admin::Communication::BlocksController < Admin::Communication::Application def reorder ids = params[:ids] || [] - ids.each.with_index do |id, index| - @block = current_university.communication_blocks.find(id) - @block.update position: index + 1 + ids.each.with_index do |identifier, index| + parts = identifier.split('_') + kind = parts.first + id = parts.last + if kind == 'block' + @block = current_university.communication_blocks.find(id) + @block.heading = @heading + @block.position = index + 1 + @block.save + @previous = nil + elsif kind == 'heading' + @heading = current_university.communication_block_headings.find(id) + @heading.position = index + 1 + @heading.parent = @previous + @heading.save + @previous = @heading + end end @block.about.sync_with_git end diff --git a/app/models/communication/block.rb b/app/models/communication/block.rb index ed1cd6658..04ab7f2db 100644 --- a/app/models/communication/block.rb +++ b/app/models/communication/block.rb @@ -36,6 +36,7 @@ class Communication::Block < ApplicationRecord FILE_MAX_SIZE = 100.megabytes belongs_to :about, polymorphic: true + belongs_to :heading, optional: true # Used to purge images when unattaching them # template_blobs would be a better name, because there are files @@ -70,6 +71,7 @@ class Communication::Block < ApplicationRecord } scope :published, -> { where(published: true) } + scope :with_no_heading, -> { where(heading: nil) } before_save :attach_template_blobs diff --git a/app/models/communication/block/heading.rb b/app/models/communication/block/heading.rb index b3ce66c1a..f8537a44c 100644 --- a/app/models/communication/block/heading.rb +++ b/app/models/communication/block/heading.rb @@ -35,11 +35,23 @@ class Communication::Block::Heading < ApplicationRecord has_many :children, class_name: 'Communication::Block::Heading', foreign_key: :parent_id + has_many :blocks + + DEFAULT_LEVEL = 2 - scope :root, -> { where(level: 1) } + scope :root, -> { where(level: DEFAULT_LEVEL) } default_scope { order(:position) } + before_validation :compute_level + def to_s "#{title}" end + + protected + + def compute_level + self.level = parent ? parent.level + 1 + : DEFAULT_LEVEL + end end diff --git a/app/views/admin/application/a11y/_widget.html.erb b/app/views/admin/application/a11y/_widget.html.erb index db26884e8..b023bd180 100644 --- a/app/views/admin/application/a11y/_widget.html.erb +++ b/app/views/admin/application/a11y/_widget.html.erb @@ -1,14 +1,17 @@ <% +horizontal ||= false color = about.accessible? ? 'text-success' : 'text-danger' action = "<i class=\"#{ Icon::A11Y } fa-2x float-end #{ color}\"></i>" %> <%= osuny_panel t('accessibility.label'), action: action do %> + <% if horizontal %> + <div class="row"><div class="offset-lg-4 col-lg-8"> + <% end %> <% if about.accessibility_errors.any? %> <%= osuny_label t('accessibility.errors', count: about.accessibility_errors.count), classes: 'text-danger' %> <ol class="list-unstyled"> <% about.accessibility_errors.each do |key| %> <li> - <hr> <%= osuny_label t("#{key}.title"), classes: 'text-danger' %> <p><%= t "#{key}.text_html" %></p> </li> @@ -22,7 +25,6 @@ action = "<i class=\"#{ Icon::A11Y } fa-2x float-end #{ color}\"></i>" <ul class="list-unstyled"> <% about.accessibility_warnings.each do |key| %> <li> - <hr> <%= osuny_label t("#{key}.title") %> <p> <%= t "#{key}.text_html" %> @@ -31,4 +33,7 @@ action = "<i class=\"#{ Icon::A11Y } fa-2x float-end #{ color}\"></i>" <% end %> </ul> <% end %> + <% if horizontal %> + </div></div> + <% end %> <% end %> \ No newline at end of file diff --git a/app/views/admin/communication/blocks/_block.html.erb b/app/views/admin/communication/blocks/_block.html.erb new file mode 100644 index 000000000..bd15d7409 --- /dev/null +++ b/app/views/admin/communication/blocks/_block.html.erb @@ -0,0 +1,16 @@ +<div class="row" data-id="block_<%= block.id %>"> + <div class="offset-lg-4 col-lg-8"> + <article class="blocks__list__element mt-4 <%= 'draft' unless block.published? %>"> + <div class="float-end"> + <%= duplicate_link block %> + <%= edit_link block %> + </div> + <span class="small text-muted"><%= block.template_kind_i18n %></span> + <span class="blocks__list__handle"><i class="<%= Icon::SORT %> handle"></i></span> + <div class="blocks__list__name"> + <%= block.to_s.truncate(50) %><br> + <%= render 'admin/application/a11y/status', about: block unless block.accessible? %> + </div> + </article> + </div> +</div> \ No newline at end of file diff --git a/app/views/admin/communication/blocks/_block_static.html.erb b/app/views/admin/communication/blocks/_block_static.html.erb new file mode 100644 index 000000000..e0deae985 --- /dev/null +++ b/app/views/admin/communication/blocks/_block_static.html.erb @@ -0,0 +1,11 @@ +<% +template_path = "admin/communication/blocks/templates/#{@block.template_kind}/static" +should_render_data = @block.data && @block.data.present? +%> + - kind: block + template: <%= @block.template_kind %> + position: <%= @block.position %> + data: +<%= render template_path, + block: @block, + about: @block.about if should_render_data %> diff --git a/app/views/admin/communication/blocks/_list.html.erb b/app/views/admin/communication/blocks/_list.html.erb index 78a279931..8be64161d 100644 --- a/app/views/admin/communication/blocks/_list.html.erb +++ b/app/views/admin/communication/blocks/_list.html.erb @@ -1,56 +1,22 @@ -<% -action = '' -action += link_to t('admin.communication.blocks.add'), - new_admin_communication_block_path(about_id: about.id, about_type: about.class.name), - class: button_classes if can? :create, Communication::Block -action += link_to t('admin.communication.blocks.headings.add'), - new_admin_communication_heading_path(about_id: about.id, about_type: about.class.name), - class: button_classes('ms-2') if can? :create, Communication::Block::Heading -%> -<%= osuny_panel Communication::Block.model_name.human(count: 2), action: action do %> - <% if about.headings.any? %> +<%= osuny_panel Communication::Block.model_name.human(count: 2) do %> + <div class="blocks" data-sortable data-sort-url="<%= reorder_admin_communication_blocks_path %>"> + <% about.blocks.with_no_heading.ordered.each do |block| %> + <%= render 'admin/communication/blocks/block', block: block %> + <% end %> <% about.headings.root.each do |heading| %> - <%= link_to heading, edit_admin_communication_heading_path(heading) %> + <%= render 'admin/communication/blocks/headings/heading', heading: heading %> <% end %> - <% end %> - <% if about.blocks.any? %> - <div class="table-responsive"> - <table class="<%= table_classes %>"> - <thead> - <tr> - <th></th> - <th><%= Communication::Block.human_attribute_name :title %></th> - <th><%= Communication::Block.human_attribute_name :template %></th> - <th></th> - </tr> - </thead> - <tbody data-sortable data-sort-url="<%= reorder_admin_communication_blocks_path %>"> - <% about.blocks.ordered.each do |block| %> - <tr data-id="<%= block.id %>" class="<%= 'draft' unless block.published? %>"> - <% if can? :reorder, Communication::Block %> - <td class="ps-0 blocks__list__handle"><i class="<%= Icon::DRAG %> handle"></i></td> - <% end %> - <td class="blocks__list__name"> - <%= block.to_s.truncate(50) %><br> - <span class="small"><%= block.template_kind_i18n %></span> - <%= render 'admin/application/a11y/status', about: block %> - </td> - <td class="blocks__list__image"> - <%= image_tag "communication/blocks/templates/#{block.template_kind}.jpg", alt: '' %> - </td> - <td class="blocks__list__buttons"> - <div class="btn-group"> - <%= duplicate_link block %> - <%= edit_link block %> - </div> - </td> - </tr> - <% end %> - </tbody> - </table> - <% if about.blocks.count > 9 %> - <%= raw action %> - <% end %> + </div> + <div class="row mt-5"> + <div class="col-lg-4"> + <%= link_to t('admin.communication.blocks.headings.add'), + new_admin_communication_heading_path(about_id: about.id, about_type: about.class.name), + class: 'p-5 d-block bg-light text-center h4' if can? :create, Communication::Block::Heading%> + </div> + <div class="col-lg-8"> + <%= link_to t('admin.communication.blocks.add'), + new_admin_communication_block_path(about_id: about.id, about_type: about.class.name), + class: 'p-5 d-block bg-light text-center h4' if can? :create, Communication::Block%> </div> - <% end %> + </div> <% end %> diff --git a/app/views/admin/communication/blocks/_static.html.erb b/app/views/admin/communication/blocks/_static.html.erb index 73a53873c..9280c0fb6 100644 --- a/app/views/admin/communication/blocks/_static.html.erb +++ b/app/views/admin/communication/blocks/_static.html.erb @@ -1,3 +1,13 @@ +<% +@university = about.university +%> +content: +<% about.blocks.with_no_heading.published.ordered.each do |block| @block = block %> +<%= render 'admin/communication/blocks/block_static' %> +<% end %> +<% about.headings.root.each do |heading| @heading = heading %> +<%= render 'admin/communication/blocks/headings/heading_static' %> +<% end %> <% if about.blocks.any? %> blocks: <% about.blocks.published.ordered.each do |block| diff --git a/app/views/admin/communication/blocks/edit.html.erb b/app/views/admin/communication/blocks/edit.html.erb index ef3bdd551..6af97a74b 100644 --- a/app/views/admin/communication/blocks/edit.html.erb +++ b/app/views/admin/communication/blocks/edit.html.erb @@ -13,9 +13,11 @@ <div class="row"> <div class="col-xxl-9 col-lg-8"> <div class="row pure__row--small"> - <div class="col-lg-6"> - <%= f.input :title %> - </div> + <% if @block.title.present? %> + <div class="col-lg-6"> + <%= f.input :title %> + </div> + <% end %> <div class="col-lg-6"> <label class="form-label d-none d-lg-block"> </label> <%= f.input :published %> diff --git a/app/views/admin/communication/blocks/headings/_heading.html.erb b/app/views/admin/communication/blocks/headings/_heading.html.erb new file mode 100644 index 000000000..9fa692da9 --- /dev/null +++ b/app/views/admin/communication/blocks/headings/_heading.html.erb @@ -0,0 +1,12 @@ +<div data-id="heading_<%= heading.id %>" class="blocks__list__element mt-5"> + <div style="padding-left: <%= (heading.level - 1) * 48 %>px"> + <%= link_to heading, edit_admin_communication_heading_path(heading), class: 'h4' %> + <span class="ps-0 blocks__list__handle"><i class="<%= Icon::SORT %> handle"></i></span> + </div> +</div> +<% heading.blocks.each do |block| %> + <%= render 'admin/communication/blocks/block', block: block %> +<% end %> +<% heading.children.each do |child| %> + <%= render 'admin/communication/blocks/headings/heading', heading: child %> +<% end %> diff --git a/app/views/admin/communication/blocks/headings/_heading_static.html.erb b/app/views/admin/communication/blocks/headings/_heading_static.html.erb new file mode 100644 index 000000000..676e10313 --- /dev/null +++ b/app/views/admin/communication/blocks/headings/_heading_static.html.erb @@ -0,0 +1,8 @@ + - kind: heading + title: >- + <%= prepare_text_for_static @heading.title, 3 %> + position: <%= @heading.position %> + level: <%= @heading.level %> +<% @heading.blocks.published.ordered.each do |block| @block = block %> +<%= render 'admin/communication/blocks/block_static' %> +<% end %> diff --git a/app/views/admin/communication/websites/_sidebar.html.erb b/app/views/admin/communication/websites/_sidebar.html.erb index e2205ef3e..690bb32c8 100644 --- a/app/views/admin/communication/websites/_sidebar.html.erb +++ b/app/views/admin/communication/websites/_sidebar.html.erb @@ -1,5 +1,5 @@ <div class="row mt-2 website__sidebar"> - <div class="col-lg-3 col-xl-2"> + <div class="col-lg-3"> <ul class="list-unstyled" role="tablist"> <% navigation = [ @@ -62,7 +62,7 @@ </select> <% end %> </div> - <div class="col-lg-9 col-xl-10"> + <div class="col-lg-9"> <%= yield %> </div> diff --git a/app/views/admin/communication/websites/pages/show.html.erb b/app/views/admin/communication/websites/pages/show.html.erb index 4f5b7bb22..92226d322 100644 --- a/app/views/admin/communication/websites/pages/show.html.erb +++ b/app/views/admin/communication/websites/pages/show.html.erb @@ -1,25 +1,37 @@ <% content_for :title, @page %> <%= render 'admin/communication/websites/sidebar' do %> - <div class="row"> - <div class="col-md-8"> - <%= render 'admin/application/summary/show', about: @page %> - <%= render 'admin/communication/blocks/list', about: @page %> - <%= render 'admin/application/dependencies', - git_dependencies: @page.git_dependencies(@page.website), - active_storage_blobs: @page.active_storage_blobs %> + <div class="col-lg-7"> + <%= osuny_panel Communication::Website::Page.human_attribute_name(:title) do %> + <p class="lead"><%= @page.title %></p> + <% end %> </div> - <div class="col-md-4"> - <%= render 'admin/application/a11y/widget', about: @page %> - <%= render 'admin/application/i18n/widget', about: @page %> - <%= render 'admin/communication/websites/pages/show/metadata' %> + <div class="offset-lg-1 col-lg-4"> <%= render 'admin/application/featured_image/show', about: @page %> + </div> + </div> + <hr class="mb-5"> + <%= render 'admin/application/a11y/widget', about: @page, horizontal: true %> + <hr class="mb-5"> + <div class="row"> + <div class="col-lg-4"> + <%= render 'admin/communication/websites/pages/show/metadata' %> + <%= render 'admin/application/i18n/widget', about: @page %> + </div> + <div class="col-lg-8"> + <%= render 'admin/application/summary/show', about: @page %> <%= render 'admin/application/meta_description/show', about: @page %> </div> </div> + <hr class="mb-5"> + <%= render 'admin/communication/blocks/list', about: @page %> <% end %> +<%= render 'admin/application/dependencies', + git_dependencies: @page.git_dependencies(@page.website), + active_storage_blobs: @page.active_storage_blobs %> + <% content_for :action_bar_left do %> <%= destroy_link @page if @page.is_regular_page? %> <%= static_link static_admin_communication_website_page_path(@page) %> diff --git a/app/views/admin/layouts/themes/pure/_commands.html.erb b/app/views/admin/layouts/themes/pure/_commands.html.erb index 452c75a6a..395c23411 100644 --- a/app/views/admin/layouts/themes/pure/_commands.html.erb +++ b/app/views/admin/layouts/themes/pure/_commands.html.erb @@ -1,6 +1,6 @@ <% if content_for?(:action_bar_left) or content_for?(:action_bar_right) %> <div class="commands"> - <div class="container-fluid py-3 d-flex"> + <div class="p-3 d-flex"> <div> <%= yield :action_bar_left %> </div> diff --git a/db/migrate/20230321213542_change_communication_block_heading_default_level.rb b/db/migrate/20230321213542_change_communication_block_heading_default_level.rb new file mode 100644 index 000000000..4da9e75f4 --- /dev/null +++ b/db/migrate/20230321213542_change_communication_block_heading_default_level.rb @@ -0,0 +1,5 @@ +class ChangeCommunicationBlockHeadingDefaultLevel < ActiveRecord::Migration[7.0] + def change + change_column :communication_block_headings, :level, :integer, default: 2 + end +end diff --git a/db/schema.rb b/db/schema.rb index aac33c6b2..8343f3524 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2023_03_21_082433) do +ActiveRecord::Schema[7.0].define(version: 2023_03_21_213542) do # These are extensions that must be enabled in order to support this database enable_extension "pgcrypto" enable_extension "plpgsql" @@ -83,7 +83,7 @@ ActiveRecord::Schema[7.0].define(version: 2023_03_21_082433) do t.string "about_type", null: false t.uuid "about_id", null: false t.string "title" - t.integer "level", default: 1 + t.integer "level", default: 2 t.uuid "parent_id" t.integer "position" t.string "slug" -- GitLab