diff --git a/app/assets/javascripts/admin/pages/communication/website/pages.js b/app/assets/javascripts/admin/pages/communication/website/pages.js new file mode 100644 index 0000000000000000000000000000000000000000..f58361494a6a2093037148dbdcdd98a479039113 --- /dev/null +++ b/app/assets/javascripts/admin/pages/communication/website/pages.js @@ -0,0 +1,43 @@ +/* global $ */ +window.osuny = window.osuny || {}; +window.osuny.pagesTree = { + + init: function () { + 'use strict'; + $('.js-tree-element').click(this.branchClicked.bind(this)); + }, + + branchClicked: function (e) { + 'use strict'; + var $target = $(e.currentTarget), + $branch = $target.parents('.js-treeview-element'); + + e.preventDefault(); + e.stopPropagation(); + + $branch.toggleClass('opened'); + + if ($branch.hasClass('opened') && !$branch.hasClass('loaded')) { + this.loadBranch($branch, $target.attr('href')); + } + }, + + loadBranch: function ($branch, url) { + 'use strict'; + // TODO + console.log('ok'); + $branch.addClass('loaded'); + }, + + invoke: function () { + 'use strict'; + return { + init: this.init.bind(this) + }; + } +}.invoke(); + +document.addEventListener('DOMContentLoaded', function () { + 'use strict'; + window.osuny.pagesTree.init(); +}); diff --git a/app/assets/stylesheets/admin/treeview.sass b/app/assets/stylesheets/admin/treeview.sass new file mode 100644 index 0000000000000000000000000000000000000000..8d0c535a6a3733ba8dc1b1255fad248bcc3a2fc0 --- /dev/null +++ b/app/assets/stylesheets/admin/treeview.sass @@ -0,0 +1,17 @@ +.treeview + .branch + .close_btn + display: none + .open_btn + display: inline + .children + display: none + margin-left: 15px + + &.opened + .close_btn + display: inline + .open_btn + display: none + .children + display: block diff --git a/app/controllers/admin/communication/website/pages_controller.rb b/app/controllers/admin/communication/website/pages_controller.rb index dd827a9f8c624a546da8ef2cded522a349814196..29936faf5dc8b2fcc9ee8e827c0e23dd53bd312c 100644 --- a/app/controllers/admin/communication/website/pages_controller.rb +++ b/app/controllers/admin/communication/website/pages_controller.rb @@ -2,10 +2,16 @@ class Admin::Communication::Website::PagesController < Admin::Communication::Web load_and_authorize_resource class: Communication::Website::Page def index - @pages = @website.pages.ordered.page params[:page] + @pages = @website.pages.root.ordered breadcrumb end + def children + return unless request.xhr? + page = @website.pages.find(params[:id]) + @children = page.children.ordered + end + def show breadcrumb end diff --git a/app/models/communication/website/page.rb b/app/models/communication/website/page.rb index a47badb528532c8474c08b38aac3a00d229682cb..ce2fd2b36453e8b9a1f17b39645e8a18e6311b6f 100644 --- a/app/models/communication/website/page.rb +++ b/app/models/communication/website/page.rb @@ -34,7 +34,7 @@ class Communication::Website::Page < ApplicationRecord include WithSlug - + belongs_to :university belongs_to :website, foreign_key: :communication_website_id @@ -54,8 +54,9 @@ class Communication::Website::Page < ApplicationRecord before_save :make_path after_save :publish_to_github - scope :ordered, -> { order(:path) } + scope :ordered, -> { order(:position) } scope :recent, -> { order(updated_at: :desc).limit(5) } + scope :root, -> { where(parent_id: nil) } def content @content ||= github.read_file_at "_pages/#{id}.html" @@ -65,6 +66,10 @@ class Communication::Website::Page < ApplicationRecord frontmatter.content end + def has_children? + children.any? + end + def to_s "#{ title }" end diff --git a/app/views/admin/communication/website/pages/children.js.erb b/app/views/admin/communication/website/pages/children.js.erb new file mode 100644 index 0000000000000000000000000000000000000000..e641cd37b736b2d3bafb033d93873ccb218a648f --- /dev/null +++ b/app/views/admin/communication/website/pages/children.js.erb @@ -0,0 +1 @@ +$('.js-treeview-element-<%= @page.id %>').addClass('opened'); diff --git a/app/views/admin/communication/website/pages/index.html.erb b/app/views/admin/communication/website/pages/index.html.erb index 956d2c7c1dbb08b389e86f18aa3346fb56762349..0f12f693f7f8f1a193c5462c975736d4c299cfbc 100644 --- a/app/views/admin/communication/website/pages/index.html.erb +++ b/app/views/admin/communication/website/pages/index.html.erb @@ -1,7 +1,26 @@ <% content_for :title, Communication::Website::Page.model_name.human(count: 2) %> -<%= render 'admin/communication/website/pages/list', pages: @pages %> -<%= paginate @pages, theme: 'bootstrap-5' %> +<ul class="list-unstyled treeview js-treeview"> + <% @pages.each do |page| %> + <li class="branch <%= 'with-children' if page.has_children? %> js-treeview-element js-treeview-element-<%= page.id %>"> + <div class="d-flex align-items-center"> + <%= link_to children_admin_communication_website_page_path(website_id: page.website.id, id: page.id), + class: 'js-tree-element' do %> + <span class="open_btn">+</span> + <span class="close_btn">-</span> + <%= page %> + <% end %> + <%= link_to 'Voir', admin_communication_website_page_path(website_id: page.website.id, id: page.id), class: button_classes %> + </div> + <ul class="list-unstyled children"> + <li>loading...</li> + </ul> + + </li> + <% end %> +</ul> +<%#= render 'admin/communication/website/pages/list', pages: @pages %> +<%#= paginate @pages, theme: 'bootstrap-5' %> <% content_for :action_bar_right do %> <%= create_link Communication::Website::Page %> diff --git a/config/routes/admin/communication.rb b/config/routes/admin/communication.rb index edff8b75eb1570af6416b7d74b990163c3e93617..d32660f95fbc8981a457086447f38b25d3afe7f4 100644 --- a/config/routes/admin/communication.rb +++ b/config/routes/admin/communication.rb @@ -4,7 +4,11 @@ namespace :communication do get :import post :import end - resources :pages, controller: 'website/pages' + resources :pages, controller: 'website/pages' do + member do + get :children + end + end resources :posts, controller: 'website/posts' end end