1 // -----------------------------------------------------------------------------
\r
2 // IWF - Interactive Website Framework. Javascript library for creating
\r
3 // responsive thin client interfaces.
\r
5 // Copyright (C) 2005 Brock Weaver brockweaver@gmail.com
\r
7 // This library is free software; you can redistribute it and/or modify
\r
8 // it under the terms of the GNU Lesser General Public License as published
\r
9 // by the Free Software Foundation; either version 2.1 of the License, or
\r
10 // (at your option) any later version.
\r
12 // This library is distributed in the hope that it will be useful, but
\r
13 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
14 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
\r
15 // License for more details.
\r
17 // You should have received a copy of the GNU Lesser General Public License
\r
18 // along with this library; if not, write to the Free Software Foundation,
\r
19 // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
22 // brockweaver@gmail.com
\r
25 // -----------------------------------------------------------------------------
\r
27 // --------------------------------------------------------------------------
\r
35 // Brock Weaver - brockweaver@sourceforge.net - iwf.sourceforge.net
\r
36 // v 0.1 - 2005-06-05
\r
38 // --------------------------------------------------------------------------
\r
40 // -----------------------------------
\r
41 // Begin: Configurable variables
\r
42 // -----------------------------------
\r
44 // 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
45 var iwfLoggingEnabled = true;
\r
48 // -----------------------------------
\r
49 // End: Configurable variables
\r
50 // -----------------------------------
\r
53 // -----------------------------------
\r
54 // Begin: Element Utility Functions
\r
55 // -----------------------------------
\r
57 function iwfGetById(id){
\r
59 if (iwfIsString(id) || iwfIsNumber(id)) el = document.getElementById(id);
\r
60 else if (typeof(id) == 'object') el = id;
\r
64 function iwfGetForm(id){
\r
65 var frm = iwfGetById(id);
\r
67 // or by forms collection...
\r
68 frm = document.forms ? document.forms[frm] : null;
\r
70 // or by tag elements...
\r
71 var allforms = iwfGetByTagName('form');
\r
72 for(var i=0;i<allforms.length;i++){
\r
73 if (iwfAttribute(allforms[i], 'name') == id){
\r
83 function iwfGetByIdWithinForm(form, id){
\r
84 var frm = iwfGetForm(form);
\r
86 iwfLog("IWF Core Error: Could not locate form by id, document.forms, or document[] named '" + form + "'", true);
\r
89 // find element within this form with given id.
\r
91 if (iwfIsString(id) || iwfIsNumber(id)) {
\r
92 for(var i=0;i<frm.elements.length;i++){
\r
93 if (frm.elements[i].name == id || frm.elements[i].id == id){
\r
94 el = frm.elements[i];
\r
101 //iwfLog('iwfGetByIdWithinForm returning:\n\n' + iwfElementToString(el), true);
\r
106 function iwfGetOrCreateWithinForm(form, id, tagNameOrHtml, typeAtt){
\r
107 var elFrm = iwfGetForm(form);
\r
110 iwfLog("IWF Core Error: <form> with name or id of '" + form + "' could not be located.", true);
\r
114 var el = iwfGetByIdWithinForm(form, id);
\r
116 // element does not exist. create it.
\r
117 el = document.createElement(tagNameOrHtml);
\r
118 iwfAttribute(el, 'name', id);
\r
121 iwfAttribute(el, 'type', typeAtt);
\r
124 elFrm.appendChild(el);
\r
130 function iwfRemoveChild(parentId, id){
\r
131 var elParent = iwfGetById(parentId);
\r
133 var elChild = iwfGetById(id);
\r
135 elParent.removeChild(elChild);
\r
141 function iwfGetOrCreateById(id, tagNameOrHtml, parentNodeId){
\r
142 var el = iwfGetById(id);
\r
144 // element does not exist. create it.
\r
145 el = document.createElement(tagNameOrHtml);
\r
146 iwfAttribute(el, 'id', id);
\r
147 iwfAttribute(el, 'name', id);
\r
150 var elParent = iwfGetById(parentNodeId);
\r
152 iwfAppendChild(elParent, el);
\r
153 } else if (parentNodeId.toLowerCase() == 'body'){
\r
154 iwfAppendChild(document.body, el);
\r
163 function iwfAppendChild(parentNodeId, childNodeId){
\r
165 var elChild = iwfGetById(childNodeId);
\r
166 if (!elChild) return null;
\r
168 // get the element who is to be the parent
\r
169 var elParent = iwfGetById(parentNodeId);
\r
171 // parent not found, try by tagName
\r
173 var nodes = iwfGetByTagName(parentNodeId);
\r
174 if (nodes && nodes.length > 0){
\r
175 // always use the first element with that tag name as the parent (think 'body')
\r
178 // couldn't find by id or tag name. bomb out.
\r
185 // append the element to the parent
\r
186 elParent.appendChild(elChild);
\r
191 function iwfRemoveNode(id){
\r
192 var el = iwfGetById(id);
\r
194 document.removeNode(el);
\r
197 function iwfElementToString(id){
\r
198 var el = iwfGetById(id);
\r
199 if (!el) return 'element ' + id + ' does not exist.';
\r
201 var s = '<' + el.tagName + ' ';
\r
202 if (el.attributes){
\r
203 for(var i=0;i<el.attributes.length;i++){
\r
204 var att = el.attributes[i];
\r
205 s += ' ' + att.nodeName + '=' + att.nodeValue + ' ';
\r
208 if (el.innerHTML == ''){
\r
211 s += '>' + el.innerHTML + '</' + el.tagName + '>';
\r
217 function iwfGetByTagName(tagName, root) {
\r
218 var nodes = new Array();
\r
219 tagName = tagName || '*';
\r
220 root = root || document;
\r
222 if (tagName == '*'){
\r
225 nodes = root.all.tags(tagName);
\r
227 } else if (root.getElementsByTagName) {
\r
228 nodes = root.getElementsByTagName(tagName);
\r
233 function iwfGetByAttribute(tagName, attName, regex, callback) {
\r
234 var a, list, found = new Array();
\r
235 var reg = new RegExp(regex, 'i');
\r
236 list = iwfGetByTagName(tagName);
\r
237 for(var i=0;i<list.length;++i) {
\r
238 a = list[i].getAttribute(attName);
\r
239 if (!a) {a = list[i][attName];}
\r
240 if (typeof(a)=='string' && a.search(regex) != -1) {
\r
241 found[found.length] = list[i];
\r
242 if (callback) callback(list[i]);
\r
248 function iwfAttribute(id, attName, newval){
\r
249 var el = iwfGetById(id);
\r
253 if (iwfExists(newval)){
\r
254 if (newval == null){
\r
255 // remove it, don't set it to null.
\r
256 iwfRemoveAttribute(el, attName);
\r
258 el.setAttribute(attName, newval);
\r
261 val = el.getAttribute(attName);
\r
265 function iwfRemoveAttribute(id, attName){
\r
266 var el = iwfGetById(id);
\r
268 el.removeAttribute(attName);
\r
273 function iwfGetParent(id, useOffsetParent){
\r
274 var el = iwfGetById(id);
\r
275 if (!el) return null;
\r
277 if (useOffsetParent && iwfExists(el.offsetParent)) { cur = el.offsetParent;
\r
278 } else if (iwfExists(el.parentNode)) { cur = el.parentNode;
\r
279 } else if (iwfExists(el.parentElement)) { cur = el.parentElement; }
\r
283 // -----------------------------------
\r
284 // End: Element Utility Functions
\r
285 // -----------------------------------
\r
288 // -----------------------------------
\r
289 // Begin: Encoding Utility Functions
\r
290 // -----------------------------------
\r
292 function iwfXmlEncode(s){
\r
296 var ret = s.replace(/&/gi, '&').replace(/>/gi,'>').replace(/</gi, '<').replace(/'/gi, ''').replace(/"/gi, '"');
\r
297 //alert('after xmlencoding: \n\n\n' + ret);
\r
301 function iwfXmlDecode(s){
\r
305 var ret = s.replace(/>/gi, '>').replace(/</gi,'<').replace(/'/gi, '\'').replace(/"/gi, '"').replace(/&/gi, '&');
\r
306 //alert('after xmldecoding: \n\n\n' + ret);
\r
310 function iwfHtmlEncode(s){
\r
314 var ret = s.replace(/&/gi, '&').replace(/>/gi,'>').replace(/</gi, '<').replace(/'/gi, ''').replace(/"/gi, '"');
\r
315 //alert('after xmlencoding: \n\n\n' + ret);
\r
319 function iwfHtmlDecode(s){
\r
323 var ret = s.replace(/>/gi, '>').replace(/</gi,'<').replace(/'/gi, '\'').replace(/"/gi, '"').replace(/&/gi, '&');
\r
324 //alert('after xmldecoding: \n\n\n' + ret);
\r
327 // -----------------------------------
\r
328 // End: Xml Utility Functions
\r
329 // -----------------------------------
\r
332 // -----------------------------------
\r
333 // Begin: Conversion / Formatting Utility Functions
\r
334 // -----------------------------------
\r
336 function iwfExists(){
\r
337 for(var i=0;i<arguments.length;i++){
\r
338 if(typeof(arguments[i])=='undefined') return false;
\r
343 function iwfIsString(s){
\r
344 return typeof(s) == 'string';
\r
347 function iwfIsNumber(n){
\r
348 return typeof(n) == 'number';
\r
351 function iwfIsBoolean(b){
\r
352 return typeof(b) == 'boolean';
\r
355 function iwfIsDate(val){
\r
356 var dt = iwfDateFormat(val);
\r
360 // determine if the month/day makes sense.
\r
361 var mo = parseInt(dt.substring(0,2), 10);
\r
362 var dy = parseInt(dt.substring(3,2), 10);
\r
363 var yr = parseInt(dt.substring(6,4), 10);
\r
374 if (yr % 4 == 0 && (yr % 100 != 0 || yr % 400 == 0)){
\r
379 // 1, 3, 5, 7, 8, 10, 12
\r
383 return dy > 0 && dy <= maxdy;
\r
387 function iwfDateFormat(val){
\r
389 if (val.indexOf(delim) == -1){
\r
393 var today = new Date();
\r
394 var mo = '00' + (today.getMonth() + 1);
\r
395 var dy = '00' + (today.getDate());
\r
396 var yr = today.getFullYear();
\r
397 var arr = val.split(delim);
\r
398 switch(arr.length){
\r
400 // possibles: 9/2, 9/2004, 09/06,
\r
401 // assume first is always month
\r
402 mo = '00' + arr[0];
\r
403 if (arr[1].length == 4){
\r
404 // assume second is year.
\r
407 // assume second is date.
\r
408 dy = '00' + arr[1];
\r
412 // possibles: 9/2/1, 9/02/04, 09/02/2004, 9/2/2004
\r
413 mo = '00' + arr[0];
\r
414 dy = '00' + arr[1];
\r
415 switch(arr[2].length){
\r
417 yr = '200' + arr[2];
\r
421 yr = '20' + arr[2];
\r
423 yr = '19' + arr[2];
\r
427 // 3 digits... assume 2000 I guess
\r
442 mo = mo.substring(mo.length - 2);
\r
443 dy = dy.substring(dy.length - 2);
\r
444 return mo + '/' + dy + '/' + yr;
\r
448 function iwfToInt(val, stripFormatting){
\r
449 var s = iwfIntFormat(val, stripFormatting);
\r
450 return parseInt(s, 10);
\r
453 function iwfToFloat(val, dp, stripFormatting){
\r
454 var s = iwfFloatFormat(val, dp, stripFormatting);
\r
455 return parseFloat(s);
\r
458 function iwfIntFormat(val, stripFormatting){
\r
459 return iwfFloatFormat(val, -1, stripFormatting);
\r
462 function iwfFloatFormat(val, dp, stripFormatting){
\r
463 if (stripFormatting && iwfIsString(val)){
\r
464 val = val.replace(/[^0-9\.]/gi,'');
\r
471 var pos = s.indexOf('.');
\r
476 s += '0000000000000000000';
\r
477 s = s.substr(0,pos+dp+1);
\r
481 // -----------------------------------
\r
482 // End: Conversion / Formatting Utility Functions
\r
483 // -----------------------------------
\r
485 // -----------------------------------
\r
486 // Begin: Form Submittal Utility Functions
\r
487 // -----------------------------------
\r
488 function iwfDoAction(act, frm, id, targetElement){
\r
489 //alert('action=' + act + '\nfrm=' + frm + '\nid=' + id + '\ntargetElement=' + targetElement);
\r
490 // validate the form first
\r
491 if (window.iwfOnFormValidate){
\r
492 if (!iwfOnFormValidate(act)){
\r
498 // try by id first...
\r
499 frm = iwfGetForm(frmId);
\r
502 iwfLog('IWF Core Error: Could not locate form with id or name of ' + frmId, true);
\r
506 // get or create the iwfId
\r
507 var elId = iwfGetOrCreateById('iwfId', 'input');
\r
510 iwfLog('IWF Core Error: Could not create iwfId element!', true);
\r
513 iwfAttribute(elId, 'value', id);
\r
514 iwfRemoveAttribute(elId, 'disabled');
\r
516 if (!iwfGetParent(elId)){
\r
517 // our element has not been added to the document yet.
\r
518 iwfAttribute(elId, 'type', 'hidden');
\r
519 if (!iwfAppendChild(frm, elId)){
\r
520 iwfLog('IWF Core Error: Created iwfId element, but could not append to form ' + frm.outerHTML, true);
\r
524 //alert(iwfElementToString(elId) + '\n\ndisabled=' + iwfAttribute(elId, 'disabled') + '\n\n' + elId.disabled);
\r
528 // get or create the iwfMode
\r
529 var elMode = iwfGetOrCreateById('iwfMode', 'input');
\r
531 iwfLog('IWF Core Error: Could not create iwfMode element!', true);
\r
534 iwfAttribute(elMode, 'value', act);
\r
535 iwfRemoveAttribute(elMode, 'disabled');
\r
536 if (!iwfGetParent(elMode)){
\r
537 // our element has not been added to the document yet.
\r
538 iwfAttribute(elMode, 'type', 'hidden');
\r
539 if (!iwfAppendChild(frm, elMode)){
\r
540 iwfLog('IWF Core Error: Created iwfMode element, but could not append to form ' + frm.outerHTML, true);
\r
546 // make our request
\r
547 var elRealTarget = iwfGetById(targetElement);
\r
549 // use ajax because they specified a particular element to shove the results into.
\r
551 // get or create the iwfTarget
\r
552 var elTarget = iwfGetOrCreateById('iwfTarget', 'input');
\r
554 iwfLog('IWF Core Error: Could not create iwfTarget element under form ' + frm.outerHTML, true);
\r
557 iwfAttribute(elTarget, 'value', iwfAttribute(elRealTarget, 'id'));
\r
558 iwfRemoveAttribute(elTarget, 'disabled');
\r
559 if (!iwfGetParent(elTarget)){
\r
560 // our element has not been added to the document yet.
\r
561 iwfAttribute(elTarget, 'type', 'hidden');
\r
562 if (!iwfAppendChild(frm, elTarget)){
\r
563 iwfLog('IWF Core Error: Created iwfTarget element, but could not append to form ' + frm.outerHTML, true);
\r
569 if (!window.iwfRequest){
\r
570 iwfLog("IWF Core Error: when using the iwfDo* functions and passing a targetElement, you must also reference the iwfajax.js file from your main html file.", true);
\r
572 //alert('calling iwfRequest(' + frm + ')');
\r
576 // do a normal html submit, since they didn't specify a particular target
\r
577 alert('doing frm.submit()');
\r
582 function iwfDoEdit(formId, id, targetElement){
\r
583 iwfDoAction('edit', formId, id, targetElement);
\r
585 function iwfDoSave(formId, id, targetElement){
\r
586 iwfDoAction('save', formId, id, targetElement);
\r
588 function iwfDoDelete(formId, id, targetElement){
\r
589 iwfDoAction('delete', formId, id, targetElement);
\r
591 function iwfDoAdd(formId, id, targetElement){
\r
592 iwfDoAction('add', formId, id, targetElement);
\r
594 function iwfDoSelect(formId, id, targetElement){
\r
595 iwfDoAction('select', formId, id, targetElement);
\r
597 function iwfDoCancel(formId, id, targetElement){
\r
598 iwfDoAction('cancel', formId, id, targetElement);
\r
601 function iwfMailTo(uid, host){
\r
602 // this is just so an email doesn't have to be output to the browser in raw text for
\r
603 // email harvesters to grab...
\r
604 return 'mailto:' + uid + '@' + host;
\r
607 function iwfClickLink(id){
\r
608 var el = iwfGetById(id);
\r
614 location.href = el.href;
\r
618 function iwfShowMessage(msg){
\r
619 var el = iwfGetById('msg');
\r
621 // window.status = msg;
\r
622 alert(msg + '\n\nTo supress this alert, add a tag with an id of "msg" to this page.');
\r
624 el.innerHTML = msg.replace(/\n/, '<br />');
\r
629 // -----------------------------------
\r
630 // End: Form Submittal Utility Functions
\r
631 // -----------------------------------
\r
633 // -----------------------------------
\r
634 // Begin: Logging Utility Functions
\r
635 // -----------------------------------
\r
637 var _iwfLoggedItems = "";
\r
638 function iwfLog(txt, showAlert){
\r
639 if (iwfLoggingEnabled){
\r
640 _iwfLoggedItems += txt + '\n';
\r
642 // send to big bit bucket in the sky
\r
650 function iwfHideLog(){
\r
651 iwfGetById("iwfLog").style.display="none";
\r
654 function iwfClearLog(){
\r
655 _iwfLoggedItems = '';
\r
659 function iwfRefreshLog(){
\r
664 function iwfShowLog(){
\r
665 if (!iwfLoggingEnabled){
\r
666 alert("Logging for IWF has been disabled.\nSet the iwfLoggingEnabled variable located in the iwfcore.js file to true to enable logging.");
\r
668 var el = iwfGetOrCreateById('iwfLog', 'div', 'body');
\r
670 alert(_iwfLoggedItems);
\r
672 el.style.position = 'absolute';
\r
673 el.style.zIndex = '999999';
\r
674 el.style.left = '10px';
\r
675 el.style.top = '200px';
\r
676 el.style.color = 'blue';
\r
677 el.style.width = '500px';
\r
678 el.style.height = '300px';
\r
679 el.style.overflow = 'scroll';
\r
680 el.style.padding = '5px 5px 5px 5px;'
\r
681 el.style.backgroundColor = '#efefef';
\r
682 el.style.border = '1px dashed blue';
\r
683 el.style.display = 'block';
\r
684 el.style.visibility = 'visible';
\r
686 // el.innerHTML = "IWF Log <span style='width:100px'> </span><a href='javascript:iwfRefreshLog();'>refresh</a> <a href='javascript:iwfHideLog();'>close</a> <a href='javascript:iwfClearLog();'>clear</a>:<hr />" + iwfXmlEncode(_iwfLoggedItems).replace(/\n/gi, '<br />').replace(/\t/gi,' ');
\r
687 el.innerHTML = "IWF Log <span style='width:100px'> </span><a href='javascript:iwfRefreshLog();'>refresh</a> <a href='javascript:iwfHideLog();'>close</a> <a href='javascript:iwfClearLog();'>clear</a>:<hr /><pre>" + _iwfLoggedItems.replace(/</gi, '<').replace(/>/gi, '>') + "</pre>";
\r
694 // -----------------------------------
\r
695 // End: Logging Utility Functions
\r
696 // -----------------------------------
\r