function(contentId, success, error) { if (confComments == null) { ui.remoteInvoke('getOldComments', [contentId], null, function(comments, spaceKey, pageId, pageType, contentVer) { confComments = []; for (var i = 0; i < comments.length; i++) { var comment = confOldCommentToDrawio(comments[i]); if (comment != null) confComments.push(comment); } //If we have no old comments, switch to the new comments format commentsVer = confComments.length == 0? 2 : 1; if (success) { success(spaceKey, pageId, pageType, contentVer); } }, function() { if (error) { error(); } }); } else if (success) { success(confComments); } }; ui.getComments = function(success, error) { if (commentsVer == null) { error(); //User can refresh to retry, we don't have content id here to get the old comments } else if (commentsVer == 1) { success(confComments); } else { ui.remoteInvoke('getComments', [null, false], null, function(comments, siteUrl) { var conComments = []; //First pass to convert replies to old comments to regular replies var commentsMap = {}; var oldVerReplies = []; var origComments = []; for (var i = 0; i < comments.length; i++) { var cnt = decodeURIComponent(comments[i].body.storage.value); if (cnt.indexOf(REPLY_MARKER) == 0) { var end = cnt.indexOf(REPLY_MARKER_END, REPLY_MARKER.length); var parentId = cnt.substring(REPLY_MARKER.length, end); comments[i].body.storage.value = cnt.substring(REPLY_MARKER_END.length + end); oldVerReplies.push({parentId: parentId, reply: comments[i]}); } else { commentsMap[comments[i].id] = comments[i]; origComments.push(comments[i]); } } for (var i = 0; i < oldVerReplies.length; i++) { var pComment = commentsMap[oldVerReplies[i].parentId]; if (pComment != null) { if (pComment.children == null) { pComment.children = {comment: {results: []}}; } pComment.children.comment.results.push(oldVerReplies[i].reply); } } for (var i = 0; i < origComments.length; i++) { conComments.push(confCommentToDrawio(origComments[i], null, siteUrl)); } success(conComments); }, error); } }; ui.addComment = function(comment, success, error) { if (commentsVer == null || !comment.content) { error(); } else if (commentsVer == 2) { ui.remoteInvoke('addComment', [comment.content], null, function(id, version, attVer) { comment.version = version; comment.file.attVer = attVer; success(id); }, error); } else { comment.id = confUser.id + ':' + Date.now(); if (ui.saveComments != null) { try { var tmpComments = JSON.parse(JSON.stringify(confComments)); tmpComments.push(comment); ui.saveComments(tmpComments, function() { success(comment.id); }, error); } catch (e) { error(); } } else { setModified(); success(comment.id); } } }; ui.newComment = function(content, user) { return new DrawioComment(commentsVer == 2? {ui: ui} : null, null, //remove file information for old format content, Date.now(), Date.now(), false, user); }; DrawioComment.prototype.addReply = function(reply, success, error, doResolve, doReopen) { if (commentsVer == null || !reply.content) { error(); } else if (commentsVer == 2) { ui.remoteInvoke('addCommentReply', [this.id, this.file.attVer, reply.content, doResolve], null, function(id, version) { reply.version = version; success(id); }, error); } else { if (ui.saveComments != null) { reply.id = confUser.id + ':' + Date.now(); this.replies.push(reply); var isResolved = this.isResolved; if (doResolve) { this.isResolved = true; } else if (doReopen) { this.isResolved = false; } try { var tmpComments = JSON.parse(JSON.stringify(confComments)); this.replies.pop(); //Undo in case more changes are done before getting the reply this.isResolved = isResolved; ui.saveComments(tmpComments, function() { success(reply.id); }, error); } catch (e) { error(); } } else { setModified(); success(); } } }; DrawioComment.prototype.editComment = function(newContent, success, error) { if (commentsVer == null) { error(); } else if (commentsVer == 2) { var _this = this; ui.remoteInvoke('editComment', [this.id, this.version, newContent], null, function(version) { _this.version = version; success(); }, error); } else { if (ui.saveComments != null) { var oldContent = this.content; this.content = newContent; try { var tmpComments = JSON.parse(JSON.stringify(confComments)); this.content = oldContent; ui.saveComments(tmpComments, success, error); } catch (e) { error(); } } else { setModified(); success(); } } }; DrawioComment.prototype.deleteComment = function(success, error) { if (commentsVer == null) { error(); } else if (commentsVer == 2) { ui.remoteInvoke('deleteComment', [this.id, this.version, this.replies != null && this.replies.length > 0], null, success, error); } else { if (ui.saveComments != null) { var that = this; this.isDeleted = true; //Mark as deleted since searching for this comment in the entire structure is complex. It will be cleaned in next save try { var tmpComments = JSON.parse(JSON.stringify(confComments)); ui.saveComments(tmpComments, success, function(err) { that.isDeleted = false; error(err); }); } catch (e) { error(); } } else { setModified(); success(); } } }; //Prefetch current user ui.getCurrentUser(); }); ØA —Eoúô