Sorting in library and title page

This commit is contained in:
Alex Ling 2020-02-22 18:16:04 +00:00
parent f837be0718
commit bf37c4aa10
4 changed files with 86 additions and 16 deletions

35
public/js/sort-items.js Normal file
View File

@ -0,0 +1,35 @@
$(() => {
$('option#name-up').attr('selected', '');
$('#sort-select').change(() => {
const sort = $('#sort-select').find(':selected').attr('id');
const ary = sort.split('-');
const by = ary[0];
const dir = ary[1];
const items = $('.item');
items.remove();
items.sort((a, b) => {
var res;
if (by === 'name')
res = $(a).find('.uk-card-title').text() > $(b).find('.uk-card-title').text();
else if (by === 'date')
res = $(a).attr('data-mtime') > $(b).attr('data-mtime');
else {
const ap = $(a).attr('data-progress');
const bp = $(b).attr('data-progress');
if (ap === bp)
// if progress is the same, we compare by name
res = $(a).find('.uk-card-title').text() > $(b).find('.uk-card-title').text();
else
res = ap > bp;
}
if (dir === 'up')
return res;
else
return !res;
});
var html = '';
$('#item-container').append(items);
});
});

View File

@ -16,7 +16,8 @@ end
class Entry class Entry
JSON.mapping zip_path: String, book_title: String, title: String, \ JSON.mapping zip_path: String, book_title: String, title: String, \
size: String, pages: Int32, cover_url: String, id: String, \ size: String, pages: Int32, cover_url: String, id: String, \
title_id: String, encoded_path: String, encoded_title: String title_id: String, encoded_path: String, encoded_title: String,
mtime: Time
def initialize(path, @book_title, @title_id, storage) def initialize(path, @book_title, @title_id, storage)
@zip_path = path @zip_path = path
@ -32,6 +33,7 @@ class Entry
.size .size
@id = storage.get_id @zip_path, false @id = storage.get_id @zip_path, false
@cover_url = "/api/page/#{@title_id}/#{@id}/1" @cover_url = "/api/page/#{@title_id}/#{@id}/1"
@mtime = File.info(@zip_path).modification_time
end end
def read_page(page_num) def read_page(page_num)
Zip::File.open @zip_path do |file| Zip::File.open @zip_path do |file|
@ -57,7 +59,7 @@ end
class Title class Title
JSON.mapping dir: String, entries: Array(Entry), title: String, JSON.mapping dir: String, entries: Array(Entry), title: String,
id: String, encoded_title: String id: String, encoded_title: String, mtime: Time
def initialize(dir : String, storage) def initialize(dir : String, storage)
@dir = dir @dir = dir
@ -73,6 +75,9 @@ class Title
} }
.select { |e| e.pages > 0 } .select { |e| e.pages > 0 }
.sort { |a, b| a.title <=> b.title } .sort { |a, b| a.title <=> b.title }
mtimes = [File.info(dir).modification_time]
mtimes += @entries.map{|e| e.mtime}
@mtime = mtimes.max
end end
# When downloading from MangaDex, the zip/cbz file would not be valid # When downloading from MangaDex, the zip/cbz file would not be valid
# before the download is completed. If we scan the zip file, # before the download is completed. If we scan the zip file,

View File

@ -1,14 +1,28 @@
<h2 class=uk-title>Library</h2> <h2 class=uk-title>Library</h2>
<p class="uk-text-meta"><%= titles.size %> titles found</p> <p class="uk-text-meta"><%= titles.size %> titles found</p>
<div class="uk-margin"> <div class="uk-grid-small" uk-grid>
<form class="uk-search uk-search-default"> <div class="uk-margin-bottom uk-width-3-4@s">
<span uk-search-icon></span> <form class="uk-search uk-search-default">
<input class="uk-search-input" type="search" placeholder="Search"> <span uk-search-icon></span>
</form> <input class="uk-search-input" type="search" placeholder="Search">
</form>
</div>
<div class="uk-margin-bottom uk-width-1-4@s">
<div class="uk-form-horizontal">
<select class="uk-select" id="sort-select">
<option id="name-up">▲ Name</option>
<option id="name-down">▼ Name</option>
<option id="date-up">▲ Date Modified</option>
<option id="date-down">▼ Date Modified</option>
<option id="progress-up">▲ Progress</option>
<option id="progress-down">▼ Progress</option>
</select>
</div>
</div>
</div> </div>
<div class="uk-child-width-1-4@m uk-child-width-1-2" uk-grid> <div id="item-container" class="uk-child-width-1-4@m uk-child-width-1-2" uk-grid>
<%- titles.each_with_index do |t, i| -%> <%- titles.each_with_index do |t, i| -%>
<div class="item"> <div class="item" data-mtime="<%= t.mtime.to_unix %>" data-progress="<%= percentage[i] %>">
<a class="acard" href="/book/<%= t.id %>"> <a class="acard" href="/book/<%= t.id %>">
<div class="uk-card uk-card-default"> <div class="uk-card uk-card-default">
<div class="uk-card-media-top"> <div class="uk-card-media-top">
@ -27,4 +41,5 @@
<% content_for "script" do %> <% content_for "script" do %>
<script src="/js/search.js"></script> <script src="/js/search.js"></script>
<script src="/js/sort-items.js"></script>
<% end %> <% end %>

View File

@ -1,15 +1,29 @@
<div id="alert"></div> <div id="alert"></div>
<h2 class=uk-title><%= title.title %></h2> <h2 class=uk-title><%= title.title %></h2>
<p class="uk-text-meta"><%= title.entries.size %> entries found</p> <p class="uk-text-meta"><%= title.entries.size %> entries found</p>
<div class="uk-margin"> <div class="uk-grid-small" uk-grid>
<form class="uk-search uk-search-default"> <div class="uk-margin-bottom uk-width-3-4@s">
<span uk-search-icon></span> <form class="uk-search uk-search-default">
<input class="uk-search-input" type="search" placeholder="Search"> <span uk-search-icon></span>
</form> <input class="uk-search-input" type="search" placeholder="Search">
</form>
</div>
<div class="uk-margin-bottom uk-width-1-4@s">
<div class="uk-form-horizontal">
<select class="uk-select" id="sort-select">
<option id="name-up">▲ Name</option>
<option id="name-down">▼ Name</option>
<option id="date-up">▲ Date Modified</option>
<option id="date-down">▼ Date Modified</option>
<option id="progress-up">▲ Progress</option>
<option id="progress-down">▼ Progress</option>
</select>
</div>
</div>
</div> </div>
<div class="uk-child-width-1-4@m uk-child-width-1-2" uk-grid> <div id="item-container" class="uk-child-width-1-4@m uk-child-width-1-2" uk-grid>
<%- title.entries.each_with_index do |e, i| -%> <%- title.entries.each_with_index do |e, i| -%>
<div class="item"> <div class="item" data-mtime="<%= e.mtime.to_unix %>" data-progress="<%= percentage[i] %>">
<a class="acard"> <a class="acard">
<div class="uk-card uk-card-default" onclick="showModal(&quot;<%= e.encoded_path %>&quot;, '<%= e.pages %>', <%= (percentage[i] * 100).round(1) %>, &quot;<%= title.encoded_title %>&quot;, &quot;<%= e.encoded_title %>&quot;, '<%= e.title_id %>', '<%= e.id %>')"> <div class="uk-card uk-card-default" onclick="showModal(&quot;<%= e.encoded_path %>&quot;, '<%= e.pages %>', <%= (percentage[i] * 100).round(1) %>, &quot;<%= title.encoded_title %>&quot;, &quot;<%= e.encoded_title %>&quot;, '<%= e.title_id %>', '<%= e.id %>')">
<div class="uk-card-media-top"> <div class="uk-card-media-top">
@ -51,4 +65,5 @@
<% content_for "script" do %> <% content_for "script" do %>
<script src="/js/title.js"></script> <script src="/js/title.js"></script>
<script src="/js/search.js"></script> <script src="/js/search.js"></script>
<script src="/js/sort-items.js"></script>
<% end %> <% end %>