/* Scripts for cnprog.com Project Name: Lanai All Rights Resevred 2008. CNPROG.COM */ var lanai = { /** * Finds any
tags which aren't registered for
* pretty printing, adds the appropriate class name and invokes prettify.
*/
highlightSyntax: function(){
var styled = false;
$("pre code").parent().each(function(){
if (!$(this).hasClass('prettyprint')){
$(this).addClass('prettyprint');
styled = true;
}
});
if (styled){
prettyPrint();
}
}
};
var Vote = function(){
// All actions are related to a question
var questionId;
// The object we operate on actually. It can be a question or an answer.
var postId;
var questionAuthorId;
var currentUserId;
var answerContainerIdPrefix = 'answer-container-';
var voteContainerId = 'vote-buttons';
var imgIdPrefixAccept = 'answer-img-accept-';
var imgClassPrefixFavorite = 'question-img-favorite';
var imgIdPrefixQuestionVoteup = 'question-img-upvote-';
var imgIdPrefixQuestionVotedown = 'question-img-downvote-';
var imgIdPrefixAnswerVoteup = 'answer-img-upvote-';
var imgIdPrefixAnswerVotedown = 'answer-img-downvote-';
var divIdFavorite = 'favorite-number';
var commentLinkIdPrefix = 'comment-';
var voteNumberClass = "vote-number";
var offensiveIdPrefixQuestionFlag = 'question-offensive-flag-';
var offensiveIdPrefixAnswerFlag = 'answer-offensive-flag-';
var offensiveClassFlag = 'offensive-flag';
var questionControlsId = 'question-controls';
var removeQuestionLinkIdPrefix = 'question-delete-link-';
var removeAnswerLinkIdPrefix = 'answer-delete-link-';
var questionSubscribeUpdates = 'question-subscribe-updates';
var acceptAnonymousMessage = $.i18n._('insufficient privilege');
var acceptOwnAnswerMessage = $.i18n._('cannot pick own answer as best');
var favoriteAnonymousMessage = $.i18n._('anonymous users cannot select favorite questions')
+ ""
+ $.i18n._('please login') + "";
var voteAnonymousMessage = $.i18n._('anonymous users cannot vote')
+ ""
+ $.i18n._('please login') + "";
var upVoteRequiredScoreMessage = $.i18n._('>15 points requried to upvote')
+ $.i18n._('please see') + "faq";
var downVoteRequiredScoreMessage = $.i18n._('>100 points requried to downvote')
+ $.i18n._('please see') + "faq";
var voteOwnDeniedMessage = $.i18n._('cannot vote for own posts');
var voteRequiredMoreVotes = $.i18n._('daily vote cap exhausted')
+ $.i18n._('please see') + "faq";
var voteDenyCancelMessage = $.i18n._('cannot revoke old vote')
+ $.i18n._('please see') + "faq";
var offensiveConfirmation = $.i18n._('please confirm offensive');
var offensiveAnonymousMessage = $.i18n._('anonymous users cannot flag offensive posts')
+ ""
+ $.i18n._('please login') + "";
var offensiveTwiceMessage = $.i18n._('cannot flag message as offensive twice')
+ $.i18n._('please see') + "faq";
var offensiveNoFlagsLeftMessage = $.i18n._('flag offensive cap exhausted')
+ $.i18n._('please see') + "faq";
var offensiveNoPermissionMessage = $.i18n._('need >15 points to report spam')
+ $.i18n._('please see') + "faq";
var removeConfirmation = $.i18n._('confirm delete');
var removeAnonymousMessage = $.i18n._('anonymous users cannot delete/undelete');
var recoveredMessage = $.i18n._('post recovered');
var deletedMessage = $.i18n._('post deleted');
var VoteType = {
acceptAnswer : 0,
questionUpVote : 1,
questionDownVote : 2,
favorite : 4,
answerUpVote: 5,
answerDownVote:6,
offensiveQuestion : 7,
offensiveAnswer:8,
removeQuestion: 9,
removeAnswer:10,
questionSubscribeUpdates:11,
questionUnsubscribeUpdates:12,
};
var getFavoriteButton = function(){
var favoriteButton = 'div.'+ voteContainerId +' img[class='+ imgClassPrefixFavorite +']';
return $(favoriteButton);
};
var getFavoriteNumber = function(){
var favoriteNumber = '#'+ divIdFavorite ;
return $(favoriteNumber);
};
var getQuestionVoteUpButton = function(){
var questionVoteUpButton = 'div.'+ voteContainerId +' img[id^='+ imgIdPrefixQuestionVoteup +']';
return $(questionVoteUpButton);
};
var getQuestionVoteDownButton = function(){
var questionVoteDownButton = 'div.'+ voteContainerId +' img[id^='+ imgIdPrefixQuestionVotedown +']';
return $(questionVoteDownButton);
};
var getAnswerVoteUpButtons = function(){
var answerVoteUpButton = 'div.'+ voteContainerId +' img[id^='+ imgIdPrefixAnswerVoteup +']';
return $(answerVoteUpButton);
};
var getAnswerVoteDownButtons = function(){
var answerVoteDownButton = 'div.'+ voteContainerId +' img[id^='+ imgIdPrefixAnswerVotedown +']';
return $(answerVoteDownButton);
};
var getAnswerVoteUpButton = function(id){
var answerVoteUpButton = 'div.'+ voteContainerId +' img[id='+ imgIdPrefixAnswerVoteup + id + ']';
return $(answerVoteUpButton);
};
var getAnswerVoteDownButton = function(id){
var answerVoteDownButton = 'div.'+ voteContainerId +' img[id='+ imgIdPrefixAnswerVotedown + id + ']';
return $(answerVoteDownButton);
};
var getOffensiveQuestionFlag = function(){
var offensiveQuestionFlag = 'table[id=question-table] span[class='+ offensiveClassFlag +']';
return $(offensiveQuestionFlag);
};
var getOffensiveAnswerFlags = function(){
var offensiveQuestionFlag = 'div.answer span[class='+ offensiveClassFlag +']';
return $(offensiveQuestionFlag);
};
var getremoveQuestionLink = function(){
var removeQuestionLink = 'div#question-controls a[id^='+ removeQuestionLinkIdPrefix +']';
return $(removeQuestionLink);
};
var getquestionSubscribeUpdatesCheckbox = function(){
return $('#' + questionSubscribeUpdates);
};
var getremoveAnswersLinks = function(){
var removeAnswerLinks = 'div.answer-controls a[id^='+ removeAnswerLinkIdPrefix +']';
return $(removeAnswerLinks);
};
var setVoteImage = function(voteType, undo, object){
var flag = undo ? "" : "-on";
var arrow = (voteType == VoteType.questionUpVote || voteType == VoteType.answerUpVote) ? "up" : "down";
object.attr("src", "/content/images/vote-arrow-"+ arrow + flag +".png");
// if undo voting, then undo the pair of arrows.
if(undo){
if(voteType == VoteType.questionUpVote || voteType == VoteType.questionDownVote){
$(getQuestionVoteUpButton()).attr("src", "/content/images/vote-arrow-up.png");
$(getQuestionVoteDownButton()).attr("src", "/content/images/vote-arrow-down.png");
}
else{
$(getAnswerVoteUpButton(postId)).attr("src", "/content/images/vote-arrow-up.png");
$(getAnswerVoteDownButton(postId)).attr("src", "/content/images/vote-arrow-down.png");
}
}
};
var setVoteNumber = function(object, number){
var voteNumber = object.parent('div.'+ voteContainerId).find('div.'+ voteNumberClass);
$(voteNumber).text(number);
};
var bindEvents = function(){
// accept answers
if(questionAuthorId == currentUserId){
var acceptedButtons = 'div.'+ voteContainerId +' img[id^='+ imgIdPrefixAccept +']';
$(acceptedButtons).unbind('click').click(function(event){
Vote.accept($(event.target))
});
}
// set favorite question
var favoriteButton = getFavoriteButton();
favoriteButton.unbind('click').click(function(event){
Vote.favorite($(event.target))
});
// question vote up
var questionVoteUpButton = getQuestionVoteUpButton();
questionVoteUpButton.unbind('click').click(function(event){
Vote.vote($(event.target), VoteType.questionUpVote)
});
var questionVoteDownButton = getQuestionVoteDownButton();
questionVoteDownButton.unbind('click').click(function(event){
Vote.vote($(event.target), VoteType.questionDownVote)
});
var answerVoteUpButton = getAnswerVoteUpButtons();
answerVoteUpButton.unbind('click').click(function(event){
Vote.vote($(event.target), VoteType.answerUpVote)
});
var answerVoteDownButton = getAnswerVoteDownButtons();
answerVoteDownButton.unbind('click').click(function(event){
Vote.vote($(event.target), VoteType.answerDownVote)
});
getOffensiveQuestionFlag().unbind('click').click(function(event){
Vote.offensive(this, VoteType.offensiveQuestion)
});
getOffensiveAnswerFlags().unbind('click').click(function(event){
Vote.offensive(this, VoteType.offensiveAnswer)
});
getremoveQuestionLink().unbind('click').click(function(event){
Vote.remove(this, VoteType.removeQuestion);
});
getquestionSubscribeUpdatesCheckbox().unbind('click').click(function(event){
if (this.checked){
Vote.vote($(event.target), VoteType.questionSubscribeUpdates);
}
else {
Vote.vote($(event.target), VoteType.questionUnsubscribeUpdates);
}
});
getremoveAnswersLinks().unbind('click').click(function(event){
Vote.remove(this, VoteType.removeAnswer)
});
};
var submit = function(object, voteType, callback) {
$.ajax({
type: "POST",
cache: false,
dataType: "json",
url: "/questions/" + questionId + "/vote/",
data: { "type": voteType, "postId": postId },
error: handleFail,
success: function(data){callback(object, voteType, data)}});
};
var handleFail = function(xhr, msg){
alert("Callback invoke error: " + msg)
};
// callback function for Accept Answer action
var callback_accept = function(object, voteType, data){
if(data.allowed == "0" && data.success == "0"){
showMessage(object, acceptAnonymousMessage);
}
else if(data.allowed == "-1"){
showMessage(object, acceptOwnAnswerMessage);
}
else if(data.status == "1"){
object.attr("src", "/content/images/vote-accepted.png");
$("#"+answerContainerIdPrefix+postId).removeClass("accepted-answer");
$("#"+commentLinkIdPrefix+postId).removeClass("comment-link-accepted");
}
else if(data.success == "1"){
var acceptedButtons = 'div.'+ voteContainerId +' img[id^='+ imgIdPrefixAccept +']';
$(acceptedButtons).attr("src", "/content/images/vote-accepted.png");
var answers = ("div[id^="+answerContainerIdPrefix +"]");
$(answers).removeClass("accepted-answer");
var commentLinks = ("div[id^="+answerContainerIdPrefix +"] div[id^="+ commentLinkIdPrefix +"]");
$(commentLinks).removeClass("comment-link-accepted");
object.attr("src", "/content/images/vote-accepted-on.png");
$("#"+answerContainerIdPrefix+postId).addClass("accepted-answer");
$("#"+commentLinkIdPrefix+postId).addClass("comment-link-accepted");
}
else{
showMessage(object, data.message);
}
};
var callback_favorite = function(object, voteType, data){
if(data.allowed == "0" && data.success == "0"){
showMessage(object, favoriteAnonymousMessage.replace("{{QuestionID}}", questionId));
}
else if(data.status == "1"){
object.attr("src", "/content/images/vote-favorite-off.png");
var fav = getFavoriteNumber();
fav.removeClass("my-favorite-number");
if(data.count == 0)
data.count = '';
fav.text(data.count);
}
else if(data.success == "1"){
object.attr("src", "/content/images/vote-favorite-on.png");
var fav = getFavoriteNumber();
fav.text(data.count);
fav.addClass("my-favorite-number");
}
else{
showMessage(object, data.message);
}
};
var callback_vote = function(object, voteType, data){
if(data.allowed == "0" && data.success == "0"){
showMessage(object, voteAnonymousMessage.replace("{{QuestionID}}", questionId));
}
else if(data.allowed == "-3"){
showMessage(object, voteRequiredMoreVotes);
}
else if(data.allowed == "-2"){
if(voteType == VoteType.questionUpVote || voteType == VoteType.answerUpVote){
showMessage(object, upVoteRequiredScoreMessage);
}
else if(voteType == VoteType.questionDownVote || voteType == VoteType.answerDownVote){
showMessage(object, downVoteRequiredScoreMessage);
}
}
else if(data.allowed == "-1"){
showMessage(object, voteOwnDeniedMessage);
}
else if(data.status == "2"){
showMessage(object, voteDenyCancelMessage);
}
else if(data.status == "1"){
setVoteImage(voteType, true, object);
setVoteNumber(object, data.count);
}
else if(data.success == "1"){
setVoteImage(voteType, false, object);
setVoteNumber(object, data.count);
if(data.message.length > 0)
showMessage(object, data.message);
}
};
var callback_offensive = function(object, voteType, data){
object = $(object);
if(data.allowed == "0" && data.success == "0"){
showMessage(object, offensiveAnonymousMessage.replace("{{QuestionID}}", questionId));
}
else if(data.allowed == "-3"){
showMessage(object, offensiveNoFlagsLeftMessage);
}
else if(data.allowed == "-2"){
showMessage(object, offensiveNoPermissionMessage);
}
else if(data.status == "1"){
showMessage(object, offensiveTwiceMessage);
}
else if(data.success == "1"){
$(object).children('span[class=darkred]').text("("+ data.count +")");
}
};
var callback_remove = function(object, voteType, data){
if(data.allowed == "0" && data.success == "0"){
showMessage(object, removeAnonymousMessage.replace("{{QuestionID}}", questionId));
}
else if (data.success == "1"){
if (removeActionType == 'delete'){
postNode.addClass('deleted');
postRemoveLink.innerHTML = $.i18n._('undelete');
showMessage(object, deletedMessage);
}
else if (removeActionType == 'undelete') {
postNode.removeClass('deleted');
postRemoveLink.innerHTML = $.i18n._('delete');
showMessage(object, recoveredMessage);
}
}
};
return {
init : function(qId, questionAuthor, userId){
questionId = qId;
questionAuthorId = questionAuthor;
currentUserId = userId;
bindEvents();
},
// Accept answer public function
accept: function(object){
postId = object.attr("id").substring(imgIdPrefixAccept.length);
submit(object, VoteType.acceptAnswer, callback_accept);
},
favorite: function(object){
if(!currentUserId || currentUserId.toUpperCase() == "NONE"){
showMessage(object, favoriteAnonymousMessage.replace("{{QuestionID}}", questionId));
return false;
}
submit(object, VoteType.favorite, callback_favorite);
},
vote: function(object, voteType){
if(!currentUserId || currentUserId.toUpperCase() == "NONE"){
showMessage(object, voteAnonymousMessage.replace("{{QuestionID}}", questionId));
return false;
}
if(voteType == VoteType.answerUpVote){
postId = object.attr("id").substring(imgIdPrefixAnswerVoteup.length);
}
else if(voteType == VoteType.answerDownVote){
postId = object.attr("id").substring(imgIdPrefixAnswerVotedown.length);
}
submit(object, voteType, callback_vote);
},
offensive: function(object, voteType){
if(!currentUserId || currentUserId.toUpperCase() == "NONE"){
showMessage($(object), offensiveAnonymousMessage.replace("{{QuestionID}}", questionId));
return false;
}
if(confirm(offensiveConfirmation)){
postId = object.id.substr(object.id.lastIndexOf('-') + 1);
submit(object, voteType, callback_offensive);
}
},
remove: function(object, voteType){
if(!currentUserId || currentUserId.toUpperCase() == "NONE"){
showMessage($(object), removeAnonymousMessage.replace("{{QuestionID}}", questionId));
return false;
}
if(confirm(removeConfirmation)){
bits = object.id.split('-');
postId = bits.pop();/* this seems to be used within submit! */
postType = bits.shift();
if (postType == 'answer'){
postNode = $('#answer-container-' + postId);
postRemoveLink = object;
if (postNode.hasClass('deleted')){
removeActionType = 'undelete';
}
else {
removeActionType = 'delete';
}
}
submit($(object), voteType, callback_remove);
}
}
}
} ();
// site comments
function createComments(type) {
var objectType = type;
var jDivInit = function(id) {
return $("#comments-" + objectType + '-' + id);
};
var appendLoaderImg = function(id) {
appendLoader("#comments-" + objectType + '-' + id + " div.comments");
};
var canPostComments = function(id, jDiv) {
var jHidden = jDiv.siblings("#can-post-comments-" + objectType + '-' + id);
return jHidden.val().toLowerCase() == "true";
};
var renderForm = function(id, jDiv) {
var formId = "form-comments-" + objectType + "-" + id;
if (canPostComments(id, jDiv)) {
if (jDiv.find("#" + formId).length == 0) {
var form = '';
jDiv.append(form);
setupFormValidation("#" + formId,
{ comment: { required: true, minlength: 10} }, '',
function() { postComment(id, formId); });
}
}
else {
var divId = "comments-rep-needed-" + objectType + '-' + id;
if (jDiv.find("#" + divId).length == 0) {
jDiv.append('