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.dev.js
1126 lines (707 loc) · 21.2 KB
/
cp-ajax-comments.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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
===============================================================
CommentPress AJAX Comment Submission
===============================================================
AUTHOR : Christian Wach <[email protected]>
LAST MODIFIED : 25/07/2012
DEPENDENCIES : jquery.js
---------------------------------------------------------------
NOTES
This script enables AJAX comment posting 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: an example ajax callback
*
*/
function cpac_ajax_callback( data ) {
// get diff
var diff = parseInt( data.cpac_comment_count ) - parseInt( CommentpressAjaxSettings.cpac_comment_count );
// did we get any new comments?
if ( diff > 0 ) {
// loop through them
for( var i = 1; i <= diff; i++ ) {
// get comment array (will rejig when I can find a way to pass nested arrays)
var comment = eval( 'data.' + 'cpac_new_comment_' + i );
// deal with each comment
cpac_add_new_comment( jQuery(comment.markup), comment.text_sig, comment.parent, comment.id );
// increment global
CommentpressAjaxSettings.cpac_comment_count++;
}
}
}
/**
* @description: add comment to page
* @todo:
*
*/
function cpac_add_new_comment( markup, text_sig, comm_parent, comm_id ) {
// get container
var comment_container = jQuery('div.comments_container');
// kick out if we have it already
if ( comment_container.find( '#li-comment-' + comm_id )[0] ) { return; }
// get useful ids
var para_id = '#para_wrapper-' + text_sig;
var head_id = '#para_heading-' + text_sig;
// find the commentlist we want
var comm_list = jQuery(para_id + ' 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 + ' > ol.children:first');
// is there a child list?
if ( child_list[0] ) {
markup.hide()
.css('background', '#c2d8bc')
.appendTo( child_list )
.slideDown( 'fast', function() {
// animate to white
markup.animate({ backgroundColor: "#ffffff" }, 1000, function () {
// then make transparent
markup.css('background', 'transparent');
});
});
} else {
markup.wrap( '<ol class="children" />' )
.parent()
.css('background', '#c2d8bc')
.hide()
.appendTo( parent_id )
.slideDown( 'fast', function() {
// animate to white
markup.parent().animate({ backgroundColor: "#ffffff" }, 1000, function () {
// then make transparent
markup.parent().css('background', 'transparent');
});
});
}
// if not, append the new comment at the bottom
} else {
// is there a comment list?
if ( comm_list[0] ) {
markup.hide()
.css('background', '#c2d8bc')
.appendTo( comm_list )
.slideDown( 'fast', function() {
// animate to white
markup.animate({ backgroundColor: "#ffffff" }, 1000, function () {
// then make transparent
markup.css('background', 'transparent');
});
});
} else {
markup.wrap( '<ol class="commentlist" />' )
.parent()
.css('background', '#c2d8bc')
.hide()
.prependTo( para_id )
.slideDown( 'fast', function() {
// animate to white
markup.parent().animate({ backgroundColor: "#ffffff" }, 1000, function () {
// then make transparent
markup.parent().css('background', 'transparent');
});
});
}
}
// get paragraph header link
var head = comment_container.find( head_id + ' a' );
// get text as array
var head_array = head.text().split(' ');
// get current comment number from paragraph header
var comment_num = parseInt( head_array[0] );
// increment
comment_num++;
// replace in array
head_array[0] = comment_num;
// make plural by default
head_array[1] = 'Comments';
// is the word 'comment' singular?
if ( comment_num == 1 ) {
// make singular
head_array[1] = 'Comment';
}
// replace head with new
head.text( head_array.join( ' ' ) );
// get existing bg (but interferes with animate below)
//var head_bg = head.css( 'backgroundColor' );
// highlight
head.css( 'background', '#c2d8bc' );
// animate to existing bg (from css file)
head.animate( { backgroundColor: '#EFEFEF' }, 1000 );
// cast comment num as string
var comment_num = comment_num.toString();
// get textblock id
var textblock_id = '#textblock-' + text_sig;
// get small tag
var small = textblock_id + ' span small';
// set comment icon text
jQuery(small).html( comment_num );
// if we're changing from 0 to 1...
if ( comment_num == '1' ) {
// set comment icon class
jQuery(textblock_id + ' span.commenticonbox a.para_permalink').addClass('has_comments');
// show it
jQuery(small).css( 'visibility', 'visible' );
}
// re-enable clicks
cp_enable_comment_permalink_clicks();
cp_setup_comment_headers();
}
/**
* @description: an example ajax update
*
*/
function cpac_ajax_update() {
// kick out if submitting a comment
if ( cpac_submitting ) { return; }
/*
// we can use this to log ajax errors from jQuery.post()
$(document).ajaxError( function( e, xhr, settings, exception ) {
alert( 'error in: ' + settings.url + ' \n'+'error:\n' + xhr.responseText );
});
*/
// use post method
jQuery.post(
// set URL
cpac_ajax_url,
// set method to call
{ action: 'cpac_get_new_comments',
// send last comment count
last_count: CommentpressAjaxSettings.cpac_comment_count,
// send post ID
post_id: cpac_post_id
},
// callback
function( data, textStatus ) {
//alert( data );
//alert( textStatus );
// if success
if ( textStatus == 'success' ) {
// call function
cpac_ajax_callback( data );
}
},
// expected format
'json'
);
}
/**
* @description: an example ajax updater
*
*/
function cpac_ajax_updater( toggle ) {
// if set
if ( toggle == '1' ) {
// NOTE: comment_flood_filter is set to 15000, so that's what we set here. this ain't chat :)
// if you want to change this to something more 'chat-like'...
// add this to your theme's functions.php or uncomment it in cp-ajax-comment.php:
// remove_filter('comment_flood_filter', 'wp_throttle_comment_flood', 10, 3);
// use at your own risk - it could be very heavy on the database.
// set repeat call
CommentpressAjaxSettings.interval = window.setInterval( cpac_ajax_update, 5000 );
} else {
// stop repeat
window.clearInterval( CommentpressAjaxSettings.interval );
}
}
/**
* @description: enable reassignment of comments
*
*/
function cpac_reassign_comments() {
// get all draggable items
var draggers = jQuery( '#comments_sidebar .comment-wrapper .comment-assign' );
// show them
draggers.show();
// remove draggability for repeated calls
draggers.draggable( 'destroy' );
// make comment reassign button draggable
draggers.draggable({
// a copy thereof...
helper: 'clone',
cursor: 'move'
});
// get all droppable items
var droppers = jQuery( '#content .post .textblock' );
// remove droppability for repeated calls
droppers.droppable( 'destroy' );
// make textblocks droppable
droppers.droppable({
// configure droppers
accept: '.comment-assign',
hoverClass: 'selected_para',
// when the button is dropped
drop: function( event, ui ) {
// get id of dropped-on item
var text_sig = jQuery( this ).attr('id').split('-')[1];
// create options for modal dialog
var options = {
resizable: false,
height: 160,
zIndex: 3999,
modal: true,
dialogClass: 'wp-dialog',
buttons: {
"Yes": function() {
// let's do it
jQuery( this ).dialog( "option", "disabled", true );
// clear buttons
jQuery( '.ui-dialog-buttonset' ).html(
'<img src="' + cpac_spinner_url + '" id="loading" alt="' + cpac_lang[0] + '" />'
);
// alert title
jQuery( '.ui-dialog-title' ).html( cpac_lang[9] );
// show message
jQuery( '.cp_alert_text' ).html( cpac_lang[10] );
// call function
cpac_reassign( text_sig, ui );
},
"Cancel": function() {
// cancel
jQuery( this ).dialog( 'close' );
jQuery( this ).dialog( 'destroy' );
jQuery( this ).remove();
}
}
};
// define message
var alert_text = cpac_lang[8];
// create modal dialog
var div = jQuery('<div><p class="cp_alert_text">' + alert_text + '</p></div>');
div.attr( 'title', cpac_lang[7] )
.appendTo( 'body' )
.dialog( options );
}
});
}
/**
* @description: reassign a comment
*
*/
function cpac_reassign( text_sig, ui ) {
// get comment id
var comment_id = jQuery( ui.draggable ).attr('id').split('-')[1];
// let's see what params we've got
//console.log( 'text_sig: ' + text_sig );
//console.log( 'comment id: ' + comment_id );
// get comment parent li
var comment_item = jQuery( ui.draggable ).closest( 'li.comment' );
// assign as comment to move
var comment_to_move = comment_item;
// get siblings
var other_comments = comment_item.siblings( 'li.comment' );
// are there any?
if ( other_comments.length == 0 ) {
// get comment list, because we need to remove the entire list
var comment_list = comment_item.parent( 'ol.commentlist' );
// overwrite comment to move
comment_to_move = comment_list;
}
// slide our comment up
jQuery( comment_to_move ).slideUp( 'slow',
// animation complete
function() {
/*
// find target paragraph wrapper
var para_wrapper = jQuery( '#para_wrapper-' + text_sig );
// get nested commentlist
var target_list = para_wrapper.children( 'ol.commentlist' );
// does it already have a commentlist?
if ( target_list.length > 0 ) {
// yes, append just the comment item to the list
comment_item.appendTo( target_list )
.css( 'display', 'block' )
.parent()
.css( 'display', 'block' );
} else {
// no, we must prepend the list, wrapping the item in one if necessary
// do we have the list defined?
if ( other_comments.length > 0 ) {
// no, wrap item in list, then prepend
comment_item.wrap( '<ol class="commentlist" />' )
.css( 'display', 'block' )
.parent()
.css( 'display', 'block' )
.prependTo( para_wrapper );
} else {
// prepend list
comment_item.css( 'display', 'block' )
.parent()
.css( 'display', 'block' )
.prependTo( para_wrapper );
}
// check
//console.log( para_wrapper );
}
*/
// use post
jQuery.post(
// set URL
cpac_ajax_url,
// set params
{ action: 'cpac_reassign_comment',
// send text sig
text_signature: text_sig,
// send post ID
comment_id: comment_id
},
// callback
function( data, textStatus ) {
//alert( data.msg );
//alert( textStatus );
// if success
if ( textStatus == 'success' ) {
// refresh from server
document.location.reload( true );
} else {
// show error
alert( textStatus );
}
},
// expected format
'json'
);
}
);
}
/**
* @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!'
*/
// trigger repeat calls
cpac_ajax_updater( cpac_live );
/**
* @description: ajax comment updating
* @todo:
*
jQuery('#btn_js').toggle( function() {
// trigger repeat calls
cpac_ajax_updater( false );
jQuery(this).text('Javascript Off');
return false;
}, function() {
// trigger repeat calls
cpac_ajax_updater( true );
jQuery(this).text('Javascript On');
return false;
});
*/
// enable comment reassignment
cpac_reassign_comments();
/**
* @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 form data
var text_sig = form.find('#text_signature').val();
var comm_parent = form.find('#comment_parent').val();
// get useful ids
var para_id = '#para_wrapper-' + text_sig;
var head_id = '#para_heading-' + text_sig;
// we no longer have zero comments
jQuery(para_id).removeClass( 'no_comments' );
// if the comment is a reply, append the comment to the children
if ( comm_parent != '0' ) {
// get parent
var parent_id = '#li-comment-' + comm_parent;
// find the child list we want
var child_list = jQuery(parent_id + ' > ol.children:first');
// is there a child list?
if ( child_list[0] ) {
cpac_nice_append(
response,
parent_id + ' > ol.children:first > li:last',
child_list,
parent_id + ' > ol.children:first > li:last'
);
} else {
cpac_nice_append(
response,
parent_id + ' > ol.children:first',
parent_id,
parent_id + ' > ol.children:first > li:last'
);
}
// if not, append the new comment at the bottom
} else {
// find the commentlist we want
var comm_list = jQuery(para_id + ' > ol.commentlist:first');
// is there a comment list?
if ( comm_list[0] ) {
cpac_nice_append(
response,
para_id + ' > ol.commentlist:first > li:last',
comm_list,
para_id + ' > ol.commentlist:first > li:last'
);
} else {
cpac_nice_prepend(
response,
para_id + ' > ol.commentlist:first',
para_id,
para_id + ' > ol.commentlist:first > li:last'
);
}
}
// slide up comment form
jQuery('#respond').slideUp( 'fast', function() {
// after slide
addComment.cancelForm();
});
// get paragraph header
var head = response.find(head_id);
// replace heading
jQuery(head_id).replaceWith(head);
// get comment number from paragraph header
var comment_num = head.text().split(' ')[0];
//alert(comment_num);
// replace paragraph icon
cpac_update_para_icon( text_sig, comment_num );
// re-enable clicks
cp_enable_comment_permalink_clicks();
cp_setup_comment_headers();
cpac_reassign_comments();
// 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);
// highlight it
comment.css('background', '#c2d8bc');
jQuery(content).slideDown('slow',
// animation complete
function() {
// scroll to new comment
jQuery('#comments_sidebar .sidebar_contents_wrapper').scrollTo(
comment,
{
duration: cp_scroll_speed,
axis: 'y',
onAfter: function() {
// animate to white
comment.animate({ backgroundColor: "#ffffff" }, 1000, function () {
// then make transparent
comment.css('background', 'transparent');
});
}
}
);
}
); // end slide
}
/**
* @description: update paragraph comment icon
* @todo:
*
*/
function cpac_update_para_icon( text_sig, comment_num ) {
// cast comment num as string
var comment_num = comment_num.toString();
// get textblock id
var textblock_id = '#textblock-' + text_sig;
// get small tag
var small = textblock_id + ' span small';
// set comment icon text
jQuery(small).html( comment_num );
// if we're changing from 0 to 1...
if ( comment_num == '1' ) {
// set comment icon
jQuery(textblock_id + ' span.commenticonbox a.para_permalink').addClass( 'has_comments' );
// show it
jQuery(small).css( 'visibility', 'visible' );
}
// if not the whole page...
if( text_sig != '' ) {
// get text block
var textblock = jQuery(textblock_id);
// scroll page to
cp_scroll_page( textblock );
} else {
// scroll to top
cp_scroll_to_top( 0, cp_scroll_speed );
}
}
/**
* @description: comment submission method
* @todo:
*
*/
jQuery('#commentform').on('submit', function(evt) {
// set global flag
cpac_submitting = true;
// hide errors
err.hide();