Author: jogep Date: Wed Mar 9 19:19:33 2011 New Revision: 1079950 URL: http://svn.apache.org/viewvc?rev=1079950&view=rev Log: WW-3437: JavaScript should following the JSLint recommendations
Modified: struts/struts2/trunk/core/src/main/resources/org/apache/struts2/static/utils.js struts/struts2/trunk/core/src/main/resources/template/css_xhtml/validation.js struts/struts2/trunk/core/src/main/resources/template/xhtml/validation.js Modified: struts/struts2/trunk/core/src/main/resources/org/apache/struts2/static/utils.js URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/resources/org/apache/struts2/static/utils.js?rev=1079950&r1=1079949&r2=1079950&view=diff ============================================================================== --- struts/struts2/trunk/core/src/main/resources/org/apache/struts2/static/utils.js (original) +++ struts/struts2/trunk/core/src/main/resources/org/apache/struts2/static/utils.js Wed Mar 9 19:19:33 2011 @@ -24,7 +24,7 @@ var StrutsUtils = {}; // gets an object with validation errors from string returned by // the ajaxValidation interceptor StrutsUtils.getValidationErrors = function(data) { - if(data.indexOf("/* {") == 0) { + if(data.indexOf("/* {") === 0) { return eval("( " + data.substring(2, data.length - 2) + " )"); } else { return null; @@ -33,7 +33,7 @@ StrutsUtils.getValidationErrors = functi StrutsUtils.clearValidationErrors = function(form) { var firstNode = StrutsUtils.firstElement(form); - var xhtml = firstNode.tagName.toLowerCase() == "table"; + var xhtml = firstNode.tagName.toLowerCase() === "table"; if(xhtml) { clearErrorMessagesXHTML(form); @@ -59,11 +59,12 @@ StrutsUtils.showValidationErrors = funct StrutsUtils.clearValidationErrors(form, errors); if (errors.errors) { - var errorList = document.createElement("ul"); + var l, errorList = document.createElement("ul"); + errorList.setAttribute("class", "errorMessage"); errorList.setAttribute("className", "errorMessage"); // ie hack cause ie does not support setAttribute - for ( var l = 0; l < errors.errors.length; l++) { + for ( l = 0; l < errors.errors.length; l++) { var item = document.createElement("li"); var itemText = document.createTextNode(errors.errors[l]); item.appendChild(itemText); @@ -75,16 +76,18 @@ StrutsUtils.showValidationErrors = funct StrutsUtils.errorLists[form] = errorList; } - var firstNode = StrutsUtils.firstElement(form); - var xhtml = firstNode.tagName.toLowerCase() == "table"; + var i, fieldName, firstNode = StrutsUtils.firstElement(form); + var xhtml = firstNode.tagName.toLowerCase() === "table"; if(errors.fieldErrors) { - for(var fieldName in errors.fieldErrors) { - for(var i = 0; i < errors.fieldErrors[fieldName].length; i++) { - if(xhtml) { - addErrorXHTML(form.elements[fieldName], errors.fieldErrors[fieldName][i]); - } else { - addErrorCSS(form.elements[fieldName], errors.fieldErrors[fieldName][i]); - } + for(fieldName in errors.fieldErrors) { + if(errors.fieldErrors.hasOwnProperty(fieldName)) { + for(i = 0; i < errors.fieldErrors[fieldName].length; i++) { + if(xhtml) { + addErrorXHTML(form.elements[fieldName], errors.fieldErrors[fieldName][i]); + } else { + addErrorCSS(form.elements[fieldName], errors.fieldErrors[fieldName][i]); + } + } } } } @@ -92,10 +95,10 @@ StrutsUtils.showValidationErrors = funct StrutsUtils.firstElement = function(parentNode, tagName) { var node = parentNode.firstChild; - while(node && node.nodeType != 1){ + while(node && node.nodeType !== 1){ node = node.nextSibling; } - if(tagName && node && node.tagName && node.tagName.toLowerCase() != tagName.toLowerCase()) { + if(tagName && node && node.tagName && node.tagName.toLowerCase() !== tagName.toLowerCase()) { node = StrutsUtils.nextElement(node, tagName); } return node; @@ -105,9 +108,9 @@ StrutsUtils.nextElement = function(node, if(!node) { return null; } do { node = node.nextSibling; - } while(node && node.nodeType != 1); + } while(node && node.nodeType !== 1); - if(node && tagName && tagName.toLowerCase() != node.tagName.toLowerCase()) { + if(node && tagName && tagName.toLowerCase() !== node.tagName.toLowerCase()) { return StrutsUtils.nextElement(node, tagName); } return node; @@ -118,9 +121,9 @@ StrutsUtils.previousElement = function(n if(tagName) { tagName = tagName.toLowerCase(); } do { node = node.previousSibling; - } while(node && node.nodeType != 1); + } while(node && node.nodeType !== 1); - if(node && tagName && tagName.toLowerCase() != node.tagName.toLowerCase()) { + if(node && tagName && tagName.toLowerCase() !== node.tagName.toLowerCase()) { return StrutsUtils.previousElement(node, tagName); } return node; @@ -128,13 +131,13 @@ StrutsUtils.previousElement = function(n StrutsUtils.addOnLoad = function(func) { var oldonload = window.onload; - if (typeof window.onload != 'function') { + if (typeof window.onload !== 'function') { window.onload = func; } else { window.onload = function() { oldonload(); func(); - } + }; } }; Modified: struts/struts2/trunk/core/src/main/resources/template/css_xhtml/validation.js URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/resources/template/css_xhtml/validation.js?rev=1079950&r1=1079949&r2=1079950&view=diff ============================================================================== --- struts/struts2/trunk/core/src/main/resources/template/css_xhtml/validation.js (original) +++ struts/struts2/trunk/core/src/main/resources/template/css_xhtml/validation.js Wed Mar 9 19:19:33 2011 @@ -19,17 +19,16 @@ * under the License. */ -function clearErrorMessages(form) { - clearErrorMessagesCSS(form); -} +var firstFieldErrorPosition = null; function clearErrorMessagesCSS(form) { firstFieldErrorPosition = null; // clear out any rows with an "errorFor" attribute - var divs = form.getElementsByTagName("div"); - var paragraphsToDelete = new Array(); + var i, + divs = form.getElementsByTagName("div"), + paragraphsToDelete = []; - for(var i = 0; i < divs.length; i++) { + for(i = 0; i < divs.length; i++) { var p = divs[i]; if (p.getAttribute("errorFor")) { paragraphsToDelete.push(p); @@ -37,24 +36,25 @@ function clearErrorMessagesCSS(form) { } // now delete the paragraphsToDelete - for (var i = 0; i < paragraphsToDelete.length; i++) { + for (i = 0; i < paragraphsToDelete.length; i++) { var r = paragraphsToDelete[i]; var parent = r.parentNode; parent.removeChild(r); } } -function clearErrorLabels(form) { - clearErrorLabelsCSS(form); +function clearErrorMessages(form) { + clearErrorMessagesCSS(form); } function clearErrorLabelsCSS(form) { // set all labels back to the normal class - var labels = form.getElementsByTagName("label"); - for (var i = 0; i < labels.length; i++) { + var i, + labels = form.getElementsByTagName("label"); + for (i = 0; i < labels.length; i++) { var label = labels[i]; if (label) { - if(label.getAttribute("class") == "errorLabel"){ + if(label.getAttribute("class") === "errorLabel"){ label.setAttribute("class", "label");//standard way.. works for ie mozilla label.setAttribute("className", "label"); //ie hack cause ie does not support setAttribute } @@ -62,21 +62,64 @@ function clearErrorLabelsCSS(form) { } } -function addError(e, errorText) { - addErrorCSS(e, errorText); +function clearErrorLabels(form) { + clearErrorLabelsCSS(form); +} + +function findWWGrpNode(elem) { + while (elem.parentNode) { + elem = elem.parentNode; + + if (elem.className && elem.className.match(/wwgrp/)) { + return elem; + } + } + return null; +} + +function findWWCtrlNode(enclosingDiv) { + var elems = enclosingDiv.getElementsByTagName("div"); + for(i = 0; i < elems.length; ++i ) { + if (elems[i].className && elems[i].className.match(/(wwlbl|wwctrl)/)) { + return elems[i]; + } + } + + elems = enclosingDiv.getElementsByTagName("span"); + for(i = 0; i < elems.length; ++i ) { + if (elems[i].className && elems[i].className.match(/(wwlbl|wwctrl)/)) { + return elems[i]; + } + } + return enclosingDiv.getElementsByTagName("span")[0]; +} + +//find field position in the form +function findFieldPosition(elem) { + if (!elem.form) { + alert("no form for " + elem); + } + + var form = elem.form; + for (i = 0; i < form.elements.length; i++) { + if (form.elements[i].name === elem.name) { + return i; + } + } + return null; } -var firstFieldErrorPosition = null; function addErrorCSS(e, errorText) { try { - if (!e) + if (!e) { return; //ignore errors for fields that are not in the form + } var elem = (e.type ? e : e[0]); //certain input types return node list, while we single first node. I.e. set of radio buttons. var enclosingDiv = findWWGrpNode(elem); // find wwgrp div/span //try to focus on first field var fieldPos = findFieldPosition(elem); - if (fieldPos != null && (firstFieldErrorPosition == null || firstFieldErrorPosition > fieldPos)) { + if (fieldPos !== null && (firstFieldErrorPosition === null || firstFieldErrorPosition > fieldPos)) { firstFieldErrorPosition = fieldPos; } @@ -84,7 +127,7 @@ function addErrorCSS(e, errorText) { alert("Could not validate: " + e.id); return; } - + var label = enclosingDiv.getElementsByTagName("label")[0]; if (label) { label.setAttribute("class", "errorLabel"); //standard way.. works for ie mozilla @@ -92,7 +135,7 @@ function addErrorCSS(e, errorText) { } var firstCtrNode = findWWCtrlNode(enclosingDiv); // either wwctrl_ or wwlbl_ - + var error = document.createTextNode(errorText); var errorDiv = document.createElement("div"); @@ -100,7 +143,7 @@ function addErrorCSS(e, errorText) { errorDiv.setAttribute("className", "errorMessage");//ie hack cause ie does not support setAttribute errorDiv.setAttribute("errorFor", elem.id); errorDiv.appendChild(error); - if(!firstCtrNode && navigator.appName == 'Microsoft Internet Explorer') { + if(!firstCtrNode && navigator.appName === 'Microsoft Internet Explorer') { enclosingDiv.insertBefore(errorDiv); } else { enclosingDiv.insertBefore(errorDiv, firstCtrNode); @@ -110,51 +153,15 @@ function addErrorCSS(e, errorText) { } } -function findWWGrpNode(elem) { - while (elem.parentNode) { - elem = elem.parentNode; - - if (elem.className && elem.className.match(/wwgrp/)) - return elem; - } - return null; -} - -function findWWCtrlNode(enclosingDiv) { - var elems = enclosingDiv.getElementsByTagName("div"); - for(i = 0; i < elems.length; ++i ) { - if (elems[i].className && elems[i].className.match(/(wwlbl|wwctrl)/)) - return elems[i]; - } - - elems = enclosingDiv.getElementsByTagName("span"); - for(i = 0; i < elems.length; ++i ) { - if (elems[i].className && elems[i].className.match(/(wwlbl|wwctrl)/)) - return elems[i]; - } - return enclosingDiv.getElementsByTagName("span")[0]; -} - -//find field position in the form -function findFieldPosition(elem) { - if (!elem.form) { - alert("no form for " + elem); - } - - var form = elem.form; - for (i = 0; i < form.elements.length; i++) { - if (form.elements[i].name == elem.name) { - return i; - } - } - return null; +function addError(e, errorText) { + addErrorCSS(e, errorText); } //focus first element var StrutsUtils_showValidationErrors = StrutsUtils.showValidationErrors; StrutsUtils.showValidationErrors = function(form, errors) { StrutsUtils_showValidationErrors(form, errors); - if (firstFieldErrorPosition != null && form.elements[firstFieldErrorPosition].focus) { + if (firstFieldErrorPosition !== null && form.elements[firstFieldErrorPosition].focus) { form.elements[firstFieldErrorPosition].focus(); } -} +}; Modified: struts/struts2/trunk/core/src/main/resources/template/xhtml/validation.js URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/resources/template/xhtml/validation.js?rev=1079950&r1=1079949&r2=1079950&view=diff ============================================================================== --- struts/struts2/trunk/core/src/main/resources/template/xhtml/validation.js (original) +++ struts/struts2/trunk/core/src/main/resources/template/xhtml/validation.js Wed Mar 9 19:19:33 2011 @@ -19,64 +19,60 @@ * under the License. */ -function clearErrorMessages(form) { - clearErrorMessagesXHTML(form); -} - function clearErrorMessagesXHTML(form) { // get field table - var table; - for (var i = 0; i < form.childNodes.length; i++) { - if (form.childNodes[i].tagName != null && form.childNodes[i].tagName.toLowerCase() == 'table') { + var table, i, r; + for (i = 0; i < form.childNodes.length; i++) { + if (form.childNodes[i].tagName !== undefined && form.childNodes[i].tagName.toLowerCase() === 'table') { table = form.childNodes[i]; break; } } - if (table == null) { + if (table === null) { return; } // clear out any rows with an "errorFor" attribute var rows = table.rows; - if (rows == null){ + if (rows === null){ return; } - var rowsToDelete = new Array(); - for(var i = 0; i < rows.length; i++) { - var r = rows[i]; + var rowsToDelete = []; + for(i = 0; i < rows.length; i++) { + r = rows[i]; // allow blank errorFor values on dojo markup - if (r.getAttribute("errorFor") != null) { + if (r.getAttribute("errorFor") !== null) { rowsToDelete.push(r); } } // now delete the rows - for (var i = 0; i < rowsToDelete.length; i++) { - var r = rowsToDelete[i]; + for (i = 0; i < rowsToDelete.length; i++) { + r = rowsToDelete[i]; table.deleteRow(r.rowIndex); - //table.removeChild(rowsToDelete[i]); + //table.removeChild(rowsToDelete[i]); } } -function clearErrorLabels(form) { - clearErrorLabelsXHTML(form); +function clearErrorMessages(form) { + clearErrorMessagesXHTML(form); } function clearErrorLabelsXHTML(form) { // set all labels back to the normal class - var elements = form.elements; - for (var i = 0; i < elements.length; i++) { + var i, elements = form.elements; + for (i = 0; i < elements.length; i++) { var parentEl = elements[i]; // search for the parent table row, abort if the form is reached // the form may contain "non-wrapped" inputs inserted by Dojo - while (parentEl.nodeName.toUpperCase() != "TR" && parentEl.nodeName.toUpperCase() != "FORM") { + while (parentEl.nodeName.toUpperCase() !== "TR" && parentEl.nodeName.toUpperCase() !== "FORM") { parentEl = parentEl.parentNode; } - if (parentEl.nodeName.toUpperCase() == "FORM") { + if (parentEl.nodeName.toUpperCase() === "FORM") { parentEl = null; } @@ -98,14 +94,14 @@ function clearErrorLabelsXHTML(form) { } -function addError(e, errorText) { - addErrorXHTML(e, errorText); +function clearErrorLabels(form) { + clearErrorLabelsXHTML(form); } function addErrorXHTML(e, errorText) { try { var row = (e.type ? e : e[0]); - while(row.nodeName.toUpperCase() != "TR") { + while(row.nodeName.toUpperCase() !== "TR") { row = row.parentNode; } var table = row.parentNode; @@ -132,7 +128,12 @@ function addErrorXHTML(e, errorText) { label.setAttribute("class", "errorLabel"); label.setAttribute("className", "errorLabel"); //ie hack cause ie does not support setAttribute } - } catch (e) { - alert(e); + } catch (err) { + alert(err); } } + +function addError(e, errorText) { + addErrorXHTML(e, errorText); +} +