diff --git a/public/js/title.js b/public/js/title.js index 284ae5e..fb4f4c1 100644 --- a/public/js/title.js +++ b/public/js/title.js @@ -255,12 +255,22 @@ const bulkProgress = (action, el) => { const tagsComponent = () => { return { - tags: tags, + loading: true, + tags: [], newTag: '', inputShown: false, + tid: $('.upload-field').attr('data-title-id'), + load() { + const url = `${base_url}api/tags/${this.tid}`; + this.request(url, 'GET', (data) => { + this.tags = data.tags; + this.loading = false; + }); + }, add() { const tag = this.newTag.trim(); - this.request(tag, 'PUT', () => { + const url = `${base_url}api/tags/${this.tid}/${encodeURIComponent(tag)}`; + this.request(url, 'PUT', () => { this.tags.push(tag); this.newTag = ''; }); @@ -271,7 +281,8 @@ const tagsComponent = () => { }, rm(event) { const tag = event.currentTarget.id.split('-')[0]; - this.request(tag, 'DELETE', () => { + const url = `${base_url}api/tags/${this.tid}/${encodeURIComponent(tag)}`; + this.request(url, 'DELETE', () => { const idx = this.tags.indexOf(tag); if (idx < 0) return; this.tags.splice(idx, 1); @@ -285,9 +296,10 @@ const tagsComponent = () => { }); } }, - request(tag, method, cb) { - const tid = $('.upload-field').attr('data-title-id'); - const url = `${base_url}api/tags/${tid}/${encodeURIComponent(tag)}`; + buildURL(tid, tag) { + + }, + request(url, method, cb) { $.ajax({ url: url, method: method, @@ -295,7 +307,7 @@ const tagsComponent = () => { }) .done(data => { if (data.success) - cb(); + cb(data); else { alert('danger', data.error); } diff --git a/src/routes/api.cr b/src/routes/api.cr index 4f5f334..9a85215 100644 --- a/src/routes/api.cr +++ b/src/routes/api.cr @@ -160,6 +160,12 @@ class APIRouter < Router "ids" => "$strAry", } + Koa.object "tagsResult", { + "success" => "boolean", + "tags" => "$strAry?", + "error" => "string?", + } + Koa.describe "Returns a page in a manga entry" Koa.path "tid", desc: "Title ID" Koa.path "eid", desc: "Entry ID" @@ -685,6 +691,27 @@ class APIRouter < Router end end + Koa.describe "Gets the tags of a title" + Koa.path "tid", desc: "A title ID" + Koa.response 200, ref: "$tagsResult" + get "/api/tags/:tid" do |env| + begin + title = (@context.library.get_title env.params.url["tid"]).not_nil! + tags = title.tags + + send_json env, { + "success" => true, + "tags" => tags, + }.to_json + rescue e + @context.error e + send_json env, { + "success" => false, + "error" => e.message, + }.to_json + end + end + Koa.describe "Adds a new tag to a title" Koa.path "tid", desc: "A title ID" Koa.response 200, ref: "$result" diff --git a/src/routes/main.cr b/src/routes/main.cr index 67719b8..67bbd31 100644 --- a/src/routes/main.cr +++ b/src/routes/main.cr @@ -132,13 +132,25 @@ class MainRouter < Router titles = sort_titles titles, sort_opt, username percentage = titles.map &.load_percentage username - layout "tags" + layout "tag" rescue e @context.error e env.response.status_code = 404 end end + get "/tags" do |env| + tags = Storage.default.list_tags + encoded_tags = tags.map do |t| + URI.encode_www_form t, space_to_plus: false + end + counts = tags.map do |t| + Storage.default.get_tag_titles(t).size + end + + layout "tags" + end + get "/api" do |env| render "src/views/api.html.ecr" end diff --git a/src/views/components/tags.html.ecr b/src/views/components/tags.html.ecr index 7795dcf..4385569 100644 --- a/src/views/components/tags.html.ecr +++ b/src/views/components/tags.html.ecr @@ -1,12 +1,9 @@ - -
+

Tags: diff --git a/src/views/tag.html.ecr b/src/views/tag.html.ecr new file mode 100644 index 0000000..c6f2332 --- /dev/null +++ b/src/views/tag.html.ecr @@ -0,0 +1,30 @@ +

Tag: <%= tag %>

+

<%= titles.size %> <%= titles.size > 1 ? "titles" : "title" %> tagged

+
+
+ +
+
+ <% hash = { + "auto" => "Auto", + "time_modified" => "Date Modified", + "progress" => "Progress" + } %> + <%= render_component "sort-form" %> +
+
+
+ <% titles.each_with_index do |item, i| %> + <% progress = percentage[i] %> + <%= render_component "card" %> + <% end %> +
+ +<% content_for "script" do %> + <%= render_component "dots-scripts" %> + + +<% end %> diff --git a/src/views/tags.html.ecr b/src/views/tags.html.ecr index 58300e1..21fc3cc 100644 --- a/src/views/tags.html.ecr +++ b/src/views/tags.html.ecr @@ -1,30 +1,8 @@ -

Tag: <%= tag %>

-

<%= titles.size %> titles tagged

-
-
- -
-
- <% hash = { - "auto" => "Auto", - "time_modified" => "Date Modified", - "progress" => "Progress" - } %> - <%= render_component "sort-form" %> -
-
-
- <% titles.each_with_index do |item, i| %> - <% progress = percentage[i] %> - <%= render_component "card" %> - <% end %> -
+

Tags

+

<%= tags.size %> <%= tags.size > 1 ? "tags" : "tag" %> found

-<% content_for "script" do %> - <%= render_component "dots-scripts" %> - - +<% tags.zip(encoded_tags, counts).each do |tag, encoded, count| %> + + <%= tag %> (<%= count %> <%= count > 1 ? "titles" : "title" %>) + <% end %> diff --git a/src/views/title.html.ecr b/src/views/title.html.ecr index ff14fee..5f5c232 100644 --- a/src/views/title.html.ecr +++ b/src/views/title.html.ecr @@ -33,8 +33,9 @@
  • <%= title.display_name %>
  • <%= title.content_label %> found

    -<% tags = title.tags %> + <%= render_component "tags" %> +