Permalink
Browse files

replies mostly work

  • Loading branch information...
pgte committed Apr 27, 2012
1 parent ca071f4 commit c2001913b057738f9d6b8a46abc209ef32eb5676
@@ -7,7 +7,7 @@
<h3 class="title"></h3>
<p class="body"></p>
<div class="meta">
- <a class="reply" href="#">Reply</a>
+ <a class="reply" href="">Reply</a>
</div>
</div>
<div class="comments">
@@ -0,0 +1,11 @@
+<form class="newcomment" action="" method="POST">
+ <fieldset>
+ <legend>Reply:</legend>
+ <p>
+ <textarea placeholder="GitHub-flavored Markdown" name="body"></textarea>
+ <input class="submit" type="submit" name="reply" value="Reply" />
+ </p>
+ </fieldset>
+ <input type="hidden" name="article" value="" />
+ <input type="hidden" name="discussion" value="" />
+</form>
View
@@ -143,7 +143,8 @@
* Discussions
*********************/
- $('#discuss form').submit(function(ev) {
+ $('#discuss form').live('submit', function(ev) {
+ console.log('submit');
ev.preventDefault();
var form = $(this);
form.attr('disabled', 'disabled')
@@ -158,4 +159,11 @@
document.location.reload();
});
});
+
+ $('#discuss .reply').click(function(ev) {
+ ev.preventDefault();
+ var link = $(this);
+ link.parent().load(link.attr('href'));
+ });
+
}());
View
@@ -455,13 +455,14 @@ module.exports = function(conf) {
map.class('created').use('created');
map.class('comments').use('comments');
map.class('discussion').use('url').as('data-url')
+ map.class('reply').use('url').as('href')
var commentsMarkup = (discussion.comments || []).map(function(comment) {
return assets['discussions/comment.html'].compose(comment);
}).join('');
var data = {
- url: '/articles/' + escape(repo.github.name) + '/comment',
+ url: '/article/' + escape(repo.github.name) + '/comment?discussion=' + discussion.number,
author: '<a href="https://github.com/' + discussion.user.login + '">' + discussion.user.login + '</a>',
title: discussion.title,
body: hl(marked(discussion.body), false, true),
@@ -491,6 +492,25 @@ module.exports = function(conf) {
created: comment.created_at
};
+ return Plates.bind(this.raw, data, map);
+ }
+ };
+
+ assets['discussions/new_comment.html'] = {
+ raw: fs.readFileSync('./public/assets/discussions/new_comment.html', 'utf8'),
+ compose: function(repo, discussion) {
+
+ var map = new Plates.Map();
+ map.where('name').is('article').use('article').as('value');
+ map.where('name').is('discussion').use('discussion').as('value');
+ map.class('newcomment').use('url').as('action');
+
+ var data = {
+ article: repo.github.name,
+ discussion: discussion,
+ url: '/article/' + escape(repo.github.name) + '/comment'
+ };
+
return Plates.bind(this.raw, data, map);
}
}
View
@@ -33,7 +33,7 @@ module.exports = function(conf) {
}
function reply(repo, issue, body, callback) {
- issues(issue).comments.create.call(this, body, callback)
+ issues(repo, issue).comments.create.call(this, body, callback)
}
return {
View
@@ -232,7 +232,7 @@ Content.prototype.downloadComments = function(repo, callback) {
if (! repo.github) { return callback(); }
comments.get(repo, function(err, comments) {
- if (err) { return next(err); }
+ if (err) { return callback(err); }
repo.discussions = comments;
callback();
});
View
@@ -4,8 +4,7 @@ var request = require('request'),
GITHUB_BASE_URL = 'https://api.github.com'
;
-function handleRequest(expectedStatusCode, callback) {
- var req = this.req;
+function handleResponse(expectedStatusCode, callback) {
return function(err, resp, body) {
if (err) {
console.error(err);
@@ -17,7 +16,6 @@ function handleRequest(expectedStatusCode, callback) {
err = new Error('Github expected response status code is ' + resp.statusCode + ': ' + body.message || body);
return callback(err)
}
- console.log('body: %j', body);
if (typeof body === 'string') {
try { body = JSON.parse(body); }
catch(error) { err = error; }
@@ -66,11 +64,11 @@ function Github(conf) {
repo.github.name],
this.req.session);
- request.put(url, handleRequest.call(that, 201, b(function() {
+ request.put(url, handleResponse(201, b(function() {
var url = githubRepoActionURL([ 'repos', conf.orgname, repo.github.name]);
- request.get(url, handleRequest.call(that, 200, b(function(body) {
+ request.get(url, handleResponse(200, b(function(body) {
repo.github = body;
})));
@@ -124,7 +122,7 @@ function Github(conf) {
});
- request.get(url, handleRequest.call(that, 200, b(function(body) {
+ request.get(url, handleResponse(200, b(function(body) {
repo.github = body;
})));
@@ -141,7 +139,7 @@ function Github(conf) {
var issues = (function() {
function get(repo, callback) {
- request.get(githubRepoActionURL([ 'repos', conf.orgname, repo.github.name, 'issues']), handleRequest.call(this, 200, callback));
+ request.get(githubRepoActionURL([ 'repos', conf.orgname, repo.github.name, 'issues']), handleResponse.call(this, 200, callback));
}
function create(repo, title, body, callback) {
@@ -161,7 +159,7 @@ function Github(conf) {
console.log(options);
- request.post(options, handleRequest.call(this, 201, callback));
+ request.post(options, handleResponse(201, callback));
}
function issues(repo, issue) {
@@ -180,8 +178,13 @@ function Github(conf) {
issue,
'comments'],
this.req.session);
+
+ var options = {
+ url: url,
+ json: { body: body }
+ };
- request.post(url, handleRequest.call(this, 201, callback));
+ request.post(options, handleResponse.call(this, 201, callback));
}
function get(callback) {
@@ -191,7 +194,7 @@ function Github(conf) {
'issues',
issue,
'comments']);
- request.get(url, handleRequest.call(this, 200, callback));
+ request.get(url, handleResponse(200, callback));
}
return {
View
@@ -66,16 +66,37 @@ server.createServer = function(assets, content, conf) {
}
},
'/article/:name/comment': {
- post: function(name) {
+ get: function(name) {
var res = this.res;
var repo = findRepo.call(this, name);
if (! repo) { return; }
- comments.create.call(this, repo, function() {
+ var discussion = this.req.query.discussion;
+ this.res.writeHead(200, { 'Content-Type': 'text/html' });
+ this.res.end(assets['discussions/new_comment.html'].compose(repo, discussion));
+ },
+ post: function(name) {
+ var res = this.res,
+ body = this.req.body,
+ repo = findRepo.call(this, name);
+
+ if (! repo) { return; }
+
+ function done(err) {
+ if (err) {
+ res.writeHead(500);
+ return res.end(err.message);
+ }
content.downloadComments(repo, function(err) {
content.composeRepo(assets, repo);
res.end();
});
- });
+ }
+
+ if (! body.discussion) {
+ comments.create.call(this, repo, done);
+ } else {
+ comments.reply.call(this, repo, body.discussion, body.body, done);
+ }
}
},
'/article/:name': {

0 comments on commit c200191

Please sign in to comment.