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

Complexity

Total Complexity 43
Complexity/F 1.59

Size

Lines of Code 388
Function Count 27

Duplication

Duplicated Lines 63
Ratio 16.24 %

Importance

Changes 0
Metric Value
eloc 215
dl 63
loc 388
rs 8.96
c 0
b 0
f 0
wmc 43
mnd 16
bc 16
fnc 27
bpm 0.5925
cpm 1.5925
noi 10

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/articles.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
 * articles.js
3
 * http://www.atarilegend.com
4
 *
5
 */
6
7
window.onload = function () {
8
    $('.interview_main_text').dotdotdot({ // This is the script to truncate the preview of the reviews
9
        //  configuration goes here
10
11
        /*  The text to add as ellipsis. */
12
        ellipsis: '... ',
13
14
        /*  How to cut off the text/html: 'word'/'letter'/'children' */
15
        wrap: 'word',
16
17
        /*  Wrap-option fallback to 'letter' for long words */
18
        fallbackToLetter: false,
19
20
        /*  jQuery-selector for the element to keep and put after the ellipsis. */
21
        after: null,
22
23
        /*  Whether to update the ellipsis: true/'window' */
24
        watch: window,
25
26
        /*  Optionally set a max-height, can be a number or function.
27
            If null, the height will be measured. */
28
        height: null,
29
30
        /*  Deviation for the height-option. */
31
        tolerance: 0,
32
33
        /*  Callback function that is fired after the ellipsis is added,
34
            receives two parameters: isTruncated(boolean), orgContent(string). */
35
        callback: function (isTruncated, orgContent) {},
0 ignored issues
show
Unused Code introduced by
The parameter isTruncated 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 orgContent 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...
36
37
        lastCharacter: {
38
39
            /*  Remove these characters from the end of the truncated text. */
40
            remove: [ ' ', ',', ';', '.', '!', '?' ],
41
42
            /*  Don't add an ellipsis if this array contains
43
                the last character of the truncated text. */
44
            noEllipsis: []
45
        }
46
    });
47
48
    $('.standard_list_entry_news_text').dotdotdot({ // This is the script to truncate the preview of the reviews
49
        //  configuration goes here
50
51
        /*  The text to add as ellipsis. */
52
        ellipsis: '... ',
53
54
        /*  How to cut off the text/html: 'word'/'letter'/'children' */
55
        wrap: 'word',
56
57
        /*  Wrap-option fallback to 'letter' for long words */
58
        fallbackToLetter: false,
59
60
        /*  jQuery-selector for the element to keep and put after the ellipsis. */
61
        after: null,
62
63
        /*  Whether to update the ellipsis: true/'window' */
64
        watch: window,
65
66
        /*  Optionally set a max-height, can be a number or function.
67
            If null, the height will be measured. */
68
        height: null,
69
70
        /*  Deviation for the height-option. */
71
        tolerance: 0,
72
73
        /*  Callback function that is fired after the ellipsis is added,
74
            receives two parameters: isTruncated(boolean), orgContent(string). */
75
        callback: function (isTruncated, orgContent) {},
0 ignored issues
show
Unused Code introduced by
The parameter isTruncated 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 orgContent 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...
76
77
        lastCharacter: {
78
79
            /*  Remove these characters from the end of the truncated text. */
80
            remove: [ ' ', ',', ';', '.', '!', '?' ],
81
82
            /*  Don't add an ellipsis if this array contains
83
                the last character of the truncated text. */
84
            noEllipsis: []
85
        }
86
    });
87
};
88
89
$(document).ready(function () {
90
    $('select[name=members]').altAutocomplete();
91
});
92
93 View Code Duplication
window.openTab = function (evt, tabName, screenshotsNr) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
94
    // Declare all variables
95
    var i, tabcontent, tablinks;
96
97
    // Get all elements with class="tabcontent" and hide them
98
    tabcontent = document.getElementsByClassName('tabcontent');
99
    for (i = 0; i < tabcontent.length; i++) {
100
        tabcontent[i].style.display = 'none';
101
    }
102
103
    // Get all elements with class="tablinks" and remove the class "active"
104
    tablinks = document.getElementsByClassName('tablinks');
105
    for (i = 0; i < tablinks.length; i++) {
106
        tablinks[i].className = tablinks[i].className.replace(' active', '');
107
    }
108
109
    // Show the current tab, and add an "active" class to the button that opened the tab
110
    document.getElementById(tabName).style.display = 'block';
111
    evt.currentTarget.className += ' active';
112
113
    // al the java code for the filling of the fields in the preview tab!
114
    if (tabName === 'preview') {
115
        // get the date field
116
        var day = document.getElementsByName('Date_Day')[0].value;
117
        document.getElementById('article_preview_date').innerHTML = day;
118
        document.getElementById('article_preview_date').innerHTML += '-';
119
        var month = document.getElementsByName('Date_Month')[0].value;
120
        document.getElementById('article_preview_date').innerHTML += month;
121
        document.getElementById('article_preview_date').innerHTML += '-';
122
        var year = document.getElementsByName('Date_Year')[0].value;
123
        document.getElementById('article_preview_date').innerHTML += year;
124
125
        var user = document.getElementById('member_select');
126
        var strUser = user.options[user.selectedIndex].text;
127
        document.getElementById('article_preview_user').innerHTML = strUser;
128
129
        // Do all the preps for the intro text!
130
        var intro = document.getElementsByName('textintro')[0].value;
131
        intro = previewText(intro);
132
        document.getElementById('article_preview_intro').innerHTML = intro;
133
134
        // Do all the preps for the actual article text!
135
        var article = document.getElementsByName('textfield')[0].value;
136
        article = previewText(article);
137
        document.getElementById('article_preview_text').innerHTML = article;
138
139
        // get the screenshots data
140
        for (i = 1; i <= screenshotsNr; i++) {
141
            var string = 'comment_';
142
            var stringComment = string.concat(i);
143
            var comment = document.getElementById(stringComment).value;
144
145
            var string2 = 'preview_comment_';
146
            var stringPreviewComment = string2.concat(i);
147
            document.getElementById(stringPreviewComment).innerHTML = comment;
148
149
            var string3 = 'output_';
150
            var stringOutput2 = string3.concat(i);
151
            var output2 = document.getElementById(stringOutput2);
152
            output2.style.display = 'inline';
153
        }
154
    }
155
}
156
157
// Delete the comments and the article
158
window.deletecomment = function (str, str2) {
159
    $('#JSGenericModal').dialog({
160
        title: 'Delete screenshot?',
161
        open: $('#JSGenericModalText').text('Are you sure you want to delete the screenshot with its comment?'),
162
        resizable: false,
163
        height: 200,
164
        modal: true,
165
        buttons: {
166
            'Delete': function () {
167
                $(this).dialog('close');
168
                $.ajaxQueue({
169
                    // The URL for the request
170
                    url: 'db_articles.php',
171
                    data: 'article_id=' + str2 + '&screenshot_id=' + str + '&action=delete_screenshot_comment',
172
                    type: 'POST',
173
                    dataType: 'html',
174
                    // Code to run if the request succeeds;
175
                    success: function (html) {
176
                        var returnHtml = html.split('[BRK]');
177
                        $('#article_screenshot_list').html(returnHtml[0]);
178
                        window.OSDMessageDisplay(returnHtml[1]);
179
                        document.getElementById('screenshot_add_to_article').reset();
180
                    }
181
                });
182
            },
183
            Cancel: function () {
184
                $(this).dialog('close');
185
            }
186
        }
187
    });
188
}
189
190
// delete the article
191
window.deletearticle = function (str) {
192
    $('#JSGenericModal').dialog({
193
        title: 'Delete article?',
194
        open: $('#JSGenericModalText').text('Are you sure you want to delete this article?'),
195
        resizable: false,
196
        height: 200,
197
        modal: true,
198
        buttons: {
199
            'Delete': function () {
200
                $(this).dialog('close');
201
                var url = 'db_articles.php?action=delete_article&article_id=' + str;
202
                location.href = url;
203
            },
204
            Cancel: function () {
205
                $(this).dialog('close');
206
            }
207
        }
208
    });
209
}
210
211
// open the screenshot selection box
212
window.popArticleAddScreenshots = function (str) {
213
    if (str === '') {
214
        $('#article_expand_screenshots').html('');
215
    } else {
216
        $.ajaxQueue({
217
            // The URL for the request
218
            url: '../articles/ajax_addscreenshots_article.php',
219
            data: 'article_id=' + str,
220
            type: 'GET',
221
            dataType: 'html',
222
            // Code to run if the request succeeds;
223
            success: function (html) {
224
                $('#article_expand_screenshots').html(html);
225
                $('#screenshot_link').html('<a onclick="closeAddScreenshots(' + str + ')" style="cursor: pointer;" class="left_nav_link"><i class="fa fa-minus-square-o" aria-hidden="true"></i> Add screenshots</a>');
226
            }
227
        });
228
    }
229
}
230
231
// close the screenshot selection box
232
window.closeAddScreenshots = function (str) {
233
    document.getElementById('article_expand_screenshots').innerHTML = '';
234
    document.getElementById('screenshot_link').innerHTML = '<a onclick="popArticleAddScreenshots(' + str + ')" style="cursor: pointer;" class="left_nav_link"><i class="fa fa-plus-square-o" aria-hidden="true"></i> Add screenshots</a>';
235
}
236
237
// Save the screenshots to the article
238
window.addScreenshottoArticle = function (articleId, styleDir) {
0 ignored issues
show
Unused Code introduced by
The parameter styleDir 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...
239
    if (articleId === '') {
240
        $('#article_screenshot_list').html('');
241
    } else {
242
        var form = $('#screenshot_add_to_article')[0];
243
        var formValues = new FormData(form);
244
        $.ajax({
245
            // The URL for the request
246
            url: '../articles/db_articles.php?action2=add_screens',
247
            data: formValues,
248
            type: 'POST',
249
            contentType: false,
250
            processData: false,
251
            dataType: 'html',
252
            // Code to run if the request succeeds;
253
            success: function (html) {
254
                var returnHtml = html.split('[BRK]');
255
                $('#article_screenshot_list').html(returnHtml[0]);
256
                window.OSDMessageDisplay(returnHtml[1]);
257
                document.getElementById('screenshot_add_to_article').reset();
258
            }
259
        });
260
    }
261
}
262
263
// the code for the screenshot selector
264
window.myFunction = function () {
265
    $('input:file[id=file_upload]').change(function () {
266
        document.getElementById('file_upload_game_screenshots').value = 'file(s) selected';
267
    });
268
269
    $('input:file[id=file_upload2]').change(function () {
270
        document.getElementById('file_upload_game_file').value = $(this).val();
271
    });
272
}
273
274
// *********************************************************************//
275
// all code for the comments adding on the detail page of the main site //
276
// *********************************************************************//
277
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...
278
    var string = 'latest_comment_edit';
279
    var editableComment = string.concat(commentId);
280
281
    var string4 = 'comment_edit_icon';
282
    var editIcon = string4.concat(commentId);
283
284
    var string2 = 'comment_save_icon';
285
    var saveIcon = string2.concat(commentId);
286
287
    var string3 = 'comment_input';
288
    var commentInput = string3.concat(commentId);
289
290
    var input = document.getElementById(commentInput);
291
    input.style.display = 'inline';
292
293
    var save = document.getElementById(saveIcon);
294
    save.style.display = 'inline';
295
296
    var edit = document.getElementById(editIcon);
297
    edit.style.display = 'none';
298
299
    var output = document.getElementById(editableComment);
300
    output.style.display = 'none';
301
}
302
303
window.SaveEditable = function (commentId, userId, articleId) {
304
    var string = 'comment_input';
305
    var commentData = string.concat(commentId);
306
307
    var comment = document.getElementById(commentData).value
308
    comment = comment.replace(/\n\r?/g, '<br />');
309
310
    var url = '../../main/articles/db_articles_detail.php?action=save_comment&comment_id=' + commentId + '&data=' + comment + '&article_id=' + articleId;
311
    var xmlhttp;
312
    var ActiveXObject;
313
314
    if (commentId === '') {
315
        document.getElementById('latest_comments_all').innerHTML = '';
316
    } else {
317
        if (window.XMLHttpRequest) {
318
            // code for IE7+, Firefox, Chrome, Opera, Safari
319
            xmlhttp = new XMLHttpRequest();
320
        } else {
321
            // code for IE6, IE5
322
            xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
0 ignored issues
show
Bug introduced by
The variable ActiveXObject seems to be never initialized.
Loading history...
323
        }
324
        xmlhttp.onreadystatechange = function () {
325
            if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
326
                document.getElementById('latest_comments_all').innerHTML = xmlhttp.responseText;
327
            }
328
        }
329
        xmlhttp.open('GET', url, true);
330
        xmlhttp.send();
331
    }
332
}
333
334
window.DeleteEditable = function (commentId, userId, articleId) {
335
    var xmlhttp;
336
    var ActiveXObject;
337
338
    var message = 'Are you sure you want to delete this comment?';
339
    var returnValue = confirm(message);
0 ignored issues
show
Debugging Code Best Practice introduced by
The confirm UI element is often considered obtrusive and is generally only used as a temporary measure. Consider replacing it with another UI element.
Loading history...
340
341
    if (returnValue !== '0') {
342
        if (commentId === '') {
343
            document.getElementById('latest_comments_all').innerHTML = '';
344
        } else {
345
            if (window.XMLHttpRequest) {
346
                // code for IE7+, Firefox, Chrome, Opera, Safari
347
                xmlhttp = new XMLHttpRequest();
348
            } else {
349
                // code for IE6, IE5
350
                xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
0 ignored issues
show
Bug introduced by
The variable ActiveXObject seems to be never initialized.
Loading history...
351
            }
352
            xmlhttp.onreadystatechange = function () {
353
                if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
354
                    document.getElementById('latest_comments_all').innerHTML = xmlhttp.responseText;
355
                }
356
            }
357
            xmlhttp.open('GET', '../../main/articles/db_articles_detail.php?action=delete_comment&comment_id=' + commentId + '&article_id=' + articleId, true);
358
            xmlhttp.send();
359
        }
360
    }
361
}
362
363
window.AddComment = function (userId, articleId) {
364
    var xmlhttp;
365
    var ActiveXObject;
366
367
    var commentData = 'comment_add';
368
369
    var comment = document.getElementById(commentData).value
370
    comment = comment.replace(/\n\r?/g, '<br />');
371
372
    var url = '../../main/articles/db_articles_detail.php?action=add_comment&user_id=' + userId + '&data=' + comment + '&article_id=' + articleId;
373
374
    document.getElementById('comment_add').value = '';
375
376
    if (articleId === '') {
377
        document.getElementById('latest_comments_all').innerHTML = '';
378
    } else {
379
        if (window.XMLHttpRequest) {
380
            // code for IE7+, Firefox, Chrome, Opera, Safari
381
            xmlhttp = new XMLHttpRequest();
382
        } else {
383
            // code for IE6, IE5
384
            xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
0 ignored issues
show
Bug introduced by
The variable ActiveXObject seems to be never initialized.
Loading history...
385
        }
386
        xmlhttp.onreadystatechange = function () {
387
            if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
388
                document.getElementById('latest_comments_all').innerHTML = xmlhttp.responseText;
389
            }
390
        }
391
        xmlhttp.open('GET', url, true);
392
        xmlhttp.send();
393
    }
394
}
395