Skip to content

Commit

Permalink
Moving docscomments jquery plugin from the pygame docs to the website.
Browse files Browse the repository at this point in the history
Also moving the examples search from nulledge to github.
  • Loading branch information
illume committed Aug 7, 2018
1 parent fe61fb2 commit 3a09f11
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 2 deletions.
136 changes: 136 additions & 0 deletions pygameweb/comment/static/jquery.plugin.docscomments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
$(document).ready(function () {


/*
- fix comment addition for module level comment.
- format dates in a more human readable way.
X - show user title for each post.
X - order comments by newest first.
X - prevent comments from being added multiple times.
X - add comment button to reference full url.
X - Add 'add comment' button in, even if there are comments.
*/

var _comments = {};

$.getJSON("https://www.pygame.org/docs/ref/docscomments.json", function (data) {
//console.log(data);

// newest comment first.
var sortComments = function (a,b) {
var x = a.datetimeon;
var y = b.datetimeon;
return ((x < y) ? 1 : ((x > y) ? -1 : 0));
}

for(var i=0; i < data.length; i+=1) {
var link = data[i]['link']
if(typeof _comments[link] === "undefined") {
_comments[link] = [];
}
_comments[link].push(data[i]);
_comments[link].sort(sortComments);
}


var getComments = function (link) {
return _comments[link];
};


// var addCommentHtml = [
// '<form action="http://www.pygame.org/docs/ref/comment_new.php" class="addcomment"><input type="hidden" value="',
// '" name="link"><input type="submit" value="Add a Comment"></form>'
// ];
var showCommentHtml = [
'<a href="#comment_',
'" title="show comments" class="commentButton">Comments ',
'</a><article id="comment_',
'" class="hidden commentsArticle"></article>'
];
var commentPartHtml = [
'<section class="commentPart"><header class="commentHeading">',
'</header>',
'<pre class="commentContent">',
'</pre></section>'
]

$('dt.title').each(function (idx, el) {
var link = $(el).attr('id');
if (typeof link === "undefined") {
return;
}

// Add "search internet for source code" buttons.
//var searchButton = $(searchButtonHtml[0] + link + searchButtonHtml[1] + link + searchButtonHtml[2]);
//$(el).next().append(searchButton);

// Add show comments buttons.
var comments = getComments(link);

// var addCommentButton = $(addCommentHtml[0] + link + addCommentHtml[1]);
// $(el).next().append(addCommentButton);

if(typeof comments === "undefined") {
} else {
// show comment button.
//console.log(link)
var $showCommentButton = $([showCommentHtml[0],
link.replace(/\./g, "_"),
showCommentHtml[1],
comments.length,
showCommentHtml[2],
link.replace(/\./g, "_"),
showCommentHtml[3]
].join(""));
$(el).next().append($showCommentButton);

$showCommentButton.click(function () {
//console.log('asdf')
var $commentSection = $("#comment_" + link.replace(/\./g, "_"));
if (!$commentSection.hasClass('hidden')) {
// we have already loaded the comments for this part.
return;
}
$commentSection.removeClass("hidden");
$.each(comments, function(idx) {
console.log(comments[idx])
// date + user
var userName = comments[idx]['user_title'];
if (userName == null) {
userName = 'Anonymous';
}
var commentHeading = comments[idx]['datetimeon'] + " - " + userName;
var commentContent= comments[idx]['content'];

var $commentPart = $([commentPartHtml[0],
commentHeading,
commentPartHtml[1],
commentPartHtml[2],
commentContent,
commentPartHtml[3]].join("\n"));
$commentSection.append($commentPart);
});

})
}
});
});


var searchButtonHtml = [
'<form action="https://github.com/search?type=Code" class="addcomment"><input type="hidden" value="',
'" name="q"><input type="submit" value="Search examples for ',
'"></form>'
];

// Add "search internet for source code" buttons.
$('dt.title').each(function (idx, el) {
var link = $(el).attr('id');
if (typeof link === "undefined") {
return;
}
var searchButton = $(searchButtonHtml[0] + link + searchButtonHtml[1] + link + searchButtonHtml[2]);
$(el).next().append(searchButton);
});
});
13 changes: 11 additions & 2 deletions pygameweb/comment/views.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
""" For commenting on things.
"""

from flask import (Blueprint, render_template, abort, redirect, flash)
from flask import (Blueprint, render_template, abort, redirect, flash, url_for)
from flask_sqlalchemy_session import current_session
from flask_security import login_required, roles_required

from pygameweb.comment.models import CommentPost

comment_blueprint = Blueprint('comment',
__name__,
template_folder='../templates/')
template_folder='../templates/',
static_folder='static')


def comments_for(forum):
Expand All @@ -26,6 +27,14 @@ def comments_for(forum):
return posts



@comment_blueprint.route('/comment/jquery.plugin.docscomments.js', methods=['GET'])
def comments_js():
""" for including in document pages.
"""
return comment_blueprint.send_static_file('jquery.plugin.docscomments.js')


@comment_blueprint.route('/comments/<forum>',
methods=['GET'])
def recent_comments(forum):
Expand Down
2 changes: 2 additions & 0 deletions pygameweb/static/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""Adding static folders.
This serves up a whole bunch of static files and folders.
"""

from flask import Blueprint, send_from_directory
Expand Down

0 comments on commit 3a09f11

Please sign in to comment.