From e68637198fe698e87048734904fd81d7ce2a6fb8 Mon Sep 17 00:00:00 2001 From: akuma06 Date: Tue, 1 Aug 2017 13:54:01 +0200 Subject: [PATCH] Add ajax tag post + improved the way templating works --- public/js/kilo.js | 2 +- public/js/modal.js | 35 ++++++- .../layouts/partials/torrent_item.jet.html | 97 ++++++++++++------- .../partials/torrent_item_upload.jet.html | 83 ++++++++++------ templates/site/torrents/upload.jet.html | 17 +++- templates/site/torrents/view.jet.html | 83 +++++++++++++++- 6 files changed, 238 insertions(+), 79 deletions(-) diff --git a/public/js/kilo.js b/public/js/kilo.js index 89cb3833..60a44f23 100644 --- a/public/js/kilo.js +++ b/public/js/kilo.js @@ -4,7 +4,7 @@ var Kilo = function (params) { // public variables // Boolean defining if we are in sukebei - this.sukebei = (params.sukebei !== undefined) ? params.sukebei : 0 + this.sukebei = (params.sukebei !== undefined) ? params.sukebei : false // Boolean defining if a user is trusted this.userTrusted = (params.userTrusted !== undefined) ? params.userTrusted : false // Boolean defining if a user is logged diff --git a/public/js/modal.js b/public/js/modal.js index e14d8920..fc547898 100644 --- a/public/js/modal.js +++ b/public/js/modal.js @@ -3,19 +3,29 @@ // Get the modal var Modal = { active: 0, + // Initialise a modal or multiple ones + // takes as parameter an object params: + // @param params Object{ elements: NodeList|Node, button: ID(eg. #something)|Class(eg. .something), before: callback, after: callback, close: callback } Init: function (params) { var elements = params.elements var button = (params.button != undefined) ? params.button : false - if (elements.innerHTML != undefined) { - - } else { + if (elements.innerHTML == undefined) { var nbEl = elements.length for (var i = 0; i < nbEl; i++) { var modal = elements[i]; this.addModal(modal, button, i, params.before, params.after, params.close) } + } else { + this.addModal(modal, button, i, params.before, params.after, params.close) } }, + // addModal prepare a modal (called by Init so you don't have use it) + // @param modal Node + // @param btn ID(eg. #something)|Class(eg. .something) + // @param i If multiple btn, points out to which btn in the array apply the event + // @param before_callback callback called before opening a modal + // @param after_callback callback called after opening a modal + // @param close_callback callback called after closing a modal addModal: function (modal, btn, i, before_callback, after_callback, close_callback) { var isBtnArray = false; // Get the button that opens the modal @@ -26,8 +36,18 @@ var Modal = { } else if (btn.match(/^\./)) { btn = document.getElementsByClassName(btn.substr(1)); isBtnArray = true; + } else if (btn instanceof Array) { + btn = btn.map(function(val, index) { + if (val.match(/^#/)) { + return document.getElementById(val.substr(1)); + } else if (val.match(/^\./)) { + return document.getElementsByClassName(val.substr(1))[index]; + } + return document.querySelector(val) + }) + isBtnArray = true; } else { - console.error("Couldn't find the button") + console.error("Couldn't find the button, please provide either a #id, a .classname or an array of #id") return } if ((isBtnArray) && (i > 0) && (btn.length > 0) && (btn.length > i)) { @@ -67,16 +87,23 @@ var Modal = { } }); }, + // CloseActive closes the opened modal, if any CloseActive: function () { if (this.active != 0) { this.active.style.display = "none"; this.active = 0; } }, + // GetActive return the opened modal div GetActive: function () { return this.active; }, + // Open opens a modal and closes the active one if any Open: function (q) { + var activeModal = this.GetActive() + if (activeModal != 0) { + this.CloseActive() + } var modal = document.querySelector(q); if (modal != undefined) { modal.style.display = "none"; diff --git a/templates/layouts/partials/torrent_item.jet.html b/templates/layouts/partials/torrent_item.jet.html index d2f64699..611b197e 100644 --- a/templates/layouts/partials/torrent_item.jet.html +++ b/templates/layouts/partials/torrent_item.jet.html @@ -1,40 +1,67 @@ -{{ range _, cat := GetCategories(false, true) }} + \ No newline at end of file diff --git a/templates/layouts/partials/torrent_item_upload.jet.html b/templates/layouts/partials/torrent_item_upload.jet.html index b31e4f13..4d6573a4 100644 --- a/templates/layouts/partials/torrent_item_upload.jet.html +++ b/templates/layouts/partials/torrent_item_upload.jet.html @@ -1,35 +1,58 @@ -{{ range _, cat := GetCategories(false, true) }} + \ No newline at end of file diff --git a/templates/site/torrents/upload.jet.html b/templates/site/torrents/upload.jet.html index 23b9d67c..e01c809a 100644 --- a/templates/site/torrents/upload.jet.html +++ b/templates/site/torrents/upload.jet.html @@ -175,13 +175,20 @@ - diff --git a/templates/site/torrents/view.jet.html b/templates/site/torrents/view.jet.html index 318eea13..cac825a9 100644 --- a/templates/site/torrents/view.jet.html +++ b/templates/site/torrents/view.jet.html @@ -1,6 +1,7 @@ {{ extends "layouts/index_site" }} {{ import "layouts/partials/helpers/csrf" }} {{ import "layouts/partials/helpers/captcha" }} +{{ import "layouts/partials/helpers/errors" }} {{ import "layouts/partials/helpers/treeview" }} {{block title()}}{{Torrent.Name}}{{end}} {{block content_body()}} @@ -65,7 +66,7 @@ {{end}} {{ if len(Torrent.Tags) != 0 || User.ID > 0 }} - + {{ T("torrent_tags")}}: {{ range Torrent.Tags}} @@ -78,7 +79,7 @@ {{ end }} {{end}} - {{ if User.ID > 0 }}{{ T("add") }}{{ end }} + {{ if User.ID > 0 }}{{ T("add") }}{{ end }} {{ end }} @@ -159,7 +160,7 @@ {{ if User.ID > 0 }} - + + + {{end}} {{end}} {{ block footer_js()}} + + {{end}}