diff -r e3d7322305bf -r 5e1f1e916419 punbb/include/js/common.js --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/punbb/include/js/common.js Sat Apr 05 23:56:45 2008 -0400 @@ -0,0 +1,205 @@ +var Punbb = { + /* attach FN to WINDOW.ONLOAD handler */ + addLoadEvent: function(fn) + { + var x = window.onload; + window.onload = (x && typeof x=='function') ? function(){x();fn()} : fn; + }, + /* return TRUE if node N has class X, else FALSE */ + hasClass: function(n, x) + { + return (new RegExp('\\b' + x + '\\b')).test(n.className) + }, + /* add X class to N node, return TRUE if added, FALSE if already exists */ + addClass: function(n, x) + { + if (Punbb.hasClass(n, x)) return false; + else n.className += ' ' + x; + return true; + }, + /* remove X class from N node, return TRUE if removed, FALSE if not present */ + removeClass: function(n, x) + { + if (!Punbb.hasClass(n, x)) return false; + x = new RegExp('\\s*\\b' + x + '\\b', 'g'); + n.className = n.className.replace(x, ''); + return true; + }, + /* blink node N twice */ + blink: function(n, i) + { + if (typeof i == 'undefined') i = 2; + var x = n.style.visibility; + if (i && x!='hidden') + { + n.style.visibility = 'hidden'; + setTimeout(function(){n.style.visibility=x}, 200); + setTimeout(function(){Punbb.blink(n,i-1)}, 400); + } + }, + /* return true if node N scrolled into view, else false (y axis only) */ + onScreen: function(n) + { + function pageYOffset() // return number of pixels page has scrolled + { + var y = -1; + if (self.pageYOffset) y = self.pageYOffset; // all except IE + else if (document.documentElement && document.documentElement.scrollTop) + y = document.documentElement.scrollTop; // IE 6 Strict + else if (document.body) y = document.body.scrollTop; // all other IE ver + return y; + } + function innerHeight() // return inner height of browser window + { + var y = -1; + if (self.innerHeight) y = self.innerHeight; // all except IE + else if (document.documentElement && document.documentElement.clientHeight) + y = document.documentElement.clientHeight; // IE 6 Strict Mode + else if (document.body) y = document.body.clientHeight; // all other IE ver + return y; + } + function nodeYOffset(n) // return y coordinate of node N + { + var y = n.offsetTop; + n = n.offsetParent; + return n ? y += nodeYOffset(n) : y; + } + var screenTop = pageYOffset(); + var screenBottom = screenTop + innerHeight(); + var nodeTop = nodeYOffset(n); + var nodeBottom = nodeTop + n.clientHeight; + return nodeTop >= screenTop && nodeBottom < screenBottom; + }, + /* apply FN to every ARR item, return array of results */ + map: function(fn, arr) + { + for (var i=0,len=arr.length; i -1) + //if (Punbb.find(fn, nodes) > -1) + { + var n = document.getElementById('req-msg'); + Punbb.removeClass(n, 'frm-warn'); + var newlyAdded = Punbb.addClass(n, 'frm-error'); + if (!Punbb.onScreen(n)) + { + n.scrollIntoView(); // method not in W3C DOM, but fully cross-browser? + setTimeout(function(){Punbb.blink(n)}, 500); + } + else if (!newlyAdded) Punbb.blink(n); + if (Punbb.onScreen(nodes[empty])) nodes[empty].focus(); + return false; + } + return true; + }, + /* attach form validation function to submit-type inputs */ + attachValidateForm: function() + { + var forms = document.forms; + for (var i=0,len=forms.length; i -1) + { + fn = function(x) { return x.type && (x.type=='submit' && x.name!='cancel') }; + var nodes = Punbb.arrayOfMatched(fn, elements) + var formRef = forms[i]; + fn = function() { return Punbb.validateForm(formRef) }; + //TODO: look at passing array of node refs instead of forum ref + //fn = function() { return Punbb.checkReq(required.slice(0)) }; + nodes = Punbb.map(function(x){x.onclick=fn}, nodes); + } + } + }, + attachWindowOpen: function() + { + if (!document.getElementsByTagName) return; + var nodes = document.getElementsByTagName('a'); + for (var i=0; i -1) nodes[n].focus(); + } +} +Punbb.addLoadEvent(Punbb.attachValidateForm); +Punbb.addLoadEvent(Punbb.attachWindowOpen); +Punbb.addLoadEvent(Punbb.autoFocus); + +/* A handful of functions in this script have been released into the Public + Domain by Shawn Brown or other authors. Although I, Shawn Brown, do not + believe that it is legally necessary to note which parts of a Copyrighted + work are based on Public Domain content, a list of the Public Domain + code (functions and methods) contained in this file is included below: + + * addLoadEvent: Released into the Public Domain by Shawn Brown and + based on Simon Willison's Public Domain function of the same name. + * hasClass, addClass & removeClass: Released into the Public Domain + by Shawn Brown. + * onScreen: Released into the Public Domain by Shawn Brown and based, + in-part, on Peter Paul-Koch's Public Domain node-position functions. + * map, find, arrayOfMatched & flatten: These basic functional methods + have been released into the Public Domain by Shawn Brown. + + It is entirely possible that, in the future, someone may contribute code + that is in the Public Domain but not note it as such. This should not be + a problem, but one should keep in mind that the list provided here is known + to be complete and accurate only up until 24-JUNE-2007. +*/ \ No newline at end of file