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 @@ - -