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 |
// -------------------------------------------------------------------------- |
40 |
// |
41 |
//! iwfajax.js |
42 |
// |
43 |
// Thread-safe background xml request via XmlHttpRequest object |
44 |
// |
45 |
//! Dependencies: |
46 |
//! iwfxml.js |
47 |
//! iwfcore.js |
48 |
//! iwfgui.js (optional -- pretty progress bar) |
49 |
// |
50 |
//! Brock Weaver - brockweaver@users.sourceforge.net |
51 |
//! v 0.2 - 2005-11-14 |
52 |
//! core bug patch |
53 |
// -------------------------------------------------------------------------- |
54 |
//! v 0.1 - 2005-06-05 |
55 |
//! Initial release. |
56 |
// |
57 |
//! Known Issues: |
58 |
//! AddToHistory does not work |
59 |
//! Iframe not implemented |
60 |
// |
61 |
// ========================================================================= |
62 |
|
63 |
|
64 |
|
65 |
|
66 |
// show progress bar if they included iwfgui.js, window.status otherwise. |
67 |
var _iwfShowGuiProgress = iwfExists(window.iwfShow); |
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
// ----------------------------------- |
75 |
// Begin: Dependency Check |
76 |
// ----------------------------------- |
77 |
|
78 |
if (!window.iwfGetById || !window.iwfXmlDoc){ |
79 |
iwfLog("IWF Dependency Error: iwfajax.js is dependent upon both iwfcore.js and iwfxml.js, so you *must* reference those files first.\n\nExample:\n\n<script type='text/javascript' src='iwfcore.js'></script>\n<script type='text/javascript' src='iwfxml.js'></script>\n<script type='text/javascript' src='iwfajax.js'></script>", true); |
80 |
} |
81 |
|
82 |
// ----------------------------------- |
83 |
// End: Dependency Check |
84 |
// ----------------------------------- |
85 |
|
86 |
// ----------------------------------- |
87 |
// Begin: AJAX Request and Response |
88 |
// ----------------------------------- |
89 |
|
90 |
function iwfRequest(urlOrForm, targetElementOnResponse, addToHistory, callback){ |
91 |
|
92 |
// we use a javascript feature here called "inner functions" |
93 |
// using these means the local variables retain their values after the outer function |
94 |
// has returned. this is useful for thread safety, so |
95 |
// reassigning the onreadystatechange function doesn't stomp over earlier requests. |
96 |
|
97 |
function iwfBindCallback(){ |
98 |
if (req.readyState == 4) { |
99 |
_iwfOnRequestEnd(); |
100 |
if (req.status == 200 || req.status == 0) { |
101 |
//iwfLog('exact response from server:\n\n' + req.responseText); |
102 |
_iwfResponseHandler(req.responseText, localCallback, localTarget); |
103 |
} else { |
104 |
_iwfOnRequestError(req.status, req.statusText, req.responseText); |
105 |
} |
106 |
return; |
107 |
} |
108 |
} |
109 |
|
110 |
// determine how to hit the server... |
111 |
var url = null; |
112 |
var method = 'GET'; |
113 |
var postdata = null; |
114 |
var isFromForm = true; |
115 |
var contentType = 'application/x-www-form-urlencoded'; |
116 |
|
117 |
if (iwfIsString(urlOrForm)){ |
118 |
|
119 |
// if we get here, they either specified the url or the name of the form. |
120 |
// either way, flag so we return nothing. |
121 |
isFromForm = false; |
122 |
|
123 |
var frm = iwfGetForm(urlOrForm); |
124 |
if (!frm){ |
125 |
// is a url. |
126 |
url = urlOrForm; |
127 |
method = 'GET'; |
128 |
postdata = null; |
129 |
} else { |
130 |
// is name of a form. |
131 |
// fill with the form object. |
132 |
urlOrForm = frm; |
133 |
} |
134 |
|
135 |
} |
136 |
|
137 |
// use a local variable to hold our callback and target until the inner function is called... |
138 |
var localCallback = callback; |
139 |
var localTarget = targetElementOnResponse; |
140 |
|
141 |
if (!iwfIsString(urlOrForm)){ |
142 |
|
143 |
var ctl = null; |
144 |
|
145 |
|
146 |
// is a form or a control in the form. |
147 |
if (iwfExists(urlOrForm.form)){ |
148 |
// is a control in the form. jump up to the form. |
149 |
ctl = urlOrForm; |
150 |
urlOrForm = urlOrForm.form; |
151 |
} |
152 |
|
153 |
|
154 |
// if they passed a form and no local target, lookup the form.iwfTarget attribute and use it if possible |
155 |
if (!localTarget){ |
156 |
localTarget = iwfAttribute(urlOrForm, 'iwfTarget'); |
157 |
} |
158 |
|
159 |
if (localTarget){ |
160 |
var elTgt = iwfGetOrCreateByNameWithinForm(urlOrForm, 'iwfTarget', 'input', 'hidden'); |
161 |
if (elTgt){ |
162 |
iwfAttribute(elTgt, 'value', localTarget); |
163 |
iwfRemoveAttribute(elTgt, 'disabled'); |
164 |
} |
165 |
} |
166 |
|
167 |
|
168 |
url = urlOrForm.action; |
169 |
method = urlOrForm.method.toUpperCase(); |
170 |
switch(method){ |
171 |
case "POST": |
172 |
postdata = _iwfGetFormData(urlOrForm, url, ctl); |
173 |
|
174 |
// we also need to properly set the content-type header... |
175 |
var frm = iwfGetForm(urlOrForm); |
176 |
if (frm){ |
177 |
var enc = iwfAttribute(frm, 'encoding'); |
178 |
if (!enc){ |
179 |
enc = iwfAttribute(frm, 'enctype'); |
180 |
} |
181 |
if (enc){ |
182 |
contentType = enc; |
183 |
} |
184 |
} |
185 |
|
186 |
break; |
187 |
case "GET": |
188 |
default: |
189 |
url = _iwfGetFormData(urlOrForm, url, ctl); |
190 |
break; |
191 |
} |
192 |
} |
193 |
|
194 |
// prevent any browser caching of our url by requesting a unique url everytime... |
195 |
url += ((url.indexOf('?') > -1) ? '&' : '?') + 'iwfRequestId=' + new Date().valueOf(); |
196 |
|
197 |
//iwfLog("url = " + url); |
198 |
//iwfLog("method = " + method); |
199 |
//iwfLog("postdata = " + postdata); |
200 |
//iwfLog("contenttype = " + contentType); |
201 |
|
202 |
|
203 |
var req = null; |
204 |
if (!addToHistory){ |
205 |
//iwfLog("using XHR to perform request..."); |
206 |
// use XHR to perform the request, as this will |
207 |
// prevent the browser from adding it to history. |
208 |
req = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP"); |
209 |
|
210 |
// bind our callback |
211 |
req.onreadystatechange = iwfBindCallback; |
212 |
|
213 |
// show progress if it's not already visible... |
214 |
_iwfOnRequestStart(); |
215 |
|
216 |
// hit the server |
217 |
req.open(method, url, true); |
218 |
req.setRequestHeader('Content-Type', contentType); |
219 |
req.send(postdata); |
220 |
} else { |
221 |
// use an IFRAME element to perform the request, |
222 |
// as this will cause the browser to add it to history. |
223 |
// TODO: make this work!!! |
224 |
iwfLog("using IFRAME to perform request..."); |
225 |
iwfLog('request and add to history not implemented yet!!!', true); |
226 |
return; |
227 |
|
228 |
var el = iwfGetById("iwfHistoryFrame"); |
229 |
if (!el){ |
230 |
iwfLog("To enable history tracking in IWF, you must add an invisible IFRAME element to your page:\n<IFRAME id='iwfHistoryFrame' style='display:none'></IFRAME>", true); |
231 |
} |
232 |
|
233 |
el.src = url; |
234 |
|
235 |
// show progress if it's not already visible... |
236 |
_iwfOnRequestStart(); |
237 |
|
238 |
|
239 |
} |
240 |
|
241 |
// if this is called from the form.onsubmit event, make sure the form doesn't submit... |
242 |
|
243 |
if (isFromForm){ |
244 |
return false; |
245 |
} else { |
246 |
// return absolutely nothing so anchor tags don't hork the current page |
247 |
} |
248 |
|
249 |
} |
250 |
|
251 |
function _iwfGetFormData(form, url, ctl){ |
252 |
|
253 |
var method = form.method; |
254 |
if (!method) method = "get"; |
255 |
|
256 |
var output = null; |
257 |
|
258 |
if (method == 'get'){ |
259 |
output = url + ((url.indexOf('?') > -1) ? '&' : '?'); |
260 |
} else { |
261 |
output = ''; |
262 |
} |
263 |
|
264 |
|
265 |
// if there is a target specified in the <form> tag, |
266 |
// copy its contents to a hidden <input type='hidden'> tag. |
267 |
|
268 |
//iwfLog("total elements in form named '" + form.name + "': " + form.elements.length); |
269 |
for(var i=0;i<form.elements.length;i++){ |
270 |
var el = form.elements[i]; |
271 |
var nm = iwfAttribute(el, 'name'); |
272 |
var val = null; |
273 |
if (!iwfAttribute(el, 'disabled') && nm){ |
274 |
switch (el.tagName.toLowerCase()){ |
275 |
case 'input': |
276 |
switch(iwfAttribute(el, 'type')){ |
277 |
case 'checkbox': |
278 |
case 'radio': |
279 |
if (iwfAttribute(el, 'checked')){ |
280 |
val = iwfAttribute(el, 'value'); |
281 |
} |
282 |
break; |
283 |
case 'button': |
284 |
if (el == ctl){ |
285 |
val = iwfAttribute(el, 'value'); |
286 |
} |
287 |
break; |
288 |
case 'submit': |
289 |
if (el == ctl){ |
290 |
val = iwfAttribute(el, 'value'); |
291 |
} |
292 |
break; |
293 |
case 'text': |
294 |
default: |
295 |
val = iwfAttribute(el, 'value'); |
296 |
break; |
297 |
case 'file': |
298 |
iwfLog('TODO: implement <input type="file"> in _iwfGetFormData', true); |
299 |
val = iwfAttribute(el, 'value'); |
300 |
break; |
301 |
case 'image': |
302 |
iwfLog('TODO: implement <input type="image"> in _iwfGetFormData', true); |
303 |
val = iwfAttribute(el, 'value'); |
304 |
break; |
305 |
case 'reset': |
306 |
iwfLog('TODO: implement <input type="reset"> in _iwfGetFormData', true); |
307 |
break; |
308 |
case 'hidden': |
309 |
val = iwfAttribute(el, 'value'); |
310 |
break; |
311 |
} |
312 |
break; |
313 |
case 'textarea': |
314 |
val = iwfAttribute(el, 'innerText'); |
315 |
break; |
316 |
case 'button': |
317 |
if (el == ctl){ |
318 |
val = iwfAttribute(el, 'innerText'); |
319 |
} |
320 |
break; |
321 |
case 'select': |
322 |
for(var j=0;j<el.options.length;j++){ |
323 |
if (iwfAttribute(el.options[j], 'selected') == 'true'){ |
324 |
if (!val){ |
325 |
val = iwfAttribute(el.options[j], 'value'); |
326 |
} else { |
327 |
val += '+' + iwfAttribute(el.options[j], 'value'); |
328 |
} |
329 |
} |
330 |
} |
331 |
} |
332 |
|
333 |
if (val){ |
334 |
if (output.length > 0){ |
335 |
output += '&'; |
336 |
} |
337 |
output += escape(nm) + '=' + escape(val); |
338 |
} |
339 |
} |
340 |
} |
341 |
if (output.length == 0){ |
342 |
return null; |
343 |
} else { |
344 |
return output; |
345 |
} |
346 |
} |
347 |
|
348 |
function _iwfResponseReceived(doc){ |
349 |
iwfLog('iframeloaded'); |
350 |
var xmlDoc = new iwfXmlDoc(doc.innerHTML); |
351 |
} |
352 |
|
353 |
function _iwfResponseHandler(origText, callback, tgt){ |
354 |
var doc = new iwfXmlDoc(origText); |
355 |
if (!doc.response){ |
356 |
// not in our default xml format. |
357 |
// just throw out the xml to the callback, if any |
358 |
if (callback){ |
359 |
callback(doc); |
360 |
} else { |
361 |
iwfLog("IWF Ajax Error: No callback defined for non-standard response:\n" + origText, true); |
362 |
} |
363 |
} else { |
364 |
if (doc.response.debugging == 'true'){ |
365 |
iwfLoggingEnabled = true; |
366 |
iwfLog("IWF Ajax Debugging:\nParsed response:\n\n" + doc.response.outerXml(true), true); |
367 |
iwfShowLog(); |
368 |
} |
369 |
for(var i=0; i< doc.response.childNodes.length; i++){ |
370 |
var node = doc.response.childNodes[i]; |
371 |
if (node.nodeName.indexOf("#") != 0){ |
372 |
//iwfLog('node.target=' + node.target + '\ntgt=' + tgt); |
373 |
if (!tgt) { |
374 |
// server target is ignored if a client target exists. |
375 |
tgt = node.target; |
376 |
} |
377 |
if (node.errorCode && iwfToInt(node.errorCode, true) != 0){ |
378 |
// an error occurred. |
379 |
_iwfOnRequestError(node.errorCode, node.errorMessage); |
380 |
} else { |
381 |
if (!node.type){ |
382 |
node.type = ""; |
383 |
} |
384 |
switch(node.type.toLowerCase()){ |
385 |
case "html": |
386 |
case "xhtml": |
387 |
var innerHtml = node.innerHtml(); |
388 |
//iwfLog('parsed html response:\n\n' + innerHtml); |
389 |
_iwfInsertHtml(innerHtml, tgt); |
390 |
break; |
391 |
case "javascript": |
392 |
case "js": |
393 |
var bomb = true; |
394 |
if (node.childNodes.length == 1){ |
395 |
var js = node.childNodes[0]; |
396 |
if (js.nodeName == '#cdata' || js.nodeName == '#comment'){ |
397 |
bomb = false; |
398 |
var code = js.getText(); |
399 |
eval(code); |
400 |
} |
401 |
} |
402 |
if (bomb){ |
403 |
iwfLog("IWF Ajax Error: When an <action> is defined of type javascript, it content must be contained by either a Comment (<!-- -->) or CDATA (<![CDATA[ ]]>).\nCDATA is the more appropriate manner, as this is the xml-compliant one.\nHowever, COMMENT is also allowed as this is html-compliant, such as within a <script> element.\n\n<action> xml returned:\n\n" + node.outerXml(), true); |
404 |
} |
405 |
break; |
406 |
case "xml": |
407 |
if (callback){ |
408 |
callback(node); |
409 |
} |
410 |
break; |
411 |
case "debug": |
412 |
iwfLog("IWF Debug: <action> type identified as 'debug'.\nXml received for current action:\n\n" + node.outerXml(), true); |
413 |
break; |
414 |
default: |
415 |
iwfLog('IWF Ajax Error: <action> type of "' + node.type + '" is not a valid option.\n\nValid options:\n\'html\' or \'xhtml\' = parse as html and inject into element with the id specified by the target attribute\n\'javascript\' or \'js\' = parse as javascript and execute\n\'xml\' = parse as xml and call the callback specified when iwfRequest() was called\n\'debug\' = parse as xml and log/alert the result\n\n<action> xml returned:\n\n' + node.outerXml(), true); |
416 |
break; |
417 |
} |
418 |
} |
419 |
} |
420 |
} |
421 |
} |
422 |
|
423 |
} |
424 |
|
425 |
function _iwfInsertHtml(html, parentNodeId){ |
426 |
if(!parentNodeId){ |
427 |
parentNodeId = 'iwfContent'; |
428 |
iwfLog("IWF Ajax Warning: <action> with a type of 'html' does not have its target attribute specified, so using the default of 'iwfContent'."); |
429 |
} |
430 |
|
431 |
if(!parentNodeId){ |
432 |
iwfLog("IWF Ajax Error: <action> node with a type of 'html' does not have its target attribute specified to a valid element.\nPlease specify the id of the element into which the contents of the <action> node should be placed.\nTo fill the entire page, simply specify 'body'.", true); |
433 |
} else { |
434 |
var el = iwfGetById(parentNodeId); |
435 |
if (!el){ |
436 |
if (parentNodeId == 'body'){ |
437 |
el = iwfGetByTagName('body'); |
438 |
if (!el || el.length == 0){ |
439 |
iwfLog("IWF Ajax Error: Could not locate the tag named 'body'", true); |
440 |
return; |
441 |
} else { |
442 |
el = el[0]; |
443 |
} |
444 |
} else { |
445 |
iwfLog('IWF Ajax Error: Could not locate element with id of ' + parentNodeId + ' into which the following html should be placed:\n\n' + html, true); |
446 |
return; |
447 |
} |
448 |
} |
449 |
|
450 |
//iwfLog(iwfElementToString(el)); |
451 |
//iwfLog(html); |
452 |
|
453 |
// trying to shove a <form> node inside another <form> node is a bad thing. |
454 |
// make sure we don't do that here. |
455 |
var re = /<form/i; |
456 |
var match = re.exec(html); |
457 |
if (match && document.forms.length > 0){ |
458 |
// our html to inject contains a form node. |
459 |
// bubble up the chain until we find a <form> node, or we have no parents |
460 |
var elParent = el; |
461 |
while (elParent && elParent.tagName.toLowerCase() != 'form'){ |
462 |
elParent = iwfGetParent(elParent); |
463 |
} |
464 |
|
465 |
if (elParent && elParent.tagName.toLowerCase() == 'form'){ |
466 |
iwfLog('IWF Ajax Error: Attempting to inject html which contains a <form> node into a target element which is itself a <form> node, or is already contained by a <form> node.\nThis is bad html, and will not work appropriately on some major browsers.', true); |
467 |
} |
468 |
} |
469 |
|
470 |
|
471 |
el.innerHTML = html; |
472 |
|
473 |
|
474 |
// if there is a script element, we have to explicitly add it to the body element, |
475 |
// as IE and firefox just don't like it when you try to add it as part of the innerHTML |
476 |
// property. Go figure. |
477 |
|
478 |
var i = 0; |
479 |
// don't stomp on any existing scripts... |
480 |
while (iwfGetById('iwfScript' + i)){ |
481 |
i++; |
482 |
} |
483 |
|
484 |
var scriptStart = html.indexOf("<script"); |
485 |
while(scriptStart > -1){ |
486 |
scriptStart = html.indexOf(">", scriptStart) + 1; |
487 |
|
488 |
// copy contents of script into a default holder |
489 |
var scriptEnd = html.indexOf("</script>", scriptStart); |
490 |
var scriptHtml = html.substr(scriptStart, scriptEnd - scriptStart); |
491 |
|
492 |
var re = /^\s*<!--/; |
493 |
var match = re.exec(scriptHtml); |
494 |
if (!match){ |
495 |
iwfLog("IWF Ajax Error: Developer, you *must* put the <!-- and //--> 'safety net' around your script within all <script> tags.\nThe offending <script> tag contains the following code:\n\n" + scriptHtml + "\n\nThis requirement is due to how IWF injects the html so the browser is able to actually parse the script and make its contents available for execution.", true); |
496 |
} |
497 |
|
498 |
// this code is the worst hack in this entire framework. |
499 |
// the code in the try portion should work anywhere -- but |
500 |
// IE barfs when you try to set the innerHTML on a script element. |
501 |
// not only that, but IE won't parse script unless a visible element |
502 |
// is contained in the innerHTML when it is set, so we need the <code>IE hack here</code> |
503 |
// and it cannot be removed. |
504 |
// I don't understand why creating a new node and setting its innerHTML causes the browsers |
505 |
// to parse and execute the script, but it does. |
506 |
// Note there is a major leak here, as we do not try to reuse existing iwfScriptXXXX elements |
507 |
// in case they are in use by other targets. To clean these up, simply call iwfCleanScripts |
508 |
// periodically. This is so app-dependent, I didn't want to try to keep track of everything |
509 |
// and possibly miss a case, causing really weird results that only show up occassionally. |
510 |
// |
511 |
// Plus I'm getting lazy. :) |
512 |
// |
513 |
try { |
514 |
//! moz (DOM) |
515 |
var elScript = iwfGetOrCreateById('iwfScript' + i, 'script'); |
516 |
elScript.type = 'text/javascript'; |
517 |
elScript.defer = 'true'; |
518 |
elScript.innerHTML = scriptHtml; |
519 |
|
520 |
iwfAppendChild('body', elScript); |
521 |
|
522 |
} catch(e){ |
523 |
iwfLog("IE Hack for injecting script tag...", true); |
524 |
//! IE hack |
525 |
// IE needs a visible tag within a non-script element to have scripting apply... Don't ask me why, ask the IE team why. |
526 |
// My guess is the visible element causes the page to at least partially re-render, which in turn kicks off any script parsing |
527 |
// code in IE. |
528 |
var elDiv = iwfGetOrCreateById('iwfScript' + i, '<div style="display:none"></div>', 'body'); |
529 |
elDiv.innerHTML = '<code>IE hack here</code><script id="iwfScript' + i + '" defer="true">' + scriptHtml + '</script' + '>'; |
530 |
} |
531 |
|
532 |
i++; |
533 |
|
534 |
scriptStart = html.indexOf("<script", scriptEnd+8); |
535 |
} |
536 |
} |
537 |
} |
538 |
|
539 |
function iwfCleanScripts(){ |
540 |
var i = 0; |
541 |
while(iwfRemoveNode('iwfScript' + (i++))); |
542 |
|
543 |
} |
544 |
|
545 |
|
546 |
|
547 |
var _iwfTotalRequests = 0; |
548 |
var _iwfPendingRequests = 0; |
549 |
var _iwfRequestTicker = null; |
550 |
var _iwfRequestTickCount = 0; |
551 |
var _iwfRequestTickDuration = 100; |
552 |
|
553 |
|
554 |
// TODO: make the styles applied be configurable variables at the top of this file. |
555 |
function _iwfGetBusyBar(inner){ |
556 |
var b = iwfGetById('iwfBusy'); |
557 |
if (!b){ |
558 |
b = iwfGetOrCreateById('iwfBusy', 'div', 'body'); |
559 |
if(b.style){ |
560 |
b.style.position = 'absolute'; |
561 |
b.style.border = '1px solid black'; |
562 |
b.style.backgroundColor = '#efefef'; |
563 |
b.style.textAlign = 'center'; |
564 |
} |
565 |
iwfWidth(b, 100); |
566 |
iwfHeight(b, 20); |
567 |
iwfZIndex(b, 9999); |
568 |
|
569 |
iwfX(b, 0); |
570 |
iwfY(b, 0); |
571 |
|
572 |
// iwfX(b, iwfClientWidth() - iwfWidth(b)-5); |
573 |
// iwfY(b, iwfClientHeight() - iwfHeight(b)-5); |
574 |
|
575 |
iwfHide(b); |
576 |
} |
577 |
|
578 |
|
579 |
|
580 |
var bb = iwfGetById('iwfBusyBar'); |
581 |
if(!bb){ |
582 |
bb = iwfGetOrCreateById('iwfBusyBar', 'div', b); |
583 |
bb.style.backgroundColor = 'navy'; |
584 |
bb.style.color = 'white'; |
585 |
bb.style.textAlign = 'center'; |
586 |
iwfWidth(bb, 1); |
587 |
iwfHeight(bb, 20); |
588 |
iwfX(bb, 0); |
589 |
iwfY(bb, 0); |
590 |
} |
591 |
|
592 |
if(inner){ |
593 |
return bb; |
594 |
} else { |
595 |
return b; |
596 |
} |
597 |
|
598 |
} |
599 |
|
600 |
function _iwfOnRequestStart(){ |
601 |
_iwfPendingRequests++; |
602 |
_iwfTotalRequests++; |
603 |
|
604 |
_iwfRequestTickDuration = 100; |
605 |
|
606 |
if (!_iwfRequestTicker){ |
607 |
_iwfRequestTickCount = 0; |
608 |
if (window.iwfOnRequestStart){ |
609 |
_iwfRequestTickDuration = iwfOnRequestStart(); |
610 |
} else if (_iwfShowGuiProgress) { |
611 |
// use gui busy implementation |
612 |
var bb = _iwfGetBusyBar(true); |
613 |
iwfWidth(bb, 1); |
614 |
bb.innerHTML = '0%'; |
615 |
iwfShow(_iwfGetBusyBar(false)); |
616 |
} else { |
617 |
// use default busy implementation... |
618 |
window.status = 'busy.'; |
619 |
} |
620 |
if (!_iwfRequestTickDuration){ |
621 |
_iwfRequestTickDuration = 100; |
622 |
} |
623 |
_iwfRequestTicker = setInterval(_iwfOnRequestTick, _iwfRequestTickDuration); |
624 |
} |
625 |
} |
626 |
|
627 |
function _iwfOnRequestTick(){ |
628 |
_iwfRequestTickCount++; |
629 |
if (window.iwfOnRequestTick){ |
630 |
iwfOnRequestTick(_iwfRequestTickCount, _iwfRequestTickDuration, _iwfPendingRequests); |
631 |
} else if (!window.iwfOnRequestStart) { |
632 |
if (_iwfShowGuiProgress) { |
633 |
// use gui busy implementation |
634 |
var bar = _iwfGetBusyBar(true); |
635 |
if(bar){ |
636 |
var w = iwfWidth(bar) + 1; |
637 |
if (w > 95){ |
638 |
w = 95; |
639 |
} |
640 |
iwfWidth(bar, w); |
641 |
bar.innerHTML = "loading " + iwfIntFormat(w) + "%"; |
642 |
} |
643 |
} else { |
644 |
// use default busy implementation... |
645 |
window.status = 'busy...............................................'.substr(0, (_iwfRequestTickCount % 45) + 5); |
646 |
} |
647 |
} else { |
648 |
// they didn't define a tick function, |
649 |
// but they did define a start one, so do nothing. |
650 |
} |
651 |
} |
652 |
|
653 |
function _iwfOnRequestEnd(){ |
654 |
_iwfPendingRequests--; |
655 |
if (_iwfPendingRequests < 1){ |
656 |
_iwfPendingRequests = 0; |
657 |
_iwfTotalRequests = 0; |
658 |
clearInterval(_iwfRequestTicker); |
659 |
_iwfRequestTicker = null; |
660 |
if (window.iwfOnRequestEnd){ |
661 |
iwfOnRequestEnd(); |
662 |
} else if (!window.iwfOnRequestStart) { |
663 |
if (_iwfShowGuiProgress) { |
664 |
// use gui busy implementation |
665 |
var bar = _iwfGetBusyBar(true); |
666 |
if(bar){ |
667 |
iwfWidth(bar, 100); |
668 |
bar.innerHTML = "Done"; |
669 |
iwfHideGentlyDelay(_iwfGetBusyBar(false), 15, 500); |
670 |
} |
671 |
} else { |
672 |
// use default busy implementation... |
673 |
window.status = 'done.'; |
674 |
} |
675 |
} else { |
676 |
// they didn't define an end function, |
677 |
// but they did define a start one, so do nothing. |
678 |
} |
679 |
|
680 |
} else { |
681 |
if (window.iwfOnRequestProgress){ |
682 |
iwfOnRequestProgress(_iwfPendingRequests, _iwfTotalRequests); |
683 |
} else if (!window.iwfOnRequestStart) { |
684 |
if (_iwfShowGuiProgress) { |
685 |
// use gui busy implementation |
686 |
var pct = (1 - (_iwfPendingRequests/_iwfTotalRequests)) * 100; |
687 |
if (pct > 100){ |
688 |
pct = 100; |
689 |
} |
690 |
var bar = _iwfGetBusyBar(true); |
691 |
if(bar){ |
692 |
iwfWidth(bar, pct); |
693 |
bar.innerHTML = "loading " + iwfIntFormat(pct) + "%"; |
694 |
} |
695 |
} else { |
696 |
// use default busy implementation... |
697 |
window.status = 'Remaining: ' + _iwfRequestsPending; |
698 |
} |
699 |
} else { |
700 |
// they didn't define an end function, |
701 |
// but they did define a start one, so do nothing. |
702 |
} |
703 |
} |
704 |
} |
705 |
|
706 |
function _iwfOnRequestError(code, msg, text){ |
707 |
iwfLog("Error " + code + ": " + msg + ":\n\n" + text); |
708 |
if (window.iwfOnRequestError){ |
709 |
iwfOnRequestError(code, msg, text); |
710 |
} else { |
711 |
alert("Error " + code + ": " + msg + ":\n\n" + text); |
712 |
} |
713 |
} |
714 |
|
715 |
// ----------------------------------- |
716 |
// End: AJAX Request and Response |
717 |
// ----------------------------------- |