/* $Id: class_comments.js 157 2009-04-08 01:58:38Z nico-izo $ */
// This file will no longer be included by default. A minified version is included as part of core-min.js
// Required language variables: 39,155,175,182,183,184,185,187,784,787,829,830,831,832,833,834,835,854,856,891,1025,1026,1032,1034,1071
SocialEngineAPI.Comments = new Class({
// Class
Implements: [Options],
// Properties
Base: {},
page: 1,
total: 0,
changed: false,
isEditing: false,
options: {
// Controls ajax request options
'ajaxURL' : 'misc_js.php',
'ajaxMethod' : 'post',
'ajaxSecure' : false,
// Can the viewer comment
'canComment' : false,
'commentHTML' : false,
'commentCode' : false,
// Controls the height of the comment box
'originalHeight' : 70,
// The type of comment ex. 'media'
'type' : false,
// The column that identifies the type ex. 'media_id'
'typeIdentifier' : false,
// The actual ID of the object commented on
'typeID' : false,
// Pagination Info
'paginate' : false,
'cpp' : false,
// Comment links
'commentLinks' : {'reply' : false, 'walltowall' : false},
// Some other stuff to identify the object
'object_owner' : false,
'object_owner_id' : false,
'typeTab' : false,
'typeCol' : false,
'typeTabParent' : false,
'typeColParent' : false,
'typeChild' : false
},
// Methods
initialize: function(options)
{
this.setOptions(options);
if( this.options.initialTotal ) this.total = this.options.initialTotal;
var bind = this;
window.addEvent('domready', function()
{
bind.showPostComment();
bind.options.originalHeight = textarea_autogrow('comment_body');
bind.getComments(1);
});
},
showPostComment: function()
{
var innerHTML = "";
// POST COMMENT
innerHTML +=
"
";
if( this.options.canComment )
{
innerHTML +=
"";
}
// DELETE COMMENT
innerHTML +=
'';
var postCommentContainerElement = $(this.options.type + '_' + this.options.typeID + '_postcomment');
postCommentContainerElement.innerHTML = innerHTML;
// Add events
var bind = this;
if( this.options.canComment )
{
postCommentContainerElement.getElement('form').addEvent('submit', function(event) { bind.checkText(event); });
postCommentContainerElement.getElement('textarea').addEvent('focus', function() { bind.removeText(this); });
postCommentContainerElement.getElement('textarea').addEvent('blur', function() { bind.addText(this); });
// New ajax-only functionality
postCommentContainerElement.getElement('form').addEvent('submit', function(event) { bind.doCommentPost(event); });
}
},
doCommentPost: function(e)
{
var event = new Event(e);
// Build data
var postData = {
'task' : 'comment_post',
'type' : this.options.type,
'iden' : this.options.typeIdentifier,
'value' : this.options.typeID,
'tab' : this.options.typeTab,
'col' : this.options.typeCol
};
if( this.options.typeTabParent )
postData.tab_parent = this.options.typeTabParent;
if( this.options.typeColParent )
postData.col_parent = this.options.typeColParent;
if( this.options.typeChild )
postData.child = this.options.typeChild;
if( this.options.object_owner && this.options.object_owner_id )
{
postData.object_owner = this.options.object_owner;
postData.object_owner_id = this.options.object_owner_id;
}
else
{
postData.user = this.Base.Owner.user_info.user_username;
}
if( $type(document.comment_post_form.comment_body) )
postData.comment_body = document.comment_post_form.comment_body.value;
if( $type(document.comment_post_form.comment_secure) )
postData.comment_secure = document.comment_post_form.comment_secure.value;
var bind = this;
var request = new Request.JSON({
'url' : this.options.ajaxURL,
'method' : this.options.ajaxMethod,
'secure' : this.options.ajaxSecure,
'data' : postData,
'onComplete' : function(responseObject, responseText)
{
bind.addComment(responseObject.is_error, responseObject.comment_body, responseObject.comment_date);
}
}).send();
// stop form submission
event.stop();
},
doCommentEdit: function()
{
//var event = new Event(e);
// Build data
var postData = {
'task' : 'comment_edit',
'type' : this.options.type,
'iden' : this.options.typeIdentifier,
'value' : this.options.typeID,
'user' : this.Base.Owner.user_info.user_username
};
if( $type(document.editCommentForm.comment_id) )
postData.comment_id = document.editCommentForm.comment_id.value;
if( $type(document.editCommentForm.comment_edit) )
postData.comment_edit = document.editCommentForm.comment_edit.value;
var bind = this;
var request = new Request.JSON({
'url' : this.options.ajaxURL,
'method' : this.options.ajaxMethod,
'secure' : this.options.ajaxSecure,
'data' : postData,
'onComplete' : function(responseObject, responseText)
{
bind.getComments();
}
}).send();
// stop form submission
//event.stop();
},
doCommentDelete: function(e, commentID)
{
var event = new Event(e);
// Build data
var postData = {
'task' : 'comment_delete',
'comment_id' : commentID,
'type' : this.options.type,
'iden' : this.options.typeIdentifier,
'value' : this.options.typeID,
'tab' : this.options.typeTab,
'col' : this.options.typeCol,
'user' : this.Base.Owner.user_info.user_username
};
if( this.options.typeTabParent )
postData.tab_parent = this.options.typeTabParent;
if( this.options.typeColParent )
postData.col_parent = this.options.typeColParent;
if( this.options.typeChild )
postData.child = this.options.typeChild;
if( this.options.object_owner )
postData.object_owner = this.options.object_owner;
if( this.options.object_owner_id )
postData.object_owner_id = this.options.object_owner_id;
if( $type(document.commentDeleteForm.comment_body) )
postData.comment_body = document.commentDeleteForm.comment_body.value;
if( $type(document.commentDeleteForm.comment_secure) )
postData.comment_secure = document.commentDeleteForm.comment_secure.value;
// Send data
var bind = this;
var request = new Request.JSON({
'url' : this.options.ajaxURL,
'method' : this.options.ajaxMethod,
'secure' : this.options.ajaxSecure,
'data' : postData,
'onComplete' : function(responseObject, responseText)
{
bind.getComments();
}
}).send();
// stop form submission
event.stop();
},
getComments: function(direction)
{
if( direction=='next' )
this.page++;
else if( direction=='previous' )
this.page--;
else if( $type(direction) )
this.page = direction;
if( this.options.paginate ) {
window.scroll(0,0);
} else {
this.options.cpp = this.total;
}
if( this.options.object_owner && this.options.object_owner_id )
{
var object_owner = this.options.object_owner;
var object_owner_id = this.options.object_owner_id;
var user = '';
} else {
var object_owner = '';
var object_owner_id = '';
var user = this.Base.Owner.user_info.user_username;
}
// AJAX
var bind = this;
var request = new Request.JSON({
'url' : this.options.ajaxURL,
'method' : this.options.ajaxMethod,
'secure' : this.options.ajaxSecure,
'data' : {
'task' : 'comment_get',
'user' : user,
'object_owner' : object_owner,
'object_owner_id' : object_owner_id,
'type' : this.options.type,
'iden' : this.options.typeIdentifier,
'value' : this.options.typeID,
'cpp' : this.options.cpp,
'p' : this.page
},
'onComplete' : function(responseObject, responseText)
{
bind.updateComments(responseObject);
}
});
request.send();
},
// THIS FUNCTION UPDATES THE COMMENTS
updateComments: function(responseObject)
{
if( $type(responseObject)!="object" )
{
alert('There was an error processing the request.');
return false;
}
// Prepare
this.total = parseInt(responseObject.total_comments) || 0;
this.page = responseObject.p;
var maxpage = responseObject.maxpage;
var p_start = responseObject.p_start;
var p_end = responseObject.p_end;
var totalCommentElement = $(this.options.type + '_' + this.options.typeID + '_totalcomments');
var commentContainerElement = $(this.options.type + '_' + this.options.typeID + '_comments');
var comments = $H(responseObject.comments);
// UPDATE TOTAL COMMENTS AND PAGE VARS
totalCommentElement.innerHTML = this.total;
// CREATE DIV TO HOLD COMMENT BODY FOR CLEANING
var commentBodyDiv = document.createElement('div');
// EMPTY CONTAINER
commentContainerElement.empty();
// LOOP OVER COMMENTS
var bind = this;
if(bind.Base.Core.settings.setting_url) { var querySeparator = '?'; } else { var querySeparator = '&'; }
comments.each(function(commentObject, commentID)
{
var newComment = new Element('div', {
'id' : 'comment_' + commentID
});
// BUILD COMMENT
var newCommentInnerHTML = "";
// AUTHOR PHOTO
if( commentObject.comment_authoruser_id && commentObject.comment_authoruser_exists )
newCommentInnerHTML += "
";
else
newCommentInnerHTML += "
";
newCommentInnerHTML += "
";
// AUTHOR NAME/LINK
if( !commentObject.comment_authoruser_id )
newCommentInnerHTML += "";
else if( !commentObject.comment_authoruser_exists )
newCommentInnerHTML += "";
else
newCommentInnerHTML += "";
// COMMENT DATE
newCommentInnerHTML += "";
// COMMENT BODY
newComment.setProperty('html', commentObject.comment_body);
newCommentInnerHTML += "
" + commentObject.comment_body + "
";
newCommentInnerHTML += "
";
// ADD NEW INNERHTML
newComment.setProperty('html', newCommentInnerHTML);
newComment.inject(commentContainerElement);
// ADD EVENTS
if( newComment.getElement('.commentEditLink') ) newComment.getElement('.commentEditLink').addEvent('click', function()
{
bind.editComment(commentID);
});
if( newComment.getElement('.commentDeleteLink') ) newComment.getElement('.commentDeleteLink').addEvent('click', function()
{
bind.confirmDelete(commentID);
});
});
// CREATE PAGINATION DIV
if(this.options.paginate && this.total > this.options.cpp) {
var commentPaginationTop = new Element('div', {'styles': {'text-align' : 'center'}});
var commentPaginationBottom = new Element('div', {'styles': {'text-align' : 'center'}});
if(this.page > 1) {
var paginationHTMLTop = "";
var paginationHTMLBottom = "";
} else {
var paginationHTMLTop = "« " + bind.Base.Language.Translate(182) + "";
var paginationHTMLBottom = "« " + bind.Base.Language.Translate(182) + "";
}
if(p_start == p_end) {
paginationHTMLTop += " | " + this.Base.Language.TranslateFormatted(184, [p_start, this.total]) + " | ";
paginationHTMLBottom += " | " + this.Base.Language.TranslateFormatted(184, [p_start, this.total]) + " | ";
} else {
paginationHTMLTop += " | " + this.Base.Language.TranslateFormatted(185, [p_start, p_end, this.total]) + " | ";
paginationHTMLBottom += " | " + this.Base.Language.TranslateFormatted(185, [p_start, p_end, this.total]) + " | ";
}
if(this.page != maxpage) {
paginationHTMLTop += "";
paginationHTMLBottom += "";
} else {
paginationHTMLTop += "" + bind.Base.Language.Translate(183) + " »";
paginationHTMLBottom += "" + bind.Base.Language.Translate(183) + " »";
}
commentPaginationTop.setProperty('html', paginationHTMLTop);
commentPaginationBottom.setProperty('html', paginationHTMLBottom);
commentPaginationTop.inject(commentContainerElement, 'top');
commentPaginationBottom.inject(commentContainerElement);
// ADD EVENTS
if( commentPaginationTop.getElement('a[id=comment_last_page_top]') ) commentPaginationTop.getElement('a[id=comment_last_page_top]').addEvent('click', function()
{
bind.getComments('previous');
});
if( commentPaginationBottom.getElement('a[id=comment_last_page_bottom]') ) commentPaginationBottom.getElement('a[id=comment_last_page_bottom]').addEvent('click', function()
{
bind.getComments('previous');
});
if( commentPaginationTop.getElement('a[id=comment_next_page_top]') ) commentPaginationTop.getElement('a[id=comment_next_page_top]').addEvent('click', function()
{
bind.getComments('next');
});
if( commentPaginationBottom.getElement('a[id=comment_next_page_bottom]') ) commentPaginationBottom.getElement('a[id=comment_next_page_bottom]').addEvent('click', function()
{
bind.getComments('next');
});
}
},
// Adds a comment
addComment: function(is_error, comment_body, comment_date)
{
if( !this.options.canComment )
return false;
if( is_error )
{
$('comment_error').style.display = 'block';
if( !comment_body.trim() )
{
this.addText($('comment_body'));
$('comment_error').innerHTML = this.Base.Language.Translate(831);
}
else
{
$('comment_error').innerHTML = this.Base.Language.Translate(832);
}
$('comment_submit').value = this.Base.Language.Translate(833);
$('comment_submit').disabled = false;
}
else
{
$('comment_error').style.display = 'none';
$('comment_error').innerHTML = '';
$('comment_body').value = '';
$('comment_body').style.height = this.options.originalHeight + 'px';
this.addText($('comment_body'));
$('comment_submit').value = this.Base.Language.Translate(833);
$('comment_submit').disabled = false;
if( $('comment_secure') )
{
$('comment_secure').value = '';
$('secure_image').src = $('secure_image').src + '?' + (new Date()).getTime();
}
// INPUT COMMENTS
this.page = 1;
this.total++;
this.getComments();
}
},
editComment: function(commentID)
{
var bind = this;
if( this.isEditing ) return false;
this.isEditing = true;
//var commentContainerElement = $(this.options.type + '_' + this.options.typeID + '_comments');
//var commentElement = commentContainerElement.getElement('profile_comment_body_' + commentID);
var commentElement = $('profile_comment_body_' + commentID);
var height = commentElement.offsetHeight + 10;
var commentText = commentElement.innerHTML.replace(/
/gi, '\r\n').replace(/>/gi, '>');
var innerHTML = '';
innerHTML += "";
// Inject
commentElement.innerHTML = innerHTML;
textarea_autogrow('comment_edit_' + commentID);
$('comment_edit_' + commentID).focus();
// Add events
$('comment_edit_' + commentID).addEvent('blur', function()
{
bind.doCommentEdit();
bind.isEditing = false;
});
},
confirmDelete: function(commentID)
{
$('del_comment_id').value = commentID;
TB_show(this.Base.Language.Translate(1025), '#TB_inline?height=100&width=300&inlineId=confirmcommentdelete', '', '../images/trans.gif');
var bind = this;
$('TB_window').getElement('form').name = 'commentDeleteForm';
$('TB_window').getElement('form').addEvent('submit', function(event) { bind.doCommentDelete(event, commentID); });
},
// UI Methods
removeText: function(commentBody)
{
if( !this.changed )
{
commentBody.value = '';
commentBody.style.color = '#000000';
this.changed = true;
}
},
addText: function(commentBody)
{
if( !commentBody.value.trim() )
{
commentBody.value = this.Base.Language.Translate(829);
commentBody.style.color = '#888888';
this.changed = false;
}
},
checkText: function(event)
{
if( !this.changed )
$('comment_body').value='';
$('comment_submit').value = this.Base.Language.Translate(830);
$('comment_submit').disabled = true;
}
});