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

Complexity

Total Complexity 46
Complexity/F 1.07

Size

Lines of Code 386
Function Count 43

Duplication

Duplicated Lines 196
Ratio 50.78 %

Importance

Changes 0
Metric Value
eloc 259
dl 196
loc 386
rs 8.72
c 0
b 0
f 0
wmc 46
mnd 3
bc 3
fnc 43
bpm 0.0697
cpm 1.0697
noi 8

1 Function

Rating   Name   Duplication   Size   Complexity  
F news.js ➔ loadMoreData 0 16 46

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/news.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
$(document).ready(function () {
2
    $('select[name=members]').altAutocomplete();
3
4
    // ***********************
5
    // NEWS APPROVE BUTTONS //
6
    // ***********************
7
8
    // Edit News Function
9
    $('.jsNewsWrapper').on('click', '.jsNewsEditButton', function () {
10
        var newsId = $(this).data('news-id');
11
        var jsNewsEditBoxId = 'jsNewsEditBox'.concat(newsId);
12
        $.ajaxQueue({
13
            // The URL for the request
14
            url: 'ajax_news_approve_edit.php',
15
            data: 'action=get_newsapprove_text&news_id=' + newsId,
16
            type: 'GET',
17
            dataType: 'html',
18
            // Code to run if the request succeeds;
19
            success: function (html) {
20
                $('#' + jsNewsEditBoxId).html(html);
21
            }
22
        });
23
    })
24
    // Edit News Function dropdown item
25
    $('.jsNewsWrapper').on('click', '.jsNewsEditDropdownItem', function () {
26
        var newsId = $(this).data('news-id');
27
        var jsNewsEditBoxId = 'jsNewsEditBox'.concat(newsId);
28
        $.ajaxQueue({
29
            // The URL for the request
30
            url: 'ajax_news_approve_edit.php',
31
            data: 'action=get_newsapprove_text&news_id=' + newsId,
32
            type: 'GET',
33
            dataType: 'html',
34
            // Code to run if the request succeeds;
35
            success: function (html) {
36
                $('#' + jsNewsEditBoxId).html(html);
37
            }
38
        });
39
    })
40
    // Edit Save News Function
41
    $('.jsNewsWrapper').on('click', '.jsNewsEditSaveButton', function () {
42
        var newsId = $(this).data('news-id');
43
        var jsNewsEditBox = 'jsNewsEditBox'.concat(newsId);
44
        var newsText = $('#' + jsNewsEditBox + ' > #jsNewsText').val();
45
        newsText = newsText.replace(/\n\r?/g, '<br />');
46
        var newsHeadline = $('#' + jsNewsEditBox + ' > #JsHeadlineText').val();
47
        var newsUserId = $('#' + jsNewsEditBox + ' > #member_select').val();
48
        var newsImageId = $('#' + jsNewsEditBox + ' > #news_images_select').val();
49
50
        var dateDay = newsId + 'Date_Day';
51
        var dateMonth = newsId + 'Date_Month';
52
        var dateYear = newsId + 'Date_Year';
53
54
        var day = document.getElementById(dateDay).value;
55
        var month = document.getElementById(dateMonth).value;
56
        var year = document.getElementById(dateYear).value;
57
58
        $.ajaxQueue({
59
            // The URL for the request
60
            url: 'db_news.php',
61
            data: 'action=save_news_text&news_id=' + newsId + '&news_text=' + newsText + '&news_headline=' + newsHeadline + '&news_userid=' + newsUserId + '&news_image_id=' + newsImageId + '&news_day=' + day + '&news_month=' + month + '&news_year=' + year,
62
            type: 'POST',
63
            dataType: 'html',
64
            // Code to run if the request succeeds;
65
            success: function (html) {
66
                var returnHtml = html.split('[BRK]');
67
                $('#news_submission_list').html(returnHtml[0]);
68
                window.OSDMessageDisplay(returnHtml[1]);
69
                document.getElementById('post').reset();
70
            },
71
            error: function (xhr, status, error) {
0 ignored issues
show
Unused Code introduced by
The parameter status 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...
Unused Code introduced by
The parameter xhr 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...
Unused Code introduced by
The parameter error 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...
72
            }
73
        });
74
    })
75
    // Delete Submission Function
76 View Code Duplication
    $('.jsNewsWrapper').on('click', '.jsNewsDeleteButton', function () {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
77
        var newsId = $(this).data('news-id');
78
        $('#JSGenericModal').dialog({
79
            title: 'Delete Submission',
80
            open: $('#JSGenericModalText').text('Are you sure you want to delete this submission?'),
81
            resizable: false,
82
            height: 200,
83
            modal: true,
84
            buttons: {
85
                'Delete': function () {
86
                    $(this).dialog('close');
87
                    $.ajaxQueue({
88
                        // The URL for the request
89
                        url: 'db_news.php',
90
                        data: 'action=delete_submission&news_id=' + newsId,
91
                        type: 'POST',
92
                        dataType: 'html',
93
                        // Code to run if the request succeeds;
94
                        success: function (html) {
95
                            var returnHtml = html.split('[BRK]');
96
                            $('#news_submission_list').html(returnHtml[0]);
97
                            window.OSDMessageDisplay(returnHtml[1]);
98
                            document.getElementById('post').reset();
99
                        }
100
                    });
101
                },
102
                Cancel: function () {
103
                    $(this).dialog('close');
104
                }
105
            }
106
        });
107
    })
108
    // Delete submission Function in dropdown
109 View Code Duplication
    $('.jsNewsWrapper').on('click', '.jsNewsDeleteDropdownItem', function () {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
110
        var newsId = $(this).data('news-id');
111
        $('#JSGenericModal').dialog({
112
            title: 'Delete Submission',
113
            open: $('#JSGenericModalText').text('Are you sure you want to delete this submission?'),
114
            resizable: false,
115
            height: 200,
116
            modal: true,
117
            buttons: {
118
                'Delete': function () {
119
                    $(this).dialog('close');
120
                    $.ajaxQueue({
121
                        // The URL for the request
122
                        url: 'db_news.php',
123
                        data: 'action=delete_submission&news_id=' + newsId,
124
                        type: 'POST',
125
                        dataType: 'html',
126
                        // Code to run if the request succeeds;
127
                        success: function (html) {
128
                            var returnHtml = html.split('[BRK]');
129
                            $('#news_submission_list').html(returnHtml[0]);
130
                            window.OSDMessageDisplay(returnHtml[1]);
131
                            document.getElementById('post').reset();
132
                        }
133
                    });
134
                },
135
                Cancel: function () {
136
                    $(this).dialog('close');
137
                }
138
            }
139
        });
140
    })
141
    // Approve Submission Function
142 View Code Duplication
    $('.jsNewsWrapper').on('click', '.jsNewsApproveButton', function () {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
143
        var newsId = $(this).data('news-id');
144
        $('#JSGenericModal').dialog({
145
            title: 'Approve Submission',
146
            open: $('#JSGenericModalText').text('Are you sure you want to approve this submission?'),
147
            resizable: false,
148
            height: 200,
149
            modal: true,
150
            buttons: {
151
                'Approve': function () {
152
                    $(this).dialog('close');
153
                    $.ajaxQueue({
154
                        // The URL for the request
155
                        url: 'db_news.php',
156
                        data: 'action=approve_submission&news_submission_id=' + newsId,
157
                        type: 'POST',
158
                        dataType: 'html',
159
                        // Code to run if the request succeeds;
160
                        success: function (html) {
161
                            var returnHtml = html.split('[BRK]');
162
                            $('#news_submission_list').html(returnHtml[0]);
163
                            window.OSDMessageDisplay(returnHtml[1]);
164
                            document.getElementById('post').reset();
165
                        }
166
                    });
167
                },
168
                Cancel: function () {
169
                    $(this).dialog('close');
170
                }
171
            }
172
        });
173
    })
174
    // Approve submission Function in dropdown
175 View Code Duplication
    $('.jsNewsWrapper').on('click', '.jsNewsApproveDropdownItem', function () {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
176
        var newsId = $(this).data('news-id');
177
        $('#JSGenericModal').dialog({
178
            title: 'Approve Submission',
179
            open: $('#JSGenericModalText').text('Are you sure you want to approve this submission?'),
180
            resizable: false,
181
            height: 200,
182
            modal: true,
183
            buttons: {
184
                'Approve': function () {
185
                    $(this).dialog('close');
186
                    $.ajaxQueue({
187
                        // The URL for the request
188
                        url: 'db_news.php',
189
                        data: 'action=approve_submission&news_submission_id=' + newsId,
190
                        type: 'POST',
191
                        dataType: 'html',
192
                        // Code to run if the request succeeds;
193
                        success: function (html) {
194
                            var returnHtml = html.split('[BRK]');
195
                            $('#news_submission_list').html(returnHtml[0]);
196
                            window.OSDMessageDisplay(returnHtml[1]);
197
                            document.getElementById('post').reset();
198
                        }
199
                    });
200
                },
201
                Cancel: function () {
202
                    $(this).dialog('close');
203
                }
204
            }
205
        });
206
    })
207
208
    // ******************
209
    // NEWS EDIT BUTTONS
210
    // ******************
211
    // Edit News Function
212
    $('.jsNewsWrapper').on('click', '.jsNewsPostEditButton', function () {
213
        var newsId = $(this).data('news-id');
214
        var jsNewsEditBoxId = 'jsNewsEditBox'.concat(newsId);
215
        $.ajaxQueue({
216
            // The URL for the request
217
            url: 'ajax_news_post_edit.php',
218
            data: 'action=get_newspost_text&news_id=' + newsId,
219
            type: 'GET',
220
            dataType: 'html',
221
            // Code to run if the request succeeds;
222
            success: function (html) {
223
                $('#' + jsNewsEditBoxId).html(html);
224
            }
225
        });
226
    })
227
    // Edit News Function dropdown item
228
    $('.jsNewsWrapper').on('click', '.jsNewsPostEditDropdownItem', function () {
229
        var newsId = $(this).data('news-id');
230
        var jsNewsEditBoxId = 'jsNewsEditBox'.concat(newsId);
231
        $.ajaxQueue({
232
            // The URL for the request
233
            url: 'ajax_news_post_edit.php',
234
            data: 'action=get_newspost_text&news_id=' + newsId,
235
            type: 'GET',
236
            dataType: 'html',
237
            // Code to run if the request succeeds;
238
            success: function (html) {
239
                $('#' + jsNewsEditBoxId).html(html);
240
            }
241
        });
242
    })
243
    // Edit Save News Function
244
    $('.jsNewsWrapper').on('click', '.jsNewsPostEditSaveButton', function () {
245
        var newsId = $(this).data('news-id');
246
        var jsNewsEditBox = 'jsNewsEditBox'.concat(newsId);
247
        var newsText = $('#' + jsNewsEditBox + ' > #jsNewsText').val();
248
        newsText = newsText.replace(/\n\r?/g, '<br />');
249
        var newsHeadline = $('#' + jsNewsEditBox + ' > #JsHeadlineText').val();
250
        var newsUserId = $('#' + jsNewsEditBox + ' > #member_select').val();
251
        var newsImageId = $('#' + jsNewsEditBox + ' > #news_images_select').val();
252
253
        var dateDay = newsId + 'Date_Day';
254
        var dateMonth = newsId + 'Date_Month';
255
        var dateYear = newsId + 'Date_Year';
256
257
        var day = document.getElementById(dateDay).value;
258
        var month = document.getElementById(dateMonth).value;
259
        var year = document.getElementById(dateYear).value;
260
261
        $.ajaxQueue({
262
            // The URL for the request
263
            url: 'db_news.php',
264
            data: {
265
                action: 'save_news_post_text',
266
                news_id: newsId,
267
                news_text: newsText,
268
                news_headline: newsHeadline,
269
                news_userid: newsUserId,
270
                news_image_id: newsImageId,
271
                news_day: day,
272
                news_month: month,
273
                news_year: year
274
            },
275
            type: 'POST',
276
            dataType: 'html',
277
            // Code to run if the request succeeds;
278
            success: function (html) {
279
                var returnHtml = html.split('[BRK]');
280
                $('#' + jsNewsEditBox).html(returnHtml[0]);
281
                window.OSDMessageDisplay(returnHtml[1]);
282
            },
283
            error: function (xhr, status, error) {
0 ignored issues
show
Unused Code introduced by
The parameter xhr 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...
Unused Code introduced by
The parameter status 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...
Unused Code introduced by
The parameter error 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...
284
            }
285
        });
286
    })
287
    // Delete news Function
288 View Code Duplication
    $('.jsNewsWrapper').on('click', '.jsNewsPostDeleteButton', function () {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
289
        var newsId = $(this).data('news-id');
290
        $('#JSGenericModal').dialog({
291
            title: 'Delete News Post',
292
            open: $('#JSGenericModalText').text('Are you sure you want to delete this news post?'),
293
            resizable: false,
294
            height: 200,
295
            modal: true,
296
            buttons: {
297
                'Delete': function () {
298
                    $(this).dialog('close');
299
                    $.ajaxQueue({
300
                        // The URL for the request
301
                        url: 'db_news.php',
302
                        data: 'action=delete_news&news_id=' + newsId,
303
                        type: 'POST',
304
                        dataType: 'html',
305
                        // Code to run if the request succeeds;
306
                        success: function (html) {
307
                            var begin = html.startsWith('You');
308
                            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...
309
                            } else {
310
                                $('#jsNewsId' + newsId).html('');
311
                            }
312
                            window.OSDMessageDisplay(html);
313
                        }
314
                    });
315
                },
316
                Cancel: function () {
317
                    $(this).dialog('close');
318
                }
319
            }
320
        });
321
    })
322
    // Delete news Function in dropdown
323 View Code Duplication
    $('.jsNewsWrapper').on('click', '.jsNewsPostDeleteDropdownItem', function () {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
324
        var newsId = $(this).data('news-id');
325
        $('#JSGenericModal').dialog({
326
            title: 'Delete News Post',
327
            open: $('#JSGenericModalText').text('Are you sure you want to delete this news post?'),
328
            resizable: false,
329
            height: 200,
330
            modal: true,
331
            buttons: {
332
                'Delete': function () {
333
                    $(this).dialog('close');
334
                    $.ajaxQueue({
335
                        // The URL for the request
336
                        url: 'db_news.php',
337
                        data: 'action=delete_news&news_id=' + newsId,
338
                        type: 'POST',
339
                        dataType: 'html',
340
                        // Code to run if the request succeeds;
341
                        success: function (html) {
342
                            var begin = html.startsWith('You');
343
                            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...
344
                            } else {
345
                                $('#jsNewsId' + newsId).html('');
346
                            }
347
                            window.OSDMessageDisplay(html);
348
                        }
349
                    });
350
                },
351
                Cancel: function () {
352
                    $(this).dialog('close');
353
                }
354
            }
355
        });
356
    })
357
358
    $('.jsNewsWrapper').on('click', '.news_button_dropdown', function () {
359
        var dropdownId = $(this).data('dropdown-id');
360
        $('#dropdown_box' + dropdownId).toggle('.dropdown_show');
361
    })
362
363
    $(window).scroll(function () {
364
        if ($(window).scrollTop() + $(window).height() >= $(document).height()) {
365
            var lastTimestamp = $('.news_post_box:last').attr('id');
366
            loadMoreData(lastTimestamp);
367
        }
368
    });
369
370
    function loadMoreData (lastTimestamp) {
371
        var userId = $('#JSCpanelAuthorBrowse option:selected').val();
372
        var newsSearch = $('#JSCpanelNewsSearch').val();
373
374
        $.ajaxQueue({
375
            // The URL for the request
376
            url: 'ajax_news_edit.php',
377
            data: 'action=autoload&last_timestamp=' + lastTimestamp + '&user_id=' + userId + '&newssearch=' + newsSearch,
378
            type: 'GET',
379
            dataType: 'html',
380
            // Code to run if the request succeeds;
381
            success: function (html) {
382
                $('.infinite-item:last').append(html);
383
            }
384
        });
385
    }
386
})
387