From 81113001df76877c2fbfd389ba2fee7133894c0e Mon Sep 17 00:00:00 2001 From: akuma06 Date: Mon, 17 Jul 2017 14:20:18 +0200 Subject: [PATCH] Context list on upload preview (#1220) * Update kilo.js * torrent item js as a separate file to prevent redudancy * Adding context list * Fix indent --- public/js/kilo.js | 20 ++++++--- public/js/main.js | 13 ++++++ public/js/template.js | 4 ++ public/js/torrents.js | 34 +++++---------- .../layouts/partials/torrent_item.jet.html | 42 +++++++++++++++++++ templates/site/torrents/listing.jet.html | 41 +----------------- templates/site/torrents/upload.jet.html | 9 +++- 7 files changed, 91 insertions(+), 72 deletions(-) create mode 100644 templates/layouts/partials/torrent_item.jet.html diff --git a/public/js/kilo.js b/public/js/kilo.js index 51027955..cf814128 100644 --- a/public/js/kilo.js +++ b/public/js/kilo.js @@ -5,6 +5,7 @@ var Kilo = function (params) { this.sukebei = (params.sukebei !== undefined) ? params.sukebei : 0 this.userTrusted = (params.userTrusted !== undefined) ? params.userTrusted : false this.isMember = (params.isMember !== undefined) ? params.isMember : false + this.listContext = (params.listContext !== undefined) ? params.listContext : false this.langSelect = (params.langSelect !== undefined) ? params.langSelect : 'languages' this.locale = (params.locale !== undefined) ? params.locale : '' this.formatDate = { @@ -35,10 +36,6 @@ var Kilo = function (params) { document.getElementsByClassName('torrent-preview-table')[0].style.display = 'block' document.getElementsByClassName('table-torrent-date')[0].innerText = new Date(Date.now()).toISOString() - //Adding the torrent under and above the previewed one. Akuma, you do this - var torrentHTML = ["", ""]; - document.getElementById("torrentListResults").innerHTML = torrentHTML[0] + document.getElementById("torrentListResults").innerHTML + torrentHTML[1]; - // Adding listener events for (var langIndex = 0; langIndex < document.getElementsByName(this.langSelect).length; langIndex++) { document.getElementsByName(this.langSelect)[langIndex].addEventListener('change', updateTorrentLang) @@ -62,7 +59,18 @@ var Kilo = function (params) { this.setName(formName.value) this.setCategory(formCategory.selectedIndex) updateTorrentLang() - + + //Adding the torrent under and above the previewed one. + if (this.listContext) { + Query.Get('/api/search?limit=2', function (torrents) { + var torrentHTML = [] + var l = torrents.length + for (var i = 0; i < l; i++) { + torrentHTML.push(Templates.Render('torrents.item', torrents[i])) + } + document.getElementById("torrentListResults").innerHTML = torrentHTML[0] + document.getElementById("torrentListResults").innerHTML + torrentHTML[1]; + }) + } } // Helpers function for events and render this.setRemake = function (b) { @@ -124,4 +132,4 @@ var Kilo = function (params) { document.getElementsByClassName('table-torrent-flag')[0].className = 'table-torrent-flag flag flag-' + langCat document.getElementsByClassName('table-torrent-flag')[0].title = langTitle } -} +} \ No newline at end of file diff --git a/public/js/main.js b/public/js/main.js index 21de68e3..28a29357 100644 --- a/public/js/main.js +++ b/public/js/main.js @@ -90,4 +90,17 @@ document.getElementsByClassName("form-input refine")[0].addEventListener("click" if(document.getElementsByClassName("box refine")[0].style.display == "block") scrollTo(0, 0); }); + function humanFileSize(bytes, si) { + var k = si ? 1000 : 1024; + var i = ~~(Math.log(bytes) / Math.log(k)); + return i == 0 ? bytes + " B" : (bytes / Math.pow(k, i)).toFixed(1) + " " + "KMGTPEZY"[i - 1] + (si ? "" : "i") + "B"; + } + + function flagCode(language) { + split = language.split("-"); + if (split.length > 1) { + return split[0]; + } + return language; + } // @license-end diff --git a/public/js/template.js b/public/js/template.js index 3533225f..fbb4083e 100644 --- a/public/js/template.js +++ b/public/js/template.js @@ -7,6 +7,10 @@ var Templates = { this.tmpl[templateName] = template }, Render: function(templateName, model) { + if (this.tmpl[templateName] === undefined) { + console.log("The template with name '%s' doesn't exist", templateName) + return + } return this.tmpl[templateName](model) }, ApplyItemListRenderer: function(params) { diff --git a/public/js/torrents.js b/public/js/torrents.js index 4c68beca..52b9ab7f 100644 --- a/public/js/torrents.js +++ b/public/js/torrents.js @@ -25,32 +25,18 @@ var Torrents = { parseAllDates(); Torrents.Refresh() }); - }, this.Seconds*1000); - } - }, - StartRefresh: function() { - this.CanRefresh = true; - this.Refresh() + }, this.Seconds*1000); } + }, + StartRefresh: function() { + this.CanRefresh = true; + this.Refresh() } +} - document.addEventListener("DOMContentLoaded", function() { // if Torrents.CanRefresh is enabled, refresh is automatically done (no need to start it anually) - if (Torrents.CanRefresh) { - Torrents.StartRefresh() - } - }) - - function humanFileSize(bytes, si) { - var k = si ? 1000 : 1024; - var i = ~~(Math.log(bytes) / Math.log(k)); - return i == 0 ? bytes + " B" : (bytes / Math.pow(k, i)).toFixed(1) + " " + "KMGTPEZY"[i - 1] + (si ? "" : "i") + "B"; - } - - function flagCode(language) { - split = language.split("-"); - if (split.length > 1) { - return split[0]; - } - return language; +document.addEventListener("DOMContentLoaded", function() { // if Torrents.CanRefresh is enabled, refresh is automatically done (no need to start it anually) + if (Torrents.CanRefresh) { + Torrents.StartRefresh() } +}) // @license-end diff --git a/templates/layouts/partials/torrent_item.jet.html b/templates/layouts/partials/torrent_item.jet.html new file mode 100644 index 00000000..b25a8676 --- /dev/null +++ b/templates/layouts/partials/torrent_item.jet.html @@ -0,0 +1,42 @@ +{{ range _, cat := GetCategories(false, true) }} +T.Add("{{ cat.ID }}", "{{ T(cat.Name) }}") +{{end}} +{{ range _, language := GetTorrentLanguages() }} +T.Add("{{ language.Code }}", "{{ LanguageName(language, T) }}") +{{ if language.Tag != language.Code }} +T.Add("{{ language.Tag }}", "{{ LanguageName(language, T) }}") +{{end}} +{{end}} +Templates.Add("torrents.item", function(torrent) { + return ""+ + {{ if User.HasAdmin() }} + ""+ + ""+ + ""+ + {{ end }} + ""+ + {{ if Sukebei() }} + "
"+ + {{else}} + "
"+ + {{end}} + ""+ + ((torrent.languages[0] != "") ? "" : "") + + ""+ + "
"+ + ""+Templates.EncodeEntities(torrent.name) +""+ + ""+ + ((torrent.comments.length > 0) ? "" + torrent.comments.length + "" : "")+ + ""+ + ""+ + ""+ + "
"+ + "
"+(torrent.torrent != "" ? "
" : "") + + ""+ + ""+humanFileSize(torrent.filesize)+""+ + ""+torrent.seeders+""+ + ""+torrent.leechers+""+ + ""+torrent.completed+""+ + ""+torrent.date+""+ + ""; +}); \ No newline at end of file diff --git a/templates/site/torrents/listing.jet.html b/templates/site/torrents/listing.jet.html index 824d80d7..2d8d9bd0 100644 --- a/templates/site/torrents/listing.jet.html +++ b/templates/site/torrents/listing.jet.html @@ -221,46 +221,7 @@ {{end}} diff --git a/templates/site/torrents/upload.jet.html b/templates/site/torrents/upload.jet.html index 0d021016..3e4e0283 100644 --- a/templates/site/torrents/upload.jet.html +++ b/templates/site/torrents/upload.jet.html @@ -106,14 +106,19 @@
{{end}} {{ block footer_js()}} - + + + + + - {{end}}