// // Base JavaScript code for the OrdinaSoft library. // // Author: OrdinaSoft // Patrick Lanz // Lausanne // info@ordinasoft.ch // // First version: May 1st, 2007 // // (c) 2007, OrdinaSoft, all rights reserved //----------------------------------------------------------------------------------------------- // Namespace initialization. if (typeof OrdinaSoft == 'undefined') OrdinaSoft = new Object (); if (typeof OrdinaSoft.Base == 'undefined') OrdinaSoft.Base = new Object (); if (typeof OrdinaSoft.Base.Select == 'undefined') OrdinaSoft.Base.Select = new Object (); //----------------------------------------------------------------------------------------------- // Global variables. OrdinaSoft.Base.OnLoadFired = false; // indicates if we already received the "onload" event OrdinaSoft.Base.OnLoadFuncs = []; // functions to execute on load OrdinaSoft.Base.DivToTrack = []; // div elements to track OrdinaSoft.Base.TrackTimer = null; // tracking timer //----------------------------------------------------------------------------------------------- // "onload" management. // Adds a function to be called when the page is completely loaded. // - Func: the function to call. function AddLoadEvent (Func) { if (OrdinaSoft.Base.OnLoadFired) { Func (); return true; } // OrdinaSoft.Base.OnLoadFired OrdinaSoft.Base.OnLoadFuncs [OrdinaSoft.Base.OnLoadFuncs.length] = Func; return true; } // AddLoadEvent // Initialization: to call the registered functions. OrdinaSoft.Base.Init = function () { OrdinaSoft.Base.OnLoadFired = true; var Funcs = OrdinaSoft.Base.OnLoadFuncs; for (var i = 0; i < Funcs.length; i++) Funcs [i] (); return true; } // OrdinaSoft.Base.Init // Initializes the firing of the "onload" event. if (typeof window.addEventListener != 'undefined') window.addEventListener ('load', OrdinaSoft.Base.Init, false); else if (typeof document.addEventListener != 'undefined') document.addEventListener ('load', OrdinaSoft.Base.Init, false); else if (typeof window.attachEvent != 'undefined') window.attachEvent ('onload', OrdinaSoft.Base.Init); else { if (typeof window.onload != 'function') window.onload = OrdinaSoft.Base.Init; else { OldFn = window.onload; window.onload = function () { OldFn (); OrdinaSoft.Base.Init (); } } } // Can't use DOM function //----------------------------------------------------------------------------------------------- // String utilities. // Quotes a string within apostrophes. // If the string contains apostrophes, they will be preceded with a '\'. // If the string contains double-quotes, they will be replaced with '"' OrdinaSoft.Base.QuoteString = function (s) { return '\'' + s.replace (/'/g, '\\\''). replace (/"/g, '"') + '\''; } // OrdinaSoft.Base.QuoteString //----------------------------------------------------------------------------------------------- // Tracking timer. // Starts the tracking timer when necessary. The tracking timer is used for repetitive tasks. OrdinaSoft.Base._StartTracking = function () { if (OrdinaSoft.Base.TrackTimer == null) OrdinaSoft.Base.TrackTimer = setInterval (OrdinaSoft.Base._DoTracking, 250); return true } // OrdinaSoft.Base._StartTracking // Do the tracking. OrdinaSoft.Base._DoTracking = function () { OrdinaSoft.Base._TrackClientDiv (); return true; } // OrdinaSoft.Base._DoTracking //----------------------------------------------------------------------------------------------- // Getting the geometry of the browser window. // Returns the current client and height. // - The result will be an object with a Width and a Height fields. OrdinaSoft.Base.GetClientSize = function () { var Res = new Object (); if ((typeof document.documentElement != 'undefined') && (typeof document.documentElement.clientWidth != 'undefined')) { Res.Width = document.documentElement.clientWidth; Res.Height = document.documentElement.clientHeight; return Res; } // document.documentElement.clientWidth is defined Res.Width = document.body.clientWidth; Res.Height = document.body.clientHeight; return Res; } // OrdinaSoft.Base.GetClientSize // Returns the current scroll position. // - The result will be an object with a Horizontal and a Vertical fields. OrdinaSoft.Base.GetScrollingPosition = function () { var Res = new Object (); if (typeof window.pageXOffset != 'undefined') { Res.Horizontal = window.pageXOffset; Res.Vertical = window.pageYOffset; return Res; } // typeof window.pageXOffset != 'undefined' if ((typeof document.documentElement != 'undefined') && (typeof document.documentElement.scrollLeft != 'undefined')) { Res.Horizontal = document.documentElement.scrollLeft; Res.Vertical = document.documentElement.scrollTop; return Res; } // document.documentElement.scrollLeft is defined Res.Horizontal = document.body.clientWidth; Res.Vertical = document.body.clientHeight; return Res; } // OrdinaSoft.Base.GetScrollingPosition //----------------------------------------------------------------------------------------------- // div elements in the window's client. // Creates a div element that is the exact size of the window's client. // - The result will be the created element. OrdinaSoft.Base.GetClientDiv = function () { var ClientSize = OrdinaSoft.Base.GetClientSize (); var ScrollingPosition = OrdinaSoft.Base.GetScrollingPosition (); var Div = document.createElement ('div'); var Style = Div.style; Style.position = 'absolute'; Style.left = ScrollingPosition.Horizontal.toString () + 'px'; Style.top = ScrollingPosition.Vertical.toString () + 'px'; Style.width = ClientSize.Width.toString () + 'px'; Style.height = ClientSize.Height.toString () + 'px'; OrdinaSoft.Base.StartClientDivTracking (Div); return Div; } // OrdinaSoft.Base.GetClientDiv // Tracks a div element so that it is always the same size and position as the window's client. // - Div is the div element to track. OrdinaSoft.Base.StartClientDivTracking = function (Div) { OrdinaSoft.Base.DivToTrack [OrdinaSoft.Base.DivToTrack.length] = Div; OrdinaSoft.Base._StartTracking (); return true; } // OrdinaSoft.Base.StartClientDivTracking // Ends the tracking of a div element. // - Div is the div element to stop to track. OrdinaSoft.Base.StopClientDivTracking = function (Div) { var Nb = 0; for (var i = 0; i < OrdinaSoft.Base.DivToTrack.length; i++) { var CurrDiv = OrdinaSoft.Base.DivToTrack [i]; if (CurrDiv != null) { if (CurrDiv == Div) OrdinaSoft.Base.DivToTrack [i] = null; else Nb++ } // CurrDiv != null } // for i, for all the divs to track if (Nb == 0) OrdinaSoft.Base.DivToTrack = []; return true; } // OrdinaSoft.Base.StopClientDivTracking // Do the tracking of the elements. OrdinaSoft.Base._TrackClientDiv = function () { var ClientSize = OrdinaSoft.Base.GetClientSize (); var ScrollingPosition = OrdinaSoft.Base.GetScrollingPosition (); for (var i = 0; i < OrdinaSoft.Base.DivToTrack.length; i++) { var CurrDiv = OrdinaSoft.Base.DivToTrack [i]; if (CurrDiv != null) { var Style = CurrDiv.style; Style.left = ScrollingPosition.Horizontal.toString () + 'px'; Style.top = ScrollingPosition.Vertical.toString () + 'px'; Style.width = ClientSize.Width.toString () + 'px'; Style.height = ClientSize.Height.toString () + 'px'; } // CurrDiv != null } // for i, for all the divs to track return true; } // OrdinaSoft.Base._TrackClientDiv //----------------------------------------------------------------------------------------------- // Management of elements. // Gets the absolute horizontal position of an element. // - Element is the element to process. // - The result will be the absolute horizontal position of the element. OrdinaSoft.Base.GetElementLeftAbs = function (Element) { var Pos = Element.offsetLeft; var Parent = Element.offsetParent; while (Parent != null) { Pos += Parent.offsetLeft; Parent = Parent.offsetParent; } // while Parent != null return Pos; } // OrdinaSoft.Base.GetElementLeftAbs // Gets the absolute vertical position of an element. // - Element is the element to process. // - The result will be the absolute vertical position of the element. OrdinaSoft.Base.GetElementTopAbs = function (Element) { var Pos = Element.offsetTop; var Parent = Element.offsetParent; while (Parent != null) { Pos += Parent.offsetTop; Parent = Parent.offsetParent; } // while Parent != null return Pos; } // OrdinaSoft.Base.GetElementTopAbs //----------------------------------------------------------------------------------------------- // Tools for select elements. // Gets the value of the currently selected item in a select. If no item is selected, the result // will be the default value. // - Select: the select element, or its identifier. // - Default: the default value. OrdinaSoft.Base.Select.GetCurrent = function (Select, Default) { switch (typeof Select) { case 'undefined' : return Default; case 'string' : Select = document.getElementById (Select); if (Select == null) return Default; break; } // switch (typeof Select) var ix = Select.selectedIndex; if (ix == -1) return Default; return Select.options [ix].value; } // OrdinaSoft.Base.Select.GetCurrent // Gets the value of the option with the specified text in a select. If no option match, the // result will be null. // - Select: the select element, or its identifier. // - Text: the text to search for. // - The result will be the value of the option with the specified text, or null if no matching // option has been found. OrdinaSoft.Base.Select.GetValueFromText = function (Select, Text) { switch (typeof Select) { case 'undefined' : return null; case 'string' : Select = document.getElementById (Select); if (Select == null) return null; break; } // switch (typeof Select) var Options = Select.options; for (var i = 0; i < Options.length; i++) { var Option = Options [i]; if (Option.text == Text) return Option.value; } // for all the options in the select return null; } // OrdinaSoft.Base.Select.GetValueFromText // Sets the selected option to the one with the specified text. If no option match, no option // will be selected. // - Select: the select element, or its identifier. // - Text: the text of the option to select. // - The result will be true if we have found an option with the selected text. OrdinaSoft.Base.Select.SelectText = function (Select, Text) { switch (typeof Select) { case 'undefined' : return false; case 'string' : Select = document.getElementById (Select); if (Select == null) return false; break; } // switch (typeof Select) var Options = Select.options; for (var i = 0; i < Options.length; i++) { var Option = Options [i]; if (Option.text == Text) { Select.selectedIndex = i; return true; } } // for all the options in the select return false; } // OrdinaSoft.Base.Select.SelectText // Sets the selected option to the one with the specified value. If no option match, no option // will be selected. // - Select: the select element, or its identifier. // - Value: the value of the option to select. // - The result will be true if we have found an option with the selected text. OrdinaSoft.Base.Select.SelectValue = function (Select, Value) { switch (typeof Select) { case 'undefined' : return false; case 'string' : Select = document.getElementById (Select); if (Select == null) return false; break; } // switch (typeof Select) Value = Value.toString (); var Options = Select.options; for (var i = 0; i < Options.length; i++) { var Option = Options [i]; if (Option.value == Value) { Select.selectedIndex = i; return true; } } // for all the options in the select return false; } // OrdinaSoft.Base.Select.SelectValue //----------------------------------------------------------------------------------------------- OrdinaSoft_Base_Initialized = true; // indicates that the library is initialized