/[webpac2]/trunk/web/iwf/iwfcore.js
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /trunk/web/iwf/iwfcore.js

Parent Directory Parent Directory | Revision Log Revision Log


Revision 55 - (show annotations)
Tue Nov 15 14:29:45 2005 UTC (18 years, 6 months ago) by dpavlin
File MIME type: text/cpp
File size: 19498 byte(s)
 r8877@llin:  dpavlin | 2005-11-14 18:40:33 +0100
 update to upstream version 0.2

1 // =========================================================================
2 /// IWF - Interactive Website Framework. Javascript library for creating
3 /// responsive thin client interfaces.
4 ///
5 /// Copyright (C) 2005 Brock Weaver brockweaver@users.sourceforge.net
6 ///
7 /// This library is free software; you can redistribute it and/or modify
8 /// it under the terms of the GNU Lesser General Public License as published
9 /// by the Free Software Foundation; either version 2.1 of the License, or
10 /// (at your option) any later version.
11 ///
12 /// This library is distributed in the hope that it will be useful, but
13 /// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 /// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 /// License for more details.
16 ///
17 /// You should have received a copy of the GNU Lesser General Public License
18 /// along with this library; if not, write to the Free Software Foundation,
19 /// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 ///
21 /// Brock Weaver
22 /// brockweaver@users.sourceforge.net
23 /// 1605 NW Maple Pl
24 /// Ankeny, IA 50021
25 ///
26 //! http://iwf.sourceforge.net/
27 // =========================================================================
28 //! NOTE: To minimize file size, strip all fluffy comments (except the LGPL, of course!)
29 //! using the following regex (global flag and multiline on):
30 //! ^\t*//([^/!].*|$)
31 //
32 // This reduces file size by about 30%, give or take.
33 //
34 //! To rip out only logging statements (commented or uncommented):
35 //! ^/{0,2}iwfLog.*
36 // =========================================================================
37
38 // --------------------------------------------------------------------------
39 //! iwfcore.js
40 //
41 // Core functions
42 //
43 //! Dependencies:
44 //! (none)
45 //
46 //! Brock Weaver - brockweaver@users.sourceforge.net
47 //! v 0.2 - 2005-11-14
48 //! iwfcore bug patch
49 //! --------------------------------------------------------------------------
50 //! - fixed iwfAttribute to return most current value instead of initial value
51 //!
52 //! v 0.1 - 2005-06-05
53 //! Initial release.
54 //! --------------------------------------------------------------------------
55
56 // -----------------------------------
57 // Begin: Configurable variables
58 // -----------------------------------
59
60 // 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.
61 var _iwfLoggingEnabled = true;
62
63
64 // -----------------------------------
65 // End: Configurable variables
66 // -----------------------------------
67
68
69 // -----------------------------------
70 // Begin: Element Utility Functions
71 // -----------------------------------
72
73 function iwfGetById(id){
74 var el = null;
75 if (iwfIsString(id) || iwfIsNumber(id)) el = document.getElementById(id);
76 else if (typeof(id) == 'object') el = id;
77 return el;
78 }
79
80 function iwfGetForm(id){
81 var frm = iwfGetById(id);
82 if (!frm){
83 // or by forms collection...
84 frm = document.forms ? document.forms[frm] : null;
85 if (!frm){
86 // or by tag elements...
87 var allforms = iwfGetByTagName('form');
88 for(var i=0;i<allforms.length;i++){
89 if (iwfAttribute(allforms[i], 'name') == id){
90 frm = allforms[i];
91 break;
92 }
93 }
94 }
95 }
96 return frm;
97 }
98
99 function iwfGetByNameWithinForm(form, nm){
100 var frm = iwfGetForm(form);
101 if (!frm){
102 iwfLog("IWF Core Error: Could not locate form by id, document.forms, or document[] named '" + form + "'", true);
103 return null;
104 } else {
105 // find element within this form with given id.
106 var el = null;
107 if (iwfIsString(nm) || iwfIsNumber(nm)) {
108 for(var i=0;i<frm.elements.length;i++){
109 if (frm.elements[i].name == nm || frm.elements[i].id == nm){
110 el = frm.elements[i];
111 break;
112 }
113 }
114 } else {
115 el = id;
116 }
117 //iwfLog('iwfGetByNameWithinForm returning:\n\n' + iwfElementToString(el), true);
118 return el;
119 }
120 }
121
122 function iwfGetOrCreateByNameWithinForm(form, nm, tagNameOrHtml, typeAtt){
123 var elFrm = iwfGetForm(form);
124
125 if (!elFrm){
126 iwfLog("IWF Core Error: <form> with name or id of '" + form + "' could not be located.", true);
127 return;
128 }
129
130 var el = iwfGetByNameWithinForm(form, nm);
131 if (!el){
132 // element does not exist. create it.
133 el = document.createElement(tagNameOrHtml);
134 iwfAttribute(el, 'name', nm);
135
136 if (typeAtt){
137 iwfAttribute(el, 'type', typeAtt);
138 }
139
140 elFrm.appendChild(el);
141 }
142
143 return el;
144 }
145
146 function iwfRemoveChild(parentId, id){
147 var elParent = iwfGetById(parentId);
148 if (elParent){
149 var elChild = iwfGetById(id);
150 if (elChild){
151 elParent.removeChild(elChild);
152 }
153 }
154 return elParent;
155 }
156
157 function iwfGetOrCreateById(id, tagNameOrHtml, parentNodeId){
158 var el = iwfGetById(id);
159 if (!el){
160 // element does not exist. create it.
161 el = document.createElement(tagNameOrHtml);
162 iwfAttribute(el, 'id', id);
163 iwfAttribute(el, 'name', id);
164
165 if (parentNodeId){
166 var elParent = iwfGetById(parentNodeId);
167 if (elParent){
168 iwfAddChild(elParent, el);
169 } else if (parentNodeId.toLowerCase() == 'body'){
170 iwfAddChild(document.body, el);
171 }
172 }
173
174
175 }
176 return el;
177 }
178
179 function iwfAddChild(parentNodeId, childNodeId, atFront){
180
181 var elChild = iwfGetById(childNodeId);
182 if (!elChild) return null;
183
184 // get the element who is to be the parent
185 var elParent = iwfGetById(parentNodeId);
186
187 // parent not found, try by tagName
188 if (!elParent) {
189 var nodes = iwfGetByTagName(parentNodeId);
190 if (nodes && nodes.length > 0){
191 // always use the first element with that tag name as the parent (think 'body')
192 parent = nodes[0];
193 } else {
194 // couldn't find by id or tag name. bomb out.
195 return null;
196 }
197 }
198
199
200
201 if (!atFront || !elParent.hasChildNodes()){
202 // append as last child of elParent
203 elParent.appendChild(elChild);
204 } else {
205 // append as first child of elParent
206 elParent.insertBefore(elChild, elParent.childNodes[0]);
207 }
208
209 return elParent;
210 }
211
212 function iwfRemoveNode(id){
213 var el = iwfGetById(id);
214 if (!el) return false;
215 document.removeNode(el);
216 return true;
217 }
218
219 function iwfElementToString(id){
220 var el = iwfGetById(id);
221 if (!el) return 'element ' + id + ' does not exist.';
222
223 var s = '<' + el.tagName + ' ';
224 if (el.attributes){
225 for(var i=0;i<el.attributes.length;i++){
226 var att = el.attributes[i];
227 s += ' ' + att.nodeName + '="' + att.nodeValue + '" ';
228 }
229 }
230 if (el.innerHTML == ''){
231 s += ' />';
232 } else {
233 s += '>' + el.innerHTML + '</' + el.tagName + '>';
234 }
235
236 return s;
237 }
238
239 function iwfGetByTagName(tagName, root) {
240 var nodes = new Array();
241 tagName = tagName || '*';
242 root = root || document;
243 if (root.all){
244 if (tagName == '*'){
245 nodes = root.all;
246 } else {
247 nodes = root.all.tags(tagName);
248 }
249 } else if (root.getElementsByTagName) {
250 nodes = root.getElementsByTagName(tagName);
251 }
252 return nodes;
253 }
254
255 function iwfGetByAttribute(tagName, attName, regex, callback) {
256 var a, list, found = new Array();
257 var reg = new RegExp(regex, 'i');
258 list = iwfGetByTagName(tagName);
259 for(var i=0;i<list.length;++i) {
260 a = list[i].getAttribute(attName);
261 if (!a) {a = list[i][attName];}
262 if (typeof(a)=='string' && a.search(regex) != -1) {
263 found[found.length] = list[i];
264 if (callback) callback(list[i]);
265 }
266 }
267 return found;
268 }
269
270 function iwfAttribute(id, attName, newval){
271 var el = iwfGetById(id);
272 if (!el) return;
273
274 var val = null;
275 if (iwfExists(newval)){
276 if (newval == null){
277 // remove it, don't set it to null.
278 iwfRemoveAttribute(el, attName);
279 } else {
280 el.setAttribute(attName, newval);
281 }
282 }
283 // 2005-11-14 Brock Weaver
284 // added check for attribute on el before trying getAttribute method
285 // (Thanks J.P. Jarolim!)
286 if (el[attName]){
287 val = el[attName];
288 } else if (el.getAttribute) {
289 val = el.getAttribute(attName);
290 }
291 return val;
292 }
293
294 function iwfRemoveAttribute(id, attName){
295 var el = iwfGetById(id);
296 if (el){
297 el.removeAttribute(attName);
298 }
299 return el;
300 }
301
302 function iwfGetParent(id, useOffsetParent){
303 var el = iwfGetById(id);
304 if (!el) return null;
305 var cur = null;
306 if (useOffsetParent && iwfExists(el.offsetParent)) { cur = el.offsetParent;
307 } else if (iwfExists(el.parentNode)) { cur = el.parentNode;
308 } else if (iwfExists(el.parentElement)) { cur = el.parentElement; }
309 return cur;
310 }
311
312 // -----------------------------------
313 // End: Element Utility Functions
314 // -----------------------------------
315
316
317 // -----------------------------------
318 // Begin: Encoding Utility Functions
319 // -----------------------------------
320
321 function iwfXmlEncode(s){
322 if (!s){
323 return '';
324 }
325 var ret = s.replace(/&/gi, '&amp;').replace(/>/gi,'&gt;').replace(/</gi, '&lt;').replace(/'/gi, '&apos;').replace(/"/gi, '&quot;');
326 //alert('after xmlencoding: \n\n\n' + ret);
327 return ret;
328 }
329
330 function iwfXmlDecode(s){
331 if (!s){
332 return '';
333 }
334 var ret = s.replace(/&gt;/gi, '>').replace(/&lt;/gi,'<').replace(/&apos;/gi, '\'').replace(/&quot;/gi, '"').replace(/&amp;/gi, '&');
335 //alert('after xmldecoding: \n\n\n' + ret);
336 return ret;
337 }
338
339 function iwfHtmlEncode(s){
340 if (!s){
341 return '';
342 }
343 var ret = s.replace(/&/gi, '&amp;').replace(/>/gi,'&gt;').replace(/</gi, '&lt;').replace(/'/gi, '&apos;').replace(/"/gi, '&quot;');
344 //alert('after xmlencoding: \n\n\n' + ret);
345 return ret;
346 }
347
348 function iwfHtmlDecode(s){
349 if (!s){
350 return '';
351 }
352 var ret = s.replace(/&gt;/gi, '>').replace(/&lt;/gi,'<').replace(/&apos;/gi, '\'').replace(/&quot;/gi, '"').replace(/&amp;/gi, '&');
353 //alert('after xmldecoding: \n\n\n' + ret);
354 return ret;
355 }
356 // -----------------------------------
357 // End: Encoding Utility Functions
358 // -----------------------------------
359
360
361 // -----------------------------------
362 // Begin: Conversion / Formatting Utility Functions
363 // -----------------------------------
364
365 function iwfExists(){
366 for(var i=0;i<arguments.length;i++){
367 if(typeof(arguments[i])=='undefined') return false;
368 }
369 return true;
370 }
371
372 function iwfIsString(s){
373 return typeof(s) == 'string';
374 }
375
376 function iwfIsNumber(n){
377 return typeof(n) == 'number';
378 }
379
380 function iwfIsBoolean(b){
381 return typeof(b) == 'boolean';
382 }
383
384 function iwfIsDate(val){
385 var dt = iwfDateFormat(val);
386 if(!dt){
387 return false;
388 } else {
389 // determine if the month/day makes sense.
390 var mo = iwfToInt(dt.substring(0,2));
391 var dy = iwfToInt(dt.substring(3,2));
392 var yr = iwfToInt(dt.substring(6,4));
393 var maxdy = 28;
394 switch(mo){
395 case 4:
396 case 6:
397 case 9:
398 case 11:
399 maxdy = 30;
400 break;
401 case 2:
402 // check leap year
403 if (yr % 4 == 0 && (yr % 100 != 0 || yr % 400 == 0)){
404 maxdy = 29;
405 }
406 break;
407 default:
408 // 1, 3, 5, 7, 8, 10, 12
409 maxdy = 31;
410 break;
411 }
412 return dy > 0 && dy <= maxdy;
413 }
414 }
415
416 function iwfDateFormat(val){
417 var delim = '/';
418 if (val.indexOf(delim) == -1){
419 delim = '-';
420 }
421
422 var today = new Date();
423 var mo = '00' + (today.getMonth() + 1);
424 var dy = '00' + (today.getDate());
425 var yr = today.getFullYear();
426 var arr = val.split(delim);
427 switch(arr.length){
428 case 2:
429 //! possibles: 9/2, 9/2004, 09/06,
430 //! assume first is always month
431 mo = '00' + arr[0];
432 if (arr[1].length == 4){
433 //! assume second is year.
434 yr = arr[1];
435 } else {
436 //! assume second is date.
437 dy = '00' + arr[1];
438 }
439 break;
440 case 3:
441 //! possibles: 9/2/1, 9/02/04, 09/02/2004, 9/2/2004
442 mo = '00' + arr[0];
443 dy = '00' + arr[1];
444 switch(arr[2].length){
445 case 1:
446 yr = '200' + arr[2];
447 break;
448 case 2:
449 if (arr[2] < 50){
450 yr = '20' + arr[2];
451 } else {
452 yr = '19' + arr[2];
453 }
454 break;
455 case 3:
456 //! 3 digits... assume 2000 I guess
457 yr = '2' + arr[2];
458 break;
459 case 4:
460 yr = arr[2];
461 break;
462 default:
463 break;
464 }
465 break;
466 default:
467 // invalid date.
468 return null;
469 break;
470 }
471 mo = mo.substring(mo.length - 2);
472 dy = dy.substring(dy.length - 2);
473 return mo + '/' + dy + '/' + yr;
474
475 }
476
477 function iwfToInt(val, stripFormatting){
478 var s = iwfIntFormat(val, stripFormatting);
479 return parseInt(s, 10);
480 }
481
482 function iwfToFloat(val, dp, stripFormatting){
483 var s = iwfFloatFormat(val, dp, stripFormatting);
484 return parseFloat(s);
485 }
486
487 function iwfIntFormat(val, stripFormatting){
488 return iwfFloatFormat(val, -1, stripFormatting);
489 }
490
491 function iwfFloatFormat(val, dp, stripFormatting){
492 if (stripFormatting && iwfIsString(val)){
493 val = val.replace(/[^0-9\.]/gi,'');
494 }
495 if (isNaN(val)) {
496 val = 0.0;
497 }
498
499 var s = '' + val;
500 var pos = s.indexOf('.');
501 if (pos == -1) {
502 s += '.';
503 pos += s.length;
504 }
505 s += '0000000000000000000';
506 s = s.substr(0,pos+dp+1);
507 return s;
508 }
509
510 // -----------------------------------
511 // End: Conversion / Formatting Utility Functions
512 // -----------------------------------
513
514 // -----------------------------------
515 // Begin: Form Submittal Utility Functions
516 // -----------------------------------
517 function iwfDoAction(act, frm, id, targetElement){
518 // validate the form first
519 if (window.iwfOnFormValidate){
520 if (!iwfOnFormValidate(act)){
521 return;
522 }
523 }
524
525 var frmId = frm;
526 // try by id first...
527 frm = iwfGetForm(frmId);
528
529 if (!frm){
530 iwfLog('IWF Core Error: Could not locate form with id or name of ' + frmId, true);
531 return;
532 }
533
534 // get or create the iwfId
535 var elId = iwfGetOrCreateById('iwfId', 'input');
536
537 if (!elId){
538 iwfLog('IWF Core Error: Could not create iwfId element!', true);
539 return;
540 } else {
541 iwfAttribute(elId, 'value', id);
542 iwfRemoveAttribute(elId, 'disabled');
543
544 if (!iwfGetParent(elId)){
545 // our element has not been added to the document yet.
546 iwfAttribute(elId, 'type', 'hidden');
547 if (!iwfAddChild(frm, elId)){
548 iwfLog('IWF Core Error: Created iwfId element, but could not append to form ' + frm.outerHTML, true);
549 return;
550 }
551 }
552 }
553
554
555 // get or create the iwfMode
556 var elMode = iwfGetOrCreateById('iwfMode', 'input');
557 if (!elMode){
558 iwfLog('IWF Core Error: Could not create iwfMode element!', true);
559 return;
560 } else {
561 iwfAttribute(elMode, 'value', act);
562 iwfRemoveAttribute(elMode, 'disabled');
563 if (!iwfGetParent(elMode)){
564 // our element has not been added to the document yet.
565 iwfAttribute(elMode, 'type', 'hidden');
566 if (!iwfAddChild(frm, elMode)){
567 iwfLog('IWF Core Error: Created iwfMode element, but could not append to form ' + frm.outerHTML, true);
568 return;
569 }
570 }
571 }
572
573 // make our request
574 var elRealTarget = iwfGetById(targetElement);
575 if (elRealTarget){
576 // use ajax because they specified a particular element to shove the results into.
577
578 // get or create the iwfTarget
579 var elTarget = iwfGetOrCreateById('iwfTarget', 'input');
580 if (!elTarget){
581 iwfLog('IWF Core Error: Could not create iwfTarget element under form ' + frm.outerHTML, true);
582 return;
583 } else {
584 iwfAttribute(elTarget, 'value', iwfAttribute(elRealTarget, 'id'));
585 iwfRemoveAttribute(elTarget, 'disabled');
586 if (!iwfGetParent(elTarget)){
587 // our element has not been added to the document yet.
588 iwfAttribute(elTarget, 'type', 'hidden');
589 if (!iwfAddChild(frm, elTarget)){
590 iwfLog('IWF Core Error: Created iwfTarget element, but could not append to form ' + frm.outerHTML, true);
591 return;
592 }
593 }
594 }
595
596 if (!window.iwfRequest){
597 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);
598 } else {
599 //alert('calling iwfRequest(' + frm + ')');
600 iwfRequest(frm);
601 }
602 } else {
603 // do a normal html submit, since they didn't specify a particular target
604 iwfLog('doing frm.submit()', true);
605 frm.submit();
606 }
607 }
608
609 function iwfDoEdit(formId, id, targetElement){
610 iwfDoAction('edit', formId, id, targetElement);
611 }
612 function iwfDoSave(formId, id, targetElement){
613 iwfDoAction('save', formId, id, targetElement);
614 }
615 function iwfDoDelete(formId, id, targetElement){
616 iwfDoAction('delete', formId, id, targetElement);
617 }
618 function iwfDoAdd(formId, id, targetElement){
619 iwfDoAction('add', formId, id, targetElement);
620 }
621 function iwfDoSelect(formId, id, targetElement){
622 iwfDoAction('select', formId, id, targetElement);
623 }
624 function iwfDoCancel(formId, id, targetElement){
625 iwfDoAction('cancel', formId, id, targetElement);
626 }
627
628 function iwfMailTo(uid, host){
629 // this is just so an email doesn't have to be output to the browser in raw text for
630 // email harvesters to grab...
631 location.href = 'mailto:' + uid + '@' + host;
632 }
633
634 function iwfClickLink(id){
635 var el = iwfGetById(id);
636 if (!el) return;
637
638 if (el.click){
639 el.click();
640 } else {
641 location.href = el.href;
642 }
643 }
644
645 function iwfShowMessage(msg){
646 var el = iwfGetById('msg');
647 if (!el){
648 // window.status = msg;
649 alert(msg + '\n\nTo supress this alert, add a tag with an id of "msg" to this page.');
650 } else {
651 el.innerHTML = msg.replace(/\n/, '<br />');
652 }
653 }
654
655
656 // -----------------------------------
657 // End: Form Submittal Utility Functions
658 // -----------------------------------
659
660 // -----------------------------------
661 // Begin: Logging Utility Functions
662 // -----------------------------------
663
664 var _iwfLoggedItems = "";
665 function iwfLog(txt, showAlert){
666 if (_iwfLoggingEnabled){
667 _iwfLoggedItems += txt + '\n';
668 } else {
669 //! send to big bit bucket in the sky (/dev/null)
670 }
671 if (showAlert){
672 alert(txt);
673 }
674 }
675
676 function iwfHideLog(){
677 iwfGetById("iwfLog").style.display="none";
678 }
679
680 function iwfClearLog(){
681 _iwfLoggedItems = '';
682 iwfRefreshLog();
683 }
684
685 function iwfRefreshLog(){
686 iwfHideLog();
687 iwfShowLog();
688 }
689
690 function iwfShowLog(){
691 if (!_iwfLoggingEnabled){
692 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.");
693 } else {
694 var el = iwfGetOrCreateById('iwfLog', 'div', 'body');
695 if (!el){
696 alert(_iwfLoggedItems);
697 } else {
698 el.style.position = 'absolute';
699 el.style.zIndex = '999999';
700 el.style.left = '10px';
701 el.style.top = '200px';
702 el.style.color = 'blue';
703 el.style.width = '500px';
704 el.style.height = '300px';
705 el.style.overflow = 'scroll';
706 el.style.padding = '5px 5px 5px 5px;'
707 el.style.backgroundColor = '#efefef';
708 el.style.border = '1px dashed blue';
709 el.style.display = 'block';
710 el.style.visibility = 'visible';
711 el.id = 'iwfLog';
712 // el.innerHTML = "IWF Log <span style='width:100px'>&nbsp;</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,'&nbsp;&nbsp;&nbsp;&nbsp;');
713 el.innerHTML = "IWF Log <span style='width:100px'>&nbsp;</span><a href='javascript:iwfRefreshLog();'>refresh</a> <a href='javascript:iwfHideLog();'>close</a> <a href='javascript:iwfClearLog();'>clear</a>:<hr /><pre>" + _iwfLoggedItems.replace(/</gi, '&lt;').replace(/>/gi, '&gt;') + "</pre>";
714
715 }
716 }
717 }
718
719
720 // -----------------------------------
721 // End: Logging Utility Functions
722 // -----------------------------------

Properties

Name Value
svn:mime-type text/cpp

  ViewVC Help
Powered by ViewVC 1.1.26