diff --git a/public/js/sort-items.js b/public/js/sort-items.js new file mode 100644 index 0000000..c2a5f81 --- /dev/null +++ b/public/js/sort-items.js @@ -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); + }); +}); diff --git a/src/library.cr b/src/library.cr index 1a3b117..ad17475 100644 --- a/src/library.cr +++ b/src/library.cr @@ -14,7 +14,7 @@ end class Entry JSON.mapping zip_path: String, book_title: String, title: String, \ - size: String, pages: Int32, cover_url: String + size: String, pages: Int32, cover_url: String, mtime: Time def initialize(path, @book_title) @zip_path = path @@ -27,6 +27,7 @@ class Entry } .size @cover_url = "/api/page/#{@book_title}/#{title}/1" + @mtime = File.info(@zip_path).modification_time end def read_page(page_num) Zip::File.open @zip_path do |file| @@ -51,7 +52,7 @@ class Entry end class Title - JSON.mapping dir: String, entries: Array(Entry), title: String + JSON.mapping dir: String, entries: Array(Entry), title: String, mtime: Time def initialize(dir : String) @dir = dir @@ -61,6 +62,9 @@ class Title .map { |path| Entry.new File.join(dir, path), @title } .select { |e| e.pages > 0 } .sort { |a, b| a.title <=> b.title } + mtimes = [File.info(dir).modification_time] + mtimes += @entries.map{|e| e.mtime} + @mtime = mtimes.max end def get_entry(name) @entries.find { |e| e.title == name } diff --git a/src/views/index.ecr b/src/views/index.ecr index ae384b5..b099811 100644 --- a/src/views/index.ecr +++ b/src/views/index.ecr @@ -1,14 +1,28 @@

Library

<%= titles.size %> titles found

-
- +
+
+ +
+
+
+ +
+
-
+