com.hfg.javascript.CommonJS Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of com_hfg Show documentation
Show all versions of com_hfg Show documentation
com.hfg xml, html, svg, and bioinformatics utility library
package com.hfg.javascript;
//------------------------------------------------------------------------------
/**
* Repository of reusable javascript snippets.
*
* The generateDomSearchJS() function produces the DOMSearch javascript which allows
* more flexible DOM element selection with search scoping capability.
*
*
* Ex: DOMSearch(inElement).getElementsByTagNameAndAttribute(inTagName, inAttriubtePattern, inAttriubteValuePattern)
*
*
*
* - inElement is the DOM Element object which is the root element from which to search.
* [No argument defaults to document]
* - inTagName is the tag name used to limit the search.
* Use '*' to specify all tag types.
* - inAttributePattern is the Pattern to use for matching
* to attribute names. 'width' would match elements with a 'width'
* attribute but would also match 'shoeWidth' (matches are case insensitive). To return just
* elements containing a 'width' attribute, a pattern of '^width$'
* should be used.
* - inAttributeValuePattern is the Pattern to use for matching
* to attribute values. [No argument defaults to '.*' which matches all values.]
*
*
*
* Sometimes it would be nice to use the same id for more than element in the page but
* where the id would be unique within a portion of the page's DOM. While getElementById()
* is only available on the document object, DOMSearch(inElement).getElementById(inId)
* can be used to scope to only a portion of the DOM tree.
*
*
* The raw DOMSearch() javascript can be seen here.
*
*
* See this page for some simple examples.
*
* @author J. Alex Taylor, hairyfatguy.com
*/
//------------------------------------------------------------------------------
// com.hfg XML/HTML Coding Library
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
// [email protected]
//------------------------------------------------------------------------------
public class CommonJS
{
//##########################################################################
// PRIVATE FIELDS
//##########################################################################
private static String sCachedBrowserDetectionJS;
//##########################################################################
// PUBLIC METHODS
//##########################################################################
//--------------------------------------------------------------------------
public static String generateBrowserDetectionJS()
{
if (null == sCachedBrowserDetectionJS)
{
initializeBrowserDetectionJS();
}
return sCachedBrowserDetectionJS;
}
//--------------------------------------------------------------------------
public static String generateDomSearchJS()
{
return JsUtil.getJSResourceAsString("DOMSearch.js");
}
//--------------------------------------------------------------------------
public static String generateXMLHttpRequestJS()
{
return JsUtil.getJSResourceAsString("XMLHttpRequester.js");
}
//##########################################################################
// PRIVATE METHODS
//##########################################################################
//--------------------------------------------------------------------------
private static void initializeBrowserDetectionJS()
{
StringBuffer js = new StringBuffer();
js.append("function Browser() {\n");
js.append(" var ua= navigator.userAgent;\n");
js.append(" this.ua = ua;\n");
js.append(" ua = ua.toLowerCase();\n");
js.append("\n");
js.append(" // DOM support?\n");
js.append(" this.isDOM1 = (Boolean(document.getElementById));\n");
js.append(" this.isDOM2Event = (Boolean(document.addEventListener)\n");
js.append(" && Boolean(document.removeEventListener));\n");
js.append(" // CSS compatibility mode?\n");
js.append(" this.mode = document.compatMode ? document.compatMode : 'BackCompat';\n");
js.append("\n");
js.append(" // Browser name\n");
js.append(" this.isSafari = (ua.indexOf('safari') != - 1);\n");
js.append(" this.isGecko = (ua.indexOf('gecko') != -1 && !this.isSafari);\n");
js.append(" this.isMozilla = (this.isGecko && ua.indexOf('gecko/') + 14 == ua.length);\n");
js.append(" this.isNS = ( (this.isGecko) ? (ua.indexOf('netscape') != -1)\n");
js.append(" : ( (ua.indexOf('mozilla') != -1) \n");
js.append(" && (ua.indexOf('spoofer') == -1)\n");
js.append(" && (ua.indexOf('compatible') == -1)\n");
js.append(" && (ua.indexOf('opera') == -1)\n");
js.append(" && (ua.indexOf('safari') == -1)\n");
js.append(" && (ua.indexOf('webtv') == -1)\n");
js.append(" && (ua.indexOf('hotjava') == -1) ) );\n");
js.append(" this.isIE = ( (ua.indexOf('msie') != -1)\n");
js.append(" && (ua.indexOf('opera') == -1) \n");
js.append(" && (ua.indexOf('webtv') == -1) );\n");
js.append(" this.isOpera = (ua.indexOf('opera') != -1);\n");
js.append(" this.isKonqueror = (ua.indexOf('konqueror') != -1 && !this.isSafari);\n");
js.append(" this.isICab = (ua.indexOf('icab') != -1);\n");
js.append(" this.isAOL = (ua.indexOf('aol') != -1);\n");
js.append(" this.isOMNI = (ua.indexOf('omni') != -1);\n");
js.append("\n");
js.append(" // Spoofing and compatible browsers\n");
js.append(" this.isIECompatible = ( (ua.indexOf('msie') != -1) && !this.isIE);\n");
js.append(" this.isNSCompatible = ( (ua.indexOf('mozilla') != -1) && !this.isNS && !this.isMozilla);\n");
js.append("\n");
js.append(" // Browser version\n");
js.append(" this.version = parseFloat(navigator.appVersion);\n");
js.append("\n");
js.append(" // correct version number\n");
js.append(" if (this.isNS && this.isGecko) {\n");
js.append(" this.version = parseFloat( ua.substring( ua.lastIndexOf('/') + 1 ) );\n");
js.append(" }\n");
js.append(" else if (this.isIE && this.version >= 4) {\n");
js.append(" this.version = parseFloat( ua.substring( ua.indexOf('msie ') + 5 ) );\n");
js.append(" }\n");
js.append(" else if (this.isMozilla) {\n");
js.append(" this.version = parseFloat( ua.substring( ua.indexOf('rv:') + 3 ) );\n");
js.append(" }\n");
js.append(" else if (this.isSafari) {\n");
js.append(" this.version = parseFloat( ua.substring( ua.lastIndexOf('/') + 1 ) );\n");
js.append(" }\n");
js.append(" else if (this.isOpera) {\n");
js.append(" if (ua.indexOf('opera/') != -1) {\n");
js.append(" this.version = parseFloat( ua.substring( ua.indexOf('opera/') + 6 ) );\n");
js.append(" }\n");
js.append(" else {\n");
js.append(" this.version = parseFloat( ua.substring( ua.indexOf('opera ') + 6 ) );\n");
js.append(" }\n");
js.append(" }\n");
js.append(" else if (this.isKonqueror) {\n");
js.append(" this.version = parseFloat( ua.substring( ua.indexOf('konqueror/') + 10 ) );\n");
js.append(" }\n");
js.append(" else if (this.isICab) {\n");
js.append(" if (ua.indexOf('icab/') != -1) {\n");
js.append(" this.version = parseFloat( ua.substring( ua.indexOf('icab/') + 6 ) );\n");
js.append(" }\n");
js.append(" else {\n");
js.append(" this.version = parseFloat( ua.substring( ua.indexOf('icab ') + 6 ) );\n");
js.append(" }\n");
js.append(" }\n");
js.append("\n");
js.append(" this.versionMajor = parseInt(this.version);\n");
js.append(" this.geckoVersion = ( (this.isGecko) ? ua.substring( (ua.lastIndexOf('gecko/') + 6), (ua.lastIndexOf('gecko/') + 14) ) : -1 );\n");
js.append("\n");
js.append(" // Platform\n");
js.append(" this.Win = (ua.indexOf('win') != -1);\n");
js.append(" this.Win32 = (this.Win && ( ua.indexOf('95') != -1 || ua.indexOf('98') != -1 || ua.indexOf('nt') != -1 || ua.indexOf('win32') != -1 || ua.indexOf('32bit') != -1 || ua.indexOf('xp') != -1) );\n");
js.append(" this.MacOS = (ua.indexOf('mac') != -1);\n");
js.append(" this.Unix = (ua.indexOf('unix') != -1 || ua.indexOf('sunos') != -1 || ua.indexOf('bsd') != -1 || ua.indexOf('x11') != -1);\n");
js.append(" this.Linux = (ua.indexOf('linux') != -1);\n");
js.append("\n");
js.append(" // Specific browser shortcuts\n");
js.append(" this.isNS4x = (this.isNS && this.versionMajor == 4);\n");
js.append(" this.isNS40x = (this.isNS4x && this.version < 4.5);\n");
js.append(" this.isNS47x = (this.isNS4x && this.version >= 4.7);\n");
js.append(" this.isNS4up = (this.isNS && this.version >= 4);\n");
js.append(" this.isNS6x = (this.isNS && this.versionMajor == 6);\n");
js.append(" this.isNS6up = (this.isNS && this.versionMajor >= 6);\n");
js.append(" this.isNS7x = (this.isNS && this.versionMajor == 7);\n");
js.append(" this.isNS7up = (this.isNS && this.versionMajor >= 7);\n");
js.append("\n");
js.append(" this.isIE4x = (this.isIE && this.versionMajor == 4);\n");
js.append(" this.isIE4up = (this.isIE && this.versionMajor >= 4);\n");
js.append(" this.isIE5x = (this.isIE && this.versionMajor == 5);\n");
js.append(" this.isIE55 = (this.isIE && this.version == 5.5);\n");
js.append(" this.isIE5up = (this.isIE && this.versionMajor >= 5);\n");
js.append(" this.isIE6x = (this.isIE && this.versionMajor == 6);\n");
js.append(" this.isIE6up = (this.isIE && this.versionMajor >= 6);\n");
js.append("\n");
js.append(" this.isIE4xMac = (this.isIE4x && this.isMacOS);\n");
js.append("\n");
js.append(" // To display all the fields in this object\n");
js.append(" this.write = writeMethod;\n");
js.append("\n");
js.append(" function writeMethod() {\n");
js.append(" document.writeln('');\n");
js.append(" for(var x in this)\n");
js.append(" // Print the value of ea. field (skipping methods).\n");
js.append(" if (!this[x].arguments)\n");
js.append(" document.writeln(padString('browser.' + x, 22) + ' = ' + this[x]);\n");
js.append(" document.writeln('<\\/pre>');\n");
js.append(" }\n");
js.append("\n");
js.append(" function padString(inString, targetLength) {\n");
js.append(" var spacesNeeded = targetLength - inString.length\n");
js.append("\n");
js.append(" for (var i = 0; i < spacesNeeded; i++) {\n");
js.append(" inString += ' '\n");
js.append(" }\n");
js.append(" return inString;\n");
js.append(" }\n");
js.append("}\n");
js.append("var browser = new Browser();\n");
sCachedBrowserDetectionJS = js.toString();
}
//--------------------------------------------------------------------------
private static void line(StringBuffer inBuffer, String inLine)
{
inBuffer.append(inLine + "\n");
}
}