r8883@llin: dpavlin | 2005-11-14 21:39:14 +0100
[webpac2] / web / iwf / iwfcore.js
index fbc9526..99d1c2a 100644 (file)
@@ -1,48 +1,64 @@
-// -----------------------------------------------------------------------------\r
-// IWF - Interactive Website Framework.  Javascript library for creating\r
-// responsive thin client interfaces.\r
+// =========================================================================\r
+/// IWF - Interactive Website Framework.  Javascript library for creating\r
+/// responsive thin client interfaces.\r
+///\r
+/// Copyright (C) 2005 Brock Weaver brockweaver@users.sourceforge.net\r
+///\r
+///     This library is free software; you can redistribute it and/or modify\r
+/// it under the terms of the GNU Lesser General Public License as published\r
+/// by the Free Software Foundation; either version 2.1 of the License, or\r
+/// (at your option) any later version.\r
+///\r
+///     This library is distributed in the hope that it will be useful, but\r
+/// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
+/// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public\r
+/// License for more details.\r
+///\r
+///    You should have received a copy of the GNU Lesser General Public License\r
+/// along with this library; if not, write to the Free Software Foundation,\r
+/// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\r
+///\r
+/// Brock Weaver\r
+/// brockweaver@users.sourceforge.net\r
+/// 1605 NW Maple Pl\r
+/// Ankeny, IA 50021\r
+///\r
+//! http://iwf.sourceforge.net/\r
+// =========================================================================\r
+//! NOTE: To minimize file size, strip all fluffy comments (except the LGPL, of course!)\r
+//! using the following regex (global flag and multiline on):\r
+//!            ^\t*//([^/!].*|$)\r
 //\r
-// Copyright (C) 2005 Brock Weaver brockweaver@gmail.com\r
+// This reduces file size by about 30%, give or take.\r
 //\r
-//     This library is free software; you can redistribute it and/or modify\r
-// it under the terms of the GNU Lesser General Public License as published\r
-// by the Free Software Foundation; either version 2.1 of the License, or\r
-// (at your option) any later version.\r
-//\r
-//     This library is distributed in the hope that it will be useful, but\r
-// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
-// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public\r
-// License for more details.\r
-//\r
-//    You should have received a copy of the GNU Lesser General Public License\r
-// along with this library; if not, write to the Free Software Foundation,\r
-// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\r
-//\r
-// Brock Weaver\r
-// brockweaver@gmail.com\r
-// 1605 NW Maple Pl\r
-// Ankeny, IA 50021\r
-// -----------------------------------------------------------------------------\r
+//!  To rip out only logging statements (commented or uncommented):\r
+//!            ^/{0,2}iwfLog.*\r
+// =========================================================================\r
 \r
 // --------------------------------------------------------------------------\r
-// iwfcore.js\r
+//! iwfcore.js\r
 //\r
 // Core functions\r
 //\r
-// Dependencies:\r
-// (none)\r
+//! Dependencies:\r
+//! (none)\r
 //\r
-// Brock Weaver - brockweaver@sourceforge.net - iwf.sourceforge.net\r
-// v 0.1 - 2005-06-05\r
-// Initial release.\r
-// --------------------------------------------------------------------------\r
+//! Brock Weaver - brockweaver@users.sourceforge.net\r
+//! v 0.2 - 2005-11-14\r
+//! iwfcore bug patch\r
+//! --------------------------------------------------------------------------\r
+//! - fixed iwfAttribute to return most current value instead of initial value\r
+//!\r
+//! v 0.1 - 2005-06-05\r
+//! Initial release.\r
+//! --------------------------------------------------------------------------\r
 \r
 // -----------------------------------\r
 // Begin: Configurable variables\r
 // -----------------------------------\r
 \r
 // set to true to enable logging.  Logging simply means certain values are appended to a string. nothing is written out or communicated over the wire anywhere.\r
-var iwfLoggingEnabled = true;\r
+var _iwfLoggingEnabled = true;\r
 \r
 \r
 // -----------------------------------\r
@@ -80,7 +96,7 @@ function iwfGetForm(id){
        return frm;\r
 }\r
 \r
-function iwfGetByIdWithinForm(form, id){\r
+function iwfGetByNameWithinForm(form, nm){\r
        var frm = iwfGetForm(form);\r
        if (!frm){\r
                iwfLog("IWF Core Error: Could not locate form by id, document.forms, or document[] named '" + form + "'", true);\r
@@ -88,9 +104,9 @@ function iwfGetByIdWithinForm(form, id){
        } else {\r
                // find element within this form with given id.\r
                var el = null;\r
-               if (iwfIsString(id) || iwfIsNumber(id)) {\r
+               if (iwfIsString(nm) || iwfIsNumber(nm)) {\r
                        for(var i=0;i<frm.elements.length;i++){\r
-                               if (frm.elements[i].name == id || frm.elements[i].id == id){\r
+                               if (frm.elements[i].name == nm || frm.elements[i].id == nm){\r
                                        el = frm.elements[i];\r
                                        break;\r
                                }\r
@@ -98,12 +114,12 @@ function iwfGetByIdWithinForm(form, id){
                } else {\r
                        el = id;\r
                }\r
-//iwfLog('iwfGetByIdWithinForm returning:\n\n' + iwfElementToString(el), true);\r
+//iwfLog('iwfGetByNameWithinForm returning:\n\n' + iwfElementToString(el), true);\r
                return el;\r
        }\r
 }\r
 \r
-function iwfGetOrCreateWithinForm(form, id, tagNameOrHtml, typeAtt){\r
+function iwfGetOrCreateByNameWithinForm(form, nm, tagNameOrHtml, typeAtt){\r
        var elFrm = iwfGetForm(form);\r
 \r
        if (!elFrm){\r
@@ -111,11 +127,11 @@ function iwfGetOrCreateWithinForm(form, id, tagNameOrHtml, typeAtt){
                return;\r
        }\r
 \r
-       var el = iwfGetByIdWithinForm(form, id);\r
+       var el = iwfGetByNameWithinForm(form, nm);\r
        if (!el){\r
                // element does not exist. create it.\r
                el = document.createElement(tagNameOrHtml);\r
-               iwfAttribute(el, 'name', id);\r
+               iwfAttribute(el, 'name', nm);\r
 \r
                if (typeAtt){\r
                        iwfAttribute(el, 'type', typeAtt);\r
@@ -149,9 +165,9 @@ function iwfGetOrCreateById(id, tagNameOrHtml, parentNodeId){
                if (parentNodeId){\r
                        var elParent = iwfGetById(parentNodeId);\r
                        if (elParent){\r
-                               iwfAppendChild(elParent, el);\r
+                               iwfAddChild(elParent, el);\r
                        } else if (parentNodeId.toLowerCase() == 'body'){\r
-                               iwfAppendChild(document.body, el);\r
+                               iwfAddChild(document.body, el);\r
                        }\r
                }\r
 \r
@@ -160,7 +176,7 @@ function iwfGetOrCreateById(id, tagNameOrHtml, parentNodeId){
        return el;\r
 }\r
 \r
-function iwfAppendChild(parentNodeId, childNodeId){\r
+function iwfAddChild(parentNodeId, childNodeId, atFront){\r
 \r
                var elChild = iwfGetById(childNodeId);\r
                if (!elChild) return null;\r
@@ -182,16 +198,22 @@ function iwfAppendChild(parentNodeId, childNodeId){
 \r
 \r
 \r
-               // append the element to the parent\r
-               elParent.appendChild(elChild);\r
+               if (!atFront || !elParent.hasChildNodes()){\r
+                       // append as last child of elParent\r
+                       elParent.appendChild(elChild);\r
+               } else {\r
+                       // append as first child of elParent\r
+                       elParent.insertBefore(elChild, elParent.childNodes[0]);\r
+               }\r
 \r
                return elParent;\r
 }\r
 \r
 function iwfRemoveNode(id){\r
        var el = iwfGetById(id);\r
-       if (!el) return;\r
+       if (!el) return false;\r
        document.removeNode(el);\r
+       return true;\r
 }\r
 \r
 function iwfElementToString(id){\r
@@ -202,7 +224,7 @@ function iwfElementToString(id){
        if (el.attributes){\r
                for(var i=0;i<el.attributes.length;i++){\r
                        var att = el.attributes[i];\r
-                       s += ' ' + att.nodeName + '=' + att.nodeValue + ' ';\r
+                       s += ' ' + att.nodeName + '="' + att.nodeValue + '" ';\r
                }\r
        }\r
        if (el.innerHTML == ''){\r
@@ -258,7 +280,14 @@ function iwfAttribute(id, attName, newval){
                        el.setAttribute(attName, newval);\r
                }\r
        }\r
-       val = el.getAttribute(attName);\r
+       // 2005-11-14 Brock Weaver\r
+       // added check for attribute on el before trying getAttribute method\r
+       // (Thanks J.P. Jarolim!)\r
+       if (el[attName]){\r
+               val = el[attName];\r
+       } else if (el.getAttribute) {\r
+               val = el.getAttribute(attName);\r
+       }\r
        return val;\r
 }\r
 \r
@@ -325,7 +354,7 @@ function iwfHtmlDecode(s){
        return ret;\r
 }\r
 // -----------------------------------\r
-// End: Xml Utility Functions\r
+// End: Encoding Utility Functions\r
 // -----------------------------------\r
 \r
 \r
@@ -358,9 +387,9 @@ function iwfIsDate(val){
                return false;\r
        } else {\r
                // determine if the month/day makes sense.\r
-               var mo = parseInt(dt.substring(0,2), 10);\r
-               var dy = parseInt(dt.substring(3,2), 10);\r
-               var yr = parseInt(dt.substring(6,4), 10);\r
+               var mo = iwfToInt(dt.substring(0,2));\r
+               var dy = iwfToInt(dt.substring(3,2));\r
+               var yr = iwfToInt(dt.substring(6,4));\r
                var maxdy = 28;\r
                switch(mo){\r
                        case 4:\r
@@ -397,19 +426,19 @@ function iwfDateFormat(val){
        var arr = val.split(delim);\r
        switch(arr.length){\r
                case 2:\r
-                       // possibles:  9/2, 9/2004, 09/06,\r
-                       // assume first is always month\r
+                       //! possibles:  9/2, 9/2004, 09/06,\r
+                       //! assume first is always month\r
                        mo = '00' + arr[0];\r
                        if (arr[1].length == 4){\r
-                               // assume second is year.\r
+                               //! assume second is year.\r
                                yr = arr[1];\r
                        } else {\r
-                               // assume second is date.\r
+                               //! assume second is date.\r
                                dy = '00' + arr[1];\r
                        }\r
                        break;\r
                case 3:\r
-                       // possibles: 9/2/1, 9/02/04, 09/02/2004, 9/2/2004\r
+                       //! possibles: 9/2/1, 9/02/04, 09/02/2004, 9/2/2004\r
                        mo = '00' + arr[0];\r
                        dy = '00' + arr[1];\r
                        switch(arr[2].length){\r
@@ -424,7 +453,7 @@ function iwfDateFormat(val){
                                        }\r
                                        break;\r
                                case 3:\r
-                                       // 3 digits... assume 2000 I guess\r
+                                       //! 3 digits... assume 2000 I guess\r
                                        yr = '2' + arr[2];\r
                                        break;\r
                                case 4:\r
@@ -486,7 +515,6 @@ function iwfFloatFormat(val, dp, stripFormatting){
 // Begin: Form Submittal Utility Functions\r
 // -----------------------------------\r
 function iwfDoAction(act, frm, id, targetElement){\r
-//alert('action=' + act + '\nfrm=' + frm + '\nid=' + id + '\ntargetElement=' + targetElement);\r
        // validate the form first\r
        if (window.iwfOnFormValidate){\r
                if (!iwfOnFormValidate(act)){\r
@@ -516,12 +544,11 @@ function iwfDoAction(act, frm, id, targetElement){
                if (!iwfGetParent(elId)){\r
                        // our element has not been added to the document yet.\r
                        iwfAttribute(elId, 'type', 'hidden');\r
-                       if (!iwfAppendChild(frm, elId)){\r
+                       if (!iwfAddChild(frm, elId)){\r
                                iwfLog('IWF Core Error: Created iwfId element, but could not append to form ' + frm.outerHTML, true);\r
                                return;\r
                        }\r
                }\r
-//alert(iwfElementToString(elId) + '\n\ndisabled=' + iwfAttribute(elId, 'disabled') + '\n\n' + elId.disabled);\r
        }\r
 \r
 \r
@@ -536,7 +563,7 @@ function iwfDoAction(act, frm, id, targetElement){
                if (!iwfGetParent(elMode)){\r
                        // our element has not been added to the document yet.\r
                        iwfAttribute(elMode, 'type', 'hidden');\r
-                       if (!iwfAppendChild(frm, elMode)){\r
+                       if (!iwfAddChild(frm, elMode)){\r
                                iwfLog('IWF Core Error: Created iwfMode element, but could not append to form ' + frm.outerHTML, true);\r
                                return;\r
                        }\r
@@ -559,7 +586,7 @@ function iwfDoAction(act, frm, id, targetElement){
                        if (!iwfGetParent(elTarget)){\r
                                // our element has not been added to the document yet.\r
                                iwfAttribute(elTarget, 'type', 'hidden');\r
-                               if (!iwfAppendChild(frm, elTarget)){\r
+                               if (!iwfAddChild(frm, elTarget)){\r
                                        iwfLog('IWF Core Error: Created iwfTarget element, but could not append to form ' + frm.outerHTML, true);\r
                                        return;\r
                                }\r
@@ -574,7 +601,7 @@ function iwfDoAction(act, frm, id, targetElement){
                }\r
        } else {\r
                // do a normal html submit, since they didn't specify a particular target\r
-alert('doing frm.submit()');\r
+iwfLog('doing frm.submit()', true);\r
                frm.submit();\r
        }\r
 }\r
@@ -601,7 +628,7 @@ function iwfDoCancel(formId, id, targetElement){
 function iwfMailTo(uid, host){\r
        // this is just so an email doesn't have to be output to the browser in raw text for\r
        // email harvesters to grab...\r
-       return 'mailto:' + uid + '@' + host;\r
+       location.href = 'mailto:' + uid + '@' + host;\r
 }\r
 \r
 function iwfClickLink(id){\r
@@ -636,11 +663,10 @@ function iwfShowMessage(msg){
 \r
 var _iwfLoggedItems = "";\r
 function iwfLog(txt, showAlert){\r
-       if (iwfLoggingEnabled){\r
+       if (_iwfLoggingEnabled){\r
                _iwfLoggedItems += txt + '\n';\r
        } else {\r
-               // send to big bit bucket in the sky\r
-               // | /dev/null\r
+               //! send to big bit bucket in the sky (/dev/null)\r
        }\r
        if (showAlert){\r
                alert(txt);\r
@@ -662,8 +688,8 @@ function iwfRefreshLog(){
 }\r
 \r
 function iwfShowLog(){\r
-       if (!iwfLoggingEnabled){\r
-               alert("Logging for IWF has been disabled.\nSet the iwfLoggingEnabled variable located in the iwfcore.js file to true to enable logging.");\r
+       if (!_iwfLoggingEnabled){\r
+               alert("Logging for IWF has been disabled.\nSet the _iwfLoggingEnabled variable located in the iwfcore.js (or iwfconfig.js) file to true to enable logging.");\r
        } else {\r
                var el = iwfGetOrCreateById('iwfLog', 'div', 'body');\r
                if (!el){\r