Skip to content

Commit

Permalink
projects#show directory browser using turbo_frames.
Browse files Browse the repository at this point in the history
  • Loading branch information
euler-room committed Nov 27, 2024
1 parent 4b67711 commit 1ec386d
Show file tree
Hide file tree
Showing 14 changed files with 335 additions and 95 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
module Pathable
module DirectoryUtilsConcern
# Constants for sorting
ASCENDING = true
DESCENDING = false
# Constants for grouping
DIRECTORIES = true
FILES = false
DEFAULT_SORTING_PARAMS = { col: 'name', direction: ASCENDING, grouped?: true }

extend ActiveSupport::Concern

def normalized_path(path)
Expand Down Expand Up @@ -33,10 +41,41 @@ def validate_path!
end
end

def set_sorting_params(parameters)
@sorting_params = {
col: parameters[:col],
direction: parameters[:direction],
grouped?: parameters[:grouped?]
}
end

def set_files
@files = @path.ls
@files = sort_by_column(@files, @sorting_params[:col], @sorting_params[:direction])
@files = group_by_type(@files) if @sorting_params[:grouped?]
end

def group_by_type(files)
directories = files.select { |file| file[:directory] } + files.select { |file| !file[:directory] }
end

def sort_by_column(files, column, direction)
col = column.to_sym
sorted_files = files.sort_by do |file|
if col == :size
file[col].to_i
else
file[col].to_s.downcase
end
end
return sorted_files if direction == ASCENDING
return sorted_files.reverse
end

def posix_file?
@path.is_a?(PosixFile)
end

def resolved_path
raise NoMethodError, "Must implement resolved_path in #{self.class.to_s} to use Pathable concern"
end
Expand Down
2 changes: 1 addition & 1 deletion apps/dashboard/app/controllers/files_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# The controller for all the files pages /dashboard/files
class FilesController < ApplicationController
include ActionController::Live
include Pathable
include DirectoryUtilsConcern

before_action :strip_sendfile_headers, only: [:fs]

Expand Down
60 changes: 45 additions & 15 deletions apps/dashboard/app/controllers/projects_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# The controller for project pages /dashboard/projects.
class ProjectsController < ApplicationController
include Pathable
include DirectoryUtilsConcern

# GET /projects/:id
def show
Expand All @@ -11,8 +11,9 @@ def show

parse_path
validate_path!
@files = @path.ls

set_sorting_params(show_project_params[:sorting_params] || default_sorting_params)
set_files

if @project.nil?
respond_to do |format|
message = I18n.t('dashboard.jobs_project_not_found', project_id: project_id)
Expand All @@ -34,20 +35,41 @@ def show
end
end
end

# GET /projects/:project_id/files/*filepath
def files
@project = Project.find(files_params[:project_id])
parse_path(files_params[:filepath])
def directory
@project = Project.find(directory_params[:project_id])
parse_path("#{directory_params[:dir_path]}")
validate_path!
Rails.logger.debug("\n\n\n==============================================================")
Rails.logger.debug("ProjectsController#files: request: #{request.methods.sort}")
Rails.logger.debug("==============================================================\n\n\n")
@files = @path.ls

render(partial: 'projects/directory', locals: { project_id: @project.id, path: @path, files: @files })
set_sorting_params(directory_params[:sorting_params] || DEFAULT_SORTING_PARAMS)
set_files
render( partial: 'projects/directory',
locals: { project: @project,
path: @path,
files: @files,
sorting_params: @sorting_params
}
)
end

def file
@project = Project.find(file_params[:project_id])
parse_path(file_params[:path])
validate_path!
@file = File.open(@path.to_s, "r") do |file|
file.read
end

render( partial: 'projects/file',
locals: {
project: @project,
path: @path,
file: @file,
sorting_params: file_params[:sorting_params]
}
)
end

# GET /projects
def index
@projects = Project.all
Expand Down Expand Up @@ -192,6 +214,10 @@ def resolved_fs
'fs'
end

def default_sorting_params
DEFAULT_SORTING_PARAMS
end

def templates
Project.templates.map do |project|
label = project.title
Expand All @@ -209,8 +235,12 @@ def project_params
.permit(:name, :directory, :description, :icon, :id, :template)
end

def files_params
params.permit(:project_id, :filepath)
def directory_params
params.permit(:project_id, :format, :dir_path, sorting_params: [:col, :direction, :grouped?])
end

def file_params
params.permit(:project_id, :format, :path, :sorting_params)
end

def show_project_params
Expand Down
89 changes: 89 additions & 0 deletions apps/dashboard/app/helpers/projects_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,93 @@ def button_category(status)
status
end
end

def files_button
link_to(
".../projects#{@path.to_s.split('projects')[1]}",
files_path(fs: 'fs', filepath: @path),
target: '_top',
class: 'link-light'
).html_safe
end

# <a id="new-dir-btn" class="btn btn-outline-dark" href="<%= files_path(fs: 'fs', filepath: path ) %>">
# <i class="fas fa-folder-open" aria-hidden="true"></i>
# <%= "Go To Files: .../projects#{@path.to_s.split('projects')[1]}" %>
# <%- if Configuration.project_size_enabled -%>
# <span data-bs-toggle="project" data-url="<%= project_path(@project.id) %>"></span>
# <%- end -%>
# </a>

def project_size

end

# DRAFT: Remove if not needed
# def group_by_type_link
# group_link_text = "#{@sorting_params[:grouped?] ? 'Ungroup' : 'Group'}"
# group_link_title = @sorting_params[:grouped?] ? 'Ungroup results by type' : 'Group results by type'
# link_to(
# "Group",
# target_path(@sorting_params[:col], !@sorting_params[:grouped?]),
# title: "Group results by type",
# class: "btn btn-1 btn-primary btn-hover btn-sm align-self-end ml-auto",
# data: data_attributes
# )
# end

def column_head_link(column)
link_to(
link_text(column),
target_path(column),
title: tool_tip,
class: "text-dark",
data: data_attributes
)
end

def direction(column)
if column.to_s == @sorting_params[:col]
@sorting_params[:direction] == ascending ? descending : ascending
else
DirectoryUtilsConcern::ASCENDING
end
end

def link_text(column)
col_title = t("dashboard.#{column.to_s}")
if column.to_s == @sorting_params[:col]
"#{col_title} #{fa_icon( direction(column) == ascending ? 'sort-up' : 'sort-down', classes: 'fa-md')}".html_safe
else
"#{col_title} #{fa_icon('sort', classes: 'fa-md')}".html_safe
end
end

def target_path(column, grouped = @sorting_params[:grouped?])
project_directory_path(
{ project_id: @project.id,
dir_path: @path.to_s,
sorting_params: { col: column,
direction: direction(column),
grouped?: grouped
}
}
)
end

def tool_tip
"Show #{@path.basename} directory"
end

def data_attributes
{ turbo_frame: 'project_directory' }
end

def ascending
DirectoryUtilsConcern::ASCENDING
end

def descending
DirectoryUtilsConcern::DESCENDING
end
end
2 changes: 2 additions & 0 deletions apps/dashboard/app/javascript/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import 'datatables.net-bs4/js/dataTables.bootstrap4';
import 'datatables.net-select/js/dataTables.select';
import 'datatables.net-plugins/api/processing().mjs';
import "@hotwired/turbo-rails"
import { Turbo } from "@hotwired/turbo-rails"
Turbo.session.drive = false

import Rails from '@rails/ujs';

Expand Down
46 changes: 26 additions & 20 deletions apps/dashboard/app/views/projects/_directory.html.erb
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
<%= turbo_frame_tag "project_files" do %>
<table class="table table-striped table-condensed w-100">
<thead>
<tr>
<th>
<input type="checkbox" id="select_all" title="Select All"></input>
</th>
<th>Type<span aria-hidden="true"></span></th>
<th>Name<span aria-hidden="true"></span></th>
<th><span class="sr-only">Actions</span></th>
<th>Size<span aria-hidden="true"></span></th>
<th>Modified at<span aria-hidden="true"></span></th>
<th>Owner<span aria-hidden="true"></span></th>
<th>Mode<span aria-hidden="true"></span></th>
</tr>
</thead>
<tbody>
<%= render partial: "files", locals: { project_id: project.id, path: path, files: files } %>
</tbody>
</table>
<%= turbo_frame_tag "project_directory" do %>
<div class="border border-2 shadow p-3 mt-3 mb-5 bg-white rounded">
<div class="d-flex justify-content-left lead text-weight-800">
<strong>.../projects<%= @path.to_s.split('projects')[1] %></strong>
</div>
<table class="table table-striped table-condensed w-100 table-hover caption-right">
<caption>
<div class="my-2 btn btn-sm btn-primary">
<span><i class="fa fa-folder-open"></i>&nbsp<%= files_button %></span>
</div>
</caption>
<thead>
<tr>
<th><%= t('dashboard.type') %></span></th>
<th><%= column_head_link(:name) %></th>
<th><%= column_head_link(:size) %></th>
<th><%= column_head_link(:date) %></th>
<th><%= column_head_link(:owner) %></th>
<th>Mode</th>
</tr>
</thead>
<tbody>
<%= render partial: "files", locals: { project: project, path: path, files: files, sorting_params: sorting_params } %>
</tbody>
</table>
</div>
<% end %>
26 changes: 26 additions & 0 deletions apps/dashboard/app/views/projects/_file.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<%= turbo_frame_tag "project_directory" do %>

<div class="border border-2 shadow p-3 mt-3 mb-5 bg-white rounded">
<div class="h5 border-bottom border-1 pb-2">
<div class="row">
<div class="pl-2">
<%= link_to("",
project_directory_path(project_id: @project.id, dir_path: "#{@path.parent}", sorting_params: @sorting_params),
class: 'fa fa-window-close align-self-start button buttom-sm text-danger',
data: { turbo_frame: "project_directory" }
)
%>&nbsp
.../projects<%= @path.to_s.split('projects')[1] %>
</div>
</div>
</div>
<div >
<pre><%= @file %></pre>
</div>
<div>
<div class="my-2 btn btn-sm btn-primary">
<span><i class="fa fa-folder-open"></i>&nbsp<%= files_button %></span>
</div>
</div>
</div>
<%- end -%>
Loading

0 comments on commit 1ec386d

Please sign in to comment.