-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathcomments.js
551 lines (477 loc) · 20.5 KB
/
comments.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
/**
* This file is part of the FOSCommentBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
/**
* To use this reference javascript, you must also have jQuery installed. If
* you want to embed comments cross-domain, then easyXDM CORS is also required.
*
* @todo: expand this explanation (also in the docs)
*
* Then a comment thread can be embedded on any page:
*
* <div id="fos_comment_thread">#comments</div>
* <script type="text/javascript">
* // Set the thread_id if you want comments to be loaded via ajax (url to thread comments api)
* var fos_comment_thread_id = 'a_unique_identifier_for_the_thread';
* var fos_comment_thread_api_base_url = 'http://example.org/api/threads';
*
* // Optionally set the cors url if you want cross-domain AJAX (also needs easyXDM)
* var fos_comment_remote_cors_url = 'http://example.org/cors/index.html';
*
* // Optionally set a custom callback function to update the comment count elements
* var fos_comment_thread_comment_count_callback = function(elem, threadObject){}
*
* // Optionally set a different element than div#fos_comment_thread as container
* var fos_comment_thread_container = $('#other_element');
*
* (function() {
* var fos_comment_script = document.createElement('script');
* fos_comment_script.async = true;
* fos_comment_script.src = 'http://example.org/path/to/this/file.js';
* fos_comment_script.type = 'text/javascript';
*
* (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(fos_comment_script);
* })();
* </script>
*/
(function(window, $, easyXDM){
"use strict";
var FOS_COMMENT = {
/**
* Shorcut post method.
*
* @param string url The url of the page to post.
* @param object data The data to be posted.
* @param function success Optional callback function to use in case of succes.
* @param function error Optional callback function to use in case of error.
*/
post: function(url, data, success, error, complete) {
// Wrap the error callback to match return data between jQuery and easyXDM
var wrappedErrorCallback = function(response){
if('undefined' !== typeof error) {
error(response.responseText, response.status);
}
};
var wrappedCompleteCallback = function(response){
if('undefined' !== typeof complete) {
complete(response.responseText, response.status);
}
};
$.post(url, data, success).error(wrappedErrorCallback).complete(wrappedCompleteCallback);
},
/**
* Shorcut get method.
*
* @param string url The url of the page to get.
* @param object data The query data.
* @param function success Optional callback function to use in case of succes.
* @param function error Optional callback function to use in case of error.
*/
get: function(url, data, success, error) {
// Wrap the error callback to match return data between jQuery and easyXDM
var wrappedErrorCallback = function(response){
if('undefined' !== typeof error) {
error(response.responseText, response.status);
}
};
$.get(url, data, success).error(wrappedErrorCallback);
},
/**
* Gets the comments of a thread and places them in the thread holder.
*
* @param string identifier Unique identifier url for the thread comments.
* @param string url Optional url for the thread. Defaults to current location.
*/
getThreadComments: function(identifier, permalink) {
var event = jQuery.Event('fos_comment_before_load_thread');
event.identifier = identifier;
event.params = {
permalink: encodeURIComponent(permalink || window.location.href)
};
FOS_COMMENT.thread_container.trigger(event);
FOS_COMMENT.get(
FOS_COMMENT.base_url + '/' + encodeURIComponent(event.identifier) + '/comments',
event.params,
// success
function(data) {
FOS_COMMENT.thread_container.html(data);
FOS_COMMENT.thread_container.attr('data-thread', event.identifier);
FOS_COMMENT.thread_container.trigger('fos_comment_load_thread', event.identifier);
}
);
},
/**
* Initialize the event listeners.
*/
initializeListeners: function() {
FOS_COMMENT.thread_container.on('submit',
'form.fos_comment_comment_new_form',
function(e) {
var that = $(this);
var serializedData = FOS_COMMENT.serializeObject(this);
e.preventDefault();
var event = $.Event('fos_comment_submitting_form');
that.trigger(event);
if (event.isDefaultPrevented()) {
return;
}
FOS_COMMENT.post(
this.action,
serializedData,
// success
function(data, statusCode) {
FOS_COMMENT.appendComment(data, that);
that.trigger('fos_comment_new_comment', data);
},
// error
function(data, statusCode) {
var parent = that.parent();
parent.after(data);
parent.remove();
},
// complete
function(data, statusCode) {
that.trigger('fos_comment_submitted_form', statusCode);
}
);
}
);
FOS_COMMENT.thread_container.on('click',
'.fos_comment_comment_reply_show_form',
function(e) {
var form_data = $(this).data();
var that = $(this);
if(that.hasClass('fos_comment_replying')) {
// $('.fos_comment_comment_reply_form').hide();
$('#fos_comment_replies_'+form_data.id).toggle();
return that;
}else{
$('#fos_comment_replies_'+form_data.id).show();
}
FOS_COMMENT.get(
form_data.url,
{parentId: form_data.id},
function(data) {
that.addClass('fos_comment_replying');
$('#fos_comment_replies_'+form_data.id+' .content').prepend(data);
that.trigger('fos_comment_show_form', data);
}
);
}
);
FOS_COMMENT.thread_container.on('click',
'.fos_comment_comment_reply_cancel',
function(e) {
var form_holder = $(this).closest('.fos_comment_comment_form_holder');
var event = $.Event('fos_comment_cancel_form');
form_holder.trigger(event);
if (event.isDefaultPrevented()) {
return;
}
form_holder.closest('.fos_comment_comment_reply').removeClass('fos_comment_replying');
form_holder.remove();
}
);
FOS_COMMENT.thread_container.on('click',
'.fos_comment_comment_edit_show_form',
function(e) {
var form_data = $(this).data();
var that = $(this);
FOS_COMMENT.get(
form_data.url,
{},
function(data) {
var commentBody = $(form_data.container);
// save the old comment for the cancel function
commentBody.data('original', commentBody.html());
// show the edit form
commentBody.html(data);
that.trigger('fos_comment_show_edit_form', data);
}
);
}
);
FOS_COMMENT.thread_container.on('submit',
'form.fos_comment_comment_edit_form',
function(e) {
var that = $(this);
FOS_COMMENT.post(
this.action,
FOS_COMMENT.serializeObject(this),
// success
function(data) {
FOS_COMMENT.editComment(data);
that.trigger('fos_comment_edit_comment', data);
},
// error
function(data, statusCode) {
var parent = that.parent();
parent.after(data);
parent.remove();
}
);
e.preventDefault();
}
);
FOS_COMMENT.thread_container.on('click',
'.fos_comment_comment_edit_cancel',
function(e) {
FOS_COMMENT.cancelEditComment($(this).parents('.fos_comment_comment_body'));
}
);
FOS_COMMENT.thread_container.on('click',
'.fos_comment_comment_vote',
function(e) {
var that = $(this);
var form_data = that.data();
// Get the form
FOS_COMMENT.get(
form_data.url,
{},
function(data) {
// Post it
var form = $($.trim(data)).children('form')[0];
var form_data = $(form).data();
FOS_COMMENT.post(
form.action,
FOS_COMMENT.serializeObject(form),
function(data) {
$('#' + form_data.scoreHolder).html(data);
that.trigger('fos_comment_vote_comment', data, form);
}
);
}
);
}
);
FOS_COMMENT.thread_container.on('click',
'.fos_comment_comment_remove',
function(e) {
var form_data = $(this).data();
var event = $.Event('fos_comment_removing_comment');
$(this).trigger(event);
if (event.isDefaultPrevented()) {
return
}
// Get the form
FOS_COMMENT.get(
form_data.url,
{},
function(data) {
// Post it
var form = $($.trim(data)).children('form')[0];
FOS_COMMENT.post(
form.action,
FOS_COMMENT.serializeObject(form),
function(data) {
var commentHtml = $($.trim(data));
var originalComment = $('#' + commentHtml.attr('id'));
originalComment.replaceWith(commentHtml);
}
);
}
);
}
);
FOS_COMMENT.thread_container.on('click',
'.fos_comment_thread_commentable_action',
function(e) {
var form_data = $(this).data();
// Get the form
FOS_COMMENT.get(
form_data.url,
{},
function(data) {
// Post it
var form = $($.trim(data)).children('form')[0];
FOS_COMMENT.post(
form.action,
FOS_COMMENT.serializeObject(form),
function(data) {
var form = $($.trim(data)).children('form')[0];
var threadId = $(form).data().fosCommentThreadId;
// reload the intire thread
FOS_COMMENT.getThreadComments(threadId);
}
);
}
);
}
);
},
appendComment: function(commentHtml, form) {
var form_data = form.data();
var parent = form_data.parent;
if(form_data.parent) {
form.next().prepend(commentHtml);
// var form_parent = form.closest('.fos_comment_comment_form_holder');
//
// // reply button holder
// var reply_button_holder = form.closest('.fos_comment_comment_reply');
//
// var comment_element = form.closest('.fos_comment_comment_show')
// .children('.fos_comment_comment_replies');
//
// reply_button_holder.removeClass('fos_comment_replying');
//
// comment_element.prepend(commentHtml);
// comment_element.trigger('fos_comment_add_comment', commentHtml);
//
// // Remove the form
// form_parent.remove();
} else {
// Insert the comment
// form.after(commentHtml);
var comment_element = $('#fos_comment_list');
comment_element.prepend(commentHtml);
}
form.trigger('fos_comment_add_comment', commentHtml);
// "reset" the form
form = $(form[0]);
form[0].reset();
form.children('.fos_comment_form_errors').remove();
},
editComment: function(commentHtml) {
var commentHtml = $($.trim(commentHtml));
var originalCommentBody = $('#' + commentHtml.attr('id')).children('.fos_comment_comment_body');
originalCommentBody.html(commentHtml.children('.fos_comment_comment_body').html());
},
cancelEditComment: function(commentBody) {
commentBody.html(commentBody.data('original'));
},
/**
* easyXdm doesn't seem to pick up 'normal' serialized forms yet in the
* data property, so use this for now.
* http://stackoverflow.com/questions/1184624/serialize-form-to-json-with-jquery#1186309
*/
serializeObject: function(obj)
{
var o = {};
var a = $(obj).serializeArray();
$.each(a, function() {
if (o[this.name] !== undefined) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return o;
},
loadCommentCounts: function()
{
var threadIds = [];
var commentCountElements = $('span.fos-comment-count');
commentCountElements.each(function(i, elem){
var threadId = $(elem).data('fosCommentThreadId');
if(threadId) {
threadIds.push(threadId);
}
});
FOS_COMMENT.get(
FOS_COMMENT.base_url + '.json',
{ids: threadIds},
function(data) {
// easyXdm doesn't always serialize
if (typeof data != "object") {
data = jQuery.parseJSON(data);
}
var threadData = {};
for (var i in data.threads) {
threadData[data.threads[i].id] = data.threads[i];
}
$.each(commentCountElements, function(){
var threadId = $(this).data('fosCommentThreadId');
if(threadId) {
FOS_COMMENT.setCommentCount(this, threadData[threadId]);
}
});
}
);
},
setCommentCount: function(elem, threadObject) {
if (threadObject == undefined) {
elem.innerHTML = '0';
return;
}
elem.innerHTML = threadObject.num_comments;
}
};
// Check if a thread container was configured. If not, use default.
FOS_COMMENT.thread_container = window.fos_comment_thread_container || $('#fos_comment_thread');
// AJAX via easyXDM if this is configured
if(typeof window.fos_comment_remote_cors_url != "undefined") {
/**
* easyXDM instance to use
*/
FOS_COMMENT.easyXDM = easyXDM.noConflict('FOS_COMMENT');
/**
* Shorcut request method.
*
* @param string method The request method to use.
* @param string url The url of the page to request.
* @param object data The data parameters.
* @param function success Optional callback function to use in case of succes.
* @param function error Optional callback function to use in case of error.
*/
FOS_COMMENT.request = function(method, url, data, success, error) {
// wrap the callbacks to match the callback parameters of jQuery
var wrappedSuccessCallback = function(response){
if('undefined' !== typeof success) {
success(response.data, response.status);
}
};
var wrappedErrorCallback = function(response){
if('undefined' !== typeof error) {
error(response.data.data, response.data.status);
}
};
// todo: is there a better way to do this?
FOS_COMMENT.xhr.request({
url: url,
method: method,
data: data
}, wrappedSuccessCallback, wrappedErrorCallback);
};
FOS_COMMENT.post = function(url, data, success, error) {
this.request('POST', url, data, success, error);
};
FOS_COMMENT.get= function(url, data, success, error) {
// make data serialization equals to that of jquery
var params = jQuery.param(data);
url += '' != params ? '?' + params : '';
this.request('GET', url, undefined, success, error);
};
/* Initialize xhr object to do cross-domain requests. */
FOS_COMMENT.xhr = new FOS_COMMENT.easyXDM.Rpc({
remote: window.fos_comment_remote_cors_url
}, {
remote: {
request: {} // request is exposed by /cors/
}
});
}
// set the appropriate base url
FOS_COMMENT.base_url = window.fos_comment_thread_api_base_url;
// Load the comment if there is a thread id defined.
if(typeof window.fos_comment_thread_id != "undefined") {
// get the thread comments and init listeners
FOS_COMMENT.getThreadComments(window.fos_comment_thread_id);
}
if(typeof window.fos_comment_thread_comment_count_callback != "undefined") {
FOS_COMMENT.setCommentCount = window.fos_comment_thread_comment_count_callback;
}
if($('span.fos-comment-count').length > 0) {
FOS_COMMENT.loadCommentCounts();
}
FOS_COMMENT.initializeListeners();
window.fos = window.fos || {};
window.fos.Comment = FOS_COMMENT;
})(window, window.jQuery, window.easyXDM);