| Conditions | 32 |
| Total Lines | 174 |
| Code Lines | 109 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like post.js ➔ onDocSent 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 | /*! |
||
| 202 | function onDocSent(XMLDoc) |
||
| 203 | { |
||
| 204 | var i = 0, |
||
| 205 | n = 0, |
||
| 206 | numErrors = 0, |
||
| 207 | numCaptions = 0, |
||
| 208 | $editor; |
||
| 209 | |||
| 210 | if (!XMLDoc || !XMLDoc.getElementsByTagName('elk')[0]) |
||
| 211 | { |
||
| 212 | document.forms[form_name].preview.onclick = function() {return true;}; |
||
| 213 | document.forms[form_name].preview.click(); |
||
| 214 | return true; |
||
| 215 | } |
||
| 216 | |||
| 217 | // Read the preview section data from the xml response |
||
| 218 | var preview = XMLDoc.getElementsByTagName('elk')[0].getElementsByTagName('preview')[0]; |
||
| 219 | |||
| 220 | // Load in the subject |
||
| 221 | document.getElementById('preview_subject').innerHTML = preview.getElementsByTagName('subject')[0].firstChild.nodeValue; |
||
| 222 | |||
| 223 | // Load in the body |
||
| 224 | var bodyText = ''; |
||
| 225 | for (i = 0, n = preview.getElementsByTagName('body')[0].childNodes.length; i < n; i++) |
||
| 226 | bodyText += preview.getElementsByTagName('body')[0].childNodes[i].nodeValue; |
||
| 227 | |||
| 228 | document.getElementById('preview_body').innerHTML = bodyText; |
||
| 229 | document.getElementById('preview_body').className = 'post'; |
||
| 230 | |||
| 231 | // Show a list of errors (if any). |
||
| 232 | var errors = XMLDoc.getElementsByTagName('elk')[0].getElementsByTagName('errors')[0], |
||
| 233 | errorList = '', |
||
| 234 | errorCode = '', |
||
| 235 | error_area = 'post_error', |
||
| 236 | error_list = error_area + '_list', |
||
| 237 | error_post = false; |
||
| 238 | |||
| 239 | // @todo: this should stay together with the rest of the error handling or |
||
| 240 | // should use errorbox_handler (at the moment it cannot be used because is not enough generic) |
||
| 241 | for (i = 0, numErrors = errors.getElementsByTagName('error').length; i < numErrors; i++) |
||
| 242 | { |
||
| 243 | errorCode = errors.getElementsByTagName('error')[i].attributes.getNamedItem("code").value; |
||
| 244 | if (errorCode === 'no_message' || errorCode === 'long_message') |
||
| 245 | error_post = true; |
||
| 246 | errorList += '<li id="' + error_area + '_' + errorCode + '" class="error">' + errors.getElementsByTagName('error')[i].firstChild.nodeValue + '</li>'; |
||
| 247 | } |
||
| 248 | |||
| 249 | var oError_box = $(document.getElementById(error_area)); |
||
| 250 | if ($.trim(oError_box.children(error_list).html()) === '') |
||
| 251 | oError_box.append("<ul id='" + error_list + "'></ul>"); |
||
| 252 | |||
| 253 | // Add the error it and show it |
||
| 254 | if (numErrors === 0) |
||
| 255 | oError_box.css("display", "none"); |
||
| 256 | else |
||
| 257 | { |
||
| 258 | document.getElementById(error_list).innerHTML = errorList; |
||
| 259 | oError_box.css("display", ""); |
||
| 260 | oError_box.attr('class', parseInt(errors.getAttribute('serious')) === 0 ? 'warningbox' : 'errorbox'); |
||
| 261 | } |
||
| 262 | |||
| 263 | // Show a warning if the topic has been locked. |
||
| 264 | if (bPost) |
||
| 265 | document.getElementById('lock_warning').style.display = parseInt(errors.getAttribute('topic_locked')) === 1 ? '' : 'none'; |
||
| 266 | |||
| 267 | // Adjust the color of captions if the given data is erroneous. |
||
| 268 | var captions = errors.getElementsByTagName('caption'); |
||
| 269 | for (i = 0, numCaptions = errors.getElementsByTagName('caption').length; i < numCaptions; i++) |
||
| 270 | { |
||
| 271 | if (document.getElementById('caption_' + captions[i].getAttribute('name'))) |
||
| 272 | document.getElementById('caption_' + captions[i].getAttribute('name')).className = captions[i].getAttribute('class'); |
||
| 273 | } |
||
| 274 | |||
| 275 | if (typeof $editor_container[post_box_name] !== 'undefined') |
||
| 276 | $editor = $editor_container[post_box_name]; |
||
| 277 | else |
||
| 278 | $editor = $(document.forms[form_name][post_box_name]); |
||
| 279 | |||
| 280 | if (error_post) |
||
| 281 | $editor.find("textarea, iframe").addClass('border_error'); |
||
| 282 | else |
||
| 283 | $editor.find("textarea, iframe").removeClass('border_error'); |
||
| 284 | |||
| 285 | // If this is a post preview, then we have some extra work to do |
||
| 286 | if (bPost) |
||
| 287 | { |
||
| 288 | // Set the new last message id. |
||
| 289 | if ('last_msg' in document.forms[form_name]) |
||
| 290 | document.forms[form_name].last_msg.value = XMLDoc.getElementsByTagName('elk')[0].getElementsByTagName('last_msg')[0].firstChild.nodeValue; |
||
| 291 | |||
| 292 | var new_replies = [], |
||
| 293 | ignored_replies = [], |
||
| 294 | ignoring = null, |
||
| 295 | newPosts = XMLDoc.getElementsByTagName('elk')[0].getElementsByTagName('new_posts')[0] ? XMLDoc.getElementsByTagName('elk')[0].getElementsByTagName('new_posts')[0].getElementsByTagName('post') : {length: 0}, |
||
| 296 | numNewPosts = newPosts.length; |
||
| 297 | |||
| 298 | if (numNewPosts !== 0) |
||
| 299 | { |
||
| 300 | var newPostsHTML = '<span id="new_replies"><' + '/span>'; |
||
| 301 | for (i = 0; i < numNewPosts; i++) |
||
| 302 | { |
||
| 303 | new_replies[new_replies.length] = newPosts[i].getAttribute("id"); |
||
| 304 | |||
| 305 | ignoring = false; |
||
| 306 | if (newPosts[i].getElementsByTagName("is_ignored")[0].firstChild.nodeValue !== '0') |
||
| 307 | ignored_replies[ignored_replies.length] = ignoring = newPosts[i].getAttribute("id"); |
||
| 308 | |||
| 309 | newPostsHTML += '<div class="content' + (++reply_counter % 2 === 0 ? '2' : '') + '"><div class="postarea2" id="msg' + newPosts[i].getAttribute("id") + '"><div class="keyinfo">'; |
||
| 310 | newPostsHTML += '<h5 class="floatleft"><span>' + txt_posted_by + '</span> ' + newPosts[i].getElementsByTagName("poster")[0].firstChild.nodeValue + ' - ' + newPosts[i].getElementsByTagName("time")[0].firstChild.nodeValue; |
||
| 311 | newPostsHTML += ' <span class="new_posts" id="image_new_' + newPosts[i].getAttribute("id") + '">' + txt_new + '</span></h5>'; |
||
| 312 | |||
| 313 | if (can_quote) |
||
| 314 | newPostsHTML += '<ul class="quickbuttons" id="msg_' + newPosts[i].getAttribute('id') + '_quote"><li class="listlevel1"><a href="#postmodify" onmousedown="return insertQuoteFast(' + newPosts[i].getAttribute('id') + ');" class="linklevel1 quote_button">' + txt_bbc_quote + '</a></li></ul>'; |
||
| 315 | |||
| 316 | newPostsHTML += '</div>'; |
||
| 317 | |||
| 318 | if (ignoring) |
||
| 319 | newPostsHTML += '<div id="msg_' + newPosts[i].getAttribute("id") + '_ignored_prompt">' + txt_ignoring_user + '<a href="#" id="msg_' + newPosts[i].getAttribute("id") + '_ignored_link" class="hide">' + show_ignore_user_post + '</a></div>'; |
||
| 320 | |||
| 321 | newPostsHTML += '<div class="inner" id="msg_' + newPosts[i].getAttribute("id") + '_body">' + newPosts[i].getElementsByTagName("message")[0].firstChild.nodeValue + '</div></div></div>'; |
||
| 322 | } |
||
| 323 | setOuterHTML(document.getElementById('new_replies'), newPostsHTML); |
||
| 324 | } |
||
| 325 | |||
| 326 | // Remove the new image from old-new replies! |
||
| 327 | for (i = 0; i < new_replies.length; i++) |
||
| 328 | document.getElementById('image_new_' + new_replies[i]).style.display = 'none'; |
||
| 329 | |||
| 330 | var numIgnoredReplies = ignored_replies.length; |
||
| 331 | if (numIgnoredReplies !== 0) |
||
| 332 | { |
||
| 333 | for (i = 0; i < numIgnoredReplies; i++) |
||
| 334 | { |
||
| 335 | aIgnoreToggles[ignored_replies[i]] = new elk_Toggle({ |
||
| 336 | bToggleEnabled: true, |
||
| 337 | bCurrentlyCollapsed: true, |
||
| 338 | aSwappableContainers: [ |
||
| 339 | 'msg_' + ignored_replies[i] + '_body', |
||
| 340 | 'msg_' + ignored_replies[i] + '_quote' |
||
| 341 | ], |
||
| 342 | aSwapLinks: [ |
||
| 343 | { |
||
| 344 | sId: 'msg_' + ignored_replies[i] + '_ignored_link', |
||
| 345 | msgExpanded: '', |
||
| 346 | msgCollapsed: show_ignore_user_post |
||
| 347 | } |
||
| 348 | ] |
||
| 349 | }); |
||
| 350 | } |
||
| 351 | } |
||
| 352 | } |
||
| 353 | |||
| 354 | $('html, body').animate({ scrollTop: $('#preview_section').offset().top }, 'slow'); |
||
| 355 | |||
| 356 | // Preview video links if the feature is available |
||
| 357 | if ($.isFunction($.fn.linkifyvideo)) |
||
| 358 | $().linkifyvideo(oEmbedtext, 'preview_body'); |
||
| 359 | |||
| 360 | // Spoilers, Sweetie |
||
| 361 | $('.spoilerheader').on('click', function(){ |
||
| 362 | $(this).next().children().slideToggle("fast"); |
||
| 363 | }); |
||
| 364 | |||
| 365 | // Fix and Prettify code blocks |
||
| 366 | if (typeof elk_codefix === 'function') |
||
| 367 | elk_codefix(); |
||
| 368 | if (typeof prettyPrint === 'function') |
||
| 369 | prettyPrint(); |
||
| 370 | |||
| 371 | // Prevent lighbox or default action on the preview |
||
| 372 | $('[data-lightboximage]').on('click.elk_lb', function(e) { |
||
| 373 | e.preventDefault(); |
||
| 374 | }); |
||
| 375 | } |
||
| 376 | |||
| 490 |
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.