This repository has been archived by the owner on Nov 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcp-ajax-comments-page.dev.js
448 lines (282 loc) · 8.22 KB
/
cp-ajax-comments-page.dev.js
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/*
===============================================================
CommentPress AJAX Comment Submission (in page)
===============================================================
AUTHOR : Christian Wach <[email protected]>
LAST MODIFIED : 25/07/2012
DEPENDENCIES : jquery.js
---------------------------------------------------------------
NOTES
This script enables AJAX comment posting when the comment list is in the
main content area and when the CommentPress theme is active.
Based loosely on the 'Ajax Comment Posting' WordPress plugin (version 2.0)
---------------------------------------------------------------
*/
// test for our localisation object
if ( 'undefined' !== typeof CommentpressAjaxSettings ) {
// reference our object vars
var cpac_live = CommentpressAjaxSettings.cpac_live;
var cpac_ajax_url = CommentpressAjaxSettings.cpac_ajax_url;
var cpac_spinner_url = CommentpressAjaxSettings.cpac_spinner_url;
var cpac_post_id = CommentpressAjaxSettings.cpac_post_id;
}
// init submitting flag
var cpac_submitting = false;
/**
* @description: define what happens when the page is ready
* @todo:
*
*/
jQuery(document).ready(function($) {
/* cpac_lang[]:
[0]: 'Loading...'
[1]: 'Please enter your name.'
[2]: 'Please enter your email address.'
[3]: 'Please enter a valid email address.'
[4]: 'Please enter your comment'
[5]: 'Your comment has been added.'
[6]: 'AJAX error!'
*/
/**
* @description: init
* @todo:
*
*/
var form, err;
function cpac_initialise() {
jQuery('#respond_title').after(
'<div id="error" style="background: #761d19; margin: 0 0 0.4em 0; padding: 0.4em; -moz-border-radius: 6px; -khtml-border-radius: 6px; -webkit-border-radius: 6px; border-radius: 6px; color: #fff; line-height: 1.3em;"></div>'
);
jQuery('#commentform').after(
'<img src="' + cpac_spinner_url + '" id="loading" alt="' + cpac_lang[0] + '" />'
);
jQuery('#loading').hide();
form = jQuery('#commentform');
err = jQuery('#error');
err.hide();
}
// do it
cpac_initialise();
/**
* @description: reset
* @todo:
*
*/
function cpac_reset() {
jQuery('#loading').hide();
jQuery('#submit').removeAttr("disabled");
jQuery('#submit').show();
addComment.enableForm();
cpac_submitting = false;
}
/**
* @description: add comment to page
* @todo:
*
*/
function cpac_add_comment( response ) {
// get comment parent
var comm_parent = form.find('#comment_parent').val();
// find the commentlist we want
var comm_list = jQuery('ol.commentlist:first');
// if the comment is a reply, append the comment to the children
if ( comm_parent != '0' ) {
//alert( comm_parent );
var parent_id = '#li-comment-' + comm_parent;
// find the child list we want
var child_list = jQuery(parent_id + ' > .children:first');
// is there a child list?
if (child_list[0]) {
cpac_nice_append(
response,
parent_id + ' .children:first > li:last',
child_list,
parent_id + ' .children:first > li:last'
);
} else {
cpac_nice_append(
response,
parent_id + ' .children:first',
parent_id,
parent_id + ' .children:first > li:last'
);
}
// if not, append the new comment at the bottom
} else {
// is there a comment list?
if (comm_list[0]) {
cpac_nice_append(
response,
'ol.commentlist:first > li:last',
comm_list,
'ol.commentlist:first > li:last'
);
} else {
cpac_nice_append(
response,
'ol.commentlist:first',
'div.comments_container',
'ol.commentlist:first > li:last'
);
}
}
// permalink clicks
cp_enable_comment_permalink_clicks();
// get head
var head = response.find('#comments_in_page_wrapper div.comments_container > h3');
// replace heading
jQuery('#comments_in_page_wrapper div.comments_container > h3').replaceWith(head);
// clear comment form
form.find('#comment').val('');
//err.html('<span class="success">' + cpac_lang[5] + '</span>');
}
/**
* @description: do comment append
* @todo:
*
*/
function cpac_nice_append( response, content, target, last ) {
// test for undefined, which may happen on replies to comments
// which have lost their original context
if ( response === undefined || response === null ) { return; }
response.find(content)
.hide()
.appendTo(target);
// clean up
cpac_cleanup( content, last );
}
/**
* @description: do comment prepend
* @todo:
*
*/
function cpac_nice_prepend( response, content, target, last ) {
// test for undefined, which may happen on replies to comments
// which have lost their original context
if ( response === undefined || response === null ) { return; }
response.find(content)
.hide()
.prependTo(target);
// clean up
cpac_cleanup( content, last );
}
/**
* @description: do comment cleanup
* @todo:
*
*/
function cpac_cleanup( content, last ) {
// get the id of the last list item
var last_id = jQuery(last).attr('id');
// construct new comment id
var new_comm_id = '#comment-' + last_id.split('-')[2];
var comment = jQuery(new_comm_id);
addComment.cancelForm();
comment.css('background', '#c2d8bc');
jQuery(content).slideDown('slow',
// animation complete
function() {
// scroll to new comment
jQuery.scrollTo(
comment,
{
duration: cp_scroll_speed,
axis: 'y',
offset: cp_get_header_offset(),
onAfter: function() {
// animate to white
comment.animate({ backgroundColor: "#ffffff" }, 1000, function () {
// then make transparent
comment.css('background', 'transparent');
});
}
}
);
}
); // end slide
}
/**
* @description: comment submission method
* @todo:
*
*/
jQuery('#commentform').on('submit', function(evt) {
// set global flag
cpac_submitting = true;
// hide errors
err.hide();
// if not logged in, validate name and email
if(form.find('#author')[0]) {
// check for name
if(form.find('#author').val() == '') {
err.html('<span class="error">' + cpac_lang[1] + '</span>');
err.show();
cpac_submitting = false;
return false;
}
// check for email
if(form.find('#email').val() == '') {
err.html('<span class="error">' + cpac_lang[2] + '</span>');
err.show();
cpac_submitting = false;
return false;
}
// validate email
var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
if(!filter.test(form.find('#email').val())) {
err.html('<span class="error">' + cpac_lang[3] + '</span>');
err.show();
if (evt.preventDefault) {evt.preventDefault();}
cpac_submitting = false;
return false;
}
} // end if
// test for tinyMCE
if ( cp_tinymce == '1' ) {
// set value of comment textarea to content
tinyMCE.triggerSave();
// unload tinyMCE
addComment.disableForm();
}
// check for comment
if(form.find('#comment').val() == '') {
err.html('<span class="error">' + cpac_lang[4] + '</span>');
err.show();
// reload tinyMCE
addComment.enableForm();
cpac_submitting = false;
return false;
}
// submit the form
jQuery(this).ajaxSubmit({
beforeSubmit: function() {
jQuery('#loading').show();
jQuery('#submit').attr('disabled','disabled');
jQuery('#submit').hide();
}, // end beforeSubmit
error: function(request) {
err.empty();
var data = request.responseText.match(/<p>(.*)<\/p>/);
err.html('<span class="error">' + data[1] + '</span>');
err.show();
cpac_reset();
return false;
}, // end error()
success: function(data) {
try {
// get our data as object
var response = jQuery(data);
//console.log( data );
// add comment
cpac_add_comment( response );
cpac_reset();
} catch (e) {
cpac_reset();
alert( cpac_lang[6] + '\n\n' + e );
} // end try
} // end success()
}); // end ajaxSubmit()
// --<
return false;
}); // end form.submit()
}); // end document.ready()