// $Header: /cvsroot/bitweaver/_bit_util/javascript/bitweaver.js,v 1.58 2010/05/31 14:36:33 spiderr Exp $
// please modify this file and leave plenty of comments. This file will be
// compressed automatically. Please make sure you only use comments beginning
// with '//' and put comments on separate lines otherwise the packer will choke
// the beginning of the clean up of bitweaver core js - use name spaces
// if you are adding new features to this file add them to the BitBase object.
BitBase = {
// windowCenter and windowSize hacked based on http://www.demtron.com/blog/post/2009/01/14/Centering-a-DIV-Window-with-Cross-Browser-JavaScript.aspx
"windowSize" : function()
{
var w = 0;
var h = 0;
//IE
if(!window.innerWidth)
{
//strict mode
if(!(document.documentElement.clientWidth == 0))
{
w = document.documentElement.clientWidth;
h = document.documentElement.clientHeight;
}
//quirks mode
else
{
w = document.body.clientWidth;
h = document.body.clientHeight;
}
}
//w3c
else
{
w = window.innerWidth;
h = window.innerHeight;
}
return {width:w,height:h};
},
"windowCenter" : function(hWnd)
{
var _x = 0;
var _y = 0;
var offsetX = 0;
var offsetY = 0;
//IE
if(!window.pageYOffset)
{
//strict mode
if(!(document.documentElement.scrollTop == 0))
{
offsetY = document.documentElement.scrollTop;
offsetX = document.documentElement.scrollLeft;
}
//quirks mode
else
{
offsetY = document.body.scrollTop;
offsetX = document.body.scrollLeft;
}
}
//w3c
else
{
offsetX = window.pageXOffset;
offsetY = window.pageYOffset;
}
_sz = BitBase.windowSize();
_x = ((_sz.width-hWnd.width)/2)+offsetX;
_y = ((_sz.height-hWnd.height)/2)+offsetY;
return{x:_x,y:_y};
},
// constants
// DATE - set in init()
// newWindow used in closeWin()
"newWindow":null,
//-- initialization --//
// desc: setup called at the end of this file
"init": function(){
var self = BitBase;
self.DATE = new Date();
var tz_offset = -(self.DATE.getTimezoneOffset() * 60);
self.DATE.setFullYear(self.DATE.getFullYear() + 1);
self.setCookie("tz_offset", tz_offset);
self.setCookie("javascript_enabled", "y");
},
"deprecatedFunc": function ( funcName ){
alert( 'Warning: Use of deprecated global function ' + funcName + ' use name space BitBase: BitBase.' + funcName )
},
// Adds a function to be called at page load time
"addLoadHook": function(func) {
if ( typeof window.addEventListener != "undefined" ) {
window.addEventListener( "load", func, false );
} else if ( typeof window.attachEvent != "undefined" ) {
window.attachEvent( "onload", func );
} else {
if ( window.onload != null ) {
var oldOnload = window.onload;
window.onload = function ( e ) {
oldOnload( e );
func(e);
};
} else {
window.onload = func(e);
}
}
},
//-- cookie coercion --//
// desc: Creates a Cookie
// params: name = the name of the cookie
// value = the value placed in the cookie
// [expires] (optional) = the expiration date of the cookie (defaults to end of current session)
// [path] (optional) = the path for which the cookie is valid (defaults to path of calling document)
// [domain] (optional) = the domain for which the cookie is valid (defaults to domain of calling document)
// [secure] (optional) = Boolean value indicating if the cookie transmission requires a secure transmission
// NOTE: * an argument defaults when it is assigned null as a placeholder
// * a null placeholder is not required for trailing omitted arguments
"setCookie": function(name, value, expire, path, domain, secure) {
var self = BitBase;
var path = (path) ? path : bitCookiePath;
var domain = escape((domain) ? domain : bitCookieDomain);
var cookie_path = ((path) ? "; path=" + path : "");
var cookie_domain = ((domain) ? "; domain=" + domain : "");
var cookie_expire = ((expire)?expire:((self.DATE) ? "; expires=" + self.DATE.toGMTString() : ""));
var cookie_secure = ((secure) ? "; secure" : "");
var curCookie = name + "=" + escape(value)
+ cookie_path
+ cookie_domain
+ cookie_expire
+ cookie_secure;
document.cookie = curCookie;
},
// desc: creates a cookie if required and sets the element in the array
// params: name - cookie name / key - in the cookie array / val - value to store
"setCookieArray": function(name, key, val){
var self = BitBase;
var curval = self.getCookie(name);
var newval;
if (curval != null) {
newval = self.unserialize(curval);
newval[key] = val;
}
else {
newval = new Array();
newval[key] = val;
}
self.setCookie(name, BitBase.serialize(newval));
},
// desc: Gets a Cookie and returns it's value
// params: name = the name of the desired cookie
"getCookie": function(name) {
var dc = document.cookie;
var prefix = name + "=";
var begin = dc.indexOf("; " + prefix);
if (begin == -1) {
begin = dc.indexOf(prefix);
if (begin != 0)
return null;
} else begin += 2;
var end = document.cookie.indexOf(";", begin);
if (end == -1)
end = dc.length;
return unescape(dc.substring(begin + prefix.length, end));
},
// desc: Returns a value from a key stored in an array in a cookie.
// params: name - cookie name / key - in the cookie array
"getCookieArray": function(name, key) {
var self = BitBase;
var curval = self.getCookie(name);
var val;
if (curval != null) {
var arr = self.unserialize(curval);
val = arr[key];
}
return val;
},
// desc: Deletes a Cookie
// params: name = the name of the cookie
// [path] (optional) = the path of the cookie (must be same path used when created)
// [domain] (optional) = the domain of the cookie (must be same domain used when created)
// NOTE: path and domain default if assigned null or omitted if no explicit argument proceeds
"deleteCookie": function(name, path, domain) {
var self = BitBase;
var cookie_path = (path) ? path : bitCookiePath;
var cookie_domain = escape((domain) ? domain : bitCookieDomain);
if (self.getCookie(name)) {
document.cookie = name + "="
+ "; path=" + cookie_path + "; domain=" + cookie_domain + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
}
},
//-- DOM coercion --//
// desc: returns a dom element
// parms: id - element id to look up or if id is an element it will be returned
"getElement": function(id){
if(typeof(id) == "string"){
if(document.layers){
return document.layers[id];
}else if(document.all){
return document.all[id];
}else if(document.getElementById){
return document.getElementById(id);
}
}
return id;
},
// desc: sets display style
// params: elm a DOM element or id / val the style value to toggle / useCookie to persist the setting
"setElementDisplay": function( elm, val, useCookie ){
var self = BitBase;
var obj = self.getElement( elm );
if( obj != null ){ obj.style.display=val; }
if (useCookie && obj.id) { self.setCookieArray('showhide', obj.id, (val=='none'?"c":"o")); }
},
// desc: toggles an element's style between the value and none
// params: elm a DOM element or id / val the style value to toggle / useCookie to persist the setting
"toggleElementDisplay": function( elm, val, useCookie ){
var self = BitBase;
var obj = self.getElement( elm );
if( typeof(val) == 'undefined' ) {
val = 'block';
}
var value = obj.style.display == 'none'?val:'none';
self.setElementDisplay( obj, value, useCookie );
return value != 'none';
},
/**
* Hides all elements that have the specified class name
* @param className The class name of the elements to hide
*/
"hideByClass": function( className ) {
var elements = document.getElementsByClassName(className);
for (var i = 0; i < elements.length; i++) {
elements[i].style.display = 'none';
}
},
/**
* Shows all elements that have the specified class name
* @param className The class name of the elements to show
* @param displayType Optional: The display style to use (e.g., 'block', 'inline'). Defaults to '' (CSS default).
*/
"showByClass": function(className, displayType) {
var elements = document.getElementsByClassName(className);
// If displayType is not provided (undefined/null), it defaults to 'block'
var display = displayType || 'block';
for (var i = 0; i < elements.length; i++) {
elements[i].style.display = display;
}
},
// desc: convenience
// params: elm - ad DOM element or id / useCookie = any value (not 0 or null) to turn cookies on
"showById": function( elm, useCookie ) {
BitBase.setElementDisplay( elm, 'block', useCookie );
},
// desc: convenience
// params: elm - ad DOM element or id / useCookie = any value (not 0 or null) to turn cookies on
"hideById": function( elm, useCookie) {
BitBase.setElementDisplay( elm, 'none', useCookie );
},
// shows or hides based on the showhide cookie
"setupShowHide": function() {
var self = BitBase;
var curval = self.getCookie('showhide');
if (curval != null) {
var ids = self.unserialize(curval);
for (id in ids) {
if (ids[id] == "o") {
self.showById(id);
}
else {
self.hideById(id);
}
}
}
},
// desc: Toggles the visibility of dynamic variable passed to it
// params: name
"toggleDynamicVar": function(name) {
var self = BitBase;
var elm1 = self.getElement('dyn_'+name+'_display');
var elm2 = self.getElement('dyn_'+name+'_edit');
if(elm1.style.display == "none") {
elm2.style.display = "none";
elm1.style.display = "inline";
} else {
elm1.style.display = "none";
elm2.style.display = "inline";
}
},
// desc: convenience
"showSpinner": function() {
// Center the spinner
div = document.getElementById( 'spinner' );
w = div.style.width.replace(/[a-z]/gi, '');
h = div.style.height.replace(/[a-z]/gi,'');
center = BitBase.windowCenter({width: w, height: h});
div.style.top = center.y + "px";
div.style.left = center.x + "px"
// show the spinner and overlay.
BitBase.setElementDisplay( 'spinner_overlay', 'block' );
BitBase.setElementDisplay( 'spinner', 'block');
},
// desc: convenience
"hideSpinner": function() {
BitBase.setElementDisplay( 'spinner','none' );
BitBase.setElementDisplay( 'spinner_overlay','none' );
},
// desc: No Idea - used by insertAt
"setSelectionRange": function(textarea, selectionStart, selectionEnd) {
if (textarea.setSelectionRange) {
textarea.focus();
textarea.setSelectionRange(selectionStart, selectionEnd);
} else if (textarea.createTextRange) {
var range = textarea.createTextRange();
textarea.collapse(true);
textarea.moveEnd('character', selectionEnd);
textarea.moveStart('character', selectionStart);
textarea.select();
}
},
// desc: No Idea - used by insertAt
"setCaretToPos": function(textarea, pos) {
BitBase.setSelectionRange(textarea, pos, pos);
},
// desc: inserts replaceString in elementId - used by QuickTags & Plugin Help
// params: elementId = a HTML Id / replaceString = string
"insertAt": function(elementId, replaceString) {
var self = BitBase;
//inserts given text at selection or cursor position
//substrings in replaceString to be replaced by the selection if a selection was done
var toBeReplaced = /text|page|textarea_id/;
// FCKEditor is completely different
if (document.FCKEditorLoaded) {
oEditor = FCKeditorAPI.GetInstance(elementId);
// Check if it is a fckeditor. If not fall back on old code.
if (oEditor) {
// Fetching selection can't be done through the 'Selection'. Stupid.
if (document.all) {
oSel = oEditor.EditorDocument.selection.createRange().text;
} else {
oSel = oEditor.EditorWindow.getSelection();
}
// Convert oSel to a string.
oSel = "" + oSel;
if (oSel.length > 0) {
replaceString = replaceString.replace(toBeReplaced, oSel);
// Delete selection
oEditor.Selection.Delete();
}
oEditor.InsertHtml(replaceString);
return;
}
}
var textarea = document.getElementById(elementId);
if (textarea.setSelectionRange) {
//Mozilla UserAgent Gecko-1.4
var selectionStart = textarea.selectionStart;
var selectionEnd = textarea.selectionEnd;
// has there been a selection
if (selectionStart != selectionEnd) {
var newString = replaceString.replace(toBeReplaced, textarea.value.substring(selectionStart, selectionEnd));
textarea.value = textarea.value.substring(0, selectionStart)
+ newString
+ textarea.value.substring(selectionEnd);
self.setSelectionRange(textarea, selectionStart, selectionStart + newString.length);
// set caret
} else {
textarea.value = textarea.value.substring(0, selectionStart)
+ replaceString
+ textarea.value.substring(selectionEnd);
self.setCaretToPos(textarea, selectionStart + replaceString.length);
}
} else if (document.selection) {
//UserAgent IE-6.0
textarea.focus();
var range = document.selection.createRange();
if (range.parentElement() == textarea) {
var isCollapsed = range.text == '';
if (! isCollapsed) {
range.text = replaceString.replace(toBeReplaced, range.text);
range.moveStart('character', -range.text.length);
range.select();
} else {
range.text = replaceString;
} }
} else {
//UserAgent Gecko-1.0.1 (NN7.0)
var obj = self.getElement(elementId);
obj.getElement(elementId).value = obj.value + replaceString;
//alert("don't know yet how to handle insert" + document);
}
},
// function: flipMulti
// desc: Toggles visibility for multiple HTML elements - can be used with an HTML Selector
// On the first call - Shows the window id's defined by (header) & (numbr) & saves them in a variable
// On subsequent calls - Hide the saved windows and shows the new windows
// added by: StarRider
// date: 1/5/06
// Note For Cross-Browser compatibility - An HTML elements id's must begin with a Letter or String.
// Operates by appending a string (header) with the number (numbr) to show/hide the window id.
// With multiple windows - the id's need to be sequencial eg. each id is 1 greater than the last
// with the first being (numbr).
// example: When used in a tpl file with div elements: $header='dog';
//