-
Notifications
You must be signed in to change notification settings - Fork 595
/
Copy pathtypecho-ajax-comm.php
executable file
·161 lines (142 loc) · 7.21 KB
/
typecho-ajax-comm.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
$options = Helper::options();
$pic_url = $options->pluginUrl . "/AjaxComments/";
$config = $options->plugin('AjaxComments');
echo "\n";
?>
<style type="text/css">
#ajax_comment_error, #ajax_comment_loading, #ajax_comment_success {
padding: 3px 5px 3px 10px;
font-size: 14px;
margin: 8px 0;
width: 215px;
-moz-border-radius: 4px;
-webkit-border-radius: 4px;
-khtml-border-radius: 4px;
border-radius: 4px;
}
#ajax_comment_error{border:1px solid #fbc2c4;background:#fbe3e4;color:#8A1F11}
#ajax_comment_loading{background:#fff6bf;border:1px solid #ffd324;color:#514721}
#ajax_comment_success{background:#e6efc2;border:1px solid #c6d880;color:#264409}
</style>
<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>