mirror of https://github.com/zlatinb/muwire
implement adding comments to files
parent
de8684bafc
commit
8bff987d30
|
@ -20,6 +20,7 @@ import com.muwire.core.files.FileSharedEvent;
|
|||
import com.muwire.core.files.FileTree;
|
||||
import com.muwire.core.files.FileTreeCallback;
|
||||
import com.muwire.core.files.FileUnsharedEvent;
|
||||
import com.muwire.core.files.UICommentEvent;
|
||||
|
||||
import net.i2p.data.Base64;
|
||||
|
||||
|
@ -113,6 +114,28 @@ public class FileManager {
|
|||
}
|
||||
}
|
||||
|
||||
void comment(File file, String comment) {
|
||||
|
||||
if (file.isFile()) {
|
||||
SharedFile sf = core.getFileManager().getFileToSharedFile().get(file);
|
||||
if (sf == null)
|
||||
return;
|
||||
UICommentEvent e = new UICommentEvent();
|
||||
e.setOldComment(sf.getComment());
|
||||
sf.setComment(comment);
|
||||
e.setSharedFile(sf);
|
||||
revision++;
|
||||
core.getEventBus().publish(e);
|
||||
} else {
|
||||
TraverseCallback cb = new TraverseCallback();
|
||||
fileTree.traverse(file, cb);
|
||||
|
||||
for (SharedFile found : cb.found) {
|
||||
comment(found.getFile(), comment);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static class TraverseCallback implements FileTreeCallback<SharedFile> {
|
||||
private final List<SharedFile> found = new ArrayList<>();
|
||||
|
||||
|
|
|
@ -89,6 +89,8 @@ public class FilesServlet extends HttpServlet {
|
|||
sb.append("<File>");
|
||||
sb.append("<Name>").append(Util.escapeHTMLinXML(f.getName())).append("</Name>");
|
||||
sb.append("<Size>").append(DataHelper.formatSize2Decimal(value.getCachedLength())).append("B").append("</Size>");
|
||||
if (value.getComment() != null)
|
||||
sb.append("<Comment>").append(Util.escapeHTMLinXML(value.getComment())).append("</Comment>");
|
||||
// TODO: other stuff
|
||||
sb.append("</File>");
|
||||
}
|
||||
|
@ -120,19 +122,42 @@ public class FilesServlet extends HttpServlet {
|
|||
resp.sendError(403,"Bad param");
|
||||
return;
|
||||
}
|
||||
File current = null;
|
||||
for (String element : DataHelper.split(pathElements,",")) {
|
||||
element = Util.unescapeHTMLinXML(Base64.decodeToString(element));
|
||||
if (element == null) {
|
||||
resp.sendError(403,"Bad param");
|
||||
return;
|
||||
}
|
||||
if (current == null)
|
||||
current = new File(element);
|
||||
else
|
||||
current = new File(current, element);
|
||||
File file = getFromPathElements(pathElements);
|
||||
if (file == null) {
|
||||
resp.sendError(403, "Bad param");
|
||||
return;
|
||||
}
|
||||
fileManager.unshareFile(current);
|
||||
fileManager.unshareFile(file);
|
||||
} else if (action.equals("comment")) {
|
||||
String pathElements = req.getParameter("path");
|
||||
if (pathElements == null) {
|
||||
resp.sendError(403,"Bad param");
|
||||
return;
|
||||
}
|
||||
File file = getFromPathElements(pathElements);
|
||||
if (file == null) {
|
||||
resp.sendError(403, "Bad param");
|
||||
return;
|
||||
}
|
||||
String comment = req.getParameter("comment"); // null is ok
|
||||
if (comment.isEmpty())
|
||||
comment = null;
|
||||
fileManager.comment(file, comment);
|
||||
}
|
||||
}
|
||||
|
||||
private static File getFromPathElements(String pathElements) {
|
||||
File current = null;
|
||||
for (String element : DataHelper.split(pathElements,",")) {
|
||||
element = Util.unescapeHTMLinXML(Base64.decodeToString(element));
|
||||
if (element == null) {
|
||||
return null;
|
||||
}
|
||||
if (current == null)
|
||||
current = new File(element);
|
||||
else
|
||||
current = new File(current, element);
|
||||
}
|
||||
return current;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,26 +1,31 @@
|
|||
|
||||
class Node {
|
||||
constructor(nodeId, parent, leaf, path, size, revision) {
|
||||
constructor(nodeId, parent, leaf, path, size, comment, revision) {
|
||||
this.nodeId = nodeId
|
||||
this.parent = parent
|
||||
this.leaf = leaf
|
||||
this.children = []
|
||||
this.path = path
|
||||
this.size = size
|
||||
this.comment = comment
|
||||
this.revision = revision
|
||||
}
|
||||
|
||||
updateDiv() {
|
||||
var div = document.getElementById(this.nodeId)
|
||||
var unshareLink = "<a href='#' onclick='window.unshare(\"" + this.nodeId +"\");return false;'>Unshare</a>"
|
||||
var commentLink = "<span id='comment-link-"+this.nodeId+"'><a href='#' onclick='window.showCommentForm(\"" + this.nodeId + "\");return false;'>Comment</a></span>";
|
||||
if (this.leaf) {
|
||||
div.innerHTML = "<li>"+this.path+"<br/>"+ unshareLink + "</li>"
|
||||
div.innerHTML = "<li>"+this.path+"<br/>"+ unshareLink + " " + commentLink + "<div id='comment-" + this.nodeId+ "'></div></li>"
|
||||
} else {
|
||||
if (this.children.length == 0) {
|
||||
div.innerHTML = "<li><span><a href='#' onclick='window.expand(\"" + this.nodeId + "\");return false'>" +
|
||||
this.path + "</a> " + unshareLink + "</span></li>"
|
||||
this.path + "</a> " + unshareLink + "</span>" + commentLink + "<div id='comment-" + this.nodeId + "'></div></li>"
|
||||
} else {
|
||||
var l = "<li><a href='#' onclick='window.collapse(\"" + this.nodeId + "\");return false;'>"+this.path+"</a> " + unshareLink + "<ul>"
|
||||
var l = "<li><a href='#' onclick='window.collapse(\"" + this.nodeId + "\");return false;'>"+this.path+"</a> " + unshareLink
|
||||
l += " " + commentLink+"<div id='comment-" + this.nodeId + "'></div>"
|
||||
|
||||
l += "<ul>"
|
||||
var i
|
||||
for (i = 0; i < this.children.length; i++) {
|
||||
l += "<li>"
|
||||
|
@ -205,7 +210,7 @@ function refreshStatus() {
|
|||
}
|
||||
|
||||
var treeRevision = -1
|
||||
var root = new Node("root",null,false,"Shared Files", -1, -1)
|
||||
var root = new Node("root",null,false,"Shared Files", -1, null, -1)
|
||||
var nodesById = new Map()
|
||||
|
||||
function initFiles() {
|
||||
|
@ -243,8 +248,14 @@ function expand(nodeId) {
|
|||
for (i = 0; i < fileElements.length; i++) {
|
||||
var fileName = fileElements[i].getElementsByTagName("Name")[0].childNodes[0].nodeValue
|
||||
var size = fileElements[i].getElementsByTagName("Size")[0].childNodes[0].nodeValue
|
||||
var comment = fileElements[i].getElementsByTagName("Comment")
|
||||
if (comment != null && comment.length == 1)
|
||||
comment = comment[0].childNodes[0].nodeValue
|
||||
else
|
||||
comment = null
|
||||
|
||||
var nodeId = node.nodeId + "_"+ Base64.encode(fileName)
|
||||
var newFileNode = new Node(nodeId, node, true, fileName, size, revision)
|
||||
var newFileNode = new Node(nodeId, node, true, fileName, size, comment, revision)
|
||||
nodesById.set(nodeId, newFileNode)
|
||||
node.children.push(newFileNode)
|
||||
}
|
||||
|
@ -253,7 +264,7 @@ function expand(nodeId) {
|
|||
for (i = 0; i < dirElements.length; i++) {
|
||||
var dirName = dirElements[i].childNodes[0].nodeValue
|
||||
var nodeId = node.nodeId + "_"+ Base64.encode(dirName)
|
||||
var newDirNode = new Node(nodeId, node, false, dirName, -1, revision)
|
||||
var newDirNode = new Node(nodeId, node, false, dirName, -1, null, revision)
|
||||
nodesById.set(nodeId, newDirNode)
|
||||
node.children.push(newDirNode)
|
||||
}
|
||||
|
@ -288,4 +299,46 @@ function unshare(nodeId) {
|
|||
xmlhttp.open("POST", "/MuWire/Files", true)
|
||||
xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
|
||||
xmlhttp.send("action=unshare&path="+encodedPath)
|
||||
}
|
||||
|
||||
function showCommentForm(nodeId) {
|
||||
var linkSpan = document.getElementById("comment-link-"+nodeId)
|
||||
linkSpan.innerHTML=""
|
||||
var commentDiv = document.getElementById("comment-"+nodeId)
|
||||
|
||||
var node = nodesById.get(nodeId)
|
||||
var existingComment = node.comment == null ? "" : node.comment
|
||||
|
||||
var textArea = "<textarea id='comment-text-" + nodeId + "'>"+existingComment+"</textarea>"
|
||||
var saveCommentLink = "<a href='#' onclick='saveComment(\"" + nodeId + "\");return false;'>Save</a>"
|
||||
var cancelCommentLink = "<a href='#' onclick='cancelComment(\"" + nodeId + "\");return false;'>Cancel</a>"
|
||||
|
||||
var html = textArea + "<br/>" + saveCommentLink + " " + cancelCommentLink
|
||||
|
||||
commentDiv.innerHTML = html
|
||||
}
|
||||
|
||||
function cancelComment(nodeId) {
|
||||
var commentDiv = document.getElementById("comment-"+nodeId)
|
||||
commentDiv.innerHTML = ""
|
||||
|
||||
var node = nodesById.get(nodeId)
|
||||
node.updateDiv()
|
||||
}
|
||||
|
||||
function saveComment(nodeId) {
|
||||
var comment = document.getElementById("comment-text-"+nodeId).value
|
||||
var node = nodesById.get(nodeId)
|
||||
var encodedPath = encodedPathToRoot(node)
|
||||
var xmlhttp = new XMLHttpRequest()
|
||||
xmlhttp.onreadystatechange = function() {
|
||||
if (this.readyState == 4 && this.status == 200) {
|
||||
cancelComment(nodeId)
|
||||
collapse(node.parent.nodeId)
|
||||
expand(node.parent.nodeId) // this can probably be done smarter
|
||||
}
|
||||
}
|
||||
xmlhttp.open("POST", "/MuWire/Files", true)
|
||||
xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
|
||||
xmlhttp.send(encodeURI("action=comment&path="+encodedPath+ "&comment="+comment))
|
||||
}
|
Loading…
Reference in New Issue