Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0
Ce dépôt a été archivé le 2022-05-07. Vous pouvez voir ses fichiers ou le cloner, mais pas ouvrir de ticket ou de demandes d'ajout, ni soumettre de changements.
nyaa-pantsu/public/js/torrentsMod.js

384 lignes
14 Kio
JavaScript
Brut Vue normale Historique

2017-06-04 07:43:41 +02:00
// @source https://github.com/NyaaPantsu/nyaa/tree/dev/public/js
// @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt Expat
var TorrentsMod = {
2017-07-16 06:43:09 +02:00
// Variables that can be modified to change the dom interactions
show_hide_button: "show_actions",
btn_class_action: "cb_action",
btn_class_submit: "cb_submit",
progress_bar_id: "progress_modtool",
status_input_name: "status_id",
owner_input_name: "owner_id",
category_input_name: "category_id",
delete_btn: "delete",
lock_delete_btn: "lock_delete",
edit_btn: "edit",
refreshTimeout: 3000,
// Internal variables used for processing the request
selected: [],
queued: [],
2017-07-23 04:50:36 +02:00
unique_id: 1,
error_count: 0,
2017-07-16 06:43:09 +02:00
progress_count: 0,
progress_max: 0,
pause: false,
enabled: false,
2017-07-16 06:43:09 +02:00
// Init method
2017-07-23 04:50:36 +02:00
Create: function () {
2017-07-16 06:43:09 +02:00
var sh_btn = document.getElementById(TorrentsMod.show_hide_button);
var btn_actions = document.getElementsByClassName(this.btn_class_action)
var btn_submit = document.getElementsByClassName(this.btn_class_submit)
btn_submit[0].disabled = true;
2017-07-23 04:50:36 +02:00
for (var i = 0; i < btn_actions.length; i++) {
2017-07-16 06:43:09 +02:00
btn_actions[i].disabled = true;
switch (btn_actions[i].id) {
case this.delete_btn:
2017-07-16 06:43:09 +02:00
btn_actions[i].addEventListener("click", this.Delete)
break;
case this.lock_delete_btn:
2017-07-16 06:43:09 +02:00
btn_actions[i].addEventListener("click", this.LockDelete)
break;
case this.edit_btn:
2017-07-16 06:43:09 +02:00
btn_actions[i].addEventListener("click", this.Edit)
break;
default:
2017-07-16 06:43:09 +02:00
break;
}
}
2017-07-23 04:50:36 +02:00
for (var i = 0; i < this.checkboxes.length; i++) {
var checkbox = this.checkboxes[i];
2017-07-16 06:43:09 +02:00
checkbox.addEventListener("change", this.checkboxEventHandler)
}
2017-07-23 04:50:36 +02:00
sh_btn.addEventListener("click", function (e) {
2017-07-16 06:43:09 +02:00
var divActions = this.nextElementSibling;
if (divActions.style.display == "inline") {
TorrentsMod.enabled = false;
document.getElementsByClassName("results box mod-open")[0].className = "results box";
2017-07-23 04:50:36 +02:00
} else {
TorrentsMod.enabled = true;
document.getElementsByClassName("results box")[0].className = "results box mod-open";
2017-07-16 06:43:09 +02:00
}
divActions.style.display = (TorrentsMod.enabled) ? "inline" : "none";
var td_cbs = document.getElementsByClassName("tr-cb")
2017-07-23 04:50:36 +02:00
for (var i = 0; i < td_cbs.length; i++) {
2017-07-16 06:43:09 +02:00
td_cb = td_cbs[i];
td_cb.style.display = (TorrentsMod.enabled) ? "table-cell" : "none";
}
var toggleText = this.dataset.toggleText;
this.dataset.toggleText = this.innerText;
this.innerText = toggleText;
});
},
// generate a unique id for a query
2017-07-23 04:50:36 +02:00
getId: function () {
2017-07-16 06:43:09 +02:00
return this.unique_id++;
},
2017-07-16 06:43:09 +02:00
// UI Methods
2017-07-23 04:50:36 +02:00
selectAll: function (bool) {
2017-07-16 06:43:09 +02:00
var l = TorrentsMod.checkboxes.length;
for (var i = 0; i < l; i++) {
TorrentsMod.checkboxes[i].checked = bool;
TorrentsMod.checkboxEventHandlerFunc(TorrentsMod.checkboxes[i]);
}
},
2017-07-23 04:50:36 +02:00
disableBtnActions: function () {
2017-07-16 06:43:09 +02:00
var btn_actions = document.getElementsByClassName(this.btn_class_action)
2017-07-23 04:50:36 +02:00
for (var i = 0; i < btn_actions.length; i++) {
2017-07-16 06:43:09 +02:00
btn_actions[i].disabled = true;
}
},
2017-07-23 04:50:36 +02:00
enableBtnActions: function () {
2017-07-16 06:43:09 +02:00
var btn_actions = document.getElementsByClassName(this.btn_class_action)
2017-07-23 04:50:36 +02:00
for (var i = 0; i < btn_actions.length; i++) {
2017-07-16 06:43:09 +02:00
btn_actions[i].disabled = false;
}
},
2017-07-23 04:50:36 +02:00
enableBtnSubmit: function () {
2017-07-16 06:43:09 +02:00
var btn_submit = document.getElementsByClassName(this.btn_class_submit)
btn_submit[0].disabled = false;
},
2017-07-23 04:50:36 +02:00
enableApplyChangesBtn: function () {
2017-07-16 06:43:09 +02:00
var btn_apply_changes = document.getElementById("confirm_changes");
2017-07-23 04:50:36 +02:00
btn_apply_changes.disabled = false;
2017-07-16 06:43:09 +02:00
},
2017-07-23 04:50:36 +02:00
disableBtnSubmit: function () {
2017-07-16 06:43:09 +02:00
var btn_submit = document.getElementsByClassName(this.btn_class_submit)
btn_submit[0].disabled = true;
},
2017-07-23 04:50:36 +02:00
disableApplyChangesBtn: function () {
2017-07-16 06:43:09 +02:00
var btn_apply_changes = document.getElementById("confirm_changes");
2017-07-23 04:50:36 +02:00
btn_apply_changes.disabled = true;
2017-07-16 06:43:09 +02:00
},
2017-07-23 04:50:36 +02:00
removeDivFromList: function (i) {
2017-07-16 06:43:09 +02:00
var queueAction = this.queued[i];
var parentDiv = document.getElementById(queueAction.unique_id).parentNode;
parentDiv.removeChild(document.getElementById(queueAction.unique_id));
},
2017-07-23 04:50:36 +02:00
removeFromParent: function (el) {
2017-07-16 06:43:09 +02:00
var parentDiv = el.parentNode;
parentDiv.removeChild(el);
},
2017-07-23 04:50:36 +02:00
generatingModal: function () {
var listLength = this.queued.length;
2017-07-23 04:50:36 +02:00
var div = {
"edit": "",
"delete": ""
};
for (var i = 0; i < listLength; i++) {
2017-07-16 06:43:09 +02:00
var listHTML = "";
2017-07-23 04:50:36 +02:00
for (key in this.queued[i].selection) {
2017-07-16 06:43:09 +02:00
var selection = this.queued[i].selection[key];
selection.key = i;
2017-07-23 04:50:36 +02:00
listHTML += Templates.Render("torrents." + this.queued[i].action + ".item", selection);
2017-07-16 06:43:09 +02:00
}
this.queued[i].list = listHTML;
this.queued[i].key = i;
2017-07-23 04:50:36 +02:00
div[this.queued[i].action] += Templates.Render("torrents." + this.queued[i].action + ".block", this.queued[i]);
2017-07-16 06:43:09 +02:00
}
this.progress_count = 0;
this.progress_max = listLength;
document.querySelector(".modal .edit_changes").innerHTML = div["edit"];
document.querySelector(".modal .delete_changes").innerHTML = div["delete"];
},
2017-07-23 04:50:36 +02:00
toggleList: function (el) {
2017-07-16 06:43:09 +02:00
el.parentNode.nextSibling.style.display = (el.parentNode.nextSibling.style.display != "block") ? "block" : "none"
},
2017-07-23 04:50:36 +02:00
addToLog: function (type, msg) {
2017-07-16 06:43:09 +02:00
var logDiv = document.querySelector(".modal .logs_mess");
if (logDiv.style.display == "none") logDiv.style.display = "block"
2017-07-23 04:50:36 +02:00
logDiv.innerHTML += Templates.Render("torrents.logs." + type, msg);
2017-07-16 06:43:09 +02:00
},
2017-07-23 04:50:36 +02:00
updateProgressBar: function () {
document.querySelector("#" + this.progress_bar_id).style.display = "block";
var progress_green = document.querySelector("#" + this.progress_bar_id + " .progress-green");
var perc = this.progress_count / this.progress_max * 100;
progress_green.style.width = perc + "%";
progress_green.innerText = this.progress_count + "/" + this.progress_max;
2017-07-16 06:43:09 +02:00
},
2017-07-23 04:50:36 +02:00
resetModal: function () {
2017-07-16 06:43:09 +02:00
var logDiv = document.querySelector(".modal .logs_mess");
logDiv.style.display = "none";
2017-07-23 04:50:36 +02:00
document.querySelector("#" + this.progress_bar_id).style.display = "none";
2017-07-16 06:43:09 +02:00
logDiv.innerHTML = "";
document.querySelector(".modal .edit_changes").innerHTML = "";
document.querySelector(".modal .delete_changes").innerHTML = "";
this.enableApplyChangesBtn();
},
2017-07-23 04:50:36 +02:00
statusToClassName: function (status) {
2017-07-16 06:43:09 +02:00
var className = ["", "normal", "remake", "trusted", "aplus", ""]
return className[status];
},
2017-07-16 06:43:09 +02:00
// Selection Management Methods
addToSelection: function (torrent) {
this.selected[torrent.id] = torrent;
},
2017-07-23 04:50:36 +02:00
removeFromSelection: function (torrent) {
2017-07-16 06:43:09 +02:00
delete this.selected[torrent.id];
for (var t in this.selected) {
2017-07-16 06:43:09 +02:00
return
}
this.selected = [];
},
2017-07-16 06:43:09 +02:00
// Query Queue Management Methods
2017-07-23 04:50:36 +02:00
AddToQueue: function (QueueAction) {
2017-07-16 06:43:09 +02:00
QueueAction.unique_id = this.getId(); // used for DOM interaction
this.queued.push(QueueAction);
this.enableBtnSubmit()
},
2017-07-23 04:50:36 +02:00
RemoveFromQueueAfterItems: function (i) {
2017-07-16 06:43:09 +02:00
for (t in this.queued[i].selection) {
this.RemoveItemFromQueue(i, t);
}
},
2017-07-23 04:50:36 +02:00
RemoveFromQueue: function (i) {
this.progress_max = (this.progress_max - 1 >= 0) ? this.progress_max - 1 : 0;
2017-07-16 06:43:09 +02:00
this.RemoveFromQueueAction(i)
return false;
},
2017-07-23 04:50:36 +02:00
RemoveFromQueueAction: function (i) {
this.removeFromParent(document.getElementById("list_" + this.queued[i].unique_id));
2017-07-16 06:43:09 +02:00
this.queued.splice(i, 1);
if (this.queued.length == 0) {
this.disableBtnSubmit();
2017-07-23 04:50:36 +02:00
if (this.progress_max > 0) {
2017-07-16 06:43:09 +02:00
this.disableApplyChangesBtn();
} else {
Modal.CloseActive();
}
}
},
2017-07-23 04:50:36 +02:00
formatSelectionToQuery: function (selection) {
2017-07-16 06:43:09 +02:00
var format = "";
for (var s in selection) {
2017-07-23 04:50:36 +02:00
format += "&torrent_id=" + selection[s].id
2017-07-16 06:43:09 +02:00
}
return (format != "") ? format.substr(1) : ""
},
2017-07-23 04:50:36 +02:00
RemoveItemFromQueue: function (i, id) {
this.removeFromParent(document.getElementById("list_item_" + id));
2017-07-16 06:43:09 +02:00
delete this.queued[i].selection[id];
2017-07-23 04:50:36 +02:00
document.getElementById("torrent_cb_" + id).checked = false;
document.getElementById("torrent_" + id).style.display = "";
2017-07-16 06:43:09 +02:00
var test = 0;
for (t in this.queued[i].selection) {
test++
break;
}
if (test == 0) this.RemoveFromQueue(i);
return false;
},
2017-07-23 04:50:36 +02:00
newQueryAttempt: function (queryUrl, queryPost, callback) {
2017-07-16 06:43:09 +02:00
Query.Post(queryUrl, queryPost, function (response) {
2017-07-23 04:50:36 +02:00
if ((response.length == 0) || (!response.ok)) { // Query has failed
2017-07-16 06:43:09 +02:00
var errorMsg = response.errors.join("<br>");
TorrentsMod.addToLog("error", errorMsg);
TorrentsMod.error_count++;
if (TorrentsMod.error_count < 2) {
TorrentsMod.addToLog("success", T.r("try_new_attempt"));
TorrentsMod.newQueryAttempt(queryUrl, queryPost, callback)
} else {
2017-07-16 06:43:09 +02:00
TorrentsMod.addToLog("error", T.r("query_is_broken", queryUrl, queryPost));
if (callback != undefined) {
TorrentsMod.addToLog("error", "Passing to the next query...");
callback(response) // So we can query only one item
}
}
2017-07-16 06:43:09 +02:00
} else {
var succesMsg = (response.infos != null) ? response.infos.join("<br>") : T.r("query_executed_success");
TorrentsMod.addToLog("success", succesMsg)
if (callback != undefined) callback(response) // So we can query only one item
}
});
},
2017-07-23 04:50:36 +02:00
QueryQueue: function (i, callback) {
2017-07-16 06:43:09 +02:00
if (this.queued.length > 0) {
var QueueAction = this.queued[i]; // we clone it so we can delete it safely
this.RemoveFromQueueAction(i);
var queryPost = "";
var queryUrl = "/mod/api/torrents";
QueueAction.queryPost = TorrentsMod.formatSelectionToQuery(QueueAction.selection)
if (QueueAction.action == "delete") {
2017-07-23 04:50:36 +02:00
queryPost = "action=" + QueueAction.action;
queryPost += "&withreport=" + QueueAction.withReport;
queryPost += "&status=" + ((QueueAction.status != undefined) ? QueueAction.status : "");
queryPost += "&" + QueueAction.queryPost; // we add torrent id
2017-07-16 06:43:09 +02:00
} else if (QueueAction.action == "edit") {
2017-07-23 04:50:36 +02:00
queryPost = "action=multiple";
queryPost += "&status=" + QueueAction.status;
queryPost += "&owner=" + QueueAction.owner;
queryPost += "&category=" + QueueAction.category;
queryPost += "&" + QueueAction.queryPost; // we add torrent id
2017-07-16 06:43:09 +02:00
}
TorrentsMod.newQueryAttempt(queryUrl, queryPost, callback)
} else {
TorrentsMod.addToLog("success", T.r("all_operations_done"))
if (TorrentsMod.refreshTimeout > 0) {
2017-07-23 04:50:36 +02:00
TorrentsMod.addToLog("success", T.r("refreshing_in", TorrentsMod.refreshTimeout / 1000))
setTimeout(function () {
2017-07-16 06:43:09 +02:00
window.location.reload()
}, TorrentsMod.refreshTimeout);
}
}
},
2017-07-23 04:50:36 +02:00
QueryLoop: function () {
2017-07-16 06:43:09 +02:00
if (TorrentsMod.progress_count <= TorrentsMod.progress_max) {
TorrentsMod.updateProgressBar()
TorrentsMod.progress_count++;
if (TorrentsMod.progress_count > TorrentsMod.progress_max) TorrentsMod.progress_count = TorrentsMod.progress_max;
TorrentsMod.QueryQueue(0, TorrentsMod.QueryLoop);
}
},
2017-07-16 06:43:09 +02:00
// Event Handlers
2017-07-23 04:50:36 +02:00
checkboxEventHandler: function (e) {
2017-07-16 06:43:09 +02:00
var el = e.target;
TorrentsMod.checkboxEventHandlerFunc(el);
},
2017-07-23 04:50:36 +02:00
checkboxEventHandlerFunc: function (el) {
2017-07-16 06:43:09 +02:00
var name = el.dataset.name;
var id = el.value;
2017-07-23 04:50:36 +02:00
if (el.checked) TorrentsMod.addToSelection({
name: name,
id: id
});
else TorrentsMod.removeFromSelection({
name: name,
id: id
});
2017-07-16 06:43:09 +02:00
if (TorrentsMod.selected.length > 0) TorrentsMod.enableBtnActions();
else TorrentsMod.disableBtnActions();
},
2017-07-16 06:43:09 +02:00
// Action Methods
2017-07-23 04:50:36 +02:00
DeleteHandler: function (locked) {
2017-07-16 06:43:09 +02:00
var withReport = confirm(T.r("delete_reports_with_torrents"))
var selection = TorrentsMod.selected;
if (locked)
2017-07-23 04:50:36 +02:00
TorrentsMod.AddToQueue({
action: "delete",
withReport: withReport,
selection: selection,
queryPost: "",
infos: T.r("with_st", T.r("lock")) + ((withReport) ? T.r("and_reports") : ""),
2017-07-23 04:50:36 +02:00
status: "5"
});
2017-07-16 06:43:09 +02:00
else TorrentsMod.AddToQueue({
action: "delete",
withReport: withReport,
selection: selection,
infos: (withReport) ? T.r("with_st", T.r("reports")) : "",
2017-07-23 04:50:36 +02:00
queryPost: ""
});
for (i in selection) document.getElementById("torrent_" + i).style.display = "none";
TorrentsMod.selected = []
TorrentsMod.disableBtnActions();
},
2017-07-23 04:50:36 +02:00
Delete: function (e) {
TorrentsMod.DeleteHandler(false);
e.preventDefault();
},
2017-07-23 04:50:36 +02:00
LockDelete: function (e) {
TorrentsMod.DeleteHandler(true);
e.preventDefault();
},
2017-07-23 04:50:36 +02:00
Edit: function (e) {
var selection = TorrentsMod.selected;
2017-07-23 04:50:36 +02:00
var status = document.querySelector(".modtools *[name='" + TorrentsMod.status_input_name + "']").value;
var owner_id = document.querySelector(".modtools *[name='" + TorrentsMod.owner_input_name + "']").value;
var category = document.querySelector(".modtools *[name='" + TorrentsMod.category_input_name + "']").value;
var infos = "";
infos += (status != "") ? T.r("status_js", status) : "";
infos += (owner_id != "") ? T.r("owner_id_js", owner_id) : "";
infos += (category != "") ? T.r("category_js", category) : "";
TorrentsMod.AddToQueue({
action: "edit",
selection: selection,
queryPost: "", // We don't format now, we wait until the query is sent
2017-07-23 04:50:36 +02:00
infos: (infos != "") ? T.r("with_st", infos) : T.r("no_changes"),
status: status,
category: category,
2017-07-23 04:50:36 +02:00
owner: owner_id
});
if (status != "") {
2017-07-23 04:50:36 +02:00
for (i in selection) document.getElementById("torrent_" + i).className = "torrent-info " + TorrentsMod.statusToClassName(status);
}
TorrentsMod.selected = []
TorrentsMod.disableBtnActions();
e.preventDefault();
},
2017-07-23 04:50:36 +02:00
ApplyChanges: function () {
this.pause = false;
this.QueryLoop();
}
};
2017-07-23 04:50:36 +02:00
// Load torrentMods when DOM is ready
document.addEventListener("DOMContentLoaded", function () {
TorrentsMod.checkboxes = document.querySelectorAll("input[name='torrent_id']");
TorrentsMod.Create();
});
2017-07-23 04:50:36 +02:00
// @license-end