/** * @fileoverview *
General Tools.
*Now contains a modified version of Douglas Crockford's json.js that doesn't * mess with the DOM's prototype methods * http://www.json.org/js.html
* @author Dav Glass/gi,
base64: /[^A-Za-z0-9\+\/\=]/g,
syntaxCheck: /^("(\\.|[^"\\\n\r])*?"|[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t])+?$/
}
jsonCodes = {
'\b': '\\b',
'\t': '\\t',
'\n': '\\n',
'\f': '\\f',
'\r': '\\r',
'"' : '\\"',
'\\': '\\\\'
}
return {
version: '1.0'
}
}();
/**
* This normalizes getting the height of an element in IE
* @param {String/HTMLElement} elm The element to get the height of
* @returns The Height in pixels
* @type String
*/
YAHOO.Tools.getHeight = function(elm) {
var elm = $(elm);
var h = $D.getStyle(elm, 'height');
if (h == 'auto') {
elm.style.zoom = 1;
h = elm.clientHeight + 'px';
}
return h;
}
/**
* Get the XY coords required to place the element at the center of the screen
* @param {String/HTMLElement} elm The element to place at the center of the screen
* @returns The XY coords required to place the element at the center of the screen
* @type Array
*/
YAHOO.Tools.getCenter = function(elm) {
var elm = $(elm);
var cX = Math.round(($D.getViewportWidth() - parseInt($D.getStyle(elm, 'width'))) / 2);
var cY = Math.round(($D.getViewportHeight() - parseInt(this.getHeight(elm))) / 2);
return [cX, cY];
}
/**
* Converts a text string into a DOM object
* @param {String} txt String to convert
* @returns A string to a textNode
*/
YAHOO.Tools.makeTextObject = function(txt) {
return document.createTextNode(txt);
}
/**
* Takes an Array of DOM objects and appends them as a child to the main Element
* @param {Array} arr Array of elements to append to elm.
* @param {HTMLElement/String} elm A reference or ID to the main Element that the children will be appended to
*/
YAHOO.Tools.makeChildren = function(arr, elm) {
var elm = $(elm);
for (var i in arr) {
_val = arr[i];
if (typeof _val == 'string') {
_val = this.makeTxtObject(_val);
}
elm.appendChild(_val);
}
}
/**
* Converts a standard CSS string to a Javascriptable Camel Case variable name
* @param {String} str The CSS string to convert to camel case Javascript String
* Example:
* background-color
* backgroundColor
* list-style-type
* listStyleType
*/
YAHOO.Tools.styleToCamel = function(str) {
var _tmp = str.split('-');
var _new_style = _tmp[0];
for (var i = 1; i < _tmp.length; i++) {
_new_style += _tmp[i].substring(0, 1).toUpperCase() + _tmp[i].substring(1, _tmp[i].length);
}
return _new_style;
}
/**
* Removes " from a given string
* @param {String} str The string to remove quotes from
*/
YAHOO.Tools.removeQuotes = function(str) {
var checkText = new String(str);
return String(checkText.replace(regExs.quotes, ''));
}
/**
* Trims starting and trailing white space from a string.
* @param {String} str The string to trim
*/
YAHOO.Tools.trim = function(str) {
return str.replace(regExs.startspace, '').replace(regExs.endspace, '');
}
/**
* Removes all HTML tags from a string.
* @param {String} str The string to remove HTML from
*/
YAHOO.Tools.stripTags = function(str) {
return str.replace(regExs.striptags, '');
}
/**
* Returns True/False if it finds BR' or P's
* @param {String} str The string to search
*/
YAHOO.Tools.hasBRs = function(str) {
return str.match(regExs.hasbr) || str.match(regExs.hasp);
}
/**
* Converts BR's and P's to Plain Text Line Feeds
* @param {String} str The string to search
*/
YAHOO.Tools.convertBRs2NLs = function(str) {
return str.replace(regExs.rbr, "\n").replace(regExs.rbr2, "\n").replace(regExs.rendp, "\n").replace(regExs.rp, "");
}
/**
* Repeats a string n number of times
* @param {String} str The string to repeat
* @param {Integer} repeat Number of times to repeat it
* @returns Repeated string
* @type String
*/
YAHOO.Tools.stringRepeat = function(str, repeat) {
return new Array(repeat + 1).join(str);
}
/**
* Reverses a string
* @param {String} str The string to reverse
* @returns Reversed string
* @type String
*/
YAHOO.Tools.stringReverse = function(str) {
var new_str = '';
for (i = 0; i < str.length; i++) {
new_str = new_str + str.charAt((str.length -1) -i);
}
return new_str;
}
/**
* printf function written in Javascript
*
var test = "You are viewing messages {0} - {1} out of {2}";
* YAHOO.Tools.printf(test, '5', '25', '500');
* obj {
* ua: 'Full UserAgent String'
* opera: boolean
* safari: boolean
* gecko: boolean
* msie: boolean
* version: string
* }
*
* @return Browser Information Object
* @type Object
*/
YAHOO.Tools.getBrowserEngine = function() {
var opera = ((window.opera && window.opera.version) ? true : false);
var safari = ((navigator.vendor && navigator.vendor.indexOf('Apple') != -1) ? true : false);
var gecko = ((document.getElementById && !document.all && !opera && !safari) ? true : false);
var msie = ((window.ActiveXObject) ? true : false);
var version = false;
if (msie) {
/**
* This checks for the maxHeight style property.
* I.E. 7 has this
*/
if (typeof document.body.style.maxHeight != "undefined") {
version = '7';
} else {
/**
* Fall back to 6 (might need to find a 5.5 object too...).
*/
version = '6';
}
}
if (opera) {
/**
* The window.opera object has a method called version();
* Here we only grab the first 2 parts of the dotted string to get 9.01, 9.02, etc..
*/
var tmp_version = window.opera.version().split('.');
version = tmp_version[0] + '.' + tmp_version[1];
}
if (gecko) {
/**
* FireFox 2 has a function called registerContentHandler();
*/
if (navigator.registerContentHandler) {
version = '2';
} else {
version = '1.5';
}
/**
* This should catch all pre Firefox 1.5 browsers
*/
if ((navigator.vendorSub) && !version) {
version = navigator.vendorSub;
}
}
if (safari) {
try {
/**
* Safari 1.3+ supports the console method
*/
if (console) {
/**
* Safari 2+ supports the onmousewheel event
*/
if ((window.onmousewheel !== 'undefined') && (window.onmousewheel === null)) {
version = '2';
} else {
version = '1.3';
}
}
} catch (e) {
/**
* Safari 1.2 does not support the console method
*/
version = '1.2';
}
}
/**
* Return the Browser Object
* @type Object
*/
var browsers = {
ua: navigator.userAgent,
opera: opera,
safari: safari,
gecko: gecko,
msie: msie,
version: version
}
return browsers;
}
/**
* User Agent Based Browser Detection
* obj {
* ua: 'Full UserAgent String'
* opera: boolean
* safari: boolean
* firefox: boolean
* mozilla: boolean
* msie: boolean
* mac: boolean
* win: boolean
* unix: boolean
* version: string
* flash: version string
* }
*
* div = YAHOO.util.Dom.create('div', 'Single DIV. This is some test text.', {
* className:'test1',
* style:'font-size: 20px'
* }
* );
* test1.appendChild(div);
*
- or -
* div = YAHOO.util.Dom.create('div', {className:'test2',style:'font-size:11px'},
* [YAHOO.util.Dom.create('p', {
* style:'border: 1px solid red; color: blue',
* listener: ['click', test]
* },
* 'This is a P inside of a DIV both styled.')
* ]
*);
* test2.appendChild(div);
*
*
* @param {String} tagName Tag name to create
* @param {Object} attrs Element attributes in object notation
* @param {Array} children Array of children to append to the created element
* @param {String} txt Text string to insert into the created element
* @returns A reference to the newly created element
* @type HTMLReference
*/
YAHOO.Tools.create = function(tagName) {
tagName = tagName.toLowerCase();
elm = document.createElement(tagName);
var txt = false;
var attrsObj = false;
if (!elm) { return false; }
for (var i = 1; i < arguments.length; i++) {
txt = arguments[i];
if (typeof txt == 'string') {
_txt = YAHOO.Tools.makeTextObject(txt);
elm.appendChild(_txt);
} else if (txt instanceof Array) {
YAHOO.Tools.makeChildren(txt, elm);
} else if (typeof txt == 'object') {
//_makeStyleObject(txt, elm);
YAHOO.Tools.setAttr(txt, elm);
}
}
return elm;
}
/**
* Inserts an HTML Element after another in the DOM Tree.
* @param {HTMLElement} elm The element to insert
* @param {HTMLElement} curNode The element to insert it before
*/
YAHOO.Tools.insertAfter = function(elm, curNode) {
if (curNode.nextSibling) {
curNode.parentNode.insertBefore(elm, curNode.nextSibling);
} else {
curNode.parentNode.appendChild(elm);
}
}
/**
* Validates that the value passed is in the Array passed.
* @param {Array} arr The Array to search (haystack)
* @param {String} val The value to search for (needle)
* @returns True if the value is found
* @type Boolean
*/
YAHOO.Tools.inArray = function(arr, val) {
if (arr instanceof Array) {
for (var i = (arr.length -1); i >= 0; i--) {
if (arr[i] === val) {
return true;
}
}
}
return false;
}
/**
* Validates that the value passed in is a boolean.
* @param {Object} str The value to validate
* @return true, if the value is valid
* @type Boolean
*/
YAHOO.Tools.checkBoolean = function(str) {
return ((typeof str == 'boolean') ? true : false);
}
/**
* Validates that the value passed in is a number.
* @param {Object} str The value to validate
* @return true, if the value is valid
* @type Boolean
*/
YAHOO.Tools.checkNumber = function(str) {
return ((isNaN(str)) ? false : true);
}
/**
* Divide your desired pixel width by 13 to find em width. Multiply that value by 0.9759 for IE via *width.
* @param {Integer} size The pixel size to convert to em.
* @return Object of sizes (2) {msie: size, other: size }
* @type Object
*/
YAHOO.Tools.PixelToEm = function(size) {
var data = {};
var sSize = (size / 13);
data.other = (Math.round(sSize * 100) / 100);
data.msie = (Math.round((sSize * 0.9759) * 100) / 100);
return data;
}
/**
* Return a string of CSS statements for this pixel size in ems
* @param {Integer} size The pixel size to convert to em.
* @param {String} prop The property to apply the style to.
* @return String of CSS style statements (width:46.15em;*width:45.04em;min-width:600px;)
* @type String
*/
YAHOO.Tools.PixelToEmStyle = function(size, prop) {
var data = '';
var prop = ((prop) ? prop.toLowerCase() : 'width');
var sSize = (size / 13);
data += prop + ':' + (Math.round(sSize * 100) / 100) + 'em;';
data += '*' + prop + ':' + (Math.round((sSize * 0.9759) * 100) / 100) + 'em;';
if ((prop == 'width') || (prop == 'height')) {
data += 'min-' + prop + ':' + size + 'px;';
}
return data;
}
/**
* Base64 Encodes a string
* @param {String} str The string to base64 encode.
* @return Base64 Encoded String
* @type String
*/
YAHOO.Tools.base64Encode = function(str) {
var data = "";
var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
var i = 0;
do {
chr1 = str.charCodeAt(i++);
chr2 = str.charCodeAt(i++);
chr3 = str.charCodeAt(i++);
enc1 = chr1 >> 2;
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
enc4 = chr3 & 63;
if (isNaN(chr2)) {
enc3 = enc4 = 64;
} else if (isNaN(chr3)) {
enc4 = 64;
}
data = data + keyStr.charAt(enc1) + keyStr.charAt(enc2) + keyStr.charAt(enc3) + keyStr.charAt(enc4);
} while (i < str.length);
return data;
}
/**
* Base64 Dncodes a string
* @param {String} str The base64 encoded string to decode.
* @return The decoded String
* @type String
*/
YAHOO.Tools.base64Decode = function(str) {
var data = "";
var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
var i = 0;
// remove all characters that are not A-Z, a-z, 0-9, +, /, or =
str = str.replace(regExs.base64, "");
do {
enc1 = keyStr.indexOf(str.charAt(i++));
enc2 = keyStr.indexOf(str.charAt(i++));
enc3 = keyStr.indexOf(str.charAt(i++));
enc4 = keyStr.indexOf(str.charAt(i++));
chr1 = (enc1 << 2) | (enc2 >> 4);
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
chr3 = ((enc3 & 3) << 6) | enc4;
data = data + String.fromCharCode(chr1);
if (enc3 != 64) {
data = data + String.fromCharCode(chr2);
}
if (enc4 != 64) {
data = data + String.fromCharCode(chr3);
}
} while (i < str.length);
return data;
}
/**
* Parses a Query String, if one is not provided, it will look in location.href