public/themes/templates/1/includes/js/comments.js   B
last analyzed

Complexity

Total Complexity 44
Complexity/F 1.1

Size

Lines of Code 339
Function Count 40

Duplication

Duplicated Lines 68
Ratio 20.06 %

Importance

Changes 0
Metric Value
eloc 214
dl 68
loc 339
rs 8.8798
c 0
b 0
f 0
wmc 44
mnd 4
bc 4
fnc 40
bpm 0.1
cpm 1.1
noi 4

1 Function

Rating   Name   Duplication   Size   Complexity  
F comments.js ➔ loadMoreData 0 17 44

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complexity

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like public/themes/templates/1/includes/js/comments.js often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
/*!
2
 * comment.js
3
 */
4
5
/*!
6
 * MAIN SITE code
7
 */
8
9
window.CommentEditable = function (commentId, userId) {
0 ignored issues
show
Unused Code introduced by
The parameter userId is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
10
    var string1 = 'latest_comment_edit';
11
    var editableComment = string1.concat(commentId);
12
13
    var string2 = 'comment_edit_icon';
14
    var editIcon = string2.concat(commentId);
15
16
    var string3 = 'comment_save_icon';
17
    var saveIcon = string3.concat(commentId);
18
19
    var string4 = 'comment_input';
20
    var commentInput = string4.concat(commentId);
21
22
    var input = document.getElementById(commentInput);
23
    input.style.display = 'inline';
24
25
    var save = document.getElementById(saveIcon);
26
    save.style.display = 'inline';
27
28
    var edit = document.getElementById(editIcon);
29
    edit.style.display = 'none';
30
31
    var output = document.getElementById(editableComment);
32
    output.style.display = 'none';
33
}
34
35
window.SaveEditable = function (commentId, url, action, extra, view = null, cCounter = null, vCounter = null) {
36
    var string = 'comment_input';
37
    var commentData = string.concat(commentId);
38
39
    var comment = document.getElementById(commentData).value
40
    comment = comment.replace(/\n\r?/g, '<br />');
41
42
    $.ajax({
43
        // The URL for the request
44
        url: url,
45
        data: 'action=' + action + '&comment_id=' + commentId + '&data=' + comment + '&view=' + view + '&c_counter=' + cCounter + '&v_counter=' + vCounter + '&' + extra,
46
        type: 'GET',
47
        dataType: 'html',
48
        // Code to run if the request succeeds;
49
        success: function (html) {
50
            $('#latest_comments_all').html(html);
51
        }
52
    });
53
}
54
55
window.DeleteEditable = function (commentId, url, action, extra, view = null, Ccounter = null, Vcounter = null) {
56
    $('#JSGenericModal').dialog({
57
        title: 'Delete',
58
        open: $('#JSGenericModalText').text('Are you sure you want to delete this comment?'),
59
        resizable: false,
60
        height: 200,
61
        modal: true,
62
        buttons: {
63
            'Delete': function () {
64
                $(this).dialog('close');
65
                $.ajax({
66
                    // The URL for the request
67
                    url: url,
68
                    data: 'action=' + action + '&comment_id=' + commentId + '&view=' + view + '&c_counter=' + Ccounter + '&v_counter=' + Vcounter + '&' + extra,
69
                    type: 'GET',
70
                    dataType: 'html',
71
                    // Code to run if the request succeeds;
72
                    success: function (html) {
73
                        $('#latest_comments_all').html(html);
74
                    }
75
                });
76
            },
77
            Cancel: function () {
78
                $(this).dialog('close');
79
            }
80
        }
81
    });
82
}
83
84
window.AddComment = function (userId, url, action, extra) {
85
    var commentData = 'comment_add';
86
    var comment = document.getElementById(commentData).value
87
88
    comment = comment.replace(/\n\r?/g, '<br />');
89
    document.getElementById('comment_add').value = '';
90
91
    $.ajax({
92
        // The URL for the request
93
        url: url,
94
        data: 'action=' + action + '&user_id=' + userId + '&data=' + comment + '&' + extra,
95
        type: 'GET',
96
        dataType: 'html',
97
        // Code to run if the request succeeds;
98
        success: function (html) {
99
            $('#latest_comments_all').html(html);
100
        }
101
    });
102
}
103
104
/*!
105
 * CPANEL code
106
 */
107
108
$(document).ready(function () {
109
    $('#comments_all').click(function () {
110
        $.ajaxQueue({
111
            // The URL for the request
112
            url: 'ajax_comments.php',
113
            data: 'view=comments_all',
114
            type: 'GET',
115
            dataType: 'html',
116
            // Code to run if the request succeeds;
117
            success: function (html) {
118
                $('.jsCommentsWrapper').html(html);
119
            }
120
        });
121
    })
122
    $('#comments_game_comments').click(function () {
123
        $.ajaxQueue({
124
            // The URL for the request
125
            url: 'ajax_comments.php',
126
            data: 'view=comments_game_comments',
127
            type: 'GET',
128
            dataType: 'html',
129
            // Code to run if the request succeeds;
130
            success: function (html) {
131
                $('.jsCommentsWrapper').html(html);
132
            }
133
        });
134
    })
135
    $('#comments_game_review_comments').click(function () {
136
        $.ajaxQueue({
137
            // The URL for the request
138
            url: 'ajax_comments.php',
139
            data: 'view=comments_game_review_comments',
140
            type: 'GET',
141
            dataType: 'html',
142
            // Code to run if the request succeeds;
143
            success: function (html) {
144
                $('.jsCommentsWrapper').html(html);
145
            }
146
        });
147
    })
148
    $('#comments_interview_comments').click(function () {
149
        $.ajaxQueue({
150
            // The URL for the request
151
            url: 'ajax_comments.php',
152
            data: 'view=comments_interview_comments',
153
            type: 'GET',
154
            dataType: 'html',
155
            // Code to run if the request succeeds;
156
            success: function (html) {
157
                $('.jsCommentsWrapper').html(html);
158
            }
159
        });
160
    })
161
    $('#comments_article_comments').click(function () {
162
        $.ajaxQueue({
163
            // The URL for the request
164
            url: 'ajax_comments.php',
165
            data: 'view=comments_article_comments',
166
            type: 'GET',
167
            dataType: 'html',
168
            // Code to run if the request succeeds;
169
            success: function (html) {
170
                $('.jsCommentsWrapper').html(html);
171
            }
172
        });
173
    })
174
    $('.jsCommentsWrapper').on('click', '.jsUserCommentsLink', function () {
175
        var view = 'users_comments';
176
        var userId = $(this).data('user-id');
177
        $.ajaxQueue({
178
            // The URL for the request
179
            url: 'ajax_comments.php',
180
            data: 'view=' + view + '&user_id=' + userId,
181
            type: 'GET',
182
            dataType: 'html',
183
            // Code to run if the request succeeds;
184
            success: function (html) {
185
                $('.jsCommentsWrapper').html(html);
186
            }
187
        });
188
    })
189
    // Edit Comment Function
190
    $('.jsCommentsWrapper').on('click', '.jsCommentsEditButton', function () {
191
        var commentsId = $(this).data('comments-id');
192
        var commentType = $(this).data('comment-type');
193
        var jsCommentTextBoxId = 'jsCommentTextBox'.concat(commentsId);
194
        $.ajaxQueue({
195
            // The URL for the request
196
            url: 'ajax_comments_edit.php',
197
            data: 'action=get_comment_text&comments_id=' + commentsId + '&comment_type=' + commentType,
198
            type: 'GET',
199
            dataType: 'html',
200
            // Code to run if the request succeeds;
201
            success: function (html) {
202
                $('#' + jsCommentTextBoxId).html(html);
203
                // listenSaveButton();
204
            }
205
        });
206
    })
207
    // Edit Comment Function dropdown item
208
    $('.jsCommentsWrapper').on('click', '.jsCommentsEditDropdownItem', function () {
209
        var commentsId = $(this).data('comments-id');
210
        var commentType = $(this).data('comment-type');
211
        var jsCommentTextBoxId = 'jsCommentTextBox'.concat(commentsId);
212
        $.ajaxQueue({
213
            // The URL for the request
214
            url: 'ajax_comments_edit.php',
215
            data: 'action=get_comment_text&comments_id=' + commentsId + '&comment_type=' + commentType,
216
            type: 'GET',
217
            dataType: 'html',
218
            // Code to run if the request succeeds;
219
            success: function (html) {
220
                $('#' + jsCommentTextBoxId).html(html);
221
                // listenSaveButton();
222
            }
223
        });
224
    })
225
    // Edit Save Comment Function
226
    $('.jsCommentsWrapper').on('click', '.jsCommentsEditSaveButton', function () {
227
        var commentsId = $(this).data('comments-id');
228
        var commentType = $(this).data('comment-type');
229
        var jsCommentTextBoxId = 'jsCommentTextBox'.concat(commentsId);
230
        var commentText = $('#' + jsCommentTextBoxId + ' > #jsCommentText').val();
231
        commentText = commentText.replace(/\n\r?/g, '<br />');
232
233
        $.ajaxQueue({
234
            // The URL for the request
235
            url: 'ajax_comments_edit.php',
236
            data: 'action=save_comment_text&comments_id=' + commentsId + '&comment_text=' + commentText + '&comment_type=' + commentType,
237
            type: 'POST',
238
            dataType: 'html',
239
            // Code to run if the request succeeds;
240
            success: function (html) {
241
                var returnHtml = html.split('[BRK]');
242
                $('#' + jsCommentTextBoxId).html(returnHtml[0]);
243
                window.OSDMessageDisplay(returnHtml[1]);
244
            }
245
        });
246
    })
247
    // Delete Comment Function
248 View Code Duplication
    $('.jsCommentsWrapper').on('click', '.jsCommentsDeleteButton', function () {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
249
        var commentsId = $(this).data('comments-id');
250
        $('#JSGenericModal').dialog({
251
            title: 'Delete Comment',
252
            open: $('#JSGenericModalText').text('Are you sure you want to delete this comment?'),
253
            resizable: false,
254
            height: 200,
255
            modal: true,
256
            buttons: {
257
                'Delete': function () {
258
                    $(this).dialog('close');
259
                    $.ajaxQueue({
260
                        // The URL for the request
261
                        url: 'db_comments.php',
262
                        data: 'action=delete&comments_id=' + commentsId,
263
                        type: 'POST',
264
                        dataType: 'html',
265
                        // Code to run if the request succeeds;
266
                        success: function (html) {
267
                            var begin = html.startsWith('You');
268
                            if (begin) {
0 ignored issues
show
Comprehensibility Documentation Best Practice introduced by
This code block is empty. Consider removing it or adding a comment to explain.
Loading history...
269
                            } else {
270
                                $('#jsCommentId' + commentsId).html('');
271
                            }
272
                            window.OSDMessageDisplay(html);
273
                        }
274
                    });
275
                },
276
                Cancel: function () {
277
                    $(this).dialog('close');
278
                }
279
            }
280
        });
281
    })
282
    // Delete Comment Function in dropdown
283 View Code Duplication
    $('.jsCommentsWrapper').on('click', '.jsCommentsDeleteDropdownItem', function () {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
284
        var commentsId = $(this).data('comments-id');
285
        $('#JSGenericModal').dialog({
286
            title: 'Delete Comment',
287
            open: $('#JSGenericModalText').text('Are you sure you want to delete this comment?'),
288
            resizable: false,
289
            height: 200,
290
            modal: true,
291
            buttons: {
292
                'Delete': function () {
293
                    $(this).dialog('close');
294
                    $.ajaxQueue({
295
                        // The URL for the request
296
                        url: 'db_comments.php',
297
                        data: 'action=delete&comments_id=' + commentsId,
298
                        type: 'POST',
299
                        dataType: 'html',
300
                        // Code to run if the request succeeds;
301
                        success: function (html) {
302
                            var begin = html.startsWith('You');
303
                            if (begin) {
0 ignored issues
show
Comprehensibility Documentation Best Practice introduced by
This code block is empty. Consider removing it or adding a comment to explain.
Loading history...
304
                            } else {
305
                                $('#jsCommentId' + commentsId).html('');
306
                            }
307
                            window.OSDMessageDisplay(html);
308
                        }
309
                    });
310
                },
311
                Cancel: function () {
312
                    $(this).dialog('close');
313
                }
314
            }
315
        });
316
    })
317
318
    $(window).scroll(function () {
319
        if ($(window).scrollTop() + $(window).height() >= $(document).height()) {
320
            var lastTimestamp = $('.comments_post_box:last').attr('id');
321
            loadMoreData(lastTimestamp);
322
        }
323
    });
324
325
    $('.jsCommentsWrapper').on('click', '.comments_button_dropdown', function () {
326
        var dropdownId = $(this).data('dropdown-id');
327
        $('#dropdown_box' + dropdownId).toggle('.dropdown_show');
328
    })
329
330
    function loadMoreData (lastTimestamp) {
331
        var view = $('#view').html();
332
        if (view === 'users_comments') {
333
            var userId = $('.jsUserCommentsLink:first').data('user-id');
334
        }
335
        $.ajaxQueue({
336
            // The URL for the request
337
            url: 'ajax_comments.php',
338
            data: 'action=autoload&view=' + view + '&last_timestamp=' + lastTimestamp + '&user_id=' + userId,
0 ignored issues
show
Bug introduced by
The variable userId does not seem to be initialized in case view === "users_comments" on line 332 is false. Are you sure this can never be the case?
Loading history...
339
            type: 'GET',
340
            dataType: 'html',
341
            // Code to run if the request succeeds;
342
            success: function (html) {
343
                $('.infinite-item:last').append(html);
344
            }
345
        });
346
    }
347
})
348