1// SpryValidationSelect.js - version 0.11 - Spry Pre-Release 1.6.1 2// 3// Copyright (c) 2006. Adobe Systems Incorporated. 4// All rights reserved. 5// 6// Redistribution and use in source and binary forms, with or without 7// modification, are permitted provided that the following conditions are met: 8// 9// * Redistributions of source code must retain the above copyright notice, 10// this list of conditions and the following disclaimer. 11// * Redistributions in binary form must reproduce the above copyright notice, 12// this list of conditions and the following disclaimer in the documentation 13// and/or other materials provided with the distribution. 14// * Neither the name of Adobe Systems Incorporated nor the names of its 15// contributors may be used to endorse or promote products derived from this 16// software without specific prior written permission. 17// 18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 22// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28// POSSIBILITY OF SUCH DAMAGE. 29 30(function() { // BeginSpryComponent 31 32if (typeof Spry == "undefined") window.Spry = {}; if (!Spry.Widget) Spry.Widget = {}; 33 34Spry.Widget.ValidationSelect = function(element, opts) 35{ 36 this.init(element); 37 38 Spry.Widget.Utils.setOptions(this, opts); 39 40 // set validateOn flags 41 var validateOn = ['submit'].concat(this.validateOn || []); 42 validateOn = validateOn.join(","); 43 this.validateOn = 0 | (validateOn.indexOf('submit') != -1 ? Spry.Widget.ValidationSelect.ONSUBMIT : 0); 44 this.validateOn = this.validateOn | (validateOn.indexOf('blur') != -1 ? Spry.Widget.ValidationSelect.ONBLUR : 0); 45 this.validateOn = this.validateOn | (validateOn.indexOf('change') != -1 ? Spry.Widget.ValidationSelect.ONCHANGE : 0); 46 47 if (this.additionalError) 48 this.additionalError = this.getElement(this.additionalError); 49 50 // Unfortunately in some browsers like Safari, the Stylesheets our 51 // page depends on may not have been loaded at the time we are called. 52 // This means we have to defer attaching our behaviors until after the 53 // onload event fires, since some of our behaviors rely on dimensions 54 // specified in the CSS. 55 56 if (Spry.Widget.ValidationSelect.onloadDidFire) 57 this.attachBehaviors(); 58 else 59 Spry.Widget.ValidationSelect.loadQueue.push(this); 60}; 61 62Spry.Widget.ValidationSelect.ONCHANGE = 1; 63Spry.Widget.ValidationSelect.ONBLUR = 2; 64Spry.Widget.ValidationSelect.ONSUBMIT = 4; 65 66Spry.Widget.ValidationSelect.prototype.init = function(element) 67{ 68 this.element = this.getElement(element); 69 this.additionalError = false; 70 this.selectElement = null; 71 this.form = null; 72 this.event_handlers = []; 73 74 // this.element can be either the container (<span>) 75 // or the <select> element, when no error messages are used. 76 77 this.requiredClass = "selectRequiredState"; 78 this.invalidClass = "selectInvalidState"; 79 this.focusClass = "selectFocusState"; 80 this.validClass = "selectValidState"; 81 82 this.emptyValue = ""; 83 this.invalidValue = null; 84 this.isRequired = true; 85 86 this.validateOn = ["submit"]; // change, blur, submit 87 // flag used to avoid cascade validation when both 88 // onChange and onBlur events are used to trigger validation 89 this.validatedByOnChangeEvent = false; 90}; 91 92Spry.Widget.ValidationSelect.prototype.destroy = function() { 93 if (this.event_handlers) 94 for (var i=0; i<this.event_handlers.length; i++) { 95 Spry.Widget.Utils.removeEventListener(this.event_handlers[i][0], this.event_handlers[i][1], this.event_handlers[i][2], false); 96 } 97 try { delete this.element; } catch(err) {} 98 try { delete this.selectElement; } catch(err) {} 99 try { delete this.form; } catch(err) {} 100 try { delete this.event_handlers; } catch(err) {} 101 102 var q = Spry.Widget.Form.onSubmitWidgetQueue; 103 var qlen = q.length; 104 for (var i = 0; i < qlen; i++) { 105 if (q[i] == this) { 106 q.splice(i, 1); 107 break; 108 } 109 } 110}; 111 112Spry.Widget.ValidationSelect.onloadDidFire = false;
113Spry.Widget.ValidationSelect.loadQueue = []; 114 115Spry.Widget.ValidationSelect.prototype.getElement = function(ele) 116{ 117 if (ele && typeof ele == "string") 118 return document.getElementById(ele); 119 return ele; 120}; 121 122Spry.Widget.ValidationSelect.processLoadQueue = function(handler) 123{ 124 Spry.Widget.ValidationSelect.onloadDidFire = true; 125 var q = Spry.Widget.ValidationSelect.loadQueue; 126 var qlen = q.length; 127 for (var i = 0; i < qlen; i++) 128 q[i].attachBehaviors(); 129}; 130 131Spry.Widget.ValidationSelect.addLoadListener = function(handler) 132{ 133 if (typeof window.addEventListener != 'undefined') 134 window.addEventListener('load', handler, false); 135 else if (typeof document.addEventListener != 'undefined') 136 document.addEventListener('load', handler, false); 137 else if (typeof window.attachEvent != 'undefined') 138 window.attachEvent('onload', handler); 139}; 140 141Spry.Widget.ValidationSelect.addLoadListener(Spry.Widget.ValidationSelect.processLoadQueue); 142Spry.Widget.ValidationSelect.addLoadListener(function(){ 143 Spry.Widget.Utils.addEventListener(window, "unload", Spry.Widget.Form.destroyAll, false); 144}); 145 146Spry.Widget.ValidationSelect.prototype.attachBehaviors = function() 147{ 148 // find the SELECT element inside current container 149 if (this.element) { 150 if (this.element.nodeName == "SELECT") { 151 this.selectElement = this.element; 152 } else { 153 this.selectElement = Spry.Widget.Utils.getFirstChildWithNodeNameAtAnyLevel(this.element, "SELECT"); 154 } 155 } 156 157 if (this.selectElement) { 158 var self = this; 159 this.event_handlers = []; 160 // focus 161 // attach on beforeactivate instead of focus for 162 // - IE 6 (to overcome this bug: setting a class name onfocus does not affect the open dropdown) 163 // - IE 7 (to overcome this bug: setting a class name, closes the select) 164 var focusEventName = "focus"; 165 var ua = navigator.userAgent.match(/msie (\d+)\./i); 166 if (ua) { 167 ua = parseInt(ua[1], 10); 168 if (ua >= 6) { 169 focusEventName = "beforeactivate"; 170 } 171 } 172 this.event_handlers.push([this.selectElement, focusEventName, function(e) { if (self.isDisabled()) return true; return self.onFocus(e); }]); 173 // blur 174 this.event_handlers.push([this.selectElement, "blur", function(e) { if (self.isDisabled()) return true; return self.onBlur(e); }]); 175 // change 176 if (this.validateOn & Spry.Widget.ValidationSelect.ONCHANGE) { 177 this.event_handlers.push([this.selectElement, "change", function(e) { if (self.isDisabled()) return true; return self.onChange(e); }]); 178 this.event_handlers.push([this.selectElement, "keypress", function(e) { if (self.isDisabled()) return true; return self.onChange(e); }]); 179 } 180 181 for (var i=0; i<this.event_handlers.length; i++) { 182 Spry.Widget.Utils.addEventListener(this.event_handlers[i][0], this.event_handlers[i][1], this.event_handlers[i][2], false); 183 } 184 185 // submit 186 this.form = Spry.Widget.Utils.getFirstParentWithNodeName(this.selectElement, "FORM"); 187 if (this.form) { 188 // if no "onSubmit" handler has been attached to the current form, attach one 189 if (!this.form.attachedSubmitHandler && !this.form.onsubmit) { 190 this.form.onsubmit = function(e) { e = e || event; return Spry.Widget.Form.onSubmit(e, e.srcElement || e.currentTarget) }; 191 this.form.attachedSubmitHandler = true; 192 } 193 if (!this.form.attachedResetHandler) { 194 Spry.Widget.Utils.addEventListener(this.form, "reset", function(e) { e = e || event; return Spry.Widget.Form.onReset(e, e.srcElement || e.currentTarget) }, false); 195 this.form.attachedResetHandler = true; 196 } 197 // add the currrent widget to the "onSubmit" check queue; 198 Spry.Widget.Form.onSubmitWidgetQueue.push(this); 199 } 200 } 201}; 202 203 204Spry.Widget.ValidationSelect.prototype.addClassName = function(ele, className) 205{ 206 if (!ele || !className || (ele.className && ele.className.search(new RegExp("\\b" + className + "\\b")) != -1)) 207 return; 208 ele.className += (ele.className ? " " : "") + className; 209}; 210 211Spry.Widget.ValidationSelect.prototype.removeClassName = function(ele, className) 212{ 213 if (!ele || !className || (ele.className && ele.className.search(new RegExp("\\b" + className + "\\b")) == -1)) 214 return; 215 ele.className = ele.className.replace(new RegExp("\\s*\\b" + className + "\\b", "g"), ""); 216}; 217 218 219 220Spry.Widget.ValidationSelect.prototype.onFocus = function(e) 221{ 222 this.hasFocus = true; 223 this.validatedByOnChangeEvent = false;
224 this.addClassName(this.element, this.focusClass); 225 this.addClassName(this.additionalError, this.focusClass); 226}; 227 228Spry.Widget.ValidationSelect.prototype.onBlur = function(e) 229{ 230 this.hasFocus = false; 231 var doValidation = false; 232 if (this.validateOn & Spry.Widget.ValidationSelect.ONBLUR) 233 doValidation = true; 234 if (doValidation && !this.validatedByOnChangeEvent) 235 this.validate(); 236 this.removeClassName(this.element, this.focusClass); 237 this.removeClassName(this.additionalError, this.focusClass); 238}; 239 240Spry.Widget.ValidationSelect.prototype.onChange = function(e) 241{ 242 this.hasFocus = false; 243 this.validate(); 244 this.validatedByOnChangeEvent = true; 245}; 246 247Spry.Widget.ValidationSelect.prototype.reset = function() { 248 this.removeClassName(this.element, this.requiredClass); 249 this.removeClassName(this.element, this.invalidClass); 250 this.removeClassName(this.element, this.validClass); 251 this.removeClassName(this.additionalError, this.requiredClass); 252 this.removeClassName(this.additionalError, this.invalidClass); 253 this.removeClassName(this.additionalError, this.validClass); 254}; 255 256Spry.Widget.ValidationSelect.prototype.validate = function() { 257 this.reset(); 258 // check isRequired 259 if (this.isRequired) { 260 // there are no options, or no option has been selected 261 if (this.selectElement.options.length == 0 || this.selectElement.selectedIndex == -1) { 262 this.addClassName(this.element, this.requiredClass); 263 this.addClassName(this.additionalError, this.requiredClass); 264 //return false; 265 } 266 // the current selected option has no "value" attribute 267 // when no value is set, browsers implement different behaviour for the value property 268 // IE: value = blank string ("") 269 // FF, Opera: value = option text 270 if (this.selectElement.options[this.selectElement.selectedIndex].getAttribute("value") == null) { 271 this.addClassName(this.element, this.requiredClass); 272 this.addClassName(this.additionalError, this.requiredClass); 273 //return false; 274 } 275 // the current selected option has blank string ("") value 276 if (this.selectElement.options[this.selectElement.selectedIndex].value == this.emptyValue) { 277 this.addClassName(this.element, this.requiredClass); 278 this.addClassName(this.additionalError, this.requiredClass); 279 //return false; 280 } 281 // the current selected option has "disabled" attribute 282 // IE 6 allows to select such options 283 if (this.selectElement.options[this.selectElement.selectedIndex].disabled) { 284 this.addClassName(this.element, this.requiredClass); 285 this.addClassName(this.additionalError, this.requiredClass); 286 //return false; 287 } 288 } 289 if (this.invalidValue) { 290 if (this.selectElement.options.length > 0 && 291 this.selectElement.selectedIndex != -1 && 292 this.selectElement.options[this.selectElement.selectedIndex].value == this.invalidValue) { 293 this.addClassName(this.element, this.invalidClass); 294 this.addClassName(this.additionalError, this.invalidClass); 295 //return false; 296 } 297 } 298 299 //ã¨ã©ã¼è¡¨ç¤º 300 if((this.isRequired && ((this.selectElement.options.length == 0 || this.selectElement.selectedIndex == -1) || (this.selectElement.options[this.selectElement.selectedIndex].getAttribute("value") == null) || (this.selectElement.options[this.selectElement.selectedIndex].value == this.emptyValue) || (this.selectElement.options[this.selectElement.selectedIndex].disabled))) || (this.invalidValue && (this.selectElement.options.length > 0 && this.selectElement.selectedIndex != -1 && this.selectElement.options[this.selectElement.selectedIndex].value == this.invalidValue))){ 301 $("#attentionBottom").addClass("errSelect"); 302 return false; 303 }else{ 304 $("#attentionBottom").removeClass("errSelect"); 305 } 306 307 this.addClassName(this.element, this.validClass); 308 this.addClassName(this.additionalError, this.validClass); 309 return true; 310}; 311 312Spry.Widget.ValidationSelect.prototype.isDisabled = function() { 313 return this.selectElement.disabled; 314}; 315 316////////////////////////////////////////////////////////////////////// 317// 318// Spry.Widget.Form - common for all widgets 319// 320////////////////////////////////////////////////////////////////////// 321 322if (!Spry.Widget.Form) Spry.Widget.Form = {}; 323if (!Spry.Widget.Form.onSubmitWidgetQueue) Spry.Widget.Form.onSubmitWidgetQueue = []; 324 325if (!Spry.Widget.Form.validate) { 326 Spry.Widget.Form.validate = function(vform) { 327 var isValid = true; 328 var isElementValid = true; 329 var q = Spry.Widget.Form.onSubmitWidgetQueue; 330 var qlen = q.length; 331 for (var i = 0; i < qlen; i++) { 332 if (!q[i].isDisabled() && q[i].form == vform) { 333 isElementValid = q[i].validate(); 334 isValid = isElementValid && isValid; 335 } 336 } 337 return isValid; 338 } 339}; 340 341if (!Spry.Widget.Form.onSubmit) { 342 Spry.Widget.Form.onSubmit = function(e, form) 343 { 344 if (Spry.Widget.Form.validate(form) == false) { 345 return false;
346 } 347 return true; 348 }; 349}; 350 351if (!Spry.Widget.Form.onReset) { 352 Spry.Widget.Form.onReset = function(e, vform) 353 { 354 var q = Spry.Widget.Form.onSubmitWidgetQueue; 355 var qlen = q.length; 356 for (var i = 0; i < qlen; i++) { 357 if (!q[i].isDisabled() && q[i].form == vform && typeof(q[i].reset) == 'function') { 358 q[i].reset(); 359 } 360 } 361 return true; 362 }; 363}; 364 365if (!Spry.Widget.Form.destroy) { 366 Spry.Widget.Form.destroy = function(form) 367 { 368 var q = Spry.Widget.Form.onSubmitWidgetQueue; 369 for (var i = 0; i < Spry.Widget.Form.onSubmitWidgetQueue.length; i++) { 370 if (q[i].form == form && typeof(q[i].destroy) == 'function') { 371 q[i].destroy(); 372 i--; 373 } 374 } 375 } 376}; 377 378if (!Spry.Widget.Form.destroyAll) { 379 Spry.Widget.Form.destroyAll = function() 380 { 381 var q = Spry.Widget.Form.onSubmitWidgetQueue; 382 for (var i = 0; i < Spry.Widget.Form.onSubmitWidgetQueue.length; i++) { 383 if (typeof(q[i].destroy) == 'function') { 384 q[i].destroy(); 385 i--; 386 } 387 } 388 } 389}; 390 391////////////////////////////////////////////////////////////////////// 392// 393// Spry.Widget.Utils 394// 395////////////////////////////////////////////////////////////////////// 396 397if (!Spry.Widget.Utils) Spry.Widget.Utils = {}; 398 399Spry.Widget.Utils.setOptions = function(obj, optionsObj, ignoreUndefinedProps) 400{ 401 if (!optionsObj) 402 return; 403 for (var optionName in optionsObj) 404 { 405 if (ignoreUndefinedProps && optionsObj[optionName] == undefined) 406 continue; 407 obj[optionName] = optionsObj[optionName]; 408 } 409}; 410 411 412Spry.Widget.Utils.getFirstChildWithNodeNameAtAnyLevel = function(node, nodeName) 413{ 414 var elements = node.getElementsByTagName(nodeName); 415 if (elements) { 416 return elements[0]; 417 } 418 return null; 419}; 420 421Spry.Widget.Utils.getFirstParentWithNodeName = function(node, nodeName) 422{ 423 while (node.parentNode 424 && node.parentNode.nodeName.toLowerCase() != nodeName.toLowerCase() 425 && node.parentNode.nodeName != 'BODY') { 426 node = node.parentNode; 427 } 428 429 if (node.parentNode && node.parentNode.nodeName.toLowerCase() == nodeName.toLowerCase()) { 430 return node.parentNode; 431 } else { 432 return null; 433 } 434}; 435 436Spry.Widget.Utils.destroyWidgets = function (container) 437{ 438 if (typeof container == 'string') { 439 container = document.getElementById(container); 440 } 441 442 var q = Spry.Widget.Form.onSubmitWidgetQueue; 443 for (var i = 0; i < Spry.Widget.Form.onSubmitWidgetQueue.length; i++) { 444 if (typeof(q[i].destroy) == 'function' && Spry.Widget.Utils.contains(container, q[i].element)) { 445 q[i].destroy(); 446 i--; 447 } 448 } 449}; 450 451Spry.Widget.Utils.contains = function (who, what) 452{ 453 if (typeof who.contains == 'object') { 454 return what && who && (who == what || who.contains(what)); 455 } else { 456 var el = what; 457 while(el) { 458 if (el == who) { 459 return true; 460 } 461 el = el.parentNode; 462 } 463 return false; 464 } 465}; 466 467Spry.Widget.Utils.addEventListener = function(element, eventType, handler, capture) 468{ 469 try 470 { 471 if (element.addEventListener) 472 element.addEventListener(eventType, handler, capture); 473 else if (element.attachEvent) 474 element.attachEvent("on" + eventType, handler, capture); 475 } 476 catch (e) {} 477}; 478 479Spry.Widget.Utils.removeEventListener = function(element, eventType, handler, capture) 480{ 481 try 482 { 483 if (element.removeEventListener) 484 element.removeEventListener(eventType, handler, capture); 485 else if (element.detachEvent) 486 element.detachEvent("on" + eventType, handler, capture); 487 } 488 catch (e) {} 489}; 490 491})(); // EndSpryComponent
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.