diff --git a/template/app/Comments.js b/template/app/Comments.js index 53e8185a4c794fcf25612684f19ca25b61858f6d..6ac43787ab9e10cd4025a225df0613ccf5f67cec 100644 --- a/template/app/Comments.js +++ b/template/app/Comments.js @@ -78,6 +78,113 @@ Ext.define('Docs.Comments', { }); }, + /** + * Votes the comment up or down. + * @param {Object} cfg + * @param {Docs.model.Comment} cfg.comment + * @param {Docs.model.Comment} cfg.direction + * @param {Function} cfg.success Called with resulting direction and total score. + * @param {Function} cfg.failure + * @param {Object} cfg.scope + */ + vote: function(cfg) { + this.request("ajax", { + url: '/comments/' + cfg.comment.get("id"), + method: 'POST', + params: { vote: cfg.direction }, + callback: function(options, success, response) { + var data = Ext.JSON.decode(response.responseText); + if (success && data.success) { + cfg.success && cfg.success.call(cfg.scope, data.direction, data.total); + } + else { + cfg.failure && cfg.failure.call(cfg.scope, data.reason); + } + }, + scope: this + }); + }, + + /** + * Loads the plain Markdown content of comment. + * @param {Object} cfg + * @param {Docs.model.Comment} cfg.comment + * @param {Function} cfg.success Called with the original Markdown content + * @param {Function} cfg.failure + * @param {Object} cfg.scope + */ + loadContent: function(cfg) { + this.request("ajax", { + url: '/comments/' + cfg.comment.get("id"), + method: 'GET', + callback: function(options, success, response) { + var data = Ext.JSON.decode(response.responseText); + if (success && data.success) { + cfg.success && cfg.success.call(cfg.scope, data.content); + } + else { + cfg.failure && cfg.failure.call(cfg.scope, data.reason); + } + }, + scope: this + }); + }, + + /** + * Saves the plain Markdown content of comment. + * @param {Object} cfg + * @param {Docs.model.Comment} cfg.comment + * @param {String} cfg.newContent The new content. + * @param {Function} cfg.success Called with the resulting HTML content + * @param {Function} cfg.failure + * @param {Object} cfg.scope + */ + saveContent: function(cfg) { + this.request("ajax", { + url: '/comments/' + cfg.comment.get("id"), + method: 'POST', + params: { + content: cfg.newContent + }, + callback: function(options, success, response) { + var data = Ext.JSON.decode(response.responseText); + if (success && data.success) { + cfg.success && cfg.success.call(cfg.scope, data.content); + } + else { + cfg.failure && cfg.failure.call(cfg.scope, data.reason); + } + }, + scope: this + }); + }, + + /** + * Marks the comment as deleted or undoes the delete. + * @param {Object} cfg + * @param {Docs.model.Comment} cfg.comment + * @param {Boolean} cfg.deleted True to delete, false to undo. + * @param {Function} cfg.success + * @param {Function} cfg.failure + * @param {Object} cfg.scope + */ + setDeleted: function(cfg) { + Docs.Comments.request("ajax", { + url: '/comments/' + cfg.comment.get("id") + (cfg.deleted ? '/delete' : '/undo_delete'), + method: 'POST', + callback: function(options, success, response) { + var data = Ext.JSON.decode(response.responseText); + if (success && data.success) { + cfg.success && cfg.success.call(cfg.scope); + } + else { + cfg.failure && cfg.failure.call(cfg.scope, data.reason); + } + }, + scope: this + }); + }, + /** * Performs request to the comments server. * diff --git a/template/app/view/comments/List.js b/template/app/view/comments/List.js index 4f3242b16c85e49154f6b3e9997b9b174f3b9246..33d82af8aecda156ea0177ca6c47d8e7c76cfb78 100644 --- a/template/app/view/comments/List.js +++ b/template/app/view/comments/List.js @@ -67,32 +67,27 @@ Ext.define('Docs.view.comments.List', { }, this, {preventDefault: true, delegate: selector}); }, - vote: function(el, record, direction) { + vote: function(el, comment, direction) { if (!Docs.Auth.isLoggedIn()) { Docs.Tip.show('Please login to vote on this comment', el); return; } - if (record.get("upVote") && direction === "up" || record.get("downVote") && direction === "down") { + if (comment.get("upVote") && direction === "up" || comment.get("downVote") && direction === "down") { Docs.Tip.show('You have already voted on this comment', el); return; } - Docs.Comments.request("ajax", { - url: '/comments/' + record.get("id"), - method: 'POST', - params: { vote: direction }, - callback: function(options, success, response) { - var data = Ext.JSON.decode(response.responseText); - - if (data.success) { - record.set("score", data.total); - record.set("upVote", data.direction === "up"); - record.set("downVote", data.direction === "down"); - record.commit(); - } - else { - Docs.Tip.show(data.reason, el); - } + Docs.Comments.vote({ + comment: comment, + direction: direction, + success: function(direction, total) { + comment.set("upVote", direction === "up"); + comment.set("downVote", direction === "down"); + comment.set("score", total); + comment.commit(); + }, + failure: function(msg) { + Docs.Tip.show(msg, el); }, scope: this }); @@ -100,78 +95,57 @@ Ext.define('Docs.view.comments.List', { // starts an editor on the comment edit: function(el, comment) { - this.loadContent(comment, function(content) { - var contentEl = Ext.get(el).up(".comment").down(".content"); - new Docs.view.comments.Form({ - renderTo: contentEl, - user: Docs.Auth.getUser(), - content: content, - listeners: { - submit: function(newContent) { - this.saveContent(comment, newContent, function(contentHtml) { - comment.set("contentHtml", contentHtml); - comment.commit(); - }, this); - }, - cancel: function() { - this.refreshComment(comment); - }, - scope: this - } - }); - }, this); - }, - - // re-renders the comment, discarding the form. - refreshComment: function(comment) { - this.refreshNode(this.getStore().findExact("id", comment.get("id"))); - }, - - loadContent: function(comment, callback, scope) { - Docs.Comments.request("ajax", { - url: '/comments/' + comment.get("id"), - method: 'GET', - callback: function(options, success, response) { - var data = Ext.JSON.decode(response.responseText); - if (data.success) { - callback.call(scope, data.content); - } + Docs.Comments.loadContent({ + comment: comment, + success: function(content) { + var contentEl = Ext.get(el).up(".comment").down(".content"); + new Docs.view.comments.Form({ + renderTo: contentEl, + user: Docs.Auth.getUser(), + content: content, + listeners: { + submit: function(newContent) { + this.saveContent(comment, newContent); + }, + cancel: function() { + this.refreshComment(comment); + }, + scope: this + } + }); }, scope: this }); }, - saveContent: function(comment, newContent, callback, scope) { - Docs.Comments.request("ajax", { - url: '/comments/' + comment.get("id"), - method: 'POST', - params: { - content: newContent - }, - callback: function(options, success, response) { - var data = Ext.JSON.decode(response.responseText); - if (data.success) { - callback.call(scope, data.content); - } + saveContent: function(comment, newContent) { + Docs.Comments.saveContent({ + comment: comment, + newContent: newContent, + success: function(contentHtml) { + comment.set("contentHtml", contentHtml); + comment.commit(); }, scope: this }); }, + // re-renders the comment, discarding the form. + refreshComment: function(comment) { + this.refreshNode(this.getStore().findExact("id", comment.get("id"))); + }, + // marks the comment as deleted or undoes the delete setDeleted: function(el, comment, deleted) { - Docs.Comments.request("ajax", { - url: '/comments/' + comment.get("id") + (deleted ? '/delete' : '/undo_delete'), - method: 'POST', - callback: function(options, success, response) { - var data = Ext.JSON.decode(response.responseText); - if (data.success) { - comment.set("deleted", deleted); - comment.commit(); - } - else { - Ext.Msg.alert('Error', data.reason || "There was an error submitting your request"); - } + Docs.Comments.setDeleted({ + comment: comment, + deleted: deleted, + success: function() { + comment.set("deleted", deleted); + comment.commit(); + }, + failure: function(msg) { + Ext.Msg.alert('Error', msg || "There was an error submitting your request"); }, scope: this });