Conditions | 39 |
Paths | 0 |
Total Lines | 410 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 defaults.methods.init 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 | /*! |
||
165 | return this.each(function() { |
||
166 | |||
167 | // Get references to everything we're interested in |
||
168 | var $this = $(this), |
||
169 | $controlGroup = $this.parents(".form-group").first(), |
||
170 | $helpBlock = $controlGroup.find(".help-block").first(), |
||
171 | $form = $this.parents("form").first(), |
||
172 | validatorNames = []; |
||
173 | |||
174 | // create message container if not exists |
||
175 | if (!$helpBlock.length && settings.options.autoAdd && settings.options.autoAdd.helpBlocks) { |
||
176 | $helpBlock = $('<div class="help-block" />'); |
||
177 | $controlGroup.find('.controls').append($helpBlock); |
||
178 | createdElements.push($helpBlock[0]); |
||
179 | } |
||
180 | |||
181 | // ============================================================= |
||
182 | // SNIFF HTML FOR VALIDATORS |
||
183 | // ============================================================= |
||
184 | |||
185 | // *snort sniff snuffle* |
||
186 | |||
187 | if (settings.options.sniffHtml) { |
||
188 | var message = ""; |
||
189 | // --------------------------------------------------------- |
||
190 | // PATTERN |
||
191 | // --------------------------------------------------------- |
||
192 | if ($this.attr("pattern") !== undefined) { |
||
193 | message = "Not in the expected format<!-- data-validation-pattern-message to override -->"; |
||
194 | if ($this.data("validationPatternMessage")) { |
||
195 | message = $this.data("validationPatternMessage"); |
||
196 | } |
||
197 | $this.data("validationPatternMessage", message); |
||
198 | $this.data("validationPatternRegex", $this.attr("pattern")); |
||
199 | } |
||
200 | // --------------------------------------------------------- |
||
201 | // MAX |
||
202 | // --------------------------------------------------------- |
||
203 | if ($this.attr("max") !== undefined || $this.attr("aria-valuemax") !== undefined) { |
||
204 | var max = ($this.attr("max") !== undefined ? $this.attr("max") : $this.attr("aria-valuemax")); |
||
205 | message = "Too high: Maximum of '" + max + "'<!-- data-validation-max-message to override -->"; |
||
206 | if ($this.data("validationMaxMessage")) { |
||
207 | message = $this.data("validationMaxMessage"); |
||
208 | } |
||
209 | $this.data("validationMaxMessage", message); |
||
210 | $this.data("validationMaxMax", max); |
||
211 | } |
||
212 | // --------------------------------------------------------- |
||
213 | // MIN |
||
214 | // --------------------------------------------------------- |
||
215 | if ($this.attr("min") !== undefined || $this.attr("aria-valuemin") !== undefined) { |
||
216 | var min = ($this.attr("min") !== undefined ? $this.attr("min") : $this.attr("aria-valuemin")); |
||
217 | message = "Too low: Minimum of '" + min + "'<!-- data-validation-min-message to override -->"; |
||
218 | if ($this.data("validationMinMessage")) { |
||
219 | message = $this.data("validationMinMessage"); |
||
220 | } |
||
221 | $this.data("validationMinMessage", message); |
||
222 | $this.data("validationMinMin", min); |
||
223 | } |
||
224 | // --------------------------------------------------------- |
||
225 | // MAXLENGTH |
||
226 | // --------------------------------------------------------- |
||
227 | if ($this.attr("maxlength") !== undefined) { |
||
228 | message = "Too long: Maximum of '" + $this.attr("maxlength") + "' characters<!-- data-validation-maxlength-message to override -->"; |
||
229 | if ($this.data("validationMaxlengthMessage")) { |
||
230 | message = $this.data("validationMaxlengthMessage"); |
||
231 | } |
||
232 | $this.data("validationMaxlengthMessage", message); |
||
233 | $this.data("validationMaxlengthMaxlength", $this.attr("maxlength")); |
||
234 | } |
||
235 | // --------------------------------------------------------- |
||
236 | // MINLENGTH |
||
237 | // --------------------------------------------------------- |
||
238 | if ($this.attr("minlength") !== undefined) { |
||
239 | message = "Too short: Minimum of '" + $this.attr("minlength") + "' characters<!-- data-validation-minlength-message to override -->"; |
||
240 | if ($this.data("validationMinlengthMessage")) { |
||
241 | message = $this.data("validationMinlengthMessage"); |
||
242 | } |
||
243 | $this.data("validationMinlengthMessage", message); |
||
244 | $this.data("validationMinlengthMinlength", $this.attr("minlength")); |
||
245 | } |
||
246 | // --------------------------------------------------------- |
||
247 | // REQUIRED |
||
248 | // --------------------------------------------------------- |
||
249 | if ($this.attr("required") !== undefined || $this.attr("aria-required") !== undefined) { |
||
250 | message = settings.builtInValidators.required.message; |
||
251 | if ($this.data("validationRequiredMessage")) { |
||
252 | message = $this.data("validationRequiredMessage"); |
||
253 | } |
||
254 | $this.data("validationRequiredMessage", message); |
||
255 | } |
||
256 | // --------------------------------------------------------- |
||
257 | // NUMBER |
||
258 | // --------------------------------------------------------- |
||
259 | if ($this.attr("type") !== undefined && $this.attr("type").toLowerCase() === "number") { |
||
260 | message = settings.builtInValidators.number.message; |
||
261 | if ($this.data("validationNumberMessage")) { |
||
262 | message = $this.data("validationNumberMessage"); |
||
263 | } |
||
264 | $this.data("validationNumberMessage", message); |
||
265 | } |
||
266 | // --------------------------------------------------------- |
||
267 | |||
268 | // --------------------------------------------------------- |
||
269 | if ($this.attr("type") !== undefined && $this.attr("type").toLowerCase() === "email") { |
||
270 | message = "Not a valid email address<!-- data-validator-validemail-message to override -->"; |
||
271 | if ($this.data("validationValidemailMessage")) { |
||
272 | message = $this.data("validationValidemailMessage"); |
||
273 | } else if ($this.data("validationEmailMessage")) { |
||
274 | message = $this.data("validationEmailMessage"); |
||
275 | } |
||
276 | $this.data("validationValidemailMessage", message); |
||
277 | } |
||
278 | // --------------------------------------------------------- |
||
279 | // MINCHECKED |
||
280 | // --------------------------------------------------------- |
||
281 | if ($this.attr("minchecked") !== undefined) { |
||
282 | message = "Not enough options checked; Minimum of '" + $this.attr("minchecked") + "' required<!-- data-validation-minchecked-message to override -->"; |
||
283 | if ($this.data("validationMincheckedMessage")) { |
||
284 | message = $this.data("validationMincheckedMessage"); |
||
285 | } |
||
286 | $this.data("validationMincheckedMessage", message); |
||
287 | $this.data("validationMincheckedMinchecked", $this.attr("minchecked")); |
||
288 | } |
||
289 | // --------------------------------------------------------- |
||
290 | // MAXCHECKED |
||
291 | // --------------------------------------------------------- |
||
292 | if ($this.attr("maxchecked") !== undefined) { |
||
293 | message = "Too many options checked; Maximum of '" + $this.attr("maxchecked") + "' required<!-- data-validation-maxchecked-message to override -->"; |
||
294 | if ($this.data("validationMaxcheckedMessage")) { |
||
295 | message = $this.data("validationMaxcheckedMessage"); |
||
296 | } |
||
297 | $this.data("validationMaxcheckedMessage", message); |
||
298 | $this.data("validationMaxcheckedMaxchecked", $this.attr("maxchecked")); |
||
299 | } |
||
300 | } |
||
301 | |||
302 | // ============================================================= |
||
303 | // COLLECT VALIDATOR NAMES |
||
304 | // ============================================================= |
||
305 | |||
306 | // Get named validators |
||
307 | if ($this.data("validation") !== undefined) { |
||
308 | validatorNames = $this.data("validation").split(","); |
||
309 | } |
||
310 | |||
311 | // Get extra ones defined on the element's data attributes |
||
312 | $.each($this.data(), function(i, el) { |
||
313 | var parts = i.replace(/([A-Z])/g, ",$1").split(","); |
||
314 | if (parts[0] === "validation" && parts[1]) { |
||
315 | validatorNames.push(parts[1]); |
||
316 | } |
||
317 | }); |
||
318 | |||
319 | // ============================================================= |
||
320 | // NORMALISE VALIDATOR NAMES |
||
321 | // ============================================================= |
||
322 | |||
323 | var validatorNamesToInspect = validatorNames; |
||
324 | var newValidatorNamesToInspect = []; |
||
325 | |||
326 | do // repeatedly expand 'shortcut' validators into their real validators |
||
327 | { |
||
328 | // Uppercase only the first letter of each name |
||
329 | $.each(validatorNames, function(i, el) { |
||
330 | validatorNames[i] = formatValidatorName(el); |
||
331 | }); |
||
332 | |||
333 | // Remove duplicate validator names |
||
334 | validatorNames = $.unique(validatorNames); |
||
335 | |||
336 | // Pull out the new validator names from each shortcut |
||
337 | newValidatorNamesToInspect = []; |
||
338 | $.each(validatorNamesToInspect, function(i, el) { |
||
339 | if ($this.data("validation" + el + "Shortcut") !== undefined) { |
||
340 | // Are these custom validators? |
||
341 | // Pull them out! |
||
342 | $.each($this.data("validation" + el + "Shortcut").split(","), function(i2, el2) { |
||
343 | newValidatorNamesToInspect.push(el2); |
||
344 | }); |
||
345 | } else if (settings.builtInValidators[el.toLowerCase()]) { |
||
346 | // Is this a recognised built-in? |
||
347 | // Pull it out! |
||
348 | var validator = settings.builtInValidators[el.toLowerCase()]; |
||
349 | if (validator.type.toLowerCase() === "shortcut") { |
||
350 | $.each(validator.shortcut.split(","), function(i, el) { |
||
351 | el = formatValidatorName(el); |
||
352 | newValidatorNamesToInspect.push(el); |
||
353 | validatorNames.push(el); |
||
354 | }); |
||
355 | } |
||
356 | } |
||
357 | }); |
||
358 | |||
359 | validatorNamesToInspect = newValidatorNamesToInspect; |
||
360 | |||
361 | } while (validatorNamesToInspect.length > 0) |
||
362 | |||
363 | // ============================================================= |
||
364 | // SET UP VALIDATOR ARRAYS |
||
365 | // ============================================================= |
||
366 | |||
367 | var validators = {}; |
||
368 | |||
369 | $.each(validatorNames, function(i, el) { |
||
370 | // Set up the 'override' message |
||
371 | var message = $this.data("validation" + el + "Message"); |
||
372 | var hasOverrideMessage = (message !== undefined); |
||
373 | var foundValidator = false; |
||
374 | message = |
||
375 | ( |
||
376 | message ? message : "'" + el + "' validation failed <!-- Add attribute 'data-validation-" + el.toLowerCase() + "-message' to input to change this message -->" |
||
377 | ); |
||
378 | |||
379 | $.each( |
||
380 | settings.validatorTypes, |
||
381 | function(validatorType, validatorTemplate) { |
||
382 | if (validators[validatorType] === undefined) { |
||
383 | validators[validatorType] = []; |
||
384 | } |
||
385 | if (!foundValidator && $this.data("validation" + el + formatValidatorName(validatorTemplate.name)) !== undefined) { |
||
386 | validators[validatorType].push( |
||
387 | $.extend( |
||
388 | true, { |
||
389 | name: formatValidatorName(validatorTemplate.name), |
||
390 | message: message |
||
391 | }, |
||
392 | validatorTemplate.init($this, el) |
||
393 | ) |
||
394 | ); |
||
395 | foundValidator = true; |
||
396 | } |
||
397 | } |
||
398 | ); |
||
399 | |||
400 | if (!foundValidator && settings.builtInValidators[el.toLowerCase()]) { |
||
401 | |||
402 | var validator = $.extend(true, {}, settings.builtInValidators[el.toLowerCase()]); |
||
403 | if (hasOverrideMessage) { |
||
404 | validator.message = message; |
||
405 | } |
||
406 | var validatorType = validator.type.toLowerCase(); |
||
407 | |||
408 | if (validatorType === "shortcut") { |
||
409 | foundValidator = true; |
||
410 | } else { |
||
411 | $.each( |
||
412 | settings.validatorTypes, |
||
413 | function(validatorTemplateType, validatorTemplate) { |
||
414 | if (validators[validatorTemplateType] === undefined) { |
||
415 | validators[validatorTemplateType] = []; |
||
416 | } |
||
417 | if (!foundValidator && validatorType === validatorTemplateType.toLowerCase()) { |
||
418 | $this.data("validation" + el + formatValidatorName(validatorTemplate.name), validator[validatorTemplate.name.toLowerCase()]); |
||
419 | validators[validatorType].push( |
||
420 | $.extend( |
||
421 | validator, |
||
422 | validatorTemplate.init($this, el) |
||
423 | ) |
||
424 | ); |
||
425 | foundValidator = true; |
||
426 | } |
||
427 | } |
||
428 | ); |
||
429 | } |
||
430 | } |
||
431 | |||
432 | if (!foundValidator) { |
||
433 | $.error("Cannot find validation info for '" + el + "'"); |
||
434 | } |
||
435 | }); |
||
436 | |||
437 | // ============================================================= |
||
438 | // STORE FALLBACK VALUES |
||
439 | // ============================================================= |
||
440 | |||
441 | $helpBlock.data( |
||
442 | "original-contents", ( |
||
443 | $helpBlock.data("original-contents") ? $helpBlock.data("original-contents") : $helpBlock.html() |
||
444 | ) |
||
445 | ); |
||
446 | |||
447 | $helpBlock.data( |
||
448 | "original-role", ( |
||
449 | $helpBlock.data("original-role") ? $helpBlock.data("original-role") : $helpBlock.attr("role") |
||
450 | ) |
||
451 | ); |
||
452 | |||
453 | $controlGroup.data( |
||
454 | "original-classes", ( |
||
455 | $controlGroup.data("original-clases") ? $controlGroup.data("original-classes") : $controlGroup.attr("class") |
||
456 | ) |
||
457 | ); |
||
458 | |||
459 | $this.data( |
||
460 | "original-aria-invalid", ( |
||
461 | $this.data("original-aria-invalid") ? $this.data("original-aria-invalid") : $this.attr("aria-invalid") |
||
462 | ) |
||
463 | ); |
||
464 | |||
465 | // ============================================================= |
||
466 | // VALIDATION |
||
467 | // ============================================================= |
||
468 | |||
469 | $this.bind( |
||
470 | "validation.validation", |
||
471 | function(event, params) { |
||
472 | |||
473 | var value = getValue($this); |
||
474 | |||
475 | // Get a list of the errors to apply |
||
476 | var errorsFound = []; |
||
477 | |||
478 | $.each(validators, function(validatorType, validatorTypeArray) { |
||
479 | if (value || value.length || (params && params.includeEmpty) || (!!settings.validatorTypes[validatorType].blockSubmit && params && !!params.submitting)) { |
||
480 | $.each(validatorTypeArray, function(i, validator) { |
||
481 | if (settings.validatorTypes[validatorType].validate($this, value, validator)) { |
||
482 | errorsFound.push(validator.message); |
||
483 | } |
||
484 | }); |
||
485 | } |
||
486 | }); |
||
487 | |||
488 | return errorsFound; |
||
489 | } |
||
490 | ); |
||
491 | |||
492 | $this.bind( |
||
493 | "getValidators.validation", |
||
494 | function() { |
||
495 | return validators; |
||
496 | } |
||
497 | ); |
||
498 | |||
499 | // ============================================================= |
||
500 | // WATCH FOR CHANGES |
||
501 | // ============================================================= |
||
502 | $this.bind( |
||
503 | "submit.validation", |
||
504 | function() { |
||
505 | return $this.triggerHandler("change.validation", { |
||
506 | submitting: true |
||
507 | }); |
||
508 | } |
||
509 | ); |
||
510 | $this.bind( |
||
511 | [ |
||
512 | "keyup", |
||
513 | "focus", |
||
514 | "blur", |
||
515 | "click", |
||
516 | "keydown", |
||
517 | "keypress", |
||
518 | "change" |
||
519 | ].join(".validation ") + ".validation", |
||
520 | function(e, params) { |
||
521 | |||
522 | var value = getValue($this); |
||
523 | |||
524 | var errorsFound = []; |
||
525 | |||
526 | $controlGroup.find("input,textarea,select").each(function(i, el) { |
||
527 | var oldCount = errorsFound.length; |
||
528 | $.each($(el).triggerHandler("validation.validation", params), function(j, message) { |
||
529 | errorsFound.push(message); |
||
530 | }); |
||
531 | if (errorsFound.length > oldCount) { |
||
532 | $(el).attr("aria-invalid", "true"); |
||
533 | } else { |
||
534 | var original = $this.data("original-aria-invalid"); |
||
535 | $(el).attr("aria-invalid", (original !== undefined ? original : false)); |
||
536 | } |
||
537 | }); |
||
538 | |||
539 | $form.find("input,select,textarea").not($this).not("[name=\"" + $this.attr("name") + "\"]").trigger("validationLostFocus.validation"); |
||
540 | |||
541 | errorsFound = $.unique(errorsFound.sort()); |
||
542 | |||
543 | // Were there any errors? |
||
544 | if (errorsFound.length) { |
||
545 | // Better flag it up as a warning. |
||
546 | $controlGroup.removeClass("success error").addClass("warning"); |
||
547 | |||
548 | // How many errors did we find? |
||
549 | if (settings.options.semanticallyStrict && errorsFound.length === 1) { |
||
550 | // Only one? Being strict? Just output it. |
||
551 | $helpBlock.html(errorsFound[0] + |
||
552 | (settings.options.prependExistingHelpBlock ? $helpBlock.data("original-contents") : "")); |
||
553 | } else { |
||
554 | // Multiple? Being sloppy? Glue them together into an UL. |
||
555 | $helpBlock.html("<ul role=\"alert\"><li>" + errorsFound.join("</li><li>") + "</li></ul>" + |
||
556 | (settings.options.prependExistingHelpBlock ? $helpBlock.data("original-contents") : "")); |
||
557 | } |
||
558 | } else { |
||
559 | $controlGroup.removeClass("warning error success"); |
||
560 | if (value.length > 0) { |
||
561 | $controlGroup.addClass("success"); |
||
562 | } |
||
563 | $helpBlock.html($helpBlock.data("original-contents")); |
||
564 | } |
||
565 | |||
566 | if (e.type === "blur") { |
||
567 | $controlGroup.removeClass("success"); |
||
568 | } |
||
569 | } |
||
570 | ); |
||
571 | $this.bind("validationLostFocus.validation", function() { |
||
572 | $controlGroup.removeClass("success"); |
||
573 | }); |
||
574 | }); |
||
575 | }, |
||
1043 |
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.