Some js fixes and improvements
Better handling of Torrents.StopRefresh() Keep the order of torrents Encode html entities from torrent name (prevent execution of unwanted html tags)
Cette révision appartient à :
Parent
b5476c7d66
révision
ae1628e921
3 fichiers modifiés avec 40 ajouts et 19 suppressions
|
@ -9,7 +9,7 @@ var Templates = {
|
||||||
},
|
},
|
||||||
ApplyItemListRenderer: function(params) {
|
ApplyItemListRenderer: function(params) {
|
||||||
return function(models) {
|
return function(models) {
|
||||||
for (var i=0; i < models.length; i++) {
|
for (var i=models.length-1; i >= 0; i--) {
|
||||||
var object = Templates.Render(params.templateName, models[i]);
|
var object = Templates.Render(params.templateName, models[i]);
|
||||||
if (params.method == "append") {
|
if (params.method == "append") {
|
||||||
params.element.innerHTML = params.element.innerHTML + object
|
params.element.innerHTML = params.element.innerHTML + object
|
||||||
|
@ -18,5 +18,19 @@ var Templates = {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
},
|
||||||
|
EncodeEntities: function(value) {
|
||||||
|
return value.
|
||||||
|
replace(/&/g, '&').
|
||||||
|
replace(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g, function(value) {
|
||||||
|
var hi = value.charCodeAt(0);
|
||||||
|
var low = value.charCodeAt(1);
|
||||||
|
return '&#' + (((hi - 0xD800) * 0x400) + (low - 0xDC00) + 0x10000) + ';';
|
||||||
|
}).
|
||||||
|
replace(/([^\#-~| |!])/g, function(value) {
|
||||||
|
return '&#' + value.charCodeAt(0) + ';';
|
||||||
|
}).
|
||||||
|
replace(/</g, '<').
|
||||||
|
replace(/>/g, '>');
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,32 +1,39 @@
|
||||||
var Torrents = {
|
var Torrents = {
|
||||||
CanRefresh: false,
|
CanRefresh: false,
|
||||||
timeout: undefined,
|
timeout: undefined,
|
||||||
Seconds: 300,
|
Seconds: 300, // Every five minutes, can be overridden directly in home.html (not here is better)
|
||||||
SearchURL: "/api/search",
|
SearchURL: "/api/search",
|
||||||
Method: "prepend",
|
Method: "prepend",
|
||||||
LastID: 0,
|
LastID: 0,
|
||||||
StopRefresh: function() {
|
StopRefresh: function() {
|
||||||
clearTimeout(this.timeout)
|
clearTimeout(this.timeout)
|
||||||
this.timeout = undefined
|
this.timeout = undefined
|
||||||
|
this.CanRefresh = false
|
||||||
|
},
|
||||||
|
Refresh: function() {
|
||||||
|
if (this.CanRefresh) {
|
||||||
|
console.log("Start Refresh...")
|
||||||
|
this.timeout = setTimeout(function() {
|
||||||
|
var searchArgs = (window.location.search != "") ? window.location.search.substr(1) : ""
|
||||||
|
searchArgs = (Torrents.LastID > 0) ? "?fromID="+Torrents.LastID+"&"+searchArgs : "?"+searchArgs
|
||||||
|
Query.Get(Torrents.SearchURL+searchArgs,
|
||||||
|
Templates.ApplyItemListRenderer({
|
||||||
|
templateName: "torrents.item", method: "prepend", element: document.getElementById("torrentListResults")
|
||||||
|
}), function(torrents) {
|
||||||
|
for (var i =0; i < torrents.length; i++) { if (Torrents.LastID < torrents[i].id) Torrents.LastID = torrents[i].id; }
|
||||||
|
parseAllDates();
|
||||||
|
Torrents.Refresh()
|
||||||
|
});
|
||||||
|
}, this.Seconds*1000);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
StartRefresh: function() {
|
StartRefresh: function() {
|
||||||
console.log("Start Refresh...")
|
this.CanRefresh = true;
|
||||||
this.timeout = setTimeout(function() {
|
this.Refresh()
|
||||||
var searchArgs = (window.location.search != "") ? window.location.search.substr(1) : ""
|
|
||||||
searchArgs = (Torrents.LastID > 0) ? "?fromID="+Torrents.LastID+"&"+searchArgs : "?"+searchArgs
|
|
||||||
Query.Get(Torrents.SearchURL+searchArgs,
|
|
||||||
Templates.ApplyItemListRenderer({
|
|
||||||
templateName: "torrents.item", method: "prepend", element: document.getElementById("torrentListResults")
|
|
||||||
}), function(torrents) {
|
|
||||||
for (var i =0; i < torrents.length; i++) { if (Torrents.LastID < torrents[i].id) Torrents.LastID = torrents[i].id; }
|
|
||||||
parseAllDates();
|
|
||||||
Torrents.StartRefresh()
|
|
||||||
});
|
|
||||||
}, this.Seconds*1000);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
document.addEventListener("DOMContentLoaded", function() {
|
document.addEventListener("DOMContentLoaded", function() { // if Torrents.CanRefresh is enabled, refresh is automatically done (no need to start it anually)
|
||||||
if (Torrents.CanRefresh) {
|
if (Torrents.CanRefresh) {
|
||||||
Torrents.StartRefresh()
|
Torrents.StartRefresh()
|
||||||
}
|
}
|
||||||
|
|
|
@ -106,11 +106,11 @@ Your browser does not support the audio element.
|
||||||
{{ end }}
|
{{ end }}
|
||||||
"</a>"+
|
"</a>"+
|
||||||
"</td>"+
|
"</td>"+
|
||||||
"<td class=\"tr-name home-td\"><a href=\"/view/"+torrent.id+"\">"+ torrent.name +"</a></td>"+
|
"<td class=\"tr-name home-td\"><a href=\"/view/"+torrent.id+"\">"+Templates.EncodeEntities(torrent.name) +"</a></td>"+
|
||||||
"<td class=\"tr-links home-td\">"+
|
"<td class=\"tr-links home-td\">"+
|
||||||
"<a href=\""+torrent.magnet +"\" title=\"{{ call $.T "magnet_link" }}\">"+
|
"<a href=\""+torrent.magnet +"\" title=\"{{ call $.T "magnet_link" }}\">"+
|
||||||
"<div class=\"magnet-icon\"></div>"+
|
"<div class=\"magnet-icon\"></div>"+
|
||||||
"</a>"+(torrent.torrent != "" ? "<a href=\""+torrent.torrent+"\" title=\"{{ call $.T "torrent_file" }}\"><div class=\"download-icon\"></div></a>" : "") +
|
"</a>"+(torrent.torrent != "" ? " <a href=\""+torrent.torrent+"\" title=\"{{ call $.T "torrent_file" }}\"><div class=\"download-icon\"></div></a>" : "") +
|
||||||
"</td>"+
|
"</td>"+
|
||||||
"<td class=\"tr-size home-td hide-xs\">"+humanFileSize(torrent.filesize)+"</td>"+
|
"<td class=\"tr-size home-td hide-xs\">"+humanFileSize(torrent.filesize)+"</td>"+
|
||||||
"<td class=\"tr-se home-td hide-xs\">"+torrent.seeders+"</td>"+
|
"<td class=\"tr-se home-td hide-xs\">"+torrent.seeders+"</td>"+
|
||||||
|
@ -120,6 +120,6 @@ Your browser does not support the audio element.
|
||||||
"</tr>";
|
"</tr>";
|
||||||
});
|
});
|
||||||
Torrents.LastID = {{ lastID .URL .Models }};
|
Torrents.LastID = {{ lastID .URL .Models }};
|
||||||
if (Torrents.LastID > 0) Torrents.StartRefresh()
|
if (Torrents.LastID > 0) Torrents.CanRefresh = true;
|
||||||
</script>
|
</script>
|
||||||
{{end}}
|
{{end}}
|
Référencer dans un nouveau ticket