Skip to content

Commit

Permalink
1.2.0 for typecho 1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
byends committed May 30, 2015
1 parent e4630b2 commit 6f4080e
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 140 deletions.
7 changes: 5 additions & 2 deletions Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
* @package AjaxComments
* @link http://www.byends.com
* @author Byends Upgrade
* @version 1.1.1
* @version 1.2.0
* @oriAuthor willin(http://kan.willin.org/typecho/)
*
* 原作者是 willin (http://kan.willin.org/typecho/),请尊重版权
*
* 1.2.0 update by Byends at 2015-05-30
* 升级兼容 typecho 1.0,此次升级不向下兼容,typecho 1.0 以下版本请使用 AjaxComments 1.1.1
*
* 1.1.1 update by Byends at 2014-001-21
* 1.1.1 update by Byends at 2014-01-21
* 修复由于 jQuery $ 对象被某些插件二货制作者无故释放导致插件无法使用的BUG
*
* 1.1.0 update by Byends at 2012-07-13
Expand Down
278 changes: 140 additions & 138 deletions typecho-ajax-comm.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,141 +19,143 @@
#ajax_comment_loading{background:#fff6bf;border:1px solid #ffd324;color:#514721}
#ajax_comment_success{background:#e6efc2;border:1px solid #c6d880;color:#264409}
</style>
<script type="text/javascript">
var
loading_dom = '<div id="ajax_comment_loading"><?php echo $config->loadingLan; ?><\/div><div id="ajax_comment_error"><\/div>',
success_dom = '<div id="ajax_comment_success"><?php echo $config->subSuccess; ?><\/div>',
name_err = '<?php echo $config->errUsername; ?>',
email_err = '<?php echo $config->errEmail; ?>',
rule_err = '<?php echo $config->errRuleEmail; ?>',
text_err = '<?php echo $config->errText; ?>';

jQuery(document).ready(function() {
$body = (window.opera) ? (document.compatMode == "CSS1Compat" ? jQuery('html') : jQuery('body')) : jQuery('html,body');

var
wait_time = '<?php echo $options->commentsPostIntervalEnable * $options->commentsPostInterval; ?>',
comments_order = '<?php echo $options->commentsOrder; ?>',
_comments = '<?php echo $config->_comments; if (strpos($config->_comments, " ")) echo ":first"; ?>',
_comment_list = '<?php echo strtr( $config->_comment_list, array( '.' => 'class="', '#' => 'id="')). '"'; ?>',
_comment_reply = '<?php echo $config->_comment_reply; ?>',
_comment_form = '<?php echo $config->_comment_form; ?>',
_respond = '<?php echo $config->_respond; ?>',
_textarea = '<?php echo $config->_textarea; ?>',
_submit = '<?php echo $config->_submit; ?>',

comments = jQuery(_comments),
comment_form = jQuery(_comment_form),
respond = jQuery(_respond),
textarea = jQuery(_comment_form +' '+ _textarea),
$submit = jQuery(_comment_form +' '+ _submit),
parent = '';
textarea.parent().before(loading_dom);
jQuery('#ajax_comment_loading').hide();
err = jQuery('#ajax_comment_error').hide();
$submit.attr('disabled', false);

click_bind();

comment_form.submit(function() {
err.empty().hide();
$submit.attr('disabled', true).fadeTo('slow', 0.5);

/* check */
if(comment_form.find('#author')[0]) {
if(comment_form.find('#author').val() == '') {
err.html(name_err);
err_effect(); return false;
}
if(comment_form.find('#mail').val() == '') {
err.html(email_err);
err_effect(); return false;
}
var filter = /^[^@\s<&>]+@([-a-z0-9]+\.)+[a-z]{2,4}$/i;
if(!filter.test(comment_form.find('#mail').val())) {
err.html(rule_err);
err_effect(); return false;
}
}
if(comment_form.find(_textarea).val() == '') {
err.html(text_err);
err_effect(); return false;
}

jQuery('#ajax_comment_loading').slideDown();

jQuery.ajax({
url: jQuery(this).attr('action'),
type: jQuery(this).attr('method'),
data: jQuery(this).serialize(),
dataType: 'text',
success: function(data) {
jQuery('#ajax_comment_loading').slideUp();

try {
if (data.indexOf(_comment_list) == -1){ // return error
var msg = data.match(/<\/h1>([\s\S]+?)<\/div>/);
err.html($.trim(msg[1]));
err_effect();
return false;
} else {
pos = data.indexOf('<li id="' + parent);
data = data.substring(pos, data.indexOf('form', pos));
$trgs = data.match(/comment-(\d+)/g);
len = $trgs.length; new_id = 0;
for (var i = 0; i < len; i++) { // find new id
trg = $trgs[i].match(/\d+/); if (Number(trg) > Number(new_id)) new_id = trg;
}

pos = data.indexOf('<li id="comment-' + new_id);
data = data.substring(pos, data.indexOf('<\/li>', pos)) + success_dom + '<\/li>'; //get new comment

data = parent != '' ? '<div class="comment-children"><ol class="comment-list">' + data + '</ol></div>' : data;
parent != '' ? ( jQuery('#' + parent + ' li').length && comments_order == 'DESC' ? jQuery('#' + parent + ' li:first').before(data) : respond.before(data),
parent = '') : (respond.before('<ol '+ _comment_list + '>' + data + '<\/ol>'));

jQuery('#comment-' + new_id).hide().fadeIn(1000);
comments.length ? ( n = parseInt(comments.text().match(/\d+/)), comments.text(comments.text().replace( n, n + 1 ))) : 0;

$body.animate({ scrollTop: jQuery('#comment-' + new_id).offset().top - 200 }, 900);
jQuery('textarea').each(function() {this.value = ''});
setTimeout(function() {jQuery('#ajax_comment_success').slideUp();}, 2000);
}
} catch (e) {
alert('Error!\n\n' + e );
}

TypechoComment.cancelReply();
jQuery(_comment_reply + ' a, #cancel-comment-reply-link').unbind('click'); click_bind();//new comment bind
jQuery('#author').length ? countdown() : $submit.attr('disabled', false).fadeTo('slow', 1);
}
});

return false;

});// end comment_form.submit()

function click_bind() { // bind
jQuery(_comment_reply + ' a').click(function() { // replay
$body.animate({ scrollTop: respond.offset().top - 180 }, 400);
h = jQuery(this)[0].href;
parent = 'comment-' + h.substring(h.indexOf('replyTo=') + 8, h.indexOf('#'));
textarea.focus();
});
jQuery('#cancel-comment-reply-link').click(function() { // cancle
parent = '';
});
}

function err_effect() { // error
err.slideDown();
setTimeout(function() {$submit.attr('disabled', false).fadeTo('', 1); err.slideUp();}, 2000);
}

var wait = wait_time, submit_val = comment_form.find(_submit).val();
function countdown() {
wait > 0 ? ($submit.val(wait), wait--, setTimeout(countdown, 1000))
: ($submit.val(submit_val).attr('disabled', false).fadeTo('slow', 1), wait = wait_time);
}
});
</script>
<script>
(function($){
var loading_dom = '<div id="ajax_comment_loading"><?php echo $config->loadingLan; ?></div><div id="ajax_comment_error"></div>',
success_dom = '<div id="ajax_comment_success"><?php echo $config->subSuccess; ?></div>',
name_err = '<?php echo $config->errUsername; ?>',
email_err = '<?php echo $config->errEmail; ?>',
rule_err = '<?php echo $config->errRuleEmail; ?>',
text_err = '<?php echo $config->errText; ?>';

$(document).ready(function() {
$body = (window.opera) ? (document.compatMode == "CSS1Compat" ? $('html') : $('body')) : $('html,body');

var
wait_time = '<?php echo $options->commentsPostIntervalEnable * $options->commentsPostInterval; ?>',
comments_order = '<?php echo $options->commentsOrder; ?>',
_comments = '<?php echo $config->_comments; if (strpos($config->_comments, " ")) echo ":first"; ?>',
_comment_list = '<?php echo strtr( $config->_comment_list, array( '.' => 'class="', '#' => 'id="')). '"'; ?>',
_comment_reply = '<?php echo $config->_comment_reply; ?>',
_comment_form = '<?php echo $config->_comment_form; ?>',
_respond = '<?php echo $config->_respond; ?>',
_textarea = '<?php echo $config->_textarea; ?>',
_submit = '<?php echo $config->_submit; ?>',

comments = $(_comments),
comment_form = $(_comment_form),
respond = $(_respond),
textarea = $(_comment_form +' '+ _textarea),
$submit = $(_comment_form +' '+ _submit),
parent = '';
textarea.parent().before(loading_dom);
$('#ajax_comment_loading').hide();
err = $('#ajax_comment_error').hide();
$submit.attr('disabled', false);

click_bind();

comment_form.submit(function() {
err.empty().hide();
$submit.attr('disabled', true).fadeTo('slow', 0.5);

/* check */
if(comment_form.find('#author')[0]) {
if(comment_form.find('#author').val() == '') {
err.html(name_err);
err_effect(); return false;
}
if(comment_form.find('#mail').val() == '') {
err.html(email_err);
err_effect(); return false;
}
var filter = /^[^@\s<&>]+@([-a-z0-9]+\.)+[a-z]{2,4}$/i;
if(!filter.test(comment_form.find('#mail').val())) {
err.html(rule_err);
err_effect(); return false;
}
}
if(comment_form.find(_textarea).val() == '') {
err.html(text_err);
err_effect(); return false;
}

$('#ajax_comment_loading').slideDown();

$.ajax({
url: $(this).attr('action'),
type: $(this).attr('method'),
data: $(this).serialize(),
dataType: 'text',
success: function(data) {
$('#ajax_comment_loading').slideUp();

try {
var pos = data.indexOf("<title>Error");
if (pos !== 0 && pos < 100) { // return error
var msg = data.match(/<div class="container">([\s\S]+?)<\/div>/);
err.html($.trim(msg[1]));
err_effect();
return false;
} else {
pos = data.indexOf('<li id="' + parent);
data = data.substring(pos, data.indexOf('form', pos));
$trgs = data.match(/comment-(\d+)/g);
len = $trgs.length; new_id = 0;
for (var i = 0; i < len; i++) { // find new id
trg = $trgs[i].match(/\d+/); if (Number(trg) > Number(new_id)) new_id = trg;
}

pos = data.indexOf('<li id="comment-' + new_id);
data = data.substring(pos, data.indexOf('<\/li>', pos)) + success_dom + '<\/li>'; //get new comment

data = parent != '' ? '<div class="comment-children"><ol class="comment-list">' + data + '</ol></div>' : data;
parent != '' ? ( $('#' + parent + ' li').length && comments_order == 'DESC' ? $('#' + parent + ' li:first').before(data) : respond.before(data),
parent = '') : (respond.before('<ol '+ _comment_list + '>' + data + '<\/ol>'));

$('#comment-' + new_id).hide().fadeIn(1000);
comments.length ? ( n = parseInt(comments.text().match(/\d+/)), comments.text(comments.text().replace( n, n + 1 ))) : 0;

$body.animate({ scrollTop: $('#comment-' + new_id).offset().top - 200 }, 900);
$('textarea').each(function() {this.value = ''});
setTimeout(function() {$('#ajax_comment_success').slideUp();}, 2000);
}
} catch (e) {
alert('Error!\n\n' + e );
}

TypechoComment.cancelReply();
$(_comment_reply + ' a, #cancel-comment-reply-link').unbind('click'); click_bind();//new comment bind
$('#author').length ? countdown() : $submit.attr('disabled', false).fadeTo('slow', 1);
}
});

return false;

});// end comment_form.submit()

function click_bind() { // bind
$(_comment_reply + ' a').click(function() { // replay
$body.animate({ scrollTop: respond.offset().top - 180 }, 400);
h = $(this)[0].href;
parent = 'comment-' + h.substring(h.indexOf('replyTo=') + 8, h.indexOf('#'));
textarea.focus();
});
$('#cancel-comment-reply-link').click(function() { // cancle
parent = '';
});
}

function err_effect() { // error
err.slideDown();
setTimeout(function() {$submit.attr('disabled', false).fadeTo('', 1); err.slideUp();}, 2000);
}

var wait = wait_time, submit_val = comment_form.find(_submit).val();
function countdown() {
wait > 0 ? ($submit.val(wait), wait--, setTimeout(countdown, 1000))
: ($submit.val(submit_val).attr('disabled', false).fadeTo('slow', 1), wait = wait_time);
}
});
})(jQuery)
</script>

0 comments on commit 6f4080e

Please sign in to comment.