/[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 46 - (show annotations)
Mon Nov 14 16:13:17 2005 UTC (18 years, 5 months ago) by dpavlin
File MIME type: text/cpp
File size: 18565 byte(s)
 r8855@llin:  dpavlin | 2005-11-14 01:49:57 +0100
 added small browser using Interactive Website Framework
 from http://iwf.sourceforge.net

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

Properties

Name Value
svn:mime-type text/cpp

  ViewVC Help
Powered by ViewVC 1.1.26